changes in quotation

master^2
Varun 5 months ago
parent d42b4fef5a
commit f911f31781

@ -2251,12 +2251,9 @@ exports.acceptQuotation = async (req, reply) => {
const { quotationId } = req.params; const { quotationId } = req.params;
let { action, storeId } = req.body; let { action, storeId } = req.body;
action = action.toLowerCase(); // Convert action to lowercase action = action.toLowerCase();
// Find the quotation by ID
const quotation = await SensorQuotation.findOne({ quatationId: quotationId }); const quotation = await SensorQuotation.findOne({ quatationId: quotationId });
console.log(quotation,"quotation")
if (!quotation) { if (!quotation) {
return reply.status(404).send({ error: "Quotation not found" }); return reply.status(404).send({ error: "Quotation not found" });
} }
@ -2270,19 +2267,16 @@ exports.acceptQuotation = async (req, reply) => {
}); });
} else if (action === "accept") { } else if (action === "accept") {
const { customerId, masters, slaves, sensors, master_connections } = quotation; const { customerId, masters, slaves, sensors, master_connections } = quotation;
console.log(customerId,masters,slaves,sensors,master_connections)
// Convert quotation to an order object and include storeId
const newOrder = new Order({ const newOrder = new Order({
...quotation.toObject(), ...quotation.toObject(),
storeId, storeId,
status: "pending", status: "pending",
}); });
// Save the order
await newOrder.save(); await newOrder.save();
// **Step 1: Block Masters in Insensors** // Step 1: Block Masters
let blockedMasters = []; let blockedMasters = [];
if (parseInt(masters) > 0) { if (parseInt(masters) > 0) {
const availableMasters = await Insensors.find({ storeId, type: "master", status: "available" }) const availableMasters = await Insensors.find({ storeId, type: "master", status: "available" })
@ -2300,19 +2294,19 @@ exports.acceptQuotation = async (req, reply) => {
} }
} }
// **Step 2: Process Master Connections & Assign Slaves** // Step 2: Process Master Connections & Assign Slaves
let blockedSlaveIds = []; let blockedSlaveIds = [];
let blockedSlaves = []; let blockedSlaves = [];
for (let i = 0; i < master_connections.length; i++) { for (let i = 0; i < master_connections.length; i++) {
const masterData = master_connections[i]; const masterData = master_connections[i];
if (i >= blockedMasters.length) break; // Ensure we don't exceed blocked masters if (i >= blockedMasters.length) break;
const masterHardwareId = blockedMasters[i].hardwareId; // Get the hardwareId of the blocked master const masterHardwareId = blockedMasters[i].hardwareId;
const slaveCount = parseInt(masterData.slaves) || 0; const slaveCount = parseInt(masterData.slaves) || 0;
// Assign Slaves
if (slaveCount > 0) { if (slaveCount > 0) {
// **Find and Assign Slaves**
const availableSlaves = await Insensors.find({ storeId, type: "slave", status: "available" }) const availableSlaves = await Insensors.find({ storeId, type: "slave", status: "available" })
.limit(slaveCount) .limit(slaveCount)
.lean(); .lean();
@ -2326,46 +2320,61 @@ exports.acceptQuotation = async (req, reply) => {
{ _id: { $in: slaveIds } }, { _id: { $in: slaveIds } },
{ $set: { status: "blocked", customerId, connected_to: masterHardwareId } } { $set: { status: "blocked", customerId, connected_to: masterHardwareId } }
); );
}
}
// **Step 3: Update Tanks in Masters** // Assign each slave a corresponding tank (1-to-1 mapping)
if (masterData.tanks && masterData.tanks.length > 0) { const tanks = masterData.tanks || [];
await Insensors.updateOne(
{ _id: blockedMasters[i]._id }, for (let j = 0; j < availableSlaves.length; j++) {
{ const slaveId = availableSlaves[j]._id;
$set: { const tank = tanks[j] || {};
tanks: masterData.tanks.map(tank => ({
tankName: tank.tankName || "", await Insensors.updateOne(
tankLocation: tank.tankLocation || "", { _id: slaveId },
})), {
}, $set: {
tankName: tank.tankName || "",
tankLocation: tank.tankLocation || "",
},
}
);
} }
); }
} }
// Update tanks and motor switches for masters
await Insensors.updateOne(
{ _id: blockedMasters[i]._id },
{
$set: {
tanks: masterData.tanks.map(tank => ({
tankName: tank.tankName || "",
tankLocation: tank.tankLocation || "",
})),
motor_switches: masterData.motor_switches || [],
},
}
);
} }
// **Step 4: Assign Sensors to Slaves** // Step 3: Assign Sensors to Slaves
if (parseInt(sensors) > 0) { if (parseInt(sensors) > 0 && blockedSlaves.length > 0) {
const availableSensors = await Insensors.find({ storeId, type: "sensor", status: "available" }) const availableSensors = await Insensors.find({ storeId, type: "sensor", status: "available" })
.limit(parseInt(sensors)) .limit(parseInt(sensors))
.lean(); .lean();
const sensorIds = availableSensors.map(sensor => sensor._id); const sensorIds = availableSensors.map(sensor => sensor._id);
if (sensorIds.length > 0) { for (let i = 0; i < sensorIds.length; i++) {
for (let i = 0; i < sensorIds.length; i++) { const assignedSlave = blockedSlaves[i % blockedSlaves.length];
const assignedSlave = blockedSlaves[i % blockedSlaves.length]; // Distribute sensors to slaves
await Insensors.updateOne( await Insensors.updateOne(
{ _id: sensorIds[i] }, { _id: sensorIds[i] },
{ $set: { status: "blocked", customerId, connected_to: assignedSlave.hardwareId } } { $set: { status: "blocked", customerId, connected_to: assignedSlave.hardwareId } }
); );
}
} }
} }
// **Step 5: Update Sensor Stock** // Step 4: Update Sensor Stock
const sensorTypes = [ const sensorTypes = [
{ type: "master", count: parseInt(masters || 0) }, { type: "master", count: parseInt(masters || 0) },
{ type: "slave", count: parseInt(slaves || 0) }, { type: "slave", count: parseInt(slaves || 0) },
@ -2379,7 +2388,6 @@ exports.acceptQuotation = async (req, reply) => {
if (stock) { if (stock) {
let available = stock.total_available || 0; let available = stock.total_available || 0;
let needed = sensor.count; let needed = sensor.count;
let toBlock = Math.min(available, needed); let toBlock = Math.min(available, needed);
let excessNeeded = needed - toBlock; let excessNeeded = needed - toBlock;
@ -2412,7 +2420,7 @@ exports.acceptQuotation = async (req, reply) => {
} }
} }
// **Step 6: Delete Quotation** // Step 5: Delete Quotation
await SensorQuotation.deleteOne({ quatationId: quotationId }); await SensorQuotation.deleteOne({ quatationId: quotationId });
return reply.send({ return reply.send({
@ -2431,7 +2439,6 @@ exports.acceptQuotation = async (req, reply) => {
exports.getOrdersByStoreId = async (req, reply) => { exports.getOrdersByStoreId = async (req, reply) => {
try { try {
const { storeId } = req.params; const { storeId } = req.params;

@ -444,6 +444,14 @@ const insensorsSchema = new mongoose.Schema({
connected_status: { type: String, enum: ["connected", "Not connected", "unknown"], default: "unknown" }, connected_status: { type: String, enum: ["connected", "Not connected", "unknown"], default: "unknown" },
masterName: { type: String, default: null }, masterName: { type: String, default: null },
location: { type: String, default: null }, location: { type: String, default: null },
motor_switches: [
{
from_tank: { type: String, default: null },
from_location: { type: String, default: null },
to_tank: { type: String, default: null },
to_location: { type: String, default: null },
},
],
connected_gsm_time: { type: String, default: null }, connected_gsm_time: { type: String, default: null },
connected_gsm_date: { type: String, default: null }, connected_gsm_date: { type: String, default: null },
connected_lora_date: { type: String, default: null }, connected_lora_date: { type: String, default: null },
@ -594,6 +602,33 @@ const orderSchema = new mongoose.Schema({
datetime: { type: String, default: null }, datetime: { type: String, default: null },
updated_at: { type: String, default: null }, updated_at: { type: String, default: null },
assignedTeamMembers: [{ type: String }], assignedTeamMembers: [{ type: String }],
master_connections: [
{
master_name: { type: String, default: null },
slaves: { type: String, default: null },
location: { type: String, default: null },
googleLocation: { type: String, default: null },
longitude: { type : Number,default: 0.0},
latitude: {type: Number,default: 0.0},
tanks: [
{
tankName: { type: String, default: null },
tankLocation: { type: String, default: null },
},
],
motor_switches: [
{
from_tank: { type: String, default: null },
from_location: { type: String, default: null },
to_tank: { type: String, default: null },
to_location: { type: String, default: null },
},
],
},
],
electricals: [ electricals: [
{ {

Loading…
Cancel
Save