add unique value route to permits
This commit is contained in:
@@ -4,6 +4,7 @@ import {
|
||||
createPermit,
|
||||
deletePermit,
|
||||
getPermit,
|
||||
getUniqueValues,
|
||||
listPermits,
|
||||
updatePermit,
|
||||
} from "./permit.service";
|
||||
@@ -90,3 +91,17 @@ export async function deletePermitHandler(
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getUniqueFieldValues(
|
||||
req: FastifyRequest,
|
||||
res: FastifyReply
|
||||
) {
|
||||
const { field } = req.params as { field: string };
|
||||
|
||||
try {
|
||||
const uniqueValues = await getUniqueValues(field, req.user.tenantId);
|
||||
return res.code(200).send(uniqueValues);
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
createPermitHandler,
|
||||
deletePermitHandler,
|
||||
getPermitHandler,
|
||||
getUniqueFieldValues,
|
||||
listPermitsHandler,
|
||||
updatePermitHandler,
|
||||
} from "./permit.controller";
|
||||
@@ -81,5 +82,20 @@ export async function permitRoutes(fastify: FastifyInstance) {
|
||||
deletePermitHandler
|
||||
);
|
||||
|
||||
fastify.get(
|
||||
"/fields/:field",
|
||||
{
|
||||
schema: {
|
||||
params: {
|
||||
type: "object",
|
||||
properties: { field: { type: "string" } },
|
||||
},
|
||||
},
|
||||
config: { requiredClaims: ["permit:read"] },
|
||||
preHandler: [fastify.authorize],
|
||||
},
|
||||
getUniqueFieldValues
|
||||
);
|
||||
|
||||
fastify.addHook("onSend", hideFields("permits"));
|
||||
}
|
||||
|
||||
@@ -43,7 +43,10 @@ const permitSchema = new mongoose.Schema({
|
||||
inspections: Object,
|
||||
createdAt: Date,
|
||||
updatedAt: Date,
|
||||
createdBy: String,
|
||||
createdBy: {
|
||||
type: mongoose.Types.ObjectId,
|
||||
ref: "user",
|
||||
},
|
||||
}).index({ tenantId: 1, permitNumber: 1 }, { unique: true });
|
||||
|
||||
export const permitFields = Object.keys(permitSchema.paths).filter(
|
||||
|
||||
@@ -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