|
|
|
@ -2241,7 +2241,6 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (action === "reject") {
|
|
|
|
|
// Update status to "rejected" in SensorQuotation
|
|
|
|
|
await SensorQuotation.updateOne({ quatationId: quotationId }, { $set: { status: "rejected" } });
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
@ -2249,7 +2248,7 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
message: "Quotation rejected successfully",
|
|
|
|
|
});
|
|
|
|
|
} else if (action === "accept") {
|
|
|
|
|
const { customerId, masters, slaves, sensors } = quotation;
|
|
|
|
|
const { customerId, masters, slaves, sensors, master_connections } = quotation;
|
|
|
|
|
|
|
|
|
|
// Convert quotation to an order object and include storeId
|
|
|
|
|
const newOrder = new Order({
|
|
|
|
@ -2261,7 +2260,90 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
// Save the order
|
|
|
|
|
await newOrder.save();
|
|
|
|
|
|
|
|
|
|
// Update Insensors: Block the sensors and assign customerId
|
|
|
|
|
// **Step 1: Block Masters in Insensors**
|
|
|
|
|
let blockedMasters = [];
|
|
|
|
|
if (parseInt(masters) > 0) {
|
|
|
|
|
const availableMasters = await Insensors.find({ storeId, type: "master", status: "available" })
|
|
|
|
|
.limit(parseInt(masters))
|
|
|
|
|
.lean();
|
|
|
|
|
|
|
|
|
|
const masterIds = availableMasters.map(master => master._id);
|
|
|
|
|
blockedMasters = availableMasters.map(master => ({ _id: master._id, hardwareId: master.hardwareId }));
|
|
|
|
|
|
|
|
|
|
if (masterIds.length > 0) {
|
|
|
|
|
await Insensors.updateMany(
|
|
|
|
|
{ _id: { $in: masterIds } },
|
|
|
|
|
{ $set: { status: "blocked", customerId } }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// **Step 2: Process Master Connections & Assign Slaves**
|
|
|
|
|
let blockedSlaveIds = [];
|
|
|
|
|
let blockedSlaves = [];
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < master_connections.length; i++) {
|
|
|
|
|
const masterData = master_connections[i];
|
|
|
|
|
if (i >= blockedMasters.length) break; // Ensure we don't exceed blocked masters
|
|
|
|
|
|
|
|
|
|
const masterHardwareId = blockedMasters[i].hardwareId; // Get the hardwareId of the blocked master
|
|
|
|
|
const slaveCount = parseInt(masterData.slaves) || 0;
|
|
|
|
|
|
|
|
|
|
if (slaveCount > 0) {
|
|
|
|
|
// **Find and Assign Slaves**
|
|
|
|
|
const availableSlaves = await Insensors.find({ storeId, type: "slave", status: "available" })
|
|
|
|
|
.limit(slaveCount)
|
|
|
|
|
.lean();
|
|
|
|
|
|
|
|
|
|
const slaveIds = availableSlaves.map(slave => slave._id);
|
|
|
|
|
blockedSlaveIds.push(...slaveIds);
|
|
|
|
|
blockedSlaves.push(...availableSlaves.map(slave => ({ _id: slave._id, hardwareId: slave.hardwareId })));
|
|
|
|
|
|
|
|
|
|
if (slaveIds.length > 0) {
|
|
|
|
|
await Insensors.updateMany(
|
|
|
|
|
{ _id: { $in: slaveIds } },
|
|
|
|
|
{ $set: { status: "blocked", customerId, connected_to: masterHardwareId } }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// **Step 3: Update Tanks in Masters**
|
|
|
|
|
if (masterData.tanks && masterData.tanks.length > 0) {
|
|
|
|
|
await Insensors.updateOne(
|
|
|
|
|
{ _id: blockedMasters[i]._id },
|
|
|
|
|
{
|
|
|
|
|
$set: {
|
|
|
|
|
tanks: masterData.tanks.map(tank => ({
|
|
|
|
|
tankName: tank.tankName || "",
|
|
|
|
|
tankLocation: tank.tankLocation || "",
|
|
|
|
|
})),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// **Step 4: Assign Sensors to Slaves**
|
|
|
|
|
if (parseInt(sensors) > 0) {
|
|
|
|
|
const availableSensors = await Insensors.find({ storeId, type: "sensor", status: "available" })
|
|
|
|
|
.limit(parseInt(sensors))
|
|
|
|
|
.lean();
|
|
|
|
|
|
|
|
|
|
const sensorIds = availableSensors.map(sensor => sensor._id);
|
|
|
|
|
|
|
|
|
|
if (sensorIds.length > 0) {
|
|
|
|
|
for (let i = 0; i < sensorIds.length; i++) {
|
|
|
|
|
const assignedSlave = blockedSlaves[i % blockedSlaves.length]; // Distribute sensors to slaves
|
|
|
|
|
|
|
|
|
|
await Insensors.updateOne(
|
|
|
|
|
{ _id: sensorIds[i] },
|
|
|
|
|
{ $set: { status: "blocked", customerId, connected_to: assignedSlave.hardwareId } }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// **Step 5: Update Sensor Stock**
|
|
|
|
|
const sensorTypes = [
|
|
|
|
|
{ type: "master", count: parseInt(masters || 0) },
|
|
|
|
|
{ type: "slave", count: parseInt(slaves || 0) },
|
|
|
|
@ -2280,7 +2362,6 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
let excessNeeded = needed - toBlock;
|
|
|
|
|
|
|
|
|
|
if (toBlock > 0) {
|
|
|
|
|
// Find the required number of available sensors
|
|
|
|
|
const availableSensors = await Insensors.find({ storeId, type: sensor.type, status: "available" })
|
|
|
|
|
.limit(toBlock)
|
|
|
|
|
.lean();
|
|
|
|
@ -2288,7 +2369,6 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
const sensorIds = availableSensors.map(sensor => sensor._id);
|
|
|
|
|
|
|
|
|
|
if (sensorIds.length > 0) {
|
|
|
|
|
// Update only the found sensors
|
|
|
|
|
await Insensors.updateMany(
|
|
|
|
|
{ _id: { $in: sensorIds } },
|
|
|
|
|
{ $set: { status: "blocked", customerId } }
|
|
|
|
@ -2296,7 +2376,6 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update SensorStock
|
|
|
|
|
await SensorStock.updateOne(
|
|
|
|
|
{ storeId, type: sensor.type },
|
|
|
|
|
{
|
|
|
|
@ -2311,7 +2390,7 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete the record from SensorQuotation
|
|
|
|
|
// **Step 6: Delete Quotation**
|
|
|
|
|
await SensorQuotation.deleteOne({ quatationId: quotationId });
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
@ -2330,6 +2409,7 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getOrdersByStoreId = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { storeId } = req.params;
|
|
|
|
|