hardwareId added in accept quatation

master^2
Bhaskar 5 months ago
parent 6ef2aeca65
commit 6ee4ef68d9

@ -2268,14 +2268,6 @@ 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;
const newOrder = new Order({
...quotation.toObject(),
storeId,
status: "pending",
});
await newOrder.save();
// Step 1: Block Masters // Step 1: Block Masters
let blockedMasters = []; let blockedMasters = [];
if (parseInt(masters) > 0) { if (parseInt(masters) > 0) {
@ -2294,7 +2286,7 @@ exports.acceptQuotation = async (req, reply) => {
} }
} }
// Step 2: Process Master Connections & Assign Slaves // Step 2: Assign Slaves to Masters
let blockedSlaveIds = []; let blockedSlaveIds = [];
let blockedSlaves = []; let blockedSlaves = [];
@ -2303,6 +2295,7 @@ exports.acceptQuotation = async (req, reply) => {
if (i >= blockedMasters.length) break; if (i >= blockedMasters.length) break;
const masterHardwareId = blockedMasters[i].hardwareId; const masterHardwareId = blockedMasters[i].hardwareId;
const masterId = blockedMasters[i]._id;
const slaveCount = parseInt(masterData.slaves) || 0; const slaveCount = parseInt(masterData.slaves) || 0;
// Assign Slaves // Assign Slaves
@ -2315,38 +2308,33 @@ exports.acceptQuotation = async (req, reply) => {
blockedSlaveIds.push(...slaveIds); blockedSlaveIds.push(...slaveIds);
blockedSlaves.push(...availableSlaves.map(slave => ({ _id: slave._id, hardwareId: slave.hardwareId }))); blockedSlaves.push(...availableSlaves.map(slave => ({ _id: slave._id, hardwareId: slave.hardwareId })));
if (slaveIds.length > 0) { for (let j = 0; j < availableSlaves.length; j++) {
await Insensors.updateMany( const slave = availableSlaves[j];
{ _id: { $in: slaveIds } }, const tank = masterData.tanks?.[j] || {};
{ $set: { status: "blocked", customerId, connected_to: masterHardwareId } }
);
// Assign each slave a corresponding tank (1-to-1 mapping)
const tanks = masterData.tanks || [];
for (let j = 0; j < availableSlaves.length; j++) {
const slaveId = availableSlaves[j]._id;
const tank = tanks[j] || {};
await Insensors.updateOne( await Insensors.updateOne(
{ _id: slaveId }, { _id: slave._id },
{ {
$set: { $set: {
tankName: tank.tankName || "", status: "blocked",
tankLocation: tank.tankLocation || "", customerId,
}, connected_to: masterHardwareId,
} hardwareId: slave.hardwareId,
); tankName: tank.tankName || "",
} tankLocation: tank.tankLocation || "",
},
}
);
} }
} }
// Update tanks and motor switches for masters // Update tanks and motor switches for master
await Insensors.updateOne( await Insensors.updateOne(
{ _id: blockedMasters[i]._id }, { _id: masterId },
{ {
$set: { $set: {
tanks: masterData.tanks.map(tank => ({ hardwareId: masterHardwareId,
tanks: (masterData.tanks || []).map(tank => ({
tankName: tank.tankName || "", tankName: tank.tankName || "",
tankLocation: tank.tankLocation || "", tankLocation: tank.tankLocation || "",
})), })),
@ -2356,6 +2344,15 @@ exports.acceptQuotation = async (req, reply) => {
); );
} }
// Step 2.5: Update master_connections.hardwareId
const updatedMasterConnections = quotation.master_connections.map((conn, index) => {
const plain = conn.toObject ? conn.toObject() : conn;
return {
...plain,
hardwareId: blockedMasters[index]?.hardwareId || null,
};
});
// Step 3: Assign Sensors to Slaves // Step 3: Assign Sensors to Slaves
if (parseInt(sensors) > 0 && blockedSlaves.length > 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" })
@ -2369,7 +2366,13 @@ exports.acceptQuotation = async (req, reply) => {
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,
},
}
); );
} }
} }
@ -2420,7 +2423,19 @@ exports.acceptQuotation = async (req, reply) => {
} }
} }
// Step 5: Delete Quotation // Step 5: Create Order
const plainQuotation = quotation.toObject();
plainQuotation.master_connections = updatedMasterConnections;
const newOrder = new Order({
...plainQuotation,
storeId,
status: "pending",
});
await newOrder.save();
// Step 6: Delete Quotation
await SensorQuotation.deleteOne({ quatationId: quotationId }); await SensorQuotation.deleteOne({ quatationId: quotationId });
return reply.send({ return reply.send({

Loading…
Cancel
Save