|
|
|
@ -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" });
|
|
|
|
|
}
|
|
|
|
|