|
|
@ -2638,41 +2638,109 @@ exports.getOrdersByStoreId = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { installationId } = req.params;
|
|
|
|
const { installationId, customerId } = req.params;
|
|
|
|
|
|
|
|
|
|
|
|
if (!installationId) {
|
|
|
|
if (!installationId) {
|
|
|
|
return reply.status(400).send({ error: "installationId is required" });
|
|
|
|
return reply.status(400).send({ error: "installationId is required" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ Do NOT use .lean() here
|
|
|
|
if (!customerId) {
|
|
|
|
const orders = await Order.find({ installationId });
|
|
|
|
return reply.status(400).send({ error: "customerId is required" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Build query — do NOT filter by work_status yet
|
|
|
|
|
|
|
|
const query = { installationId, customerId };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const orders = await Order.find(query);
|
|
|
|
|
|
|
|
|
|
|
|
if (!orders.length) {
|
|
|
|
if (!orders.length) {
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|
message: "No orders found for this installation",
|
|
|
|
message: "No orders found for this installation and customer",
|
|
|
|
data: [],
|
|
|
|
data: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const uniqueCustomersMap = new Map();
|
|
|
|
const ordersWithDetails = [];
|
|
|
|
|
|
|
|
|
|
|
|
for (const order of orders) {
|
|
|
|
for (const order of orders) {
|
|
|
|
if (!uniqueCustomersMap.has(order.customerId)) {
|
|
|
|
// Ensure work_status is set
|
|
|
|
uniqueCustomersMap.set(order.customerId, order);
|
|
|
|
if (!order.work_status || order.work_status.trim() === "") {
|
|
|
|
|
|
|
|
order.work_status = "active";
|
|
|
|
|
|
|
|
await order.save();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ Only push if work_status is "active"
|
|
|
|
|
|
|
|
if (order.work_status === "active") {
|
|
|
|
|
|
|
|
const customer = await User.findOne({ customerId: order.customerId }).lean();
|
|
|
|
|
|
|
|
const allocatedSensors = await Insensors.find({
|
|
|
|
|
|
|
|
storeId: order.storeId,
|
|
|
|
|
|
|
|
customerId: order.customerId,
|
|
|
|
|
|
|
|
status: "blocked",
|
|
|
|
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ordersWithDetails.push({
|
|
|
|
|
|
|
|
...order.toObject(),
|
|
|
|
|
|
|
|
customer: customer || null,
|
|
|
|
|
|
|
|
allocated_sensors: allocatedSensors,
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const uniqueOrders = Array.from(uniqueCustomersMap.values());
|
|
|
|
return reply.send({
|
|
|
|
|
|
|
|
status_code: 200,
|
|
|
|
|
|
|
|
message: "Orders fetched successfully",
|
|
|
|
|
|
|
|
data: ordersWithDetails,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
console.error("Error fetching orders:", err);
|
|
|
|
|
|
|
|
return reply.status(500).send({ error: "Internal server error" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ordersWithDetails = await Promise.all(
|
|
|
|
|
|
|
|
uniqueOrders.map(async (order) => {
|
|
|
|
|
|
|
|
// ✅ This checks if work_status is missing or empty, then sets and saves
|
|
|
|
|
|
|
|
if (!order.work_status || order.work_status.trim() === "") {
|
|
|
|
|
|
|
|
order.work_status = "active";
|
|
|
|
|
|
|
|
await order.save();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.updateWorkStatusByInstallationId = async (req, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const { installationId, customerId } = req.params;
|
|
|
|
|
|
|
|
const { work_status } = req.body; // 🟢 pass in body
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!installationId) {
|
|
|
|
|
|
|
|
return reply.status(400).send({ error: "installationId is required" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!customerId) {
|
|
|
|
|
|
|
|
return reply.status(400).send({ error: "customerId is required" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!work_status || !['active', 'pending', 'complete'].includes(work_status)) {
|
|
|
|
|
|
|
|
return reply.status(400).send({ error: "Valid work_status is required: active, pending or complete" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Find all orders for this installation + customer
|
|
|
|
|
|
|
|
const orders = await Order.find({ installationId, customerId });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!orders.length) {
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
|
|
|
status_code: 200,
|
|
|
|
|
|
|
|
message: "No orders found for this installation and customer",
|
|
|
|
|
|
|
|
data: [],
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update all found orders to new work_status
|
|
|
|
|
|
|
|
for (const order of orders) {
|
|
|
|
|
|
|
|
order.work_status = work_status;
|
|
|
|
|
|
|
|
await order.save();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// After update, fetch all with updated work_status only
|
|
|
|
|
|
|
|
const updatedOrders = await Order.find({ installationId, customerId, work_status });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ordersWithDetails = await Promise.all(
|
|
|
|
|
|
|
|
updatedOrders.map(async (order) => {
|
|
|
|
const customer = await User.findOne({ customerId: order.customerId }).lean();
|
|
|
|
const customer = await User.findOne({ customerId: order.customerId }).lean();
|
|
|
|
const allocatedSensors = await Insensors.find({
|
|
|
|
const allocatedSensors = await Insensors.find({
|
|
|
|
storeId: order.storeId,
|
|
|
|
storeId: order.storeId,
|
|
|
@ -2681,21 +2749,21 @@ exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
}).lean();
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
...order.toObject(), // after save
|
|
|
|
...order.toObject(),
|
|
|
|
customer: customer || null,
|
|
|
|
customer: customer || null,
|
|
|
|
allocated_sensors: allocatedSensors,
|
|
|
|
allocated_sensors: allocatedSensors,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
})
|
|
|
|
);
|
|
|
|
);
|
|
|
|
console.log("orders", orders)
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|
message: "Orders fetched successfully",
|
|
|
|
message: `Orders updated to work_status '${work_status}' successfully`,
|
|
|
|
data: ordersWithDetails,
|
|
|
|
data: ordersWithDetails,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error fetching orders:", err);
|
|
|
|
console.error("Error updating work_status:", err);
|
|
|
|
return reply.status(500).send({ error: "Internal server error" });
|
|
|
|
return reply.status(500).send({ error: "Internal server error" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -2704,8 +2772,6 @@ exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getallocatedsensorstouser= async (req, reply) => {
|
|
|
|
exports.getallocatedsensorstouser= async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { customerId } = req.params;
|
|
|
|
const { customerId } = req.params;
|
|
|
|