import { In } from "typeorm"; import { publishMainStream } from "../../../services/stream.js"; import { pushNotification } from "../../../services/push-notification.js"; import { Notifications, Users } from "../../../models/index.js"; export async function readNotification(userId, notificationIds) { if (notificationIds.length === 0) return; // Update documents const result = await Notifications.update({ notifieeId: userId, id: In(notificationIds), isRead: false }, { isRead: true }); if (result.affected === 0) return; if (!await Users.getHasUnreadNotification(userId)) return postReadAllNotifications(userId); else return postReadNotifications(userId, notificationIds); } export async function readNotificationByQuery(userId, query) { const notificationIds = await Notifications.findBy({ ...query, notifieeId: userId, isRead: false }).then((notifications)=>notifications.map((notification)=>notification.id)); return readNotification(userId, notificationIds); } function postReadAllNotifications(userId) { publishMainStream(userId, "readAllNotifications"); return pushNotification(userId, "readAllNotifications", undefined); } function postReadNotifications(userId, notificationIds) { publishMainStream(userId, "readNotifications", notificationIds); return pushNotification(userId, "readNotifications", { notificationIds }); }