added search to notifications
This commit is contained in:
@@ -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,8 +45,23 @@ 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] } },
|
{
|
||||||
|
$match: { $and: [{ tenantId: tenantId }, ...filterObj] },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
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: {
|
$project: {
|
||||||
_id: 1,
|
_id: 1,
|
||||||
@@ -67,7 +82,7 @@ export async function listNotifications(
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
$facet: {
|
$facet: {
|
||||||
metadata: [{ $count: "count" }],
|
metadata: [{ $count: 'count' }],
|
||||||
data: [
|
data: [
|
||||||
{ $sort: sortObj },
|
{ $sort: sortObj },
|
||||||
{ $skip: (page - 1) * pageSize },
|
{ $skip: (page - 1) * pageSize },
|
||||||
@@ -75,7 +90,10 @@ export async function listNotifications(
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]);
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user