From 75fc88b0595709de44788ed7c2d6c322c45a3672 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 22 Aug 2025 16:38:49 +0530 Subject: [PATCH] Edit department details by branchId or cityId --- src/controllers/departmentController.js | 142 +++++++++++++++++++++++- 1 file changed, 140 insertions(+), 2 deletions(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index 81c1c459..8c175a4b 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -1937,6 +1937,104 @@ exports.getStaffDepartmentDetails = async (request, reply) => { +// exports.updateBranchOrCompanyDetails = async (request, reply) => { +// try { +// const { id } = request.params; +// const updateData = request.body; +// let updatedDoc; + +// if (id.startsWith("AWBR")) { +// // Update Branch +// updatedDoc = await Branch.findOneAndUpdate( +// { branchId: id }, +// { ...updateData, updatedAt: new Date() }, +// { new: true } +// ); +// } else if (id.startsWith("AWCI")) { +// // Update City +// updatedDoc = await City.findOneAndUpdate( +// { cityId: id }, +// { ...updateData, updatedAt: new Date() }, +// { new: true } +// ); +// } else { +// return reply.code(400).send({ error: "Invalid ID format" }); +// } + +// if (!updatedDoc) { +// return reply.code(404).send({ message: "Record not found" }); +// } + +// return reply.send({ +// message: "Details updated successfully", +// data: updatedDoc, +// }); +// } catch (err) { +// request.log.error(err); +// return reply +// .code(500) +// .send({ error: "Failed to update details", details: err.message }); +// } +// }; + + +// exports.updateBranchOrCompanyDetails = async (request, reply) => { +// try { +// const { id } = request.params; +// const updateData = request.body; +// let updatedDoc; + +// if (id.startsWith("AWBR")) { +// // Update Branch +// updatedDoc = await Branch.findOneAndUpdate( +// { branchId: id }, +// { ...updateData, updatedAt: new Date() }, +// { new: true } +// ); + +// // Update department schema if office name matches +// if (updateData.city) { +// await Deparments.updateMany( +// { officeName: updatedDoc.officeName }, +// { $set: { city: updateData.city } } +// ); +// } +// } else if (id.startsWith("AWCI")) { +// // Update City +// updatedDoc = await City.findOneAndUpdate( +// { cityId: id }, +// { ...updateData, updatedAt: new Date() }, +// { new: true } +// ); + +// // Update department schema if office name matches +// if (updateData.city) { +// await Department.updateMany( +// { officeName: updatedDoc.officeName }, +// { $set: { city: updateData.city } } +// ); +// } +// } else { +// return reply.code(400).send({ error: "Invalid ID format" }); +// } + +// if (!updatedDoc) { +// return reply.code(404).send({ message: "Record not found" }); +// } + +// return reply.send({ +// message: "Details updated successfully", +// data: updatedDoc, +// }); +// } catch (err) { +// request.log.error(err); +// return reply +// .code(500) +// .send({ error: "Failed to update details", details: err.message }); +// } +// }; + + exports.updateBranchOrCompanyDetails = async (request, reply) => { try { const { id } = request.params; @@ -1944,19 +2042,59 @@ exports.updateBranchOrCompanyDetails = async (request, reply) => { let updatedDoc; if (id.startsWith("AWBR")) { - // Update Branch + // Find existing Branch before update + const existing = await Branch.findOne({ branchId: id }); + updatedDoc = await Branch.findOneAndUpdate( { branchId: id }, { ...updateData, updatedAt: new Date() }, { new: true } ); + + // 🔄 Cascade update to Department employees if city/officeName changed + if (updatedDoc && existing) { + if ( + (updateData.city && updateData.city !== existing.city) || + (updateData.officeName && updateData.officeName !== existing.officeName) + ) { + await Deparments.updateMany( + { officeName: existing.officeName, city: existing.city }, + { + $set: { + city: updateData.city || existing.city, + officeName: updateData.officeName || existing.officeName, + }, + } + ); + } + } } else if (id.startsWith("AWCI")) { - // Update City + // Find existing City before update + const existing = await City.findOne({ cityId: id }); + updatedDoc = await City.findOneAndUpdate( { cityId: id }, { ...updateData, updatedAt: new Date() }, { new: true } ); + + // 🔄 Cascade update to Department employees if city/officeName changed + if (updatedDoc && existing) { + if ( + (updateData.city && updateData.city !== existing.city) || + (updateData.officeName && updateData.officeName !== existing.officeName) + ) { + await Deparments.updateMany( + { officeName: existing.officeName, city: existing.city }, + { + $set: { + city: updateData.city || existing.city, + officeName: updateData.officeName || existing.officeName, + }, + } + ); + } + } } else { return reply.code(400).send({ error: "Invalid ID format" }); }