ashok 3 months ago
commit 8f64d469db

@ -2849,43 +2849,120 @@ exports.getPendingOrdersByInstallationId = async (req, reply) => {
// 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 allocatedSensors = await Insensors.find({
// storeId: order.storeId,
// customerId: order.customerId,
// status: "blocked",
// }).lean();
// return {
// ...order.toObject(),
// customer: customer || null,
// allocated_sensors: allocatedSensors,
// };
// })
// );
// return reply.send({
// status_code: 200,
// message: `Orders updated to work_status '${work_status}' successfully`,
// data: ordersWithDetails,
// });
// } catch (err) {
// console.error("Error updating work_status:", err);
// return reply.status(500).send({ error: "Internal server error" });
// }
// };
exports.updateWorkStatusByInstallationId = async (req, reply) => {
try {
const { installationId, customerId } = req.params;
const { work_status } = req.body; // 🟢 pass in body
const { work_status, hardwareId } = req.body;
if (!installationId) {
return reply.status(400).send({ error: "installationId is required" });
}
if (!customerId) {
return reply.status(400).send({ error: "customerId is required" });
}
if (!hardwareId) {
return reply.status(400).send({ error: "hardwareId 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 });
// ✅ Correct query
const orders = await Order.find({
installationId,
customerId,
'master_connections.hardwareId': hardwareId
});
if (!orders.length) {
return reply.send({
status_code: 200,
message: "No orders found for this installation and customer",
message: "No orders found for this installation, customer and hardwareId",
data: [],
});
}
// Update all found orders to new work_status
// Update 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 });
// Fetch updated orders
const updatedOrders = await Order.find({
installationId,
customerId,
'master_connections.hardwareId': hardwareId,
work_status
});
// Enrich data
const ordersWithDetails = await Promise.all(
updatedOrders.map(async (order) => {
const customer = await User.findOne({ customerId: order.customerId }).lean();
@ -2919,6 +2996,7 @@ exports.updateWorkStatusByInstallationId = async (req, reply) => {
exports.getallocatedsensorstouser= async (req, reply) => {
try {
const { customerId } = req.params;

@ -2033,25 +2033,29 @@ fastify.post(
},
body: {
type: 'object',
required: ['work_status'],
required: ['work_status', 'hardwareId'],
properties: {
work_status: {
type: 'string',
enum: ['active', 'pending', 'complete'],
description: 'The new work status',
},
},
},
response: {
200: {
type: 'object',
properties: {
status_code: { type: 'integer' },
message: { type: 'string' },
data: { type: 'array' }, // you can make this stricter too if needed
hardwareId: {
type: 'string',
description: 'Hardware ID to update orders for',
},
},
},
// response: {
// 200: {
// type: 'object',
// properties: {
// status_code: { type: 'integer' },
// message: { type: 'string' },
// data: { type: 'array' },
// },
// },
// },
},
handler: storeController.updateWorkStatusByInstallationId,
}

Loading…
Cancel
Save