|
|
@ -2636,6 +2636,60 @@ exports.getOrdersByStoreId = async (req, reply) => {
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// const { installationId } = req.params;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (!installationId) {
|
|
|
|
|
|
|
|
// return reply.status(400).send({ error: "installationId is required" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Step 1: Fetch orders by installationId
|
|
|
|
|
|
|
|
// const orders = await Order.find({ installationId });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (!orders.length) {
|
|
|
|
|
|
|
|
// return reply.send({
|
|
|
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
|
|
|
// message: "No orders found for this installation",
|
|
|
|
|
|
|
|
// data: [],
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const ordersWithActiveMasters = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Step 2: Process each order
|
|
|
|
|
|
|
|
// for (const order of orders) {
|
|
|
|
|
|
|
|
// // Filter master_connections to keep only those where work_status is 'active' or empty/null
|
|
|
|
|
|
|
|
// const activeMasters = (order.master_connections || []).filter(mc =>
|
|
|
|
|
|
|
|
// mc.work_status === 'active' || mc.work_status === '' || mc.work_status == null
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (activeMasters.length) {
|
|
|
|
|
|
|
|
// // Fetch customer details
|
|
|
|
|
|
|
|
// const customer = await User.findOne({ customerId: order.customerId }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Build response object
|
|
|
|
|
|
|
|
// ordersWithActiveMasters.push({
|
|
|
|
|
|
|
|
// ...order.toObject(),
|
|
|
|
|
|
|
|
// master_connections: activeMasters, // only active master_connections
|
|
|
|
|
|
|
|
// customer: customer || null,
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Step 3: Return response
|
|
|
|
|
|
|
|
// return reply.send({
|
|
|
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
|
|
|
// message: "Orders with active master connections fetched successfully",
|
|
|
|
|
|
|
|
// data: ordersWithActiveMasters,
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
|
|
|
// console.error("Error fetching orders:", err);
|
|
|
|
|
|
|
|
// return reply.status(500).send({ error: "Internal server error" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { installationId } = req.params;
|
|
|
|
const { installationId } = req.params;
|
|
|
@ -2655,11 +2709,14 @@ exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const ordersWithActiveMasters = [];
|
|
|
|
const ordersWithDetails = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Fetch installation document once (contains all team members)
|
|
|
|
|
|
|
|
const installationDoc = await Install.findOne({ installationId }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
// Step 2: Process each order
|
|
|
|
// Step 2: Process each order
|
|
|
|
for (const order of orders) {
|
|
|
|
for (const order of orders) {
|
|
|
|
// Filter master_connections to keep only those where work_status is 'active' or empty/null
|
|
|
|
// Filter master_connections to keep active ones
|
|
|
|
const activeMasters = (order.master_connections || []).filter(mc =>
|
|
|
|
const activeMasters = (order.master_connections || []).filter(mc =>
|
|
|
|
mc.work_status === 'active' || mc.work_status === '' || mc.work_status == null
|
|
|
|
mc.work_status === 'active' || mc.work_status === '' || mc.work_status == null
|
|
|
|
);
|
|
|
|
);
|
|
|
@ -2668,20 +2725,34 @@ exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
// Fetch customer details
|
|
|
|
// Fetch customer details
|
|
|
|
const customer = await User.findOne({ customerId: order.customerId }).lean();
|
|
|
|
const customer = await User.findOne({ customerId: order.customerId }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
// Build response object
|
|
|
|
// Find assigned team members from installation.team_member.team_member
|
|
|
|
ordersWithActiveMasters.push({
|
|
|
|
let assignedTeamMembersDetails = [];
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
|
|
installationDoc &&
|
|
|
|
|
|
|
|
installationDoc.team_member &&
|
|
|
|
|
|
|
|
Array.isArray(installationDoc.team_member.team_member) &&
|
|
|
|
|
|
|
|
order.assignedTeamMembers &&
|
|
|
|
|
|
|
|
order.assignedTeamMembers.length > 0
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
assignedTeamMembersDetails = installationDoc.team_member.team_member.filter(tm =>
|
|
|
|
|
|
|
|
order.assignedTeamMembers.includes(tm.teamMemberId)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Build response
|
|
|
|
|
|
|
|
ordersWithDetails.push({
|
|
|
|
...order.toObject(),
|
|
|
|
...order.toObject(),
|
|
|
|
master_connections: activeMasters, // only active master_connections
|
|
|
|
master_connections: activeMasters,
|
|
|
|
customer: customer || null,
|
|
|
|
customer: customer || null,
|
|
|
|
|
|
|
|
assignedTeamMembersDetails,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Step 3: Return response
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|
message: "Orders with active master connections fetched successfully",
|
|
|
|
message: "Orders with active master connections fetched successfully",
|
|
|
|
data: ordersWithActiveMasters,
|
|
|
|
data: ordersWithDetails,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
@ -2694,7 +2765,6 @@ 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;
|
|
|
|