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