|
|
|
@ -1622,6 +1622,7 @@ exports.createquotationforSensor = async (req, reply) => {
|
|
|
|
|
const { surveyId } = req.params;
|
|
|
|
|
const { customerId, masters, slaves, sensors, motor_switches, electricals,master_connections } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Format electricals field
|
|
|
|
|
const formattedElectricals = electricals.map((item) => ({
|
|
|
|
|
type: item.type || "",
|
|
|
|
@ -1633,6 +1634,9 @@ exports.createquotationforSensor = async (req, reply) => {
|
|
|
|
|
master_name: item.master_name || "",
|
|
|
|
|
slaves: item.slaves || "",
|
|
|
|
|
location: item.location || "",
|
|
|
|
|
googleLocation: item.googleLocation || "",
|
|
|
|
|
latitude: item.latitude || "",
|
|
|
|
|
longitude: item.longitude || "",
|
|
|
|
|
tanks: Array.isArray(item.tanks)
|
|
|
|
|
? item.tanks.map(tank => ({
|
|
|
|
|
tankName: tank.tankName || "",
|
|
|
|
@ -2247,12 +2251,9 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
const { quotationId } = req.params;
|
|
|
|
|
let { action, storeId } = req.body;
|
|
|
|
|
|
|
|
|
|
action = action.toLowerCase(); // Convert action to lowercase
|
|
|
|
|
action = action.toLowerCase();
|
|
|
|
|
|
|
|
|
|
// Find the quotation by ID
|
|
|
|
|
const quotation = await SensorQuotation.findOne({ quatationId: quotationId });
|
|
|
|
|
console.log(quotation,"quotation")
|
|
|
|
|
|
|
|
|
|
if (!quotation) {
|
|
|
|
|
return reply.status(404).send({ error: "Quotation not found" });
|
|
|
|
|
}
|
|
|
|
@ -2266,19 +2267,16 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
});
|
|
|
|
|
} else if (action === "accept") {
|
|
|
|
|
const { customerId, masters, slaves, sensors, master_connections } = quotation;
|
|
|
|
|
console.log(customerId,masters,slaves,sensors,master_connections)
|
|
|
|
|
|
|
|
|
|
// Convert quotation to an order object and include storeId
|
|
|
|
|
const newOrder = new Order({
|
|
|
|
|
...quotation.toObject(),
|
|
|
|
|
storeId,
|
|
|
|
|
status: "pending",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Save the order
|
|
|
|
|
await newOrder.save();
|
|
|
|
|
|
|
|
|
|
// **Step 1: Block Masters in Insensors**
|
|
|
|
|
// Step 1: Block Masters
|
|
|
|
|
let blockedMasters = [];
|
|
|
|
|
if (parseInt(masters) > 0) {
|
|
|
|
|
const availableMasters = await Insensors.find({ storeId, type: "master", status: "available" })
|
|
|
|
@ -2296,19 +2294,19 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// **Step 2: Process Master Connections & Assign Slaves**
|
|
|
|
|
// 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
|
|
|
|
|
if (i >= blockedMasters.length) break;
|
|
|
|
|
|
|
|
|
|
const masterHardwareId = blockedMasters[i].hardwareId; // Get the hardwareId of the blocked master
|
|
|
|
|
const masterHardwareId = blockedMasters[i].hardwareId;
|
|
|
|
|
const slaveCount = parseInt(masterData.slaves) || 0;
|
|
|
|
|
|
|
|
|
|
// Assign Slaves
|
|
|
|
|
if (slaveCount > 0) {
|
|
|
|
|
// **Find and Assign Slaves**
|
|
|
|
|
const availableSlaves = await Insensors.find({ storeId, type: "slave", status: "available" })
|
|
|
|
|
.limit(slaveCount)
|
|
|
|
|
.lean();
|
|
|
|
@ -2322,11 +2320,28 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
{ _id: { $in: slaveIds } },
|
|
|
|
|
{ $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(
|
|
|
|
|
{ _id: slaveId },
|
|
|
|
|
{
|
|
|
|
|
$set: {
|
|
|
|
|
tankName: tank.tankName || "",
|
|
|
|
|
tankLocation: tank.tankLocation || "",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// **Step 3: Update Tanks in Masters**
|
|
|
|
|
if (masterData.tanks && masterData.tanks.length > 0) {
|
|
|
|
|
// Update tanks and motor switches for masters
|
|
|
|
|
await Insensors.updateOne(
|
|
|
|
|
{ _id: blockedMasters[i]._id },
|
|
|
|
|
{
|
|
|
|
@ -2335,23 +2350,22 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
tankName: tank.tankName || "",
|
|
|
|
|
tankLocation: tank.tankLocation || "",
|
|
|
|
|
})),
|
|
|
|
|
motor_switches: masterData.motor_switches || [],
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// **Step 4: Assign Sensors to Slaves**
|
|
|
|
|
if (parseInt(sensors) > 0) {
|
|
|
|
|
// Step 3: Assign Sensors to Slaves
|
|
|
|
|
if (parseInt(sensors) > 0 && blockedSlaves.length > 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
|
|
|
|
|
const assignedSlave = blockedSlaves[i % blockedSlaves.length];
|
|
|
|
|
|
|
|
|
|
await Insensors.updateOne(
|
|
|
|
|
{ _id: sensorIds[i] },
|
|
|
|
@ -2359,9 +2373,8 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// **Step 5: Update Sensor Stock**
|
|
|
|
|
// Step 4: Update Sensor Stock
|
|
|
|
|
const sensorTypes = [
|
|
|
|
|
{ type: "master", count: parseInt(masters || 0) },
|
|
|
|
|
{ type: "slave", count: parseInt(slaves || 0) },
|
|
|
|
@ -2375,7 +2388,6 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
if (stock) {
|
|
|
|
|
let available = stock.total_available || 0;
|
|
|
|
|
let needed = sensor.count;
|
|
|
|
|
|
|
|
|
|
let toBlock = Math.min(available, needed);
|
|
|
|
|
let excessNeeded = needed - toBlock;
|
|
|
|
|
|
|
|
|
@ -2408,7 +2420,7 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// **Step 6: Delete Quotation**
|
|
|
|
|
// Step 5: Delete Quotation
|
|
|
|
|
await SensorQuotation.deleteOne({ quatationId: quotationId });
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
@ -2427,7 +2439,6 @@ exports.acceptQuotation = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getOrdersByStoreId = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { storeId } = req.params;
|
|
|
|
|