sales send back quatation and single quatation

master
Bhaskar 1 year ago
parent 2a241a6c62
commit cfb98c8f9f

@ -10,7 +10,7 @@ const fastify = require("fastify")({
return uuidv4(); 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') const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfilePicture} = require('../models/User')
@ -1075,36 +1075,34 @@ exports.getusersofParticularInstaller = async (req, reply) => {
exports.createquotationforSensor = async (req, reply) => { exports.createquotationforSensor = async (req, reply) => {
try { try {
// Extract parameters and body data from the request const i_id = await generatequatationId();
const quatationId = `AWQU${i_id}`;
const { installationId } = req.params; const { installationId } = req.params;
const { customerId, masters, slaves, motor_switches, electricals,master_type,sensor_type,switch_type } = req.body; 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 // Create a new SensorQuotation document
const newQuotation = new SensorQuotation({ const newQuotation = new SensorQuotation({
quatationId,
customerId: customerId, customerId: customerId,
installationId: installationId, // Using installationId for the field installationId: installationId,
masters, masters,
slaves, slaves,
motor_switches, motor_switches,
electricals, electricals,
switch_type, switch_type,
master_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(); const savedQuotation = await newQuotation.save();
// Send a success response with the saved document
reply.code(200).send({ reply.code(200).send({
success: true, success: true,
message: 'Quotation for sensors created successfully.', message: 'Quotation for sensors created successfully.',
data: savedQuotation, data: savedQuotation,
}); });
} catch (error) { } catch (error) {
// Handle errors and send error response
console.error('Error creating quotation:', error); console.error('Error creating quotation:', error);
reply.code(500).send({ reply.code(500).send({
success: false, 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,
});
}
};

@ -1417,10 +1417,6 @@ exports.motorAction = async (req, reply) => {
// Send immediate response to the client // Send immediate response to the client
reply.code(200).send({ message: "Motor stopped successfully." }); 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 // Perform stop operations in the background
(async () => { (async () => {
await delay(300000); 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 }, { customerId, "connections.inputConnections.motor_id": motorId },
{ $set: { "connections.inputConnections.$.motor_stop_status": "2" } } { $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 // 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); clearInterval(intervalId);
await delay(300000); 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); }, 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 }, 20000); // Check water level every minute

@ -15,6 +15,16 @@ const generateinstallationId = async () => {
return result.seq; 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({ const installationschema = new mongoose.Schema({
// name: { type: String }, // name: { type: String },
phone: { type: String, unique: true, trim: true }, phone: { type: String, unique: true, trim: true },
@ -254,16 +264,29 @@ const insensorsSchema = new mongoose.Schema({
const sensorquotationSchema = new mongoose.Schema({ const sensorquotationSchema = new mongoose.Schema({
customerId: { type: String }, customerId: { type: String },
installationId: { type: String, default: null }, installationId: { type: String, default: null },
quatationId: { type: String, default: null },
masters: { type: String }, masters: { type: String },
masters_quantity_price: { type: String },
masters_total_price: { type: String },
slaves: { type: String }, slaves: { type: String },
slaves_quantity_price: { type: String },
slaves_total_price: { type: String },
motor_switches: { type: String }, motor_switches: { type: String },
motor_switches_quantity_price: { type: String },
motor_switches_total_price: { type: String },
quote_status: { type: String, default: null }, quote_status: { type: String, default: null },
quoted_amount: { type: String, default: null }, quoted_amount: { type: String, default: null },
comments: { type: String, default: null }, comments: { type: String, default: null },
master_type: { 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: { 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: { 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); 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};

@ -766,9 +766,9 @@ fastify.get("/api/getbatchnumbers/:storeId", {
fastify.post("/api/createquotationforSensor/:installationId", { fastify.post("/api/createquotationforSensor/:installationId", {
schema: { schema: {
description: "This is for sending quotation for sensors", description: "This is for create quotation for sensors",
tags: ["Install"], tags: ["Install"],
summary: "This is for sending quotation for sensors", summary: "This is for create quotation for sensors",
params: { params: {
required: ["installationId"], required: ["installationId"],
type: "object", type: "object",
@ -818,5 +818,68 @@ fastify.get("/api/getallquotationdata", {
// preHandler: fastify.auth([fastify.authenticate]), // preHandler: fastify.auth([fastify.authenticate]),
handler: storeController.getallquotationdata, 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(); next();
}; };

Loading…
Cancel
Save