|
|
|
@ -2913,79 +2913,79 @@ exports.getWaitingManagerPendingOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.getCompleteOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { installationId } = req.params;
|
|
|
|
|
// exports.getCompleteOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { installationId } = req.params;
|
|
|
|
|
|
|
|
|
|
if (!installationId) {
|
|
|
|
|
return reply.status(400).send({ error: "installationId is required" });
|
|
|
|
|
}
|
|
|
|
|
// if (!installationId) {
|
|
|
|
|
// return reply.status(400).send({ error: "installationId is required" });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Fetch orders with the matching installationId
|
|
|
|
|
const orders = await Order.find({ installationId });
|
|
|
|
|
// // Fetch orders with the matching installationId
|
|
|
|
|
// const orders = await Order.find({ installationId });
|
|
|
|
|
|
|
|
|
|
if (!orders.length) {
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "No orders found for this installation",
|
|
|
|
|
data: [],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// if (!orders.length) {
|
|
|
|
|
// return reply.send({
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
// message: "No orders found for this installation",
|
|
|
|
|
// data: [],
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const uniqueCustomersMap = new Map();
|
|
|
|
|
// const uniqueCustomersMap = new Map();
|
|
|
|
|
|
|
|
|
|
// Build unique customerId-based map
|
|
|
|
|
for (const order of orders) {
|
|
|
|
|
if (!uniqueCustomersMap.has(order.customerId)) {
|
|
|
|
|
uniqueCustomersMap.set(order.customerId, order);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// // Build unique customerId-based map
|
|
|
|
|
// for (const order of orders) {
|
|
|
|
|
// if (!uniqueCustomersMap.has(order.customerId)) {
|
|
|
|
|
// uniqueCustomersMap.set(order.customerId, order);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Only keep one order per customerId
|
|
|
|
|
let uniqueOrders = Array.from(uniqueCustomersMap.values());
|
|
|
|
|
// // Only keep one order per customerId
|
|
|
|
|
// let uniqueOrders = Array.from(uniqueCustomersMap.values());
|
|
|
|
|
|
|
|
|
|
// ✅ Filter: keep only those where work_status is "pending"
|
|
|
|
|
uniqueOrders = uniqueOrders.filter(order => order.work_status === 'complete');
|
|
|
|
|
// // ✅ Filter: keep only those where work_status is "pending"
|
|
|
|
|
// uniqueOrders = uniqueOrders.filter(order => order.work_status === 'complete');
|
|
|
|
|
|
|
|
|
|
// If nothing left after filtering, return empty
|
|
|
|
|
if (!uniqueOrders.length) {
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "No pending orders found for this installation",
|
|
|
|
|
data: [],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// // If nothing left after filtering, return empty
|
|
|
|
|
// if (!uniqueOrders.length) {
|
|
|
|
|
// return reply.send({
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
// message: "No pending orders found for this installation",
|
|
|
|
|
// data: [],
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Enrich with customer and sensor info
|
|
|
|
|
const ordersWithDetails = await Promise.all(
|
|
|
|
|
uniqueOrders.map(async (order) => {
|
|
|
|
|
const customer = await User.findOne({ customerId: order.customerId }).lean();
|
|
|
|
|
// // Enrich with customer and sensor info
|
|
|
|
|
// const ordersWithDetails = await Promise.all(
|
|
|
|
|
// uniqueOrders.map(async (order) => {
|
|
|
|
|
// const customer = await User.findOne({ customerId: order.customerId }).lean();
|
|
|
|
|
|
|
|
|
|
const allocatedSensors = await Insensors.find({
|
|
|
|
|
storeId: order.storeId,
|
|
|
|
|
customerId: order.customerId,
|
|
|
|
|
status: "blocked",
|
|
|
|
|
}).lean();
|
|
|
|
|
// const allocatedSensors = await Insensors.find({
|
|
|
|
|
// storeId: order.storeId,
|
|
|
|
|
// customerId: order.customerId,
|
|
|
|
|
// status: "blocked",
|
|
|
|
|
// }).lean();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...order.toObject(),
|
|
|
|
|
customer: customer || null,
|
|
|
|
|
allocated_sensors: allocatedSensors,
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
// return {
|
|
|
|
|
// ...order.toObject(),
|
|
|
|
|
// customer: customer || null,
|
|
|
|
|
// allocated_sensors: allocatedSensors,
|
|
|
|
|
// };
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "Complete orders fetched successfully",
|
|
|
|
|
data: ordersWithDetails,
|
|
|
|
|
});
|
|
|
|
|
// return reply.send({
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
// message: "Complete orders fetched successfully",
|
|
|
|
|
// data: ordersWithDetails,
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error fetching orders:", err);
|
|
|
|
|
return reply.status(500).send({ error: "Internal server error" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
// console.error("Error fetching orders:", err);
|
|
|
|
|
// return reply.status(500).send({ error: "Internal server error" });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
@ -3117,6 +3117,72 @@ exports.getCompleteOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
exports.getCompleteManagerPendingOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { installationId } = req.params;
|
|
|
|
|
|
|
|
|
|
if (!installationId) {
|
|
|
|
|
return reply.status(400).send({ error: "installationId is required" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 1: Fetch orders matching installationId
|
|
|
|
|
const orders = await Order.find({ installationId });
|
|
|
|
|
|
|
|
|
|
if (!orders.length) {
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "No orders found for this installation",
|
|
|
|
|
data: [],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 2: Filter orders to keep only those having at least one master_connection with work_status === 'pending'
|
|
|
|
|
const ordersWithPendingMasters = [];
|
|
|
|
|
|
|
|
|
|
for (const order of orders) {
|
|
|
|
|
const pendingMasters = (order.master_connections || []).filter(mc => mc.work_status === 'complete');
|
|
|
|
|
|
|
|
|
|
if (pendingMasters.length) {
|
|
|
|
|
// Fetch customer details
|
|
|
|
|
const customer = await User.findOne({ customerId: order.customerId }).lean();
|
|
|
|
|
|
|
|
|
|
// Fetch allocated sensors (status blocked)
|
|
|
|
|
const allocatedSensors = await Insensors.find({
|
|
|
|
|
storeId: order.storeId,
|
|
|
|
|
customerId: order.customerId,
|
|
|
|
|
status: "blocked",
|
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
// Build response object
|
|
|
|
|
ordersWithPendingMasters.push({
|
|
|
|
|
...order.toObject(),
|
|
|
|
|
master_connections: pendingMasters, // keep only pending masters
|
|
|
|
|
customer: customer || null,
|
|
|
|
|
allocated_sensors: allocatedSensors,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ordersWithPendingMasters.length) {
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "No pending master connections found for this installation",
|
|
|
|
|
data: [],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "Complete orders fetched successfully",
|
|
|
|
|
data: ordersWithPendingMasters,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error fetching pending orders:", err);
|
|
|
|
|
return reply.status(500).send({ error: "Internal server error" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.updateWorkStatusByInstallationId = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { installationId, customerId, teamMemberId } = req.params;
|
|
|
|
|