waiting orders team member installation

master^2
Bhaskar 3 months ago
parent 52051930f3
commit b354c63b60

@ -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) => { exports.getCompleteOrdersByInstallationAndTeamMember = async (req, reply) => {
try { try {
const { installationId, teamMemberId } = req.params; const { installationId, teamMemberId } = req.params;

@ -2015,6 +2015,29 @@ fastify.get("/api/Pendingordersofinstall/:installationId/:teamMemberId", {
handler: storeController.getPendingOrdersByInstallationAndTeamMember, handler: storeController.getPendingOrdersByInstallationAndTeamMember,
}); });
fastify.get("/api/waitingordersofinstall/:installationId/:teamMemberId", {
schema: {
tags: ["Installation"],
description: "Fetches orders based on installationId",
summary: "Get waiting orders team member by installationId",
params: {
type: "object",
properties: {
installationId: { type: "string" },
teamMemberId: { type: "string"},
//customerId: { type: "string"},
},
// required: ["installationId"],
},
security: [
{
basicAuth: [],
},
],
},
handler: storeController.getWaitingOrdersByInstallationAndTeamMember,
});
fastify.get("/api/Completeordersofinstall/:installationId/:teamMemberId", { fastify.get("/api/Completeordersofinstall/:installationId/:teamMemberId", {
schema: { schema: {
tags: ["Installation"], tags: ["Installation"],

Loading…
Cancel
Save