From cfb98c8f9fc6c64ba1cb97429ee1fca9ed8668b1 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 10 Sep 2024 14:04:28 +0530 Subject: [PATCH] sales send back quatation and single quatation --- src/controllers/storeController.js | 113 +++++++++++++++++++++++++++-- src/controllers/tanksController.js | 28 ------- src/models/store.js | 27 ++++++- src/routes/storeRoute.js | 67 ++++++++++++++++- 4 files changed, 195 insertions(+), 40 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index d2a8ee73..2385621e 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -10,7 +10,7 @@ const fastify = require("fastify")({ return uuidv4(); }, }); -const { Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,WaterLeverSensor,MotorSwitchSenso,Insensors} = require("../models/store"); +const { Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,WaterLeverSensor,MotorSwitchSenso,Insensors,generatequatationId} = require("../models/store"); const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfilePicture} = require('../models/User') @@ -1075,36 +1075,34 @@ exports.getusersofParticularInstaller = async (req, reply) => { exports.createquotationforSensor = async (req, reply) => { try { - // Extract parameters and body data from the request + const i_id = await generatequatationId(); + const quatationId = `AWQU${i_id}`; const { installationId } = req.params; const { customerId, masters, slaves, motor_switches, electricals,master_type,sensor_type,switch_type } = req.body; - // Validate electricals field to ensure it's an array of objects // Create a new SensorQuotation document const newQuotation = new SensorQuotation({ + quatationId, customerId: customerId, - installationId: installationId, // Using installationId for the field + installationId: installationId, masters, slaves, motor_switches, electricals, switch_type, master_type, - sensor_type// This will store the array of electrical items (objects) + sensor_type }); - // Save the document to the database const savedQuotation = await newQuotation.save(); - // Send a success response with the saved document reply.code(200).send({ success: true, message: 'Quotation for sensors created successfully.', data: savedQuotation, }); } catch (error) { - // Handle errors and send error response console.error('Error creating quotation:', error); reply.code(500).send({ success: false, @@ -1131,3 +1129,102 @@ exports.getallquotationdata = async (req, reply) => { }; + +exports.saveQuotationData = async (req, reply) => { + try { + const { quotationId } = req.params; // Retrieve the quotationId from the request parameters + + // Fetch the quotation data from the database + const quotation = await SensorQuotation.findOne({ quatationId: quotationId }); + console.log(quotation) + if (!quotation) { + return reply.code(404).send({ + success: false, + message: 'Quotation not found.' + }); + } + + // Extract the price-per-unit and total price values from the request body + const { + masters_quantity_price, + masters_total_price, + slaves_quantity_price, + slaves_total_price, + motor_switches_quantity_price, + motor_switches_total_price, + electricals_quantity_price, + electricals_total_price, + master_type_quantity_price, + master_type_total_price, + sensor_type_quantity_price, + sensor_type_total_price, + switch_type_quantity_price, + switch_type_total_price, + quotation_total_price + } = req.body; + + // Update the quotation document with the data from the request body + quotation.masters_quantity_price = masters_quantity_price; + quotation.masters_total_price = masters_total_price; + quotation.slaves_quantity_price = slaves_quantity_price; + quotation.slaves_total_price = slaves_total_price; + quotation.motor_switches_quantity_price = motor_switches_quantity_price; + quotation.motor_switches_total_price = motor_switches_total_price; + quotation.electricals_quantity_price = electricals_quantity_price; + quotation.electricals_total_price = electricals_total_price; + quotation.master_type_quantity_price = master_type_quantity_price; + quotation.master_type_total_price = master_type_total_price; + quotation.sensor_type_quantity_price = sensor_type_quantity_price; + quotation.sensor_type_total_price = sensor_type_total_price; + quotation.switch_type_quantity_price = switch_type_quantity_price; + quotation.switch_type_total_price = switch_type_total_price; + quotation.qutation_total_price = quotation_total_price; + + // Save the updated document back to the database + await quotation.save(); + + // Send back the updated data + reply.code(200).send({ + success: true, + message: 'Quotation data saved successfully.', + data: quotation + }); + } catch (error) { + console.error('Error saving quotation data to the database:', error); + reply.code(500).send({ + success: false, + message: 'Failed to save quotation data.', + error: error.message, + }); + } +}; + + +exports.getSinleQuotationData = async (req, reply) => { + try { + const { quotationId } = req.params; // Correct the parameter name + + // Fetch the quotation data from the database + const quotation = await SensorQuotation.findOne({ quatationId: quotationId }); // Correctly referencing quotationId + + if (!quotation) { + return reply.code(404).send({ + success: false, + message: 'Quotation not found.' + }); + } + + reply.code(200).send({ + success: true, + message: 'Quotation data retrieved successfully.', + data: quotation + }); + } catch (error) { + console.error('Error fetching quotation data:', error); + reply.code(500).send({ + success: false, + message: 'Failed to retrieve quotation data.', + error: error.message, + }); + } +}; diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index a89267ac..798e5a3c 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1417,10 +1417,6 @@ exports.motorAction = async (req, reply) => { // Send immediate response to the client reply.code(200).send({ message: "Motor stopped successfully." }); - if (fcmToken) { - await sendNotification(fcmToken, 'Motor Stopped', `Motor ${motorId} has been stopped.`); - } - // Perform stop operations in the background (async () => { await delay(300000); @@ -1449,11 +1445,6 @@ exports.motorAction = async (req, reply) => { } ); - if (receiverFinalWaterLevel / parseInt(receiverTank.capacity, 10) * 100 >= 90) { - if (fcmToken) { - await sendNotification(fcmToken, 'High Water Level Alert', `Water level in tank ${motorData.receiverTank} has reached 90%.`); - } - } } })(); @@ -1465,10 +1456,6 @@ exports.motorAction = async (req, reply) => { { customerId, "connections.inputConnections.motor_id": motorId }, { $set: { "connections.inputConnections.$.motor_stop_status": "2" } } ); - // Send start notification - if (fcmToken) { - await sendNotification(fcmToken, 'Motor Started', `Motor ${motorId} has been started.`); - } } // Check threshold settings if action is start @@ -1530,10 +1517,6 @@ exports.motorAction = async (req, reply) => { } } ); - // Send notification after motor stops due to time threshold - if (fcmToken) { - await sendNotification(fcmToken, 'Motor Stopped After Time Threshold', `Motor ${motorId} has been stopped after reaching the set time threshold.`); - } clearInterval(intervalId); await delay(300000); @@ -1559,11 +1542,6 @@ exports.motorAction = async (req, reply) => { } } ); - if (receiverFinalWaterLevel / parseInt(receiverTank.capacity, 10) * 100 <= 20) { - if (fcmToken) { - await sendNotification(fcmToken, 'Low Water Level Alert', `Water level in tank ${motorData.receiverTank} has dropped below 20%.`); - } - } } } }, 60000); @@ -1661,12 +1639,6 @@ exports.motorAction = async (req, reply) => { } } ); - // Send low water level notification - if (receiverFinalWaterLevel / parseInt(receiverTank.capacity, 10) * 100 <= 20) { - if (fcmToken) { - await sendNotification(fcmToken, 'Low Water Level Alert', `Water level in tank ${motorData.receiverTank} has dropped below 20%.`); - } - } } } }, 20000); // Check water level every minute diff --git a/src/models/store.js b/src/models/store.js index c06a2455..15146ae1 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -15,6 +15,16 @@ const generateinstallationId = async () => { return result.seq; }; + const generatequatationId = async () => { + var result = await Counter.findOneAndUpdate( + { _id: 'installation_id' }, + { $inc: { seq: 1 } }, + { upsert: true, new: true } + ); + + return result.seq; + }; + const installationschema = new mongoose.Schema({ // name: { type: String }, phone: { type: String, unique: true, trim: true }, @@ -254,16 +264,29 @@ const insensorsSchema = new mongoose.Schema({ const sensorquotationSchema = new mongoose.Schema({ customerId: { type: String }, installationId: { type: String, default: null }, + quatationId: { type: String, default: null }, masters: { type: String }, + masters_quantity_price: { type: String }, + masters_total_price: { type: String }, slaves: { type: String }, + slaves_quantity_price: { type: String }, + slaves_total_price: { type: String }, motor_switches: { type: String }, + motor_switches_quantity_price: { type: String }, + motor_switches_total_price: { type: String }, quote_status: { type: String, default: null }, quoted_amount: { type: String, default: null }, comments: { type: String, default: null }, master_type: { type: String, default: null }, + master_type_quantity_price: { type: String, default: null }, + master_type_total_price: { type: String, default: null }, sensor_type: { type: String , default: null}, + sensor_type_quantity_price: { type: String , default: null}, + sensor_type_total_price: { type: String , default: null}, switch_type: { type: String, default: null }, - + switch_type_quantity_price: { type: String, default: null }, + switch_type_total_price: { type: String, default: null }, + qutation_total_price: { type: String, default: null }, }); @@ -279,4 +302,4 @@ const sensorquotationSchema = new mongoose.Schema({ const Install = mongoose.model("Install", installationschema); - module.exports = { Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,}; + module.exports = { Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,generatequatationId}; diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index 2b2f6ed6..a179b77a 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -766,9 +766,9 @@ fastify.get("/api/getbatchnumbers/:storeId", { fastify.post("/api/createquotationforSensor/:installationId", { schema: { - description: "This is for sending quotation for sensors", + description: "This is for create quotation for sensors", tags: ["Install"], - summary: "This is for sending quotation for sensors", + summary: "This is for create quotation for sensors", params: { required: ["installationId"], type: "object", @@ -818,5 +818,68 @@ fastify.get("/api/getallquotationdata", { // preHandler: fastify.auth([fastify.authenticate]), handler: storeController.getallquotationdata, }); + +fastify.post("/api/sendBackQuotationforSensor/:quotationId", { + schema: { + description: "This is for sending back the quotation for sensors", + tags: ["Install"], + summary: "Send back quotation for sensors", + params: { + type: "object", + properties: { + quotationId: { + type: "string", + description: "quotationId", + }, + }, + }, + body: { + type: "object", + properties: { + masters_quantity_price: { type: "string" }, + masters_total_price: { type: "string" }, + slaves_quantity_price: { type: "string" }, + slaves_total_price: { type: "string" }, + motor_switches_quantity_price: { type: "string" }, + motor_switches_total_price: { type: "string" }, + electricals_quantity_price: { type: "string" }, + electricals_total_price: { type: "string" }, + master_type_quantity_price: { type: "string" }, + master_type_total_price: { type: "string" }, + sensor_type_quantity_price: { type: "string" }, + sensor_type_total_price: { type: "string" }, + switch_type_quantity_price: { type: "string" }, + switch_type_total_price: { type: "string" }, + quotation_total_price: { type: "string" }, + }, + }, + }, + handler: storeController.saveQuotationData, +}); + +fastify.get("/api/getSingleQuotationData/:quotationId", { + schema: { + tags: ["Install"], + description: "This is for Get Single Quotation Data", + summary: "This is to Get Single Quotation Data", + params: { + type: "object", + properties: { + quotationId: { + type: "string", + description: "quotationId", + }, + }, + }, + + security: [ + { + basicAuth: [], + }, + ], + }, + handler: storeController.getSinleQuotationData, +}); + next(); };