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