From e6c610a3c09f4596a42addf34d838beb2aa6ca7e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 24 Mar 2020 08:07:50 +0100 Subject: [PATCH] Ready to create the notification --- src/entities/Notification.ts | 39 ++++++++++- src/helpers/NotificationsHelper.ts | 108 ++++++++++++++++++++++++++++- 2 files changed, 143 insertions(+), 4 deletions(-) diff --git a/src/entities/Notification.ts b/src/entities/Notification.ts index 012aebc..636034e 100644 --- a/src/entities/Notification.ts +++ b/src/entities/Notification.ts @@ -48,7 +48,7 @@ export enum NotifEventVisibility { export interface NotifBuilder { id ?: number, - timeCreate: number, + timeCreate ?: number, seen ?: boolean, fromUserID: number, destUserID ?: number, @@ -61,7 +61,8 @@ export interface NotifBuilder { } export class Notif implements NotifBuilder { - id ?: number; timeCreate: number; + id ?: number; + timeCreate ?: number; seen ?: boolean; fromUserID: number; destUserID ?: number; @@ -79,4 +80,38 @@ export class Notif implements NotifBuilder { } } } + + get hasId() : boolean { + return this.id > 0 + } + + get hasSeen() : boolean { + return this.seen === true || this.seen === false + } + + get hasFromUserID() : boolean { + return this.fromUserID > 0 + } + + get hasDestUserID() : boolean { + return this.destUserID > 0 + } + + get hasType() : boolean { + return this.type != null && this.type != undefined + } + + get hasOnElemID() : boolean { + return this.onElemID > 0 + } + + get hasOnElemType() : boolean { + return this.onElemType != null && this.onElemType != undefined + } + + get hasTimeCreate() : boolean { + return this.timeCreate != null + && this.timeCreate != undefined + && this.timeCreate > 0; + } } \ No newline at end of file diff --git a/src/helpers/NotificationsHelper.ts b/src/helpers/NotificationsHelper.ts index aba146f..5d96dd1 100644 --- a/src/helpers/NotificationsHelper.ts +++ b/src/helpers/NotificationsHelper.ts @@ -1,5 +1,8 @@ import { DatabaseHelper } from "./DatabaseHelper"; -import { Notif } from "../entities/Notification"; +import { Notif, NotifElemType, NotifEventVisibility } from "../entities/Notification"; +import { time } from "../utils/DateUtils"; +import { PostsHelper } from "./PostsHelper"; +import { PostPageKind, PostVisibilityLevel } from "../entities/Post"; /** * Notifications helper @@ -17,7 +20,81 @@ export class NotificationsHelper { * @param n Notification to push */ public static async Push(n: Notif) { - // TODO : push notification + + if(!n.hasTimeCreate) + n.timeCreate = time(); + + // Determine the visibility level of the notification + if(n.onElemType == NotifElemType.POST) { + const post = await PostsHelper.GetSingle(n.onElemID); + + // Determine post container + switch(post.kindPage) { + + case PostPageKind.PAGE_KIND_USER: + n.fromContainerType = NotifElemType.USER_PAGE + n.fromContainerID = post.userPageID + break; + + case PostPageKind.PAGE_KIND_GROUP: + n.fromContainerType = NotifElemType.GROUP_PAGE + n.fromContainerID = post.groupID + break; + + default: + throw new Error("Unsupported page kind: " + post.kindPage) + } + + + // Check the scope of the notification + if(post.visibilityLevel == PostVisibilityLevel.VISIBILITY_USER) { + + // Check if the user does not own the post + if(post.userID == post.userPageID) + return; + + // If the personn who posted that is not the owner of the page + else if(post.userPageID != n.fromUserID) + n.destUserID = post.userPageID + + // If the user is the one who own the page + else + n.destUserID = post.userID + + await this.PushPrivate(n) + } + + // TODO : continue + } + } + + /** + * Push a private notification + * + * @param n The notification + */ + private static async PushPrivate(n: Notif) { + n.eventVisibility = NotifEventVisibility.EVENT_PRIVATE + + if(await this.SimilarExists(n)) + return; + + + throw new Error("Create the notification!") + //await this.Create(n) + } + + /** + * Check out whether a similar notification exists + * for a given notification + * + * @param n The notification + */ + private static async SimilarExists(n: Notif) : Promise { + return await DatabaseHelper.Count({ + table: NOTIFICATIONS_TABLE, + where: this.NotifToDB(n) + }) > 0; } /** @@ -53,6 +130,33 @@ export class NotificationsHelper { return list.map((e) => this.DBToNotif(e)); } + /** + * Turn a notification into a database entry + * + * @param n The notification + * @param allInfo True to return a full notification entry + */ + private static NotifToDB(n: Notif, allInfo = true) : any { + let data: any = {} + + if(n.hasId) data.id = n.id + if(n.hasSeen) data.seen = n.seen ? 1 : 0 + if(n.hasFromUserID) data.from_user_id = n.fromUserID + if(n.hasDestUserID) data.dest_user_id = n.destUserID + if(n.hasType) data.type = n.type + if(n.hasOnElemID) data.on_elem_id = n.onElemID + if(n.hasOnElemType) data.on_elem_type = n.onElemType + + if(allInfo) { + data.from_container_id = n.fromContainerID + data.from_container_type = n.fromContainerType + data.time_create = n.timeCreate + data.visibility = n.eventVisibility + } + + return data; + } + /** * Database entry to notification *