From 804066c97a9e79b6a40cead34598da4bb96ec87a Mon Sep 17 00:00:00 2001 From: Akhil Meka Date: Wed, 23 Apr 2025 14:37:37 +0530 Subject: [PATCH] added search to notifications --- src/notification/notification.service.ts | 96 ++++++++++++++---------- 1 file changed, 57 insertions(+), 39 deletions(-) diff --git a/src/notification/notification.service.ts b/src/notification/notification.service.ts index 42d1a72..a347dba 100644 --- a/src/notification/notification.service.ts +++ b/src/notification/notification.service.ts @@ -1,13 +1,13 @@ -import { orgModel } from "../organization/organization.schema"; -import { getFilterObject, getSortObject, PageQueryParams } from "../pagination"; -import { userModel } from "../user/user.schema"; -import { generateId } from "../utils/id"; +import { orgModel } from '../organization/organization.schema'; +import { getFilterObject, getSortObject, PageQueryParams } from '../pagination'; +import { userModel } from '../user/user.schema'; +import { generateId } from '../utils/id'; import { CreateNotificationInput, notificationFields, notificationModel, UpdateNotificationInput, -} from "./notification.schema"; +} from './notification.schema'; export async function createNotification( input: CreateNotificationInput, @@ -45,37 +45,55 @@ export async function listNotifications( const sortObj = getSortObject(params, notificationFields); const filterObj = getFilterObject(params, notificationFields); - const notifications = await notificationModel.aggregate([ - { $match: { $and: [{ tenantId: tenantId }, ...filterObj] } }, + const pipeline: any = [ { - $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, - }, + $match: { $and: [{ tenantId: tenantId }, ...filterObj] }, }, - { - $facet: { - metadata: [{ $count: "count" }], - data: [ - { $sort: sortObj }, - { $skip: (page - 1) * pageSize }, - { $limit: pageSize }, - ], + ]; + + if (params.searchToken && params.searchToken != '') { + const regex = new RegExp(params.searchToken, 'i'); + pipeline.push({ + $match: { + $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) return { notifications: [], metadata: { count: 0, page, pageSize } }; @@ -103,12 +121,12 @@ export async function getUniqueValuesNotification( let values = await notificationModel.distinct(field, { tenantId: tenenatId }); let matchedValues = []; - if (field === "county.name") { - matchedValues = await orgModel.find().where("name").in(values).exec(); - } else if (field === "client") { - matchedValues = await orgModel.find().where("_id").in(values).exec(); - } else if (field === "assignedTo") { - matchedValues = await userModel.find().where("name").in(values).exec(); + if (field === 'county.name') { + matchedValues = await orgModel.find().where('name').in(values).exec(); + } else if (field === 'client') { + matchedValues = await orgModel.find().where('_id').in(values).exec(); + } else if (field === 'assignedTo') { + matchedValues = await userModel.find().where('name').in(values).exec(); } if (matchedValues.length > 0) {