From 9a510b0bffad5dbedc3b0e1ef39ca5160937778b Mon Sep 17 00:00:00 2001 From: Varun Date: Mon, 30 Dec 2024 15:49:20 +0530 Subject: [PATCH] edit quotation --- src/controllers/storeController.js | 51 ++++++++++++++++++++++++++++++ src/models/store.js | 1 + src/routes/storeRoute.js | 43 +++++++++++++++++++++++++ 3 files changed, 95 insertions(+) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 697ddfcb..c02a0585 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -1338,6 +1338,57 @@ exports.createquotationforSensor = async (req, reply) => { }; +exports.editQuotationForSensor = async (req, reply) => { + try { + const { quatationId } = req.params; // Get the ID of the quotation to edit + const { masters, slaves, sensors, motor_switches, electricals } = req.body; + + // Format electricals field + const formattedElectricals = electricals.map((item) => ({ + type: item.type || "", + wire: item.wire || "", + switch: item.switch || "", + text: item.text || "", + })); + + // Find and update the quotation + const updatedQuotation = await SensorQuotation.findOneAndUpdate( + { quatationId }, + { + masters, + slaves, + sensors, + motor_switches, + electricals: formattedElectricals, + updated_at: dayjs().tz("Asia/Kolkata").format('DD-MMM-YYYY - HH:mm'), + }, + { new: true } // Return the updated document + ); + + if (!updatedQuotation) { + return reply.code(404).send({ + success: false, + message: 'Quotation not found.', + }); + } + + reply.code(200).send({ + success: true, + message: 'Quotation updated successfully.', + data: updatedQuotation, + }); + } catch (error) { + console.error('Error updating quotation:', error); + reply.code(500).send({ + success: false, + message: 'Failed to update quotation.', + error: error.message, + }); + } +}; + + + exports.getallquotationdata = async (req, reply) => { try { await SensorQuotation.find({}) diff --git a/src/models/store.js b/src/models/store.js index 8c59578b..c207b0b6 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -292,6 +292,7 @@ const sensorquotationSchema = new mongoose.Schema({ quoted_amount: { type: String, default: null }, comments: { type: String, default: null }, datetime: { type: String, default: null }, + updated_at: { type: String, default: null }, electricals: [ { type: { type: String, default: null }, diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index f401687d..fdb814e4 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -1178,6 +1178,49 @@ fastify.post("/api/createquotationforSensor/:installationId", { }); + +fastify.post("/api/editQuotationForSensor/:quatationId", { + schema: { + description: "This is for edit quotation for sensors", + tags: ["Install"], + summary: "This is for edit quotation for sensors", + params: { + required: ["quatationId"], + type: "object", + properties: { + quatationId: { + type: "string", + description: "quatationId", + }, + }, + }, + body: { + type: "object", + properties: { + + masters: { type: "string" }, + slaves: { type: "string" }, + sensors: { type: "string" }, + motor_switches: { type: "string" }, + electricals: { + type: "array", + maxItems: 2500, + items: { + type: "object", + properties: { + type: { type: "string", default: null }, + wire: { type: "string", default: null }, + switch: { type: "string", default: null }, + text: { type: "string", default: null }, + }, + }, + }, + }, + }, + }, + handler: storeController.editQuotationForSensor, +}); + fastify.post("/api/getquotationofinstalleranduser/:installationId", { schema: { tags: ["Install"],