diff --git a/src/controllers/NotificationsController.ts b/src/controllers/NotificationsController.ts index 5615ca0..b0d5b36 100644 --- a/src/controllers/NotificationsController.ts +++ b/src/controllers/NotificationsController.ts @@ -1,5 +1,7 @@ import { RequestHandler } from "../entities/RequestHandler"; import { NotificationsHelper } from "../helpers/NotificationsHelper"; +import { ConversationsHelper } from "../helpers/ConversationsHelper"; +import { FriendsHelper } from "../helpers/FriendsHelper"; /** * Notifications controller @@ -20,4 +22,22 @@ export class NotificationsController { }); } + /** + * Count the entire number of unread notifications + * + * @param h Request handler + */ + public static async CountAllNews(h: RequestHandler) { + let data = { + notifications: await NotificationsHelper.CountUnread(h.getUserId()), + conversations: await ConversationsHelper.CountUnreadForUser(h.getUserId()) + } + + if(h.postBool("friends_request", false)) + data["friends_requests"] = await FriendsHelper.CountRequests(h.getUserId()); + + // TODO : add pending calls + + h.send(data); + } } \ No newline at end of file diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index e2f217e..c4bc353 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -136,4 +136,6 @@ export const Routes : Route[] = [ // Notifications controller {path: "/notifications/count_unread", cb: (h) => NotificationsController.CountUnread(h)}, + + {path: "/notifications/count_all_news", cb: (h) => NotificationsController.CountAllNews(h)}, ] \ No newline at end of file diff --git a/src/helpers/FriendsHelper.ts b/src/helpers/FriendsHelper.ts new file mode 100644 index 0000000..3195180 --- /dev/null +++ b/src/helpers/FriendsHelper.ts @@ -0,0 +1,29 @@ +import { DatabaseHelper } from "./DatabaseHelper"; + +/** + * Friends helper + * + * @author Pierre HUBERT + */ + +const FRIENDS_TABLE = "amis"; + +export class FriendsHelper { + + /** + * Count the number of friendship requests a user + * received + * + * @param userID Target user ID + */ + public static async CountRequests(userID: number) : Promise { + return await DatabaseHelper.Count({ + table: FRIENDS_TABLE, + where: { + ID_personne: userID, + actif: 0 + } + }); + } + +} \ No newline at end of file