|
|
|
|
@ -1173,10 +1173,10 @@ exports.installmotorswitch = async (request, reply) => {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.generateHardwareMasterId = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { from, to, type } = req.body;
|
|
|
|
|
const storeId = req.params.storeId
|
|
|
|
|
const { from, to, type, quantity } = req.body;
|
|
|
|
|
const sensorType = type.toLowerCase();
|
|
|
|
|
const fromInt = parseInt(from, 10);
|
|
|
|
|
const toInt = parseInt(to, 10);
|
|
|
|
|
@ -1185,17 +1185,22 @@ exports.generateHardwareMasterId = async (req, reply) => {
|
|
|
|
|
return reply.code(400).send({ message: 'Invalid from/to values' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch pending sensors of the given type
|
|
|
|
|
const pendingSensors = await Insensors.find({ status: 'pending', type: sensorType, hardwareId: null });
|
|
|
|
|
// Fetch pending sensors of the given type and storeId whose hardwareId is null
|
|
|
|
|
const pendingSensors = await Insensors.find({ status: 'pending', type: sensorType, storeId, hardwareId: null });
|
|
|
|
|
|
|
|
|
|
if (!pendingSensors.length) {
|
|
|
|
|
return reply.code(404).send({ message: 'No pending sensors found for the given type' });
|
|
|
|
|
return reply.code(404).send({ message: 'No pending sensors found for the given type and storeId' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (quantity > pendingSensors.length) {
|
|
|
|
|
return reply.code(400).send({ message: `Available quantity is less than requested: ${pendingSensors.length} available.` });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let hardwareIdSequence = fromInt;
|
|
|
|
|
const date = moment().format('MM-DD');
|
|
|
|
|
|
|
|
|
|
for (let sensor of pendingSensors) {
|
|
|
|
|
for (let i = 0; i < quantity && i < pendingSensors.length; i++) {
|
|
|
|
|
let sensor = pendingSensors[i];
|
|
|
|
|
if (hardwareIdSequence > toInt) break;
|
|
|
|
|
|
|
|
|
|
sensor.hardwareId = hardwareIdSequence.toString().padStart(8, '0');
|
|
|
|
|
@ -2335,41 +2340,37 @@ exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
const { installationId } = req.params;
|
|
|
|
|
|
|
|
|
|
if (!installationId) {
|
|
|
|
|
return reply.status(400).send({ error: "installationId is required" });
|
|
|
|
|
return reply.status(400).send({ error: "storeId is required" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch orders with the matching storeId
|
|
|
|
|
const orders = await Order.find({ installationId });
|
|
|
|
|
|
|
|
|
|
if (!orders.length) {
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "No orders found for this installationId",
|
|
|
|
|
message: "No orders found for this store",
|
|
|
|
|
data: [],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch customer details & allocated sensors for each order
|
|
|
|
|
const ordersWithDetails = await Promise.all(
|
|
|
|
|
orders.map(async (order) => {
|
|
|
|
|
// Fetch customer details
|
|
|
|
|
const customer = await User.findOne({ customerId: order.customerId }).lean();
|
|
|
|
|
|
|
|
|
|
console.log("Fetching allocated sensors for:", {
|
|
|
|
|
storeId: order.storeId,
|
|
|
|
|
customerId: order.customerId,
|
|
|
|
|
status: "blocked",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Fetch allocated sensors for this customer
|
|
|
|
|
const allocatedSensors = await Insensors.find({
|
|
|
|
|
storeId: order.storeId,
|
|
|
|
|
customerId: order.customerId,
|
|
|
|
|
status: "blocked",
|
|
|
|
|
installationId,
|
|
|
|
|
customerId: order.customerId, // Match only sensors allocated to this customer
|
|
|
|
|
status: "blocked", // Only fetch sensors that are allocated (blocked)
|
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
console.log("✅ Found Sensors:", allocatedSensors);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...order.toObject(),
|
|
|
|
|
customer: customer || null,
|
|
|
|
|
allocated_sensors: allocatedSensors, // Now it should return the correct sensors
|
|
|
|
|
customer: customer || null, // Include customer details or null if not found
|
|
|
|
|
allocated_sensors: allocatedSensors, // List of allocated sensors
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
@ -2380,12 +2381,11 @@ exports.getOrdersByInstallationId = async (req, reply) => {
|
|
|
|
|
data: ordersWithDetails,
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("❌ Error fetching orders:", err);
|
|
|
|
|
console.error("Error fetching orders:", err);
|
|
|
|
|
return reply.status(500).send({ error: "Internal server error" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getallocatedsensorstouser= async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId } = req.params;
|
|
|
|
|
|