146 lines
3.7 KiB
TypeScript
146 lines
3.7 KiB
TypeScript
import { orgModel } from "../organization/organization.schema";
|
|
import { getFilterObject, getSortObject, PageQueryParams } from "../pagination";
|
|
import { userModel } from "../user/user.schema";
|
|
import { processedFields, processedModel } from "./processed.schema";
|
|
|
|
export async function listProcessedPermits(
|
|
params: PageQueryParams,
|
|
tenantId: string
|
|
) {
|
|
const page = params.page || 1;
|
|
const pageSize = params.pageSize || 10;
|
|
const sortObj = getSortObject(params, processedFields);
|
|
const filterObj = getFilterObject(params, processedFields);
|
|
|
|
const pipeline: any = [
|
|
{
|
|
$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 } },
|
|
{ "address.full_address": { $regex: regex } },
|
|
],
|
|
},
|
|
});
|
|
}
|
|
|
|
pipeline.push(
|
|
...[
|
|
{
|
|
$lookup: {
|
|
from: "users",
|
|
localField: "assignedTo",
|
|
foreignField: "_id",
|
|
as: "assignedRec",
|
|
},
|
|
},
|
|
{
|
|
$project: {
|
|
_id: 1,
|
|
pid: 1,
|
|
permitNumber: 1,
|
|
permitDate: 1,
|
|
stage: 1,
|
|
status: 1,
|
|
manualStatus: 1,
|
|
link: 1,
|
|
address: 1,
|
|
recordType: 1,
|
|
description: 1,
|
|
applicationDetails: 1,
|
|
applicationInfo: 1,
|
|
applicationInfoTable: 1,
|
|
conditions: 1,
|
|
ownerDetails: 1,
|
|
parcelInfo: 1,
|
|
paymentData: 1,
|
|
inspections: 1,
|
|
newProcessingStatus: 1,
|
|
newPayment: 1,
|
|
newConditions: 1,
|
|
professionals: 1,
|
|
recordid: 1,
|
|
relatedRecords: 1,
|
|
accelaStatus: 1,
|
|
createdAt: 1,
|
|
county: 1,
|
|
client: 1,
|
|
clientData: 1,
|
|
openDate: 1,
|
|
lastUpdateDate: 1,
|
|
statusUpdated: 1,
|
|
transferDate: 1,
|
|
assignedTo: {
|
|
$let: {
|
|
vars: { assigned: { $arrayElemAt: ["$assignedRec", 0] } },
|
|
in: {
|
|
_id: "$$assigned._id",
|
|
pid: "$$assigned.pid",
|
|
name: "$$assigned.name",
|
|
avatar: "$$assigned.avatar",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
$facet: {
|
|
metadata: [{ $count: "count" }],
|
|
data: [
|
|
{ $sort: sortObj },
|
|
{ $skip: (page - 1) * pageSize },
|
|
{ $limit: pageSize },
|
|
],
|
|
},
|
|
},
|
|
]
|
|
);
|
|
|
|
const permitsList = await processedModel.aggregate(pipeline);
|
|
|
|
if (permitsList[0].data.length === 0)
|
|
return { permits: [], metadata: { count: 0, page, pageSize } };
|
|
|
|
return {
|
|
permits: permitsList[0]?.data,
|
|
metadata: {
|
|
count: permitsList[0].metadata[0].count,
|
|
page,
|
|
pageSize,
|
|
},
|
|
};
|
|
}
|
|
|
|
export async function getUniqueValuesProcessed(
|
|
field: string,
|
|
tenenatId: string
|
|
) {
|
|
let values = await processedModel.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 (matchedValues.length > 0) {
|
|
const newValues = {};
|
|
for (const item of matchedValues) {
|
|
newValues[item.id] = item.name;
|
|
}
|
|
return newValues;
|
|
}
|
|
|
|
return values;
|
|
}
|