unique values route for processed permits
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||
import { PageQueryParams } from "../pagination";
|
||||
import { listProcessedPermits } from "./processed.service";
|
||||
import {
|
||||
getUniqueValuesProcessed,
|
||||
listProcessedPermits,
|
||||
} from "./processed.service";
|
||||
import { $processed } from "./processed.schema";
|
||||
|
||||
export async function processedRoutes(fastify: FastifyInstance) {
|
||||
@@ -24,4 +27,33 @@ export async function processedRoutes(fastify: FastifyInstance) {
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
fastify.get(
|
||||
"/fields/:field",
|
||||
{
|
||||
schema: {
|
||||
params: {
|
||||
type: "object",
|
||||
properties: {
|
||||
field: { type: "string" },
|
||||
},
|
||||
},
|
||||
},
|
||||
config: { requiredClaims: ["permit:read"] },
|
||||
preHandler: [fastify.authorize],
|
||||
},
|
||||
async (req: FastifyRequest, res: FastifyReply) => {
|
||||
const { field } = req.params as { field: string };
|
||||
|
||||
try {
|
||||
const uniqueValues = await getUniqueValuesProcessed(
|
||||
field,
|
||||
req.user.tenantId
|
||||
);
|
||||
return res.code(200).send(uniqueValues);
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
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(
|
||||
@@ -114,3 +116,29 @@ export async function listProcessedPermits(
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user