Loading notifications async.

This commit is contained in:
2024-05-28 03:48:50 +02:00
parent 652c389840
commit 70882e0baf
5 changed files with 274 additions and 231 deletions

View File

@@ -145,12 +145,26 @@ export async function removeAlert(alertId, reqUser) {
.delete();
}
export async function fetchUnseenNotificationsCount(reqUser) {
if (!reqUser) {
return null;
}
const rawUnseen = await knex('notifications')
.select(knex.raw('count(id)'))
.where('user_id', reqUser.id)
.where('seen', false)
.first();
return Number(rawUnseen.count);
}
export async function fetchNotifications(reqUser, options = {}) {
if (!reqUser) {
return [];
}
const [notifications, rawUnseen] = await Promise.all([
const [notifications, unseen] = await Promise.all([
knex('notifications')
.select(
'notifications.*',
@@ -169,15 +183,10 @@ export async function fetchNotifications(reqUser, options = {}) {
.limit(options.limit)
.groupBy('notifications.id', 'alerts.id')
.orderBy('created_at', 'desc'),
knex('notifications')
.select(knex.raw('count(id)'))
.where('user_id', reqUser.id)
.where('seen', false)
.first(),
fetchUnseenNotificationsCount(reqUser),
]);
const scenes = await fetchScenesById(notifications.map((notification) => notification.scene_id));
const unseen = Number(rawUnseen.count);
const curatedNotifications = notifications.map((notification) => {
const scene = scenes.find((sceneX) => sceneX.id === notification.scene_id);