notifications
This commit is contained in:
parent
6105367a6e
commit
b404ab84a3
@ -62,11 +62,10 @@ export class LeantimeAdapter implements NotificationAdapter {
|
||||
console.log('[LEANTIME_ADAPTER] Sending request to Leantime API for notifications');
|
||||
const jsonRpcBody = {
|
||||
jsonrpc: '2.0',
|
||||
method: 'leantime.rpc.notifications.getAll',
|
||||
method: 'leantime.rpc.notifications.getNotifications',
|
||||
params: {
|
||||
userId: leantimeUserId,
|
||||
limit: limit,
|
||||
offset: offset
|
||||
limit: limit
|
||||
},
|
||||
id: 1
|
||||
};
|
||||
@ -104,7 +103,11 @@ export class LeantimeAdapter implements NotificationAdapter {
|
||||
});
|
||||
|
||||
if (!data.result || !Array.isArray(data.result)) {
|
||||
console.error('[LEANTIME_ADAPTER] Invalid response format from Leantime notifications API');
|
||||
if (data.error) {
|
||||
console.error(`[LEANTIME_ADAPTER] API error: ${data.error.message || JSON.stringify(data.error)}`);
|
||||
} else {
|
||||
console.error('[LEANTIME_ADAPTER] Invalid response format from Leantime notifications API');
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
@ -138,11 +141,11 @@ export class LeantimeAdapter implements NotificationAdapter {
|
||||
return this.getEmptyCount();
|
||||
}
|
||||
|
||||
// Make request to Leantime API using jsonrpc
|
||||
// Make request to Leantime API using jsonrpc to get all notifications
|
||||
console.log('[LEANTIME_ADAPTER] Sending request to Leantime API for notification count');
|
||||
const jsonRpcBody = {
|
||||
jsonrpc: '2.0',
|
||||
method: 'leantime.rpc.notifications.getNotificationCount',
|
||||
method: 'leantime.rpc.notifications.getNotifications',
|
||||
params: {
|
||||
userId: leantimeUserId
|
||||
},
|
||||
@ -171,22 +174,29 @@ export class LeantimeAdapter implements NotificationAdapter {
|
||||
}
|
||||
|
||||
const responseText = await response.text();
|
||||
console.log('[LEANTIME_ADAPTER] Raw response:', responseText);
|
||||
console.log('[LEANTIME_ADAPTER] Raw response:', responseText.substring(0, 200) + (responseText.length > 200 ? '...' : ''));
|
||||
|
||||
const data = JSON.parse(responseText);
|
||||
console.log('[LEANTIME_ADAPTER] Parsed response data:', {
|
||||
hasResult: !!data.result,
|
||||
resultData: data.result,
|
||||
resultIsArray: Array.isArray(data.result),
|
||||
resultLength: Array.isArray(data.result) ? data.result.length : 'n/a',
|
||||
error: data.error
|
||||
});
|
||||
|
||||
if (!data.result) {
|
||||
console.error('[LEANTIME_ADAPTER] Invalid response format from Leantime notification count API');
|
||||
if (!data.result || !Array.isArray(data.result)) {
|
||||
if (data.error) {
|
||||
console.error(`[LEANTIME_ADAPTER] API error: ${data.error.message || JSON.stringify(data.error)}`);
|
||||
} else {
|
||||
console.error('[LEANTIME_ADAPTER] Invalid response format from Leantime notifications API');
|
||||
}
|
||||
return this.getEmptyCount();
|
||||
}
|
||||
|
||||
const unreadCount = data.result.unread || 0;
|
||||
const totalCount = data.result.total || 0;
|
||||
// Count total and unread notifications
|
||||
const notifications = data.result;
|
||||
const totalCount = notifications.length;
|
||||
const unreadCount = notifications.filter((n: any) => n.read === 0).length;
|
||||
|
||||
console.log('[LEANTIME_ADAPTER] Notification counts:', { unread: unreadCount, total: totalCount });
|
||||
|
||||
@ -220,9 +230,9 @@ export class LeantimeAdapter implements NotificationAdapter {
|
||||
},
|
||||
body: JSON.stringify({
|
||||
jsonrpc: '2.0',
|
||||
method: 'leantime.rpc.notifications.markAsRead',
|
||||
method: 'leantime.rpc.notifications.markNotificationRead',
|
||||
params: {
|
||||
notificationId: sourceId
|
||||
id: parseInt(sourceId)
|
||||
},
|
||||
id: 1
|
||||
})
|
||||
@ -238,6 +248,11 @@ export class LeantimeAdapter implements NotificationAdapter {
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
if (data.error) {
|
||||
console.error(`[LEANTIME_ADAPTER] API error: ${data.error.message || JSON.stringify(data.error)}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
return data.result === true;
|
||||
} catch (error) {
|
||||
console.error('[LEANTIME_ADAPTER] Error marking Leantime notification as read:', error);
|
||||
@ -260,34 +275,22 @@ export class LeantimeAdapter implements NotificationAdapter {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Make request to Leantime API using jsonrpc
|
||||
const response = await fetch(`${this.apiUrl}/api/jsonrpc`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-API-Key': this.apiToken
|
||||
},
|
||||
body: JSON.stringify({
|
||||
jsonrpc: '2.0',
|
||||
method: 'leantime.rpc.notifications.markAllAsRead',
|
||||
params: {
|
||||
userId: leantimeUserId
|
||||
},
|
||||
id: 1
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
console.error('[LEANTIME_ADAPTER] Failed to mark all Leantime notifications as read:', {
|
||||
status: response.status,
|
||||
body: errorText.substring(0, 200) + (errorText.length > 200 ? '...' : '')
|
||||
});
|
||||
return false;
|
||||
// Get all unread notifications
|
||||
const notifications = await this.getNotifications(userId);
|
||||
const unreadNotifications = notifications.filter(n => !n.isRead);
|
||||
|
||||
if (unreadNotifications.length === 0) {
|
||||
// If there are no unread notifications, consider it a success
|
||||
return true;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data.result === true;
|
||||
|
||||
// Mark each notification as read individually
|
||||
const promises = unreadNotifications.map(notification =>
|
||||
this.markAsRead(userId, notification.id)
|
||||
);
|
||||
|
||||
const results = await Promise.all(promises);
|
||||
return results.every(result => result);
|
||||
} catch (error) {
|
||||
console.error('[LEANTIME_ADAPTER] Error marking all Leantime notifications as read:', error);
|
||||
return false;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user