master^2
Bhaskar 5 months ago
parent 8f82de0ddf
commit c745b56536

@ -4181,15 +4181,13 @@ exports.particularCategory = async (req, reply) => {
tankName: slave.tankName || "", tankName: slave.tankName || "",
location: slave.tankLocation || "", location: slave.tankLocation || "",
connected_status: slave.connected_status, connected_status: slave.connected_status,
connected_lora_time: slave.connected_lora_time || null,
connected_lora_date: slave.connected_lora_date || null,
lora_last_check_time: slave.lora_last_check_time || null,
lora_last_disconnect_time: slave.lora_last_disconnect_time || null, lora_last_disconnect_time: slave.lora_last_disconnect_time || null,
connected_to: slave.connected_to || "", connected_to: slave.connected_to || "",
masterName: orderMap[master.hardwareId]?.masterName || "", masterName: orderMap[master.hardwareId]?.masterName || "",
type: "slave", type: "slave",
typeOfWater: slave.typeOfWater || "", typeOfWater: slave.typeOfWater || "",
support_lora_last_check_time: null support_lora_last_check_time: null,
category // category included for each slave
})); }));
disconnectedIssues.push({ disconnectedIssues.push({
@ -4198,15 +4196,11 @@ exports.particularCategory = async (req, reply) => {
location: orderMap[master.hardwareId]?.location || "", location: orderMap[master.hardwareId]?.location || "",
type: "master", type: "master",
connected_status: master.connected_status, connected_status: master.connected_status,
gsm_last_check_time: master.gsm_last_check_time || null,
gsm_last_disconnect_time: master.gsm_last_disconnect_time || null, gsm_last_disconnect_time: master.gsm_last_disconnect_time || null,
connected_gsm_date: master.connected_gsm_date || null,
connected_gsm_time: master.connected_gsm_time || null,
connected_lora_date: master.connected_lora_date || null,
connected_lora_time: master.connected_lora_time || null,
support_gsm_last_check_time: null, support_gsm_last_check_time: null,
connected_slave_count: slaveDetails.length, connected_slave_count: slaveDetails.length,
connected_slaves: slaveDetails connected_slaves: slaveDetails,
category // category included for master
}); });
} }

@ -2582,7 +2582,7 @@ exports.getOrdersByInstallationId = async (req, reply) => {
} }
// Fetch orders with the matching installationId // Fetch orders with the matching installationId
const orders = await Order.find({ installationId }).lean(); const orders = await Order.find({ installationId });
if (!orders.length) { if (!orders.length) {
return reply.send({ return reply.send({
@ -2592,42 +2592,31 @@ exports.getOrdersByInstallationId = async (req, reply) => {
}); });
} }
// Group orders by customerId // Fetch customer details & allocated sensors for each order
const groupedByCustomer = {}; const ordersWithDetails = await Promise.all(
orders.map(async (order) => {
for (const order of orders) { // Fetch customer details
const { customerId, storeId } = order; const customer = await User.findOne({ customerId: order.customerId }).lean();
// Fetch allocated sensors
const allocatedSensors = await Insensors.find({
storeId,
customerId,
status: "blocked",
}).lean();
const enrichedOrder = { // Fetch allocated sensors for this customer
...order, const allocatedSensors = await Insensors.find({
allocated_sensors: allocatedSensors, storeId: order.storeId,
}; customerId: order.customerId, // Match only sensors allocated to this customer
status: "blocked", // Only fetch sensors that are allocated (blocked)
}).lean();
if (!groupedByCustomer[customerId]) { return {
// Fetch customer once ...order.toObject(),
const customer = await User.findOne({ customerId }).lean(); customer: customer || null, // Include customer details or null if not found
groupedByCustomer[customerId] = { allocated_sensors: allocatedSensors, // List of allocated sensors
customer: customer || null,
orders: [enrichedOrder],
}; };
} else { })
groupedByCustomer[customerId].orders.push(enrichedOrder); );
}
}
const response = Object.values(groupedByCustomer);
return reply.send({ return reply.send({
status_code: 200, status_code: 200,
message: "Orders grouped by customer fetched successfully", message: "Orders fetched successfully",
data: response, data: ordersWithDetails,
}); });
} catch (err) { } catch (err) {
console.error("Error fetching orders:", err); console.error("Error fetching orders:", err);
@ -2637,6 +2626,7 @@ exports.getOrdersByInstallationId = async (req, reply) => {
exports.getallocatedsensorstouser= async (req, reply) => { exports.getallocatedsensorstouser= async (req, reply) => {
try { try {
const { customerId } = req.params; const { customerId } = req.params;

Loading…
Cancel
Save