From 39c747147a85585ba9fff507022966957e8cc2e0 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 19 Mar 2025 14:28:33 +0530 Subject: [PATCH] added master_connections in survey --- src/controllers/storeController.js | 31 ++++++++++++-- src/models/store.js | 17 ++++++++ src/routes/storeRoute.js | 67 +++++++++++++++++++++++++----- 3 files changed, 101 insertions(+), 14 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 3781520b..9da81768 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -1620,7 +1620,7 @@ exports.createquotationforSensor = async (req, reply) => { const i_id = await generatequatationId(); const quatationId = `AWQU${i_id}`; const { surveyId } = req.params; - const { customerId, masters, slaves, sensors, motor_switches, electricals } = req.body; + const { customerId, masters, slaves, sensors, motor_switches, electricals,master_connections } = req.body; // Format electricals field const formattedElectricals = electricals.map((item) => ({ @@ -1629,6 +1629,16 @@ exports.createquotationforSensor = async (req, reply) => { switch: item.switch || "", text: item.text || "", })); + const formattedMasterDetails = master_connections.map((item) => ({ + master_name: item.master_name || "", + slaves: item.slaves || "", + tanks: Array.isArray(item.tanks) + ? item.tanks.map(tank => ({ + tankName: tank.tankName || "", + tankLocation: tank.tankLocation || "" + })) + : [] + })); // Fetch pricing data from Iotprice database const getPrice = async (name, type) => { @@ -1662,7 +1672,7 @@ exports.createquotationforSensor = async (req, reply) => { const newQuotation = new SensorQuotation({ quatationId, customerId, - + master_connections: formattedMasterDetails, surveyId, quote_status: "sentfromsurvey", masters, @@ -1697,7 +1707,7 @@ 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; + const { masters, slaves, sensors, motor_switches, electricals, master_connections } = req.body; // Format electricals field const formattedElectricals = electricals.map((item) => ({ @@ -1707,6 +1717,20 @@ exports.editQuotationForSensor = async (req, reply) => { text: item.text || "", })); + // Format master_connections field + const formattedMasterDetails = master_connections.map((item) => ({ + master_name: item.master_name || "", + slaves: item.slaves || "", + tanks: Array.isArray(item.tanks) + ? item.tanks.map(tank => ({ + tankName: tank.tankName || "", + tankLocation: tank.tankLocation || "" + })) + : [] + })); + + console.log("Formatted Master Connections:", formattedMasterDetails); // Debugging + // Find and update the quotation const updatedQuotation = await SensorQuotation.findOneAndUpdate( { quatationId }, @@ -1716,6 +1740,7 @@ exports.editQuotationForSensor = async (req, reply) => { sensors, motor_switches, electricals: formattedElectricals, + master_connections: formattedMasterDetails, // <- Ensure it's included updated_at: dayjs().tz("Asia/Kolkata").format('DD-MMM-YYYY - HH:mm'), }, { new: true } // Return the updated document diff --git a/src/models/store.js b/src/models/store.js index 6fe2dc64..48b6a4da 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -419,6 +419,22 @@ const sensorquotationSchema = new mongoose.Schema({ comments: { type: String, default: null }, datetime: { type: String, default: null }, updated_at: { type: String, default: null }, + + master_connections: [ + { + master_name: { type: String, default: null }, + slaves: { type: String, default: null }, + + tanks: [ + { + tankName: { type: String, default: null }, + tankLocation: { type: String, default: null }, + + }, + ], + + }, + ], electricals: [ { type: { type: String, default: null }, @@ -466,6 +482,7 @@ const orderSchema = new mongoose.Schema({ datetime: { type: String, default: null }, updated_at: { type: String, default: null }, assignedTeamMembers: [{ type: String }], + electricals: [ { type: { type: String, default: null }, diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index 2fa07d09..dbe651f7 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -1419,6 +1419,29 @@ fastify.post("/api/createquotationforSensor/:surveyId", { slaves: { type: "string" }, sensors: { type: "string" }, motor_switches: { type: "string" }, + master_connections: { + type: "array", + maxItems: 2500, + items: { + type: "object", + properties: { + master_name: { type: "string", default: null }, + slaves: { type: "string", default: null }, + tanks: { + type: "array", + items: { + type: "object", + properties: { + tankName: { type: "string", default: null }, + tankLocation: { type: "string", default: null }, + }, + }, + default: [], + }, + }, + }, + }, + electricals: { type: "array", maxItems: 2500, @@ -1554,23 +1577,22 @@ fastify.get("/api/orders/:customerId", { fastify.post("/api/editQuotationForSensor/:quatationId", { schema: { - description: "This is for edit quotation for sensors", + description: "This is for editing quotation for sensors", tags: ["Install"], - summary: "This is for edit quotation for sensors", + summary: "Edit an existing sensor quotation", params: { required: ["quatationId"], type: "object", properties: { quatationId: { type: "string", - description: "quatationId", + description: "Quotation ID", }, }, }, body: { type: "object", properties: { - masters: { type: "string" }, slaves: { type: "string" }, sensors: { type: "string" }, @@ -1579,21 +1601,44 @@ fastify.post("/api/editQuotationForSensor/:quatationId", { 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 }, + type: "object", + properties: { + type: { type: "string", default: null }, + wire: { type: "string", default: null }, + switch: { type: "string", default: null }, + text: { type: "string", default: null }, + }, + }, + }, + master_connections: { // Added master_connections to schema + type: "array", + maxItems: 2500, + items: { + type: "object", + properties: { + master_name: { type: "string", default: null }, + slaves: { type: "string", default: null }, + tanks: { + type: "array", + items: { + type: "object", + properties: { + tankName: { type: "string", default: null }, + tankLocation: { type: "string", default: null }, + }, + }, + default: [], }, + }, }, - }, + }, }, }, }, handler: storeController.editQuotationForSensor, }); + fastify.post("/api/getquotationofinstalleranduser/:surveyId", { schema: { tags: ["Install"],