1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-26 15:29:22 +00:00
comunicapiv2/src/helpers/NotificationsHelper.ts

67 lines
1.4 KiB
TypeScript
Raw Normal View History

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<number> {
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<Array<Notif>> {
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
});
}
}