feat: convert assignedTo field to string for permits
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { z } from "zod";
|
||||
import mongoose from "mongoose";
|
||||
import mongoose, { Schema } from "mongoose";
|
||||
import { buildJsonSchemas } from "fastify-zod";
|
||||
import { pageMetadata, pageQueryParams } from "../pagination";
|
||||
|
||||
@@ -38,7 +38,7 @@ const permitSchema = new mongoose.Schema({
|
||||
permitType: String,
|
||||
utility: String,
|
||||
assignedTo: {
|
||||
type: mongoose.Types.ObjectId,
|
||||
type: [Schema.Types.ObjectId],
|
||||
ref: "user",
|
||||
},
|
||||
link: String,
|
||||
@@ -158,7 +158,7 @@ const permitCore = {
|
||||
cleanStatus: z.string().optional(),
|
||||
permitType: z.string().optional(),
|
||||
utility: z.string().nullable().optional(),
|
||||
assignedTo: z.string().nullable().optional(),
|
||||
assignedTo: z.array(z.string()).nullable().optional(),
|
||||
link: z.string().optional(),
|
||||
address: z
|
||||
.object({
|
||||
|
||||
@@ -20,6 +20,7 @@ import { createNote } from "../note/note.service";
|
||||
import { createAlert } from "../alert/alert.service";
|
||||
import { processedModel } from "../processed/processed.schema";
|
||||
import { orgModel } from "../organization/organization.schema";
|
||||
import { arrayDiff } from "../utils/diff";
|
||||
|
||||
export async function createPermit(
|
||||
input: CreatePermitInput,
|
||||
@@ -137,7 +138,7 @@ export async function listPermits(
|
||||
from: "users",
|
||||
localField: "assignedTo",
|
||||
foreignField: "_id",
|
||||
as: "assignedRec",
|
||||
as: "assignedTo",
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -190,13 +191,14 @@ export async function listPermits(
|
||||
deed: 1,
|
||||
requests: 1,
|
||||
assignedTo: {
|
||||
$let: {
|
||||
vars: { assigned: { $arrayElemAt: ["$assignedRec", 0] } },
|
||||
$map: {
|
||||
input: "$assignedTo",
|
||||
as: "user",
|
||||
in: {
|
||||
_id: "$$assigned._id",
|
||||
pid: "$$assigned.pid",
|
||||
name: "$$assigned.name",
|
||||
avatar: "$$assigned.avatar",
|
||||
_id: "$$user._id",
|
||||
pid: "$$user.pid",
|
||||
name: "$$user.name",
|
||||
avatar: "$$user.avatar",
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -232,6 +234,10 @@ export async function updatePermit(
|
||||
permitId: string,
|
||||
user: AuthenticatedUser
|
||||
) {
|
||||
const oldPermitResult = await permitModel.findOne(
|
||||
{ pid: permitId },
|
||||
{ assignedTo: 1 }
|
||||
);
|
||||
const updatePermitResult = await permitModel
|
||||
.findOneAndUpdate(
|
||||
{
|
||||
@@ -246,16 +252,11 @@ export async function updatePermit(
|
||||
|
||||
if (updatePermitResult) {
|
||||
for (const key in input) {
|
||||
if (["manualStatus", "utility", "assignedTo", "requests"].includes(key)) {
|
||||
if (["manualStatus", "utility", "requests"].includes(key)) {
|
||||
let msg = "";
|
||||
|
||||
if (input[key] === null) {
|
||||
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") {
|
||||
msg = `Updated ${key} to '${input[key].join(", ")}'`;
|
||||
} else {
|
||||
@@ -271,17 +272,6 @@ export async function updatePermit(
|
||||
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) {
|
||||
const requestAlertsUsers = [
|
||||
"6830d9ac46971e8148fda973", //Lucy
|
||||
@@ -314,6 +304,40 @@ export async function updatePermit(
|
||||
updatePermitResult.markModified("clientData");
|
||||
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",
|
||||
localField: "assignedTo",
|
||||
foreignField: "_id",
|
||||
as: "assignedRec",
|
||||
as: "assignedTo",
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -444,13 +468,14 @@ export async function searchPermit(
|
||||
deed: 1,
|
||||
requests: 1,
|
||||
assignedTo: {
|
||||
$let: {
|
||||
vars: { assigned: { $arrayElemAt: ["$assignedRec", 0] } },
|
||||
$map: {
|
||||
input: "$assignedTo",
|
||||
as: "user",
|
||||
in: {
|
||||
_id: "$$assigned._id",
|
||||
pid: "$$assigned.pid",
|
||||
name: "$$assigned.name",
|
||||
avatar: "$$assigned.avatar",
|
||||
_id: "$$user._id",
|
||||
pid: "$$user.pid",
|
||||
name: "$$user.name",
|
||||
avatar: "$$user.avatar",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
11
src/utils/diff.ts
Normal file
11
src/utils/diff.ts
Normal 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));
|
||||
}
|
||||
Reference in New Issue
Block a user