|
|
|
@ -2268,14 +2268,6 @@ 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) {
|
|
|
|
@ -2321,9 +2313,8 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
{ $set: { status: "blocked", customerId, connected_to: masterHardwareId } }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Assign each slave a corresponding tank
|
|
|
|
|
// Assign tanks to slaves
|
|
|
|
|
const tanks = masterData.tanks || [];
|
|
|
|
|
|
|
|
|
|
for (let j = 0; j < availableSlaves.length; j++) {
|
|
|
|
|
const slaveId = availableSlaves[j]._id;
|
|
|
|
|
const tank = tanks[j] || {};
|
|
|
|
@ -2356,18 +2347,11 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ✅ 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 2.5: Update master_connections.hardwareId
|
|
|
|
|
const updatedMasterConnections = quotation.master_connections.map((conn, index) => ({
|
|
|
|
|
...conn,
|
|
|
|
|
hardwareId: blockedMasters[index]?.hardwareId || null,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Step 3: Assign Sensors to Slaves
|
|
|
|
|
if (parseInt(sensors) > 0 && blockedSlaves.length > 0) {
|
|
|
|
@ -2433,7 +2417,17 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 5: Delete Quotation
|
|
|
|
|
// 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
|
|
|
|
|
await SensorQuotation.deleteOne({ quatationId: quotationId });
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
@ -2452,6 +2446,7 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getOrdersByStoreId = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { storeId } = req.params;
|
|
|
|
|