ashok 1 month ago
commit 931e7a5bed

@ -2035,87 +2035,6 @@ 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) => { exports.updateBranchOrCompanyDetails = async (request, reply) => {
try { try {
const { id } = request.params; const { id } = request.params;
@ -2138,7 +2057,7 @@ exports.updateBranchOrCompanyDetails = async (request, reply) => {
(updateData.city && updateData.city !== existing.city) || (updateData.city && updateData.city !== existing.city) ||
(updateData.officeName && updateData.officeName !== existing.officeName) (updateData.officeName && updateData.officeName !== existing.officeName)
) { ) {
await Departments.updateMany( await Deparments.updateMany(
{ officeName: existing.officeName, city: existing.city }, { officeName: existing.officeName, city: existing.city },
{ {
$set: { $set: {
@ -2149,7 +2068,6 @@ exports.updateBranchOrCompanyDetails = async (request, reply) => {
); );
} }
} }
} else if (id.startsWith("AWCI")) { } else if (id.startsWith("AWCI")) {
// Find existing City before update // Find existing City before update
const existing = await City.findOne({ cityId: id }); const existing = await City.findOne({ cityId: id });
@ -2166,7 +2084,7 @@ exports.updateBranchOrCompanyDetails = async (request, reply) => {
(updateData.city && updateData.city !== existing.city) || (updateData.city && updateData.city !== existing.city) ||
(updateData.officeName && updateData.officeName !== existing.officeName) (updateData.officeName && updateData.officeName !== existing.officeName)
) { ) {
await Departments.updateMany( await Deparments.updateMany(
{ officeName: existing.officeName, city: existing.city }, { officeName: existing.officeName, city: existing.city },
{ {
$set: { $set: {
@ -2185,36 +2103,14 @@ exports.updateBranchOrCompanyDetails = async (request, reply) => {
return reply.code(404).send({ message: "Record not found" }); 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({ return reply.send({
message: "Details updated successfully", message: "Details updated successfully",
data: updatedDoc, data: updatedDoc,
picture: updateData.picture || null
}); });
} catch (err) { } catch (err) {
request.log.error(err); request.log.error(err);
return reply return reply
.code(500) .code(500)
.send({ error: "Failed to update details", details: err.message }); .send({ error: "Failed to update details", details: err.message });
} }
}; };

@ -242,8 +242,6 @@ fastify.route({
latitude: { type: "number" }, latitude: { type: "number" },
googleLocation: { type: "string" }, googleLocation: { type: "string" },
gstNo: { type: "string" }, gstNo: { type: "string" },
picture: { type: "string" }
}, },
additionalProperties: true, // allow extra fields if needed additionalProperties: true, // allow extra fields if needed
}, },

Loading…
Cancel
Save