import { DatabaseHelper } from "./DatabaseHelper"; import { Notif } from "../entities/Notification"; /** * Notifications helper * * @author Pierre HUBERT */ const NOTIFICATIONS_TABLE = "comunic_notifications"; export class NotificationsHelper { /** * Count the number of unread notifications of a user * * @param userID Target user ID */ public static async CountUnread(userID: number) : Promise { return await DatabaseHelper.Count({ table: NOTIFICATIONS_TABLE, where: { dest_user_id: userID, seen: 0 } }); } /** * Get the list of unread notifications of a user * * @param userID Target user ID */ public static async GetListUnread(userID: number) : Promise> { const list = await DatabaseHelper.Query({ table: NOTIFICATIONS_TABLE, where: { dest_user_id: userID, seen: 0 }, order: "id DESC" }); return list.map((e) => this.DBToNotif(e)); } /** * Database entry to notification * * @param row Database entry */ private static DBToNotif(row: any) : Notif { return new Notif({ id: row.id, seen: row.seen == 1, timeCreate: row.time_create, fromUserID: row.from_user_id, destUserID: row.dest_user_id, onElemID: row.on_elem_id, onElemType: row.on_elem_type, type: row.type, eventVisibility: row.visibility, fromContainerID: row.from_container_id, fromContainerType: row.from_container_type }); } }