feat: convert assignedTo field to string for permits

This commit is contained in:
2025-11-14 17:51:31 +05:30
parent a27e9da4d3
commit b7f63479a6
3 changed files with 70 additions and 34 deletions

View File

@@ -1,5 +1,5 @@
import { z } from "zod"; import { z } from "zod";
import mongoose from "mongoose"; import mongoose, { Schema } from "mongoose";
import { buildJsonSchemas } from "fastify-zod"; import { buildJsonSchemas } from "fastify-zod";
import { pageMetadata, pageQueryParams } from "../pagination"; import { pageMetadata, pageQueryParams } from "../pagination";
@@ -38,7 +38,7 @@ const permitSchema = new mongoose.Schema({
permitType: String, permitType: String,
utility: String, utility: String,
assignedTo: { assignedTo: {
type: mongoose.Types.ObjectId, type: [Schema.Types.ObjectId],
ref: "user", ref: "user",
}, },
link: String, link: String,
@@ -158,7 +158,7 @@ const permitCore = {
cleanStatus: z.string().optional(), cleanStatus: z.string().optional(),
permitType: z.string().optional(), permitType: z.string().optional(),
utility: z.string().nullable().optional(), utility: z.string().nullable().optional(),
assignedTo: z.string().nullable().optional(), assignedTo: z.array(z.string()).nullable().optional(),
link: z.string().optional(), link: z.string().optional(),
address: z address: z
.object({ .object({

View File

@@ -20,6 +20,7 @@ import { createNote } from "../note/note.service";
import { createAlert } from "../alert/alert.service"; import { createAlert } from "../alert/alert.service";
import { processedModel } from "../processed/processed.schema"; import { processedModel } from "../processed/processed.schema";
import { orgModel } from "../organization/organization.schema"; import { orgModel } from "../organization/organization.schema";
import { arrayDiff } from "../utils/diff";
export async function createPermit( export async function createPermit(
input: CreatePermitInput, input: CreatePermitInput,
@@ -137,7 +138,7 @@ export async function listPermits(
from: "users", from: "users",
localField: "assignedTo", localField: "assignedTo",
foreignField: "_id", foreignField: "_id",
as: "assignedRec", as: "assignedTo",
}, },
}, },
{ {
@@ -190,13 +191,14 @@ export async function listPermits(
deed: 1, deed: 1,
requests: 1, requests: 1,
assignedTo: { assignedTo: {
$let: { $map: {
vars: { assigned: { $arrayElemAt: ["$assignedRec", 0] } }, input: "$assignedTo",
as: "user",
in: { in: {
_id: "$$assigned._id", _id: "$$user._id",
pid: "$$assigned.pid", pid: "$$user.pid",
name: "$$assigned.name", name: "$$user.name",
avatar: "$$assigned.avatar", avatar: "$$user.avatar",
}, },
}, },
}, },
@@ -232,6 +234,10 @@ export async function updatePermit(
permitId: string, permitId: string,
user: AuthenticatedUser user: AuthenticatedUser
) { ) {
const oldPermitResult = await permitModel.findOne(
{ pid: permitId },
{ assignedTo: 1 }
);
const updatePermitResult = await permitModel const updatePermitResult = await permitModel
.findOneAndUpdate( .findOneAndUpdate(
{ {
@@ -246,16 +252,11 @@ export async function updatePermit(
if (updatePermitResult) { if (updatePermitResult) {
for (const key in input) { for (const key in input) {
if (["manualStatus", "utility", "assignedTo", "requests"].includes(key)) { if (["manualStatus", "utility", "requests"].includes(key)) {
let msg = ""; let msg = "";
if (input[key] === null) { if (input[key] === null) {
msg = `Cleared ${key}`; msg = `Cleared ${key}`;
} else if (key == "assignedTo") {
const user = await getUser(input.assignedTo);
if (!user) continue;
msg = `Assigned to ${user.firstName + " " + user.lastName}`;
} else if (key == "requests") { } else if (key == "requests") {
msg = `Updated ${key} to '${input[key].join(", ")}'`; msg = `Updated ${key} to '${input[key].join(", ")}'`;
} else { } else {
@@ -271,17 +272,6 @@ export async function updatePermit(
user user
); );
if (key == "assignedTo" && input[key] != null) {
await createAlert(
user.tenantId,
`You are assigned to ${updatePermitResult.permitNumber}`,
"user",
input.assignedTo,
updatePermitResult.pid,
"permits"
);
}
if (key == "requests" && input[key] != null) { if (key == "requests" && input[key] != null) {
const requestAlertsUsers = [ const requestAlertsUsers = [
"6830d9ac46971e8148fda973", //Lucy "6830d9ac46971e8148fda973", //Lucy
@@ -314,6 +304,40 @@ export async function updatePermit(
updatePermitResult.markModified("clientData"); updatePermitResult.markModified("clientData");
await updatePermitResult.save(); await updatePermitResult.save();
} }
} else if (key == "assignedTo") {
const newAssignees = arrayDiff(
updatePermitResult.assignedTo.map((item) => item._id),
oldPermitResult.assignedTo
);
if (newAssignees.length == 0) continue;
let msg = "Assigned to:\n\n";
for (const assignee of newAssignees) {
const user = await getUser(assignee);
if (!user) continue;
msg += `${user.firstName + " " + user.lastName}\n`;
await createAlert(
user.tenantId,
`You are assigned to ${updatePermitResult.permitNumber}`,
"user",
assignee,
updatePermitResult.pid,
"permits"
);
}
await createNote(
{
content: msg,
},
permitId,
"permits",
user
);
} }
} }
@@ -391,7 +415,7 @@ export async function searchPermit(
from: "users", from: "users",
localField: "assignedTo", localField: "assignedTo",
foreignField: "_id", foreignField: "_id",
as: "assignedRec", as: "assignedTo",
}, },
}, },
{ {
@@ -444,13 +468,14 @@ export async function searchPermit(
deed: 1, deed: 1,
requests: 1, requests: 1,
assignedTo: { assignedTo: {
$let: { $map: {
vars: { assigned: { $arrayElemAt: ["$assignedRec", 0] } }, input: "$assignedTo",
as: "user",
in: { in: {
_id: "$$assigned._id", _id: "$$user._id",
pid: "$$assigned.pid", pid: "$$user.pid",
name: "$$assigned.name", name: "$$user.name",
avatar: "$$assigned.avatar", avatar: "$$user.avatar",
}, },
}, },
}, },

11
src/utils/diff.ts Normal file
View File

@@ -0,0 +1,11 @@
import mongoose from "mongoose";
export function arrayDiff(
newArr: Array<mongoose.Types.ObjectId>,
oldArr: Array<mongoose.Types.ObjectId>
) {
const convertedNewArr = newArr.map((item) => item.toString());
const convertedOldArr = oldArr.map((item) => item.toString());
return convertedNewArr.filter((item) => !convertedOldArr.includes(item));
}