reverted changes

master^2
Varun 5 months ago
parent 07d338c86f
commit 94e28aa2ff

@ -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) => ({
...conn,
hardwareId: blockedMasters[index]?.hardwareId || null,
}));
// ✅ 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" });
}
};
}

@ -605,7 +605,7 @@ const orderSchema = new mongoose.Schema({
master_connections: [
{
master_name: { type: String, default: null },
hardwareId:{ type: String, default: null },
slaves: { type: String, default: null },
location: { type: String, default: null },
googleLocation: { type: String, default: null },

Loading…
Cancel
Save