|
|
|
@ -2711,21 +2711,27 @@ exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getPendingOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
exports.getPendingOrdersByInstallationAndTeamMember = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { installationId } = req.params;
|
|
|
|
|
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 with the matching installationId
|
|
|
|
|
const orders = await Order.find({ installationId });
|
|
|
|
|
// 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",
|
|
|
|
|
message: "No orders found for this installation and team member",
|
|
|
|
|
data: [],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
@ -2739,22 +2745,23 @@ exports.getPendingOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 === 'pending');
|
|
|
|
|
// ✅ 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 === 'pending')
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// If nothing left after filtering, return empty
|
|
|
|
|
if (!uniqueOrders.length) {
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "No pending orders found for this installation",
|
|
|
|
|
message: "No pending orders found for this installation and team member",
|
|
|
|
|
data: [],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Enrich with customer and sensor info
|
|
|
|
|
// 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();
|
|
|
|
@ -2765,8 +2772,12 @@ exports.getPendingOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
status: "blocked",
|
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
// Keep only master_connections with work_status === 'pending'
|
|
|
|
|
const pendingMasters = order.master_connections.filter(mc => mc.work_status === 'pending');
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...order.toObject(),
|
|
|
|
|
master_connections: pendingMasters,
|
|
|
|
|
customer: customer || null,
|
|
|
|
|
allocated_sensors: allocatedSensors,
|
|
|
|
|
};
|
|
|
|
@ -2780,11 +2791,13 @@ exports.getPendingOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error fetching orders:", err);
|
|
|
|
|
console.error("Error fetching pending orders:", err);
|
|
|
|
|
return reply.status(500).send({ error: "Internal server error" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getCompleteOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { installationId } = req.params;
|
|
|
|
@ -2991,7 +3004,7 @@ exports.getCompleteOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
exports.updateWorkStatusByInstallationId = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { installationId, customerId } = req.params;
|
|
|
|
|
const { installationId, customerId, teamMemberId } = req.params;
|
|
|
|
|
const { work_status, hardwareId } = req.body;
|
|
|
|
|
|
|
|
|
|
if (!installationId) {
|
|
|
|
@ -3000,6 +3013,9 @@ exports.updateWorkStatusByInstallationId = async (req, reply) => {
|
|
|
|
|
if (!customerId) {
|
|
|
|
|
return reply.status(400).send({ error: "customerId is required" });
|
|
|
|
|
}
|
|
|
|
|
if (!teamMemberId) {
|
|
|
|
|
return reply.status(400).send({ error: "teamMemberId is required" });
|
|
|
|
|
}
|
|
|
|
|
if (!hardwareId) {
|
|
|
|
|
return reply.status(400).send({ error: "hardwareId is required" });
|
|
|
|
|
}
|
|
|
|
@ -3007,36 +3023,47 @@ exports.updateWorkStatusByInstallationId = async (req, reply) => {
|
|
|
|
|
return reply.status(400).send({ error: "Valid work_status is required: active, pending or complete" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ✅ Correct query
|
|
|
|
|
// ✅ Find orders that match installationId, customerId, assignedTeamMembers, and master_connections.hardwareId
|
|
|
|
|
const orders = await Order.find({
|
|
|
|
|
installationId,
|
|
|
|
|
customerId,
|
|
|
|
|
assignedTeamMembers: teamMemberId,
|
|
|
|
|
'master_connections.hardwareId': hardwareId
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!orders.length) {
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "No orders found for this installation, customer and hardwareId",
|
|
|
|
|
message: "No orders found for this installation, customer, team member, and hardwareId",
|
|
|
|
|
data: [],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update work_status
|
|
|
|
|
// 🔧 Update work_status in master_connections inside each order
|
|
|
|
|
for (const order of orders) {
|
|
|
|
|
order.work_status = work_status;
|
|
|
|
|
let modified = false;
|
|
|
|
|
if (Array.isArray(order.master_connections)) {
|
|
|
|
|
for (const mc of order.master_connections) {
|
|
|
|
|
if (mc.hardwareId === hardwareId) {
|
|
|
|
|
mc.work_status = work_status;
|
|
|
|
|
modified = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (modified) {
|
|
|
|
|
await order.save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch updated orders
|
|
|
|
|
// ✅ Fetch updated orders to return
|
|
|
|
|
const updatedOrders = await Order.find({
|
|
|
|
|
installationId,
|
|
|
|
|
customerId,
|
|
|
|
|
'master_connections.hardwareId': hardwareId,
|
|
|
|
|
work_status
|
|
|
|
|
});
|
|
|
|
|
assignedTeamMembers: teamMemberId,
|
|
|
|
|
'master_connections.hardwareId': hardwareId
|
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
// Enrich data
|
|
|
|
|
// 🔹 Enrich each order with customer and allocated sensors
|
|
|
|
|
const ordersWithDetails = await Promise.all(
|
|
|
|
|
updatedOrders.map(async (order) => {
|
|
|
|
|
const customer = await User.findOne({ customerId: order.customerId }).lean();
|
|
|
|
@ -3047,7 +3074,7 @@ exports.updateWorkStatusByInstallationId = async (req, reply) => {
|
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...order.toObject(),
|
|
|
|
|
...order,
|
|
|
|
|
customer: customer || null,
|
|
|
|
|
allocated_sensors: allocatedSensors,
|
|
|
|
|
};
|
|
|
|
@ -3056,7 +3083,7 @@ exports.updateWorkStatusByInstallationId = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: `Orders updated to work_status '${work_status}' successfully`,
|
|
|
|
|
message: `Orders updated: master_connections with hardwareId '${hardwareId}' now has work_status '${work_status}'`,
|
|
|
|
|
data: ordersWithDetails,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -3071,6 +3098,7 @@ exports.updateWorkStatusByInstallationId = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getallocatedsensorstouser= async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId } = req.params;
|
|
|
|
|