added search to notifications

This commit is contained in:
2025-04-23 14:37:37 +05:30
parent c003a1513c
commit 804066c97a

View File

@@ -1,13 +1,13 @@
import { orgModel } from "../organization/organization.schema"; import { orgModel } from '../organization/organization.schema';
import { getFilterObject, getSortObject, PageQueryParams } from "../pagination"; import { getFilterObject, getSortObject, PageQueryParams } from '../pagination';
import { userModel } from "../user/user.schema"; import { userModel } from '../user/user.schema';
import { generateId } from "../utils/id"; import { generateId } from '../utils/id';
import { import {
CreateNotificationInput, CreateNotificationInput,
notificationFields, notificationFields,
notificationModel, notificationModel,
UpdateNotificationInput, UpdateNotificationInput,
} from "./notification.schema"; } from './notification.schema';
export async function createNotification( export async function createNotification(
input: CreateNotificationInput, input: CreateNotificationInput,
@@ -45,37 +45,55 @@ export async function listNotifications(
const sortObj = getSortObject(params, notificationFields); const sortObj = getSortObject(params, notificationFields);
const filterObj = getFilterObject(params, notificationFields); const filterObj = getFilterObject(params, notificationFields);
const notifications = await notificationModel.aggregate([ const pipeline: any = [
{ $match: { $and: [{ tenantId: tenantId }, ...filterObj] } },
{ {
$project: { $match: { $and: [{ tenantId: tenantId }, ...filterObj] },
_id: 1,
pid: 1,
permitId: 1,
tenantId: 1,
permitNumber: 1,
link: 1,
status: 1,
accelaStatus: 1,
changes: 1,
county: 1,
client: 1,
clientData: 1,
createdAt: 1,
updatedAt: 1,
},
}, },
{ ];
$facet: {
metadata: [{ $count: "count" }], if (params.searchToken && params.searchToken != '') {
data: [ const regex = new RegExp(params.searchToken, 'i');
{ $sort: sortObj }, pipeline.push({
{ $skip: (page - 1) * pageSize }, $match: {
{ $limit: pageSize }, $or: [{ permitNumber: { $regex: regex } }, { link: { $regex: regex } }],
],
}, },
}, });
]); }
pipeline.push(
...[
{
$project: {
_id: 1,
pid: 1,
permitId: 1,
tenantId: 1,
permitNumber: 1,
link: 1,
status: 1,
accelaStatus: 1,
changes: 1,
county: 1,
client: 1,
clientData: 1,
createdAt: 1,
updatedAt: 1,
},
},
{
$facet: {
metadata: [{ $count: 'count' }],
data: [
{ $sort: sortObj },
{ $skip: (page - 1) * pageSize },
{ $limit: pageSize },
],
},
},
]
);
const notifications = await notificationModel.aggregate(pipeline);
if (notifications[0].data.length === 0) if (notifications[0].data.length === 0)
return { notifications: [], metadata: { count: 0, page, pageSize } }; return { notifications: [], metadata: { count: 0, page, pageSize } };
@@ -103,12 +121,12 @@ export async function getUniqueValuesNotification(
let values = await notificationModel.distinct(field, { tenantId: tenenatId }); let values = await notificationModel.distinct(field, { tenantId: tenenatId });
let matchedValues = []; let matchedValues = [];
if (field === "county.name") { if (field === 'county.name') {
matchedValues = await orgModel.find().where("name").in(values).exec(); matchedValues = await orgModel.find().where('name').in(values).exec();
} else if (field === "client") { } else if (field === 'client') {
matchedValues = await orgModel.find().where("_id").in(values).exec(); matchedValues = await orgModel.find().where('_id').in(values).exec();
} else if (field === "assignedTo") { } else if (field === 'assignedTo') {
matchedValues = await userModel.find().where("name").in(values).exec(); matchedValues = await userModel.find().where('name').in(values).exec();
} }
if (matchedValues.length > 0) { if (matchedValues.length > 0) {