add unique value route to permits
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import mongoose from "mongoose";
|
||||
import { orgModel } from "../organization/organization.schema";
|
||||
import { getFilterObject, getSortObject, PageQueryParams } from "../pagination";
|
||||
import { userModel } from "../user/user.schema";
|
||||
import { generateId } from "../utils/id";
|
||||
import {
|
||||
CreatePermitInput,
|
||||
@@ -24,7 +27,8 @@ export async function getPermit(permitId: string, tenantId: string) {
|
||||
})
|
||||
.populate({ path: "county", select: "pid name avatar" })
|
||||
.populate({ path: "client", select: "pid name avatar" })
|
||||
.populate({ path: "assignedTo", select: "pid name avatar" });
|
||||
.populate({ path: "assignedTo", select: "pid name avatar" })
|
||||
.populate({ path: "createdBy", select: "pid name avatar" });
|
||||
}
|
||||
|
||||
export async function listPermits(params: PageQueryParams, tenantId: string) {
|
||||
@@ -61,6 +65,14 @@ export async function listPermits(params: PageQueryParams, tenantId: string) {
|
||||
as: "assignedRec",
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "users",
|
||||
localField: "createdBy",
|
||||
foreignField: "_id",
|
||||
as: "createdRec",
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 1,
|
||||
@@ -104,6 +116,17 @@ export async function listPermits(params: PageQueryParams, tenantId: string) {
|
||||
},
|
||||
},
|
||||
},
|
||||
createdBy: {
|
||||
$let: {
|
||||
vars: { created: { $arrayElemAt: ["$createdRec", 0] } },
|
||||
in: {
|
||||
_id: "$$created._id",
|
||||
pid: "$$created.pid",
|
||||
name: "$$created.name",
|
||||
avatar: "$$created.avatar",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -146,7 +169,8 @@ export async function updatePermit(
|
||||
)
|
||||
.populate({ path: "county", select: "pid name avatar" })
|
||||
.populate({ path: "client", select: "pid name avatar" })
|
||||
.populate({ path: "assignedTo", select: "pid name avatar" });
|
||||
.populate({ path: "assignedTo", select: "pid name avatar" })
|
||||
.populate({ path: "createdBy", select: "pid name avatar" });
|
||||
|
||||
return updatePermitResult;
|
||||
}
|
||||
@@ -156,3 +180,24 @@ export async function deletePermit(permitId: string, tenantId: string) {
|
||||
$and: [{ tenantId: tenantId }, { pid: permitId }],
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUniqueValues(field: string, tenenatId: string) {
|
||||
let values = await permitModel.distinct(field, { tenantId: tenenatId });
|
||||
|
||||
let matchedValues = [];
|
||||
if (field === "client" || field === "county") {
|
||||
matchedValues = await orgModel.find().where("_id").in(values).exec();
|
||||
} else if (field === "assignedTo") {
|
||||
matchedValues = await userModel.find().where("_id").in(values).exec();
|
||||
}
|
||||
|
||||
if (matchedValues.length > 0) {
|
||||
const newValues = {};
|
||||
for (const item of matchedValues) {
|
||||
newValues[item.id] = item.name;
|
||||
}
|
||||
return newValues;
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user