|
|
|
@ -2268,6 +2268,14 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
} else if (action === "accept") {
|
|
|
|
|
const { customerId, masters, slaves, sensors, master_connections } = quotation;
|
|
|
|
|
|
|
|
|
|
const newOrder = new Order({
|
|
|
|
|
...quotation.toObject(),
|
|
|
|
|
storeId,
|
|
|
|
|
status: "pending",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await newOrder.save();
|
|
|
|
|
|
|
|
|
|
// Step 1: Block Masters
|
|
|
|
|
let blockedMasters = [];
|
|
|
|
|
if (parseInt(masters) > 0) {
|
|
|
|
@ -2313,8 +2321,9 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
{ $set: { status: "blocked", customerId, connected_to: masterHardwareId } }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Assign tanks to slaves
|
|
|
|
|
// Assign each slave a corresponding tank
|
|
|
|
|
const tanks = masterData.tanks || [];
|
|
|
|
|
|
|
|
|
|
for (let j = 0; j < availableSlaves.length; j++) {
|
|
|
|
|
const slaveId = availableSlaves[j]._id;
|
|
|
|
|
const tank = tanks[j] || {};
|
|
|
|
@ -2347,11 +2356,18 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ✅ Step 2.5: Update master_connections.hardwareId
|
|
|
|
|
const updatedMasterConnections = quotation.master_connections.map((conn, index) => ({
|
|
|
|
|
// ✅ Step 2.5: Update master_connections.hardwareId in Order
|
|
|
|
|
const updatedMasterConnections = newOrder.master_connections.map((conn, index) => {
|
|
|
|
|
return {
|
|
|
|
|
...conn,
|
|
|
|
|
hardwareId: blockedMasters[index]?.hardwareId || null,
|
|
|
|
|
}));
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await Order.updateOne(
|
|
|
|
|
{ _id: newOrder._id },
|
|
|
|
|
{ $set: { master_connections: updatedMasterConnections } }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Step 3: Assign Sensors to Slaves
|
|
|
|
|
if (parseInt(sensors) > 0 && blockedSlaves.length > 0) {
|
|
|
|
@ -2417,17 +2433,7 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 5: Create Order (with updated master_connections)
|
|
|
|
|
const newOrder = new Order({
|
|
|
|
|
...quotation.toObject(),
|
|
|
|
|
storeId,
|
|
|
|
|
status: "pending",
|
|
|
|
|
master_connections: updatedMasterConnections,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await newOrder.save();
|
|
|
|
|
|
|
|
|
|
// Step 6: Delete Quotation
|
|
|
|
|
// Step 5: Delete Quotation
|
|
|
|
|
await SensorQuotation.deleteOne({ quatationId: quotationId });
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
@ -2442,7 +2448,7 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
console.error("Error processing quotation:", err);
|
|
|
|
|
return reply.status(500).send({ error: "Internal server error" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|