diff --git a/src/entities/Notification.ts b/src/entities/Notification.ts index eb112ff..5e32d71 100644 --- a/src/entities/Notification.ts +++ b/src/entities/Notification.ts @@ -53,9 +53,9 @@ export interface NotifBuilder { seen ?: boolean, fromUserID: number, destUserID ?: number, - onElemID: number, + onElemID ?: number, onElemType: NotifElemType, - type: NotifEventType, + type ?: NotifEventType, eventVisibility ?: NotifEventVisibility, fromContainerID ?: number, fromContainerType ?: NotifElemType @@ -67,9 +67,9 @@ export class Notif implements NotifBuilder { seen ?: boolean; fromUserID: number; destUserID ?: number; - onElemID: number; + onElemID ?: number; onElemType: NotifElemType; - type: NotifEventType; + type ?: NotifEventType; eventVisibility ?: NotifEventVisibility; fromContainerID ?: number; fromContainerType ?: NotifElemType; diff --git a/src/helpers/NotificationsHelper.ts b/src/helpers/NotificationsHelper.ts index 90436ea..97bca7a 100644 --- a/src/helpers/NotificationsHelper.ts +++ b/src/helpers/NotificationsHelper.ts @@ -197,6 +197,22 @@ export class NotificationsHelper { ) } + /** + * Delete notifications + * + * @param n Notification + */ + public static async Delete(n: Notif) { + // Delete a specific notification + if(n.hasId) + throw new Error("Please implement notification deletion with known ID!"); + + // Delete wider range of notifications + else + await DatabaseHelper.DeleteRows(NOTIFICATIONS_TABLE, this.NotifToDB(n, false)) + + } + /** * Count the number of unread notifications of a user * diff --git a/src/utils/NotificationsUtils.ts b/src/utils/NotificationsUtils.ts index 1ca8cff..9a4fad6 100644 --- a/src/utils/NotificationsUtils.ts +++ b/src/utils/NotificationsUtils.ts @@ -32,20 +32,42 @@ export class NotificationsUtils { * Create & push friendship request notification * * @param fromUser Source user ID - * @param toUser Destination user ID + * @param destUser Destination user ID * @param action The kind of action */ - public static async CreateFriendsNotifications(fromUser: number, toUser: number, action: NotifEventType) { - // TODO : Delete all the previous notifications + public static async CreateFriendsNotifications(fromUser: number, destUser: number, action: NotifEventType) { + await this.DeleteNotificationsFrienshipRequest(fromUser, destUser); - // PUsh the notification + // Push the notification await NotificationsHelper.Push(new Notif({ fromUserID: fromUser, - destUserID: toUser, + destUserID: destUser, onElemID: fromUser, // Same as fromUser onElemType: NotifElemType.FRIENDSHIP_REQUEST, type: action })); } + + /** + * Delete all the notifications related to a friendship request between two users + * + * @param userOne First user + * @param userTwo Second user + */ + public static async DeleteNotificationsFrienshipRequest(userOne: number, userTwo: number) { + + // Delete notifications in two ways + await NotificationsHelper.Delete(new Notif({ + onElemType: NotifElemType.FRIENDSHIP_REQUEST, + destUserID: userOne, + fromUserID: userTwo, + })); + + await NotificationsHelper.Delete(new Notif({ + onElemType: NotifElemType.FRIENDSHIP_REQUEST, + destUserID: userTwo, + fromUserID: userOne, + })); + } } \ No newline at end of file