|
|
|
@ -2850,6 +2850,91 @@ exports.getPendingOrdersByInstallationAndTeamMember = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.getWaitingOrdersByInstallationAndTeamMember = 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 === 'waiting')
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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 === 'waiting');
|
|
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
|
try {
|
|
|
|
|
const { installationId, teamMemberId } = req.params;
|
|
|
|
|