|
|
@ -2507,6 +2507,72 @@ exports.getOrdersByStoreId = async (req, reply) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// const { installationId } = req.params;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (!installationId) {
|
|
|
|
|
|
|
|
// return reply.status(400).send({ error: "installationId is required" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Fetch all orders by installationId
|
|
|
|
|
|
|
|
// const orders = await Order.find({ installationId });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (!orders.length) {
|
|
|
|
|
|
|
|
// return reply.send({
|
|
|
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
|
|
|
// message: "No orders found for this installation",
|
|
|
|
|
|
|
|
// data: {
|
|
|
|
|
|
|
|
// customers: [],
|
|
|
|
|
|
|
|
// orders: []
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Get unique customerIds from orders
|
|
|
|
|
|
|
|
// const uniqueCustomerIds = [...new Set(orders.map(order => order.customerId))];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Fetch all customers in a single query
|
|
|
|
|
|
|
|
// const customers = await User.find({ customerId: { $in: uniqueCustomerIds } }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Map customerId -> customer object
|
|
|
|
|
|
|
|
// const customerMap = {};
|
|
|
|
|
|
|
|
// customers.forEach(c => {
|
|
|
|
|
|
|
|
// customerMap[c.customerId] = c;
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // For each order, attach allocated sensors only
|
|
|
|
|
|
|
|
// const ordersWithSensors = await Promise.all(
|
|
|
|
|
|
|
|
// orders.map(async (order) => {
|
|
|
|
|
|
|
|
// const allocatedSensors = await Insensors.find({
|
|
|
|
|
|
|
|
// storeId: order.storeId,
|
|
|
|
|
|
|
|
// customerId: order.customerId,
|
|
|
|
|
|
|
|
// status: "blocked"
|
|
|
|
|
|
|
|
// }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return {
|
|
|
|
|
|
|
|
// ...order.toObject(),
|
|
|
|
|
|
|
|
// allocated_sensors: allocatedSensors
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return reply.send({
|
|
|
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
|
|
|
// message: "Orders fetched successfully",
|
|
|
|
|
|
|
|
// data: customers
|
|
|
|
|
|
|
|
// // data: {
|
|
|
|
|
|
|
|
// // customers,
|
|
|
|
|
|
|
|
// // // orders: ordersWithSensors
|
|
|
|
|
|
|
|
// // }
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
|
|
|
// console.error("Error fetching orders:", err);
|
|
|
|
|
|
|
|
// return reply.status(500).send({ error: "Internal server error" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { installationId } = req.params;
|
|
|
|
const { installationId } = req.params;
|
|
|
@ -2515,58 +2581,54 @@ exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
return reply.status(400).send({ error: "installationId is required" });
|
|
|
|
return reply.status(400).send({ error: "installationId is required" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Fetch all orders by installationId
|
|
|
|
// Fetch orders with the matching installationId
|
|
|
|
const orders = await Order.find({ installationId });
|
|
|
|
const orders = await Order.find({ installationId }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
if (!orders.length) {
|
|
|
|
if (!orders.length) {
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|
message: "No orders found for this installation",
|
|
|
|
message: "No orders found for this installation",
|
|
|
|
data: {
|
|
|
|
data: [],
|
|
|
|
customers: [],
|
|
|
|
|
|
|
|
orders: []
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get unique customerIds from orders
|
|
|
|
// Group orders by customerId
|
|
|
|
const uniqueCustomerIds = [...new Set(orders.map(order => order.customerId))];
|
|
|
|
const groupedByCustomer = {};
|
|
|
|
|
|
|
|
|
|
|
|
// Fetch all customers in a single query
|
|
|
|
for (const order of orders) {
|
|
|
|
const customers = await User.find({ customerId: { $in: uniqueCustomerIds } }).lean();
|
|
|
|
const { customerId, storeId } = order;
|
|
|
|
|
|
|
|
|
|
|
|
// Map customerId -> customer object
|
|
|
|
// Fetch allocated sensors
|
|
|
|
const customerMap = {};
|
|
|
|
|
|
|
|
customers.forEach(c => {
|
|
|
|
|
|
|
|
customerMap[c.customerId] = c;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// For each order, attach allocated sensors only
|
|
|
|
|
|
|
|
const ordersWithSensors = await Promise.all(
|
|
|
|
|
|
|
|
orders.map(async (order) => {
|
|
|
|
|
|
|
|
const allocatedSensors = await Insensors.find({
|
|
|
|
const allocatedSensors = await Insensors.find({
|
|
|
|
storeId: order.storeId,
|
|
|
|
storeId,
|
|
|
|
customerId: order.customerId,
|
|
|
|
customerId,
|
|
|
|
status: "blocked"
|
|
|
|
status: "blocked",
|
|
|
|
}).lean();
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
const enrichedOrder = {
|
|
|
|
...order.toObject(),
|
|
|
|
...order,
|
|
|
|
allocated_sensors: allocatedSensors
|
|
|
|
allocated_sensors: allocatedSensors,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
|
|
|
|
);
|
|
|
|
if (!groupedByCustomer[customerId]) {
|
|
|
|
|
|
|
|
// Fetch customer once
|
|
|
|
|
|
|
|
const customer = await User.findOne({ customerId }).lean();
|
|
|
|
|
|
|
|
groupedByCustomer[customerId] = {
|
|
|
|
|
|
|
|
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 fetched successfully",
|
|
|
|
message: "Orders grouped by customer fetched successfully",
|
|
|
|
data: customers
|
|
|
|
data: response,
|
|
|
|
// data: {
|
|
|
|
|
|
|
|
// customers,
|
|
|
|
|
|
|
|
// // orders: ordersWithSensors
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error fetching orders:", err);
|
|
|
|
console.error("Error fetching orders:", err);
|
|
|
|
return reply.status(500).send({ error: "Internal server error" });
|
|
|
|
return reply.status(500).send({ error: "Internal server error" });
|
|
|
|