66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import { orgModel } from "./organization/organization.schema";
|
|
import { userModel } from "./user/user.schema";
|
|
import { permitModel } from "./permit/permit.schema";
|
|
import { processedModel } from "./processed/processed.schema";
|
|
import { notificationModel } from "./notification/notification.schema";
|
|
import { rtsModel } from "./rts/rts.schema";
|
|
import { taskModel } from "./task/task.schema";
|
|
import { AuthenticatedUser } from "./auth";
|
|
|
|
type Collection =
|
|
| "users"
|
|
| "permits"
|
|
| "organizations"
|
|
| "processed"
|
|
| "notifications"
|
|
| "rts"
|
|
| "task";
|
|
|
|
const modelMap = {
|
|
users: userModel,
|
|
permits: permitModel,
|
|
organizations: orgModel,
|
|
processed: processedModel,
|
|
notifications: notificationModel,
|
|
rts: rtsModel,
|
|
task: taskModel,
|
|
};
|
|
|
|
export async function getUniqueFields(
|
|
field: string,
|
|
collection: Collection,
|
|
user: AuthenticatedUser
|
|
) {
|
|
const model = modelMap[collection];
|
|
if (!model) throw new Error("invalid collection");
|
|
|
|
let values = await model.distinct(field, { tenantId: user.tenantId });
|
|
|
|
let matchedValues = [];
|
|
if (field === "county.name") {
|
|
matchedValues = await orgModel.find().where("name").in(values).exec();
|
|
} else if (field === "county") {
|
|
matchedValues = await orgModel.find().where("_id").in(values).exec();
|
|
} else if (field === "client") {
|
|
if (user.role == "client") {
|
|
values = [user.orgId];
|
|
}
|
|
|
|
matchedValues = await orgModel.find().where("_id").in(values).exec();
|
|
} else if (field === "createdBy") {
|
|
matchedValues = await userModel.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;
|
|
}
|