2019-12-27 17:56:22 +00:00
|
|
|
import { DatabaseHelper } from "./DatabaseHelper";
|
2019-12-28 11:57:55 +00:00
|
|
|
import { Notif } from "../entities/Notification";
|
2019-12-27 17:56:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-28 11:57:55 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
});
|
|
|
|
}
|
2019-12-27 17:56:22 +00:00
|
|
|
}
|