add notification routes
This commit is contained in:
57
src/notification/notification.service.ts
Normal file
57
src/notification/notification.service.ts
Normal 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,
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user