pending active work status apis

master^2
Bhaskar 3 months ago
parent 80822ed665
commit 6a1f78e172

@ -2638,41 +2638,109 @@ exports.getOrdersByStoreId = async (req, reply) => {
exports.getOrdersByInstallationId = async (req, reply) => {
try {
const { installationId } = req.params;
const { installationId, customerId } = req.params;
if (!installationId) {
return reply.status(400).send({ error: "installationId is required" });
}
// ✅ Do NOT use .lean() here
const orders = await Order.find({ installationId });
if (!customerId) {
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) {
return reply.send({
status_code: 200,
message: "No orders found for this installation",
message: "No orders found for this installation and customer",
data: [],
});
}
const uniqueCustomersMap = new Map();
const ordersWithDetails = [];
for (const order of orders) {
if (!uniqueCustomersMap.has(order.customerId)) {
uniqueCustomersMap.set(order.customerId, order);
// Ensure work_status is set
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,
});
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";
} catch (err) {
console.error("Error fetching orders:", 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
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,
@ -2681,21 +2749,21 @@ exports.getOrdersByInstallationId = async (req, reply) => {
}).lean();
return {
...order.toObject(), // after save
...order.toObject(),
customer: customer || null,
allocated_sensors: allocatedSensors,
};
})
);
console.log("orders", orders)
return reply.send({
status_code: 200,
message: "Orders fetched successfully",
message: `Orders updated to work_status '${work_status}' successfully`,
data: ordersWithDetails,
});
} catch (err) {
console.error("Error fetching orders:", err);
console.error("Error updating work_status:", err);
return reply.status(500).send({ error: "Internal server error" });
}
};
@ -2704,8 +2772,6 @@ exports.getOrdersByInstallationId = async (req, reply) => {
exports.getallocatedsensorstouser= async (req, reply) => {
try {
const { customerId } = req.params;

@ -1969,7 +1969,7 @@ fastify.get("/api/ordersofstore/:storeId", {
});
fastify.get("/api/ordersofinstall/:installationId", {
fastify.get("/api/ordersofinstall/:installationId/:customerId", {
schema: {
tags: ["Installation"],
description: "Fetches orders based on installationId",
@ -1978,8 +1978,10 @@ fastify.get("/api/ordersofinstall/:installationId", {
type: "object",
properties: {
installationId: { type: "string" },
//work_status: { type: "string"},
customerId: { type: "string"},
},
required: ["installationId"],
// required: ["installationId"],
},
security: [
{
@ -1990,6 +1992,47 @@ fastify.get("/api/ordersofinstall/:installationId", {
handler: storeController.getOrdersByInstallationId,
});
fastify.post(
'/api/orders/:installationId/:customerId',
{
schema: {
tags: ["Installation"],
description: "Update the work status",
summary: "Update the work status",
params: {
type: 'object',
required: ['installationId', 'customerId'],
properties: {
installationId: { type: 'string' },
customerId: { type: 'string' },
},
},
body: {
type: 'object',
required: ['work_status'],
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
},
},
},
},
handler: storeController.updateWorkStatusByInstallationId,
}
);
fastify.get("/api/getallocatedsensors/:customerId", {
schema: {

Loading…
Cancel
Save