diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index 45f0b488..ff6c45af 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -1866,4 +1866,46 @@ exports.getCitiesBasedState = async (request, reply) => { } catch (err) { reply.status(500).send({ message: err.message }); } -}; \ No newline at end of file +}; + + + +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 }); + } +}; diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index a948d45a..428da809 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -207,6 +207,46 @@ fastify.route({ } }, handler: departmentController.getDetails +}); +fastify.route({ + method: "PUT", + url: "/api/updateBranchOrCompanydetails/:id", + schema: { + tags: ["Department"], + description: "Update details of a branch or city", + summary: "Edit department details by branchId or cityId", + params: { + type: "object", + properties: { + id: { type: "string" }, // branchId or cityId + }, + required: ["id"], + }, + body: { + type: "object", + properties: { + phone: { type: "string" }, + land_line_number: { type: "string" }, + officeName: { type: "string" }, + office_address1: { type: "string" }, + address2: { type: "string" }, + email: { type: "string" }, + pincode: { type: "string" }, + zone: { type: "string" }, + city: { type: "string" }, + state: { type: "string" }, + country: { type: "string" }, + nameoftheContactPerson: { type: "string" }, + location: { type: "array", items: { type: "string" } }, + longitude: { type: "number" }, + latitude: { type: "number" }, + googleLocation: { type: "string" }, + gstNo: { type: "string" } + }, + additionalProperties: true, // allow extra fields if needed + }, + }, + handler: departmentController.updateBranchOrCompanyDetails }); fastify.route({ method: "GET",