|
|
@ -2939,6 +2939,91 @@ exports.getWaitingOrdersByInstallationAndTeamMember = async (req, reply) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getCompleteOrdersByInstallationAndTeamMember = async (req, reply) => {
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// const { installationId, teamMemberId } = req.params;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (!installationId) {
|
|
|
|
|
|
|
|
// return reply.status(400).send({ error: "installationId is required" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (!teamMemberId) {
|
|
|
|
|
|
|
|
// return reply.status(400).send({ error: "teamMemberId is required" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Fetch orders matching installationId and assignedTeamMembers
|
|
|
|
|
|
|
|
// const orders = await Order.find({
|
|
|
|
|
|
|
|
// installationId,
|
|
|
|
|
|
|
|
// assignedTeamMembers: teamMemberId
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (!orders.length) {
|
|
|
|
|
|
|
|
// return reply.send({
|
|
|
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
|
|
|
// message: "No orders found for this installation and team member",
|
|
|
|
|
|
|
|
// data: [],
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const uniqueCustomersMap = new Map();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Build unique customerId-based map
|
|
|
|
|
|
|
|
// for (const order of orders) {
|
|
|
|
|
|
|
|
// if (!uniqueCustomersMap.has(order.customerId)) {
|
|
|
|
|
|
|
|
// uniqueCustomersMap.set(order.customerId, order);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// let uniqueOrders = Array.from(uniqueCustomersMap.values());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // ✅ Filter orders that have at least one pending master_connection
|
|
|
|
|
|
|
|
// uniqueOrders = uniqueOrders.filter(order =>
|
|
|
|
|
|
|
|
// Array.isArray(order.master_connections) &&
|
|
|
|
|
|
|
|
// order.master_connections.some(mc => mc.work_status === 'complete')
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (!uniqueOrders.length) {
|
|
|
|
|
|
|
|
// return reply.send({
|
|
|
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
|
|
|
// message: "No pending orders found for this installation and team member",
|
|
|
|
|
|
|
|
// data: [],
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Enrich and also filter master_connections inside each order
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Keep only master_connections with work_status === 'pending'
|
|
|
|
|
|
|
|
// const pendingMasters = order.master_connections.filter(mc => mc.work_status === 'complete');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return {
|
|
|
|
|
|
|
|
// ...order.toObject(),
|
|
|
|
|
|
|
|
// master_connections: pendingMasters,
|
|
|
|
|
|
|
|
// customer: customer || null,
|
|
|
|
|
|
|
|
// allocated_sensors: allocatedSensors,
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return reply.send({
|
|
|
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
|
|
|
// message: "Pending orders fetched successfully",
|
|
|
|
|
|
|
|
// data: ordersWithDetails,
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
|
|
|
// console.error("Error fetching pending orders:", err);
|
|
|
|
|
|
|
|
// return reply.status(500).send({ error: "Internal server error" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
exports.getCompleteOrdersByInstallationAndTeamMember = async (req, reply) => {
|
|
|
|
exports.getCompleteOrdersByInstallationAndTeamMember = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { installationId, teamMemberId } = req.params;
|
|
|
|
const { installationId, teamMemberId } = req.params;
|
|
|
@ -2975,7 +3060,7 @@ exports.getCompleteOrdersByInstallationAndTeamMember = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
let uniqueOrders = Array.from(uniqueCustomersMap.values());
|
|
|
|
let uniqueOrders = Array.from(uniqueCustomersMap.values());
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ Filter orders that have at least one pending master_connection
|
|
|
|
// ✅ Filter orders that have at least one master_connection with work_status === 'complete'
|
|
|
|
uniqueOrders = uniqueOrders.filter(order =>
|
|
|
|
uniqueOrders = uniqueOrders.filter(order =>
|
|
|
|
Array.isArray(order.master_connections) &&
|
|
|
|
Array.isArray(order.master_connections) &&
|
|
|
|
order.master_connections.some(mc => mc.work_status === 'complete')
|
|
|
|
order.master_connections.some(mc => mc.work_status === 'complete')
|
|
|
@ -2984,12 +3069,12 @@ exports.getCompleteOrdersByInstallationAndTeamMember = async (req, reply) => {
|
|
|
|
if (!uniqueOrders.length) {
|
|
|
|
if (!uniqueOrders.length) {
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|
message: "No pending orders found for this installation and team member",
|
|
|
|
message: "No complete orders found for this installation and team member",
|
|
|
|
data: [],
|
|
|
|
data: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Enrich and also filter master_connections inside each order
|
|
|
|
// Enrich orders and add work_status at top level
|
|
|
|
const ordersWithDetails = await Promise.all(
|
|
|
|
const ordersWithDetails = await Promise.all(
|
|
|
|
uniqueOrders.map(async (order) => {
|
|
|
|
uniqueOrders.map(async (order) => {
|
|
|
|
const customer = await User.findOne({ customerId: order.customerId }).lean();
|
|
|
|
const customer = await User.findOne({ customerId: order.customerId }).lean();
|
|
|
@ -3000,12 +3085,16 @@ exports.getCompleteOrdersByInstallationAndTeamMember = async (req, reply) => {
|
|
|
|
status: "blocked",
|
|
|
|
status: "blocked",
|
|
|
|
}).lean();
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
|
|
// Keep only master_connections with work_status === 'pending'
|
|
|
|
// Keep only master_connections with work_status === 'complete'
|
|
|
|
const pendingMasters = order.master_connections.filter(mc => mc.work_status === 'complete');
|
|
|
|
const completeMasters = order.master_connections.filter(mc => mc.work_status === 'complete');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Take work_status from the first matched master (which will be 'complete')
|
|
|
|
|
|
|
|
const work_status = completeMasters[0]?.work_status || null;
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
...order.toObject(),
|
|
|
|
...order.toObject(),
|
|
|
|
master_connections: pendingMasters,
|
|
|
|
master_connections: completeMasters,
|
|
|
|
|
|
|
|
work_status, // add at top level
|
|
|
|
customer: customer || null,
|
|
|
|
customer: customer || null,
|
|
|
|
allocated_sensors: allocatedSensors,
|
|
|
|
allocated_sensors: allocatedSensors,
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -3014,16 +3103,17 @@ exports.getCompleteOrdersByInstallationAndTeamMember = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|
message: "Pending orders fetched successfully",
|
|
|
|
message: "Complete orders fetched successfully",
|
|
|
|
data: ordersWithDetails,
|
|
|
|
data: ordersWithDetails,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error fetching pending orders:", err);
|
|
|
|
console.error("Error fetching complete orders:", err);
|
|
|
|
return reply.status(500).send({ error: "Internal server error" });
|
|
|
|
return reply.status(500).send({ error: "Internal server error" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getManagerPendingOrdersByInstallationId = async (req, reply) => {
|
|
|
|
// exports.getManagerPendingOrdersByInstallationId = async (req, reply) => {
|
|
|
|
// try {
|
|
|
|
// try {
|
|
|
|
// const { installationId } = req.params;
|
|
|
|
// const { installationId } = req.params;
|
|
|
|