add notification routes

This commit is contained in:
2025-02-05 13:52:04 +05:30
parent 8559aab3da
commit 2d62e9712b
8 changed files with 193 additions and 1 deletions

View File

@@ -0,0 +1,57 @@
import { getFilterObject, getSortObject, PageQueryParams } from "../pagination";
import {
notificationFields,
notificationModel,
UpdateNotificationInput,
} from "./notification.schema";
export async function updateNotification(
notifId: string,
input: UpdateNotificationInput,
tenantId: string
) {
return await notificationModel.findOneAndUpdate(
{ $and: [{ tenantId: tenantId }, { pid: notifId }] },
{
...input,
updatedAt: new Date(),
},
{ new: true }
);
}
export async function listNotifications(
params: PageQueryParams,
tenantId: string
) {
const page = params.page || 1;
const pageSize = params.pageSize || 10;
const sortObj = getSortObject(params, notificationFields);
const filterObj = getFilterObject(params, notificationFields);
const notifications = await notificationModel.aggregate([
{ $match: { $and: [{ tenantId: tenantId }, ...filterObj] } },
{
$facet: {
metadata: [{ $count: "count" }],
data: [
{ $skip: (page - 1) * pageSize },
{ $limit: pageSize },
{ $sort: sortObj },
],
},
},
]);
if (notifications[0].data.length === 0)
return { orgs: [], metadata: { count: 0, page, pageSize } };
return {
orgs: notifications[0].data,
metadata: {
count: notifications[0].metadata[0].count,
page,
pageSize,
},
};
}