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 { 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) {