diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index 8c175a4b..296f7d51 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -2035,6 +2035,87 @@ 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")) { +// // 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")) { +// // 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" }); +// } + +// 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; @@ -2057,7 +2138,7 @@ exports.updateBranchOrCompanyDetails = async (request, reply) => { (updateData.city && updateData.city !== existing.city) || (updateData.officeName && updateData.officeName !== existing.officeName) ) { - await Deparments.updateMany( + await Departments.updateMany( { officeName: existing.officeName, city: existing.city }, { $set: { @@ -2068,6 +2149,7 @@ exports.updateBranchOrCompanyDetails = async (request, reply) => { ); } } + } else if (id.startsWith("AWCI")) { // Find existing City before update const existing = await City.findOne({ cityId: id }); @@ -2084,7 +2166,7 @@ exports.updateBranchOrCompanyDetails = async (request, reply) => { (updateData.city && updateData.city !== existing.city) || (updateData.officeName && updateData.officeName !== existing.officeName) ) { - await Deparments.updateMany( + await Departments.updateMany( { officeName: existing.officeName, city: existing.city }, { $set: { @@ -2103,14 +2185,36 @@ exports.updateBranchOrCompanyDetails = async (request, reply) => { return reply.code(404).send({ message: "Record not found" }); } + // ✅ Handle profile picture update if provided + if (updateData.picture) { + const supportedFormats = ["jpg", "jpeg", "png"]; + const fileExtension = updateData.picture.split(".").pop().toLowerCase(); + + if (!supportedFormats.includes(fileExtension)) { + return reply.code(400).send({ + error: "Invalid file format", + message: "Picture must be a JPEG, PNG, or JPG image" + }); + } + + await CompanyProfilePicture.findOneAndUpdate( + { customerId: id }, + { customerId: id, picture: updateData.picture }, + { upsert: true, new: true } + ); + } + + return reply.send({ message: "Details updated successfully", data: updatedDoc, + picture: updateData.picture || null }); + } catch (err) { request.log.error(err); return reply .code(500) .send({ error: "Failed to update details", details: err.message }); } -}; +}; \ No newline at end of file diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index 76798205..b7c1238c 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -241,7 +241,9 @@ fastify.route({ longitude: { type: "number" }, latitude: { type: "number" }, googleLocation: { type: "string" }, - gstNo: { type: "string" } + gstNo: { type: "string" }, + picture: { type: "string" } + }, additionalProperties: true, // allow extra fields if needed },