|
|
@ -2644,7 +2644,7 @@ exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
return reply.status(400).send({ error: "installationId is required" });
|
|
|
|
return reply.status(400).send({ error: "installationId is required" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Fetch orders with the matching installationId
|
|
|
|
// Step 1: Fetch orders by installationId
|
|
|
|
const orders = await Order.find({ installationId });
|
|
|
|
const orders = await Order.find({ installationId });
|
|
|
|
|
|
|
|
|
|
|
|
if (!orders.length) {
|
|
|
|
if (!orders.length) {
|
|
|
@ -2655,53 +2655,33 @@ exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const uniqueCustomersMap = new Map();
|
|
|
|
const ordersWithActiveMasters = [];
|
|
|
|
|
|
|
|
|
|
|
|
// Build unique customerId-based map
|
|
|
|
// Step 2: Process each order
|
|
|
|
for (const order of orders) {
|
|
|
|
for (const order of orders) {
|
|
|
|
if (!uniqueCustomersMap.has(order.customerId)) {
|
|
|
|
// Filter master_connections to keep only those where work_status is 'active' or empty/null
|
|
|
|
uniqueCustomersMap.set(order.customerId, order);
|
|
|
|
const activeMasters = (order.master_connections || []).filter(mc =>
|
|
|
|
}
|
|
|
|
mc.work_status === 'active' || mc.work_status === '' || mc.work_status == null
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Only keep one order per customerId
|
|
|
|
|
|
|
|
let uniqueOrders = Array.from(uniqueCustomersMap.values());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ Filter: keep only orders where work_status is 'active'
|
|
|
|
|
|
|
|
uniqueOrders = uniqueOrders.filter(order => order.work_status === 'active');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If no orders left after filtering, return empty list
|
|
|
|
|
|
|
|
if (!uniqueOrders.length) {
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
|
|
|
status_code: 200,
|
|
|
|
|
|
|
|
message: "No active orders found for this installation",
|
|
|
|
|
|
|
|
data: [],
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Enrich with customer and sensor info
|
|
|
|
if (activeMasters.length) {
|
|
|
|
const ordersWithDetails = await Promise.all(
|
|
|
|
// Fetch customer details
|
|
|
|
uniqueOrders.map(async (order) => {
|
|
|
|
|
|
|
|
const customer = await User.findOne({ customerId: order.customerId }).lean();
|
|
|
|
const customer = await User.findOne({ customerId: order.customerId }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
const allocatedSensors = await Insensors.find({
|
|
|
|
// Build response object
|
|
|
|
storeId: order.storeId,
|
|
|
|
ordersWithActiveMasters.push({
|
|
|
|
customerId: order.customerId,
|
|
|
|
|
|
|
|
status: "blocked",
|
|
|
|
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
...order.toObject(),
|
|
|
|
...order.toObject(),
|
|
|
|
|
|
|
|
master_connections: activeMasters, // only active master_connections
|
|
|
|
customer: customer || null,
|
|
|
|
customer: customer || null,
|
|
|
|
allocated_sensors: allocatedSensors,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Step 3: Return response
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|
message: "Active orders fetched successfully",
|
|
|
|
message: "Orders with active master connections fetched successfully",
|
|
|
|
data: ordersWithDetails,
|
|
|
|
data: ordersWithActiveMasters,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
@ -2711,6 +2691,10 @@ exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getPendingOrdersByInstallationAndTeamMember = async (req, reply) => {
|
|
|
|
exports.getPendingOrdersByInstallationAndTeamMember = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { installationId, teamMemberId } = req.params;
|
|
|
|
const { installationId, teamMemberId } = req.params;
|
|
|
|