|
|
@ -2644,7 +2644,7 @@ exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
return reply.status(400).send({ error: "installationId is required" });
|
|
|
|
return reply.status(400).send({ error: "installationId is required" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Fetch orders with the matching installationId
|
|
|
|
// ✅ Do NOT use .lean() here
|
|
|
|
const orders = await Order.find({ installationId });
|
|
|
|
const orders = await Order.find({ installationId });
|
|
|
|
|
|
|
|
|
|
|
|
if (!orders.length) {
|
|
|
|
if (!orders.length) {
|
|
|
@ -2657,42 +2657,37 @@ exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
const uniqueCustomersMap = new Map();
|
|
|
|
const uniqueCustomersMap = new Map();
|
|
|
|
|
|
|
|
|
|
|
|
// Build unique customerId-based map
|
|
|
|
|
|
|
|
for (const order of orders) {
|
|
|
|
for (const order of orders) {
|
|
|
|
if (!uniqueCustomersMap.has(order.customerId)) {
|
|
|
|
if (!uniqueCustomersMap.has(order.customerId)) {
|
|
|
|
uniqueCustomersMap.set(order.customerId, order);
|
|
|
|
uniqueCustomersMap.set(order.customerId, order);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Only keep one order per customerId
|
|
|
|
|
|
|
|
const uniqueOrders = Array.from(uniqueCustomersMap.values());
|
|
|
|
const uniqueOrders = Array.from(uniqueCustomersMap.values());
|
|
|
|
|
|
|
|
|
|
|
|
// Enrich with customer and sensor info, AND update work_status if missing
|
|
|
|
|
|
|
|
const ordersWithDetails = await Promise.all(
|
|
|
|
const ordersWithDetails = await Promise.all(
|
|
|
|
uniqueOrders.map(async (order) => {
|
|
|
|
uniqueOrders.map(async (order) => {
|
|
|
|
const customer = await User.findOne({ customerId: order.customerId }).lean();
|
|
|
|
// ✅ 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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
customerId: order.customerId,
|
|
|
|
customerId: order.customerId,
|
|
|
|
status: "blocked",
|
|
|
|
status: "blocked",
|
|
|
|
}).lean();
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ If missing, update work_status to "active" and save in DB
|
|
|
|
|
|
|
|
if (!order.work_status) {
|
|
|
|
|
|
|
|
order.work_status = "active";
|
|
|
|
|
|
|
|
await order.save(); // This persists the change to MongoDB
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
...order.toObject(),
|
|
|
|
...order.toObject(), // after save
|
|
|
|
customer: customer || null,
|
|
|
|
customer: customer || null,
|
|
|
|
allocated_sensors: allocatedSensors,
|
|
|
|
allocated_sensors: allocatedSensors,
|
|
|
|
work_status: order.work_status // Now always persisted
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
})
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
console.log("orders", orders)
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|
message: "Orders fetched successfully",
|
|
|
|
message: "Orders fetched successfully",
|
|
|
@ -2710,6 +2705,7 @@ 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;
|
|
|
|