|
|
|
@ -2414,6 +2414,91 @@ exports.raiseATicket = async (req, reply) => {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.raiseATicketBuildingDetails = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId, connected_to, installationId } = req.params;
|
|
|
|
|
|
|
|
|
|
if (!customerId || !connected_to || !installationId) {
|
|
|
|
|
return reply.code(400).send({ error: "customerId, connected_to, and installationId are required" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const customer = await User.findOne({ customerId, installationId }).lean();
|
|
|
|
|
if (!customer) {
|
|
|
|
|
return reply.code(404).send({ message: "Customer not found." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sensors = await Insensors.find({ customerId });
|
|
|
|
|
if (!sensors.length) {
|
|
|
|
|
return reply.code(404).send({ message: "No sensors found for this customer." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const masterSensor = sensors.find(s => (s.hardwareId?.trim() === connected_to.trim()));
|
|
|
|
|
if (!masterSensor) {
|
|
|
|
|
return reply.code(404).send({ message: "Master hardwareId not found." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const latestMasterRecord = await IotData.findOne({ hardwareId: connected_to }).sort({ date: -1 }).lean();
|
|
|
|
|
if (!latestMasterRecord) {
|
|
|
|
|
return reply.code(404).send({ message: "No IoT data found for this hardwareId." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const indiaTime = moment.tz(latestMasterRecord.date, "Asia/Kolkata");
|
|
|
|
|
const now = moment.tz("Asia/Kolkata");
|
|
|
|
|
const formattedNow = now.format("YYYY-MM-DD HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
const diffInMinutesMaster = now.diff(indiaTime, "minutes");
|
|
|
|
|
|
|
|
|
|
if (diffInMinutesMaster > 1) {
|
|
|
|
|
await Insensors.updateOne(
|
|
|
|
|
{ hardwareId: connected_to },
|
|
|
|
|
{ $set: { lastTicketRaisedAt: formattedNow } }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === connected_to.trim());
|
|
|
|
|
|
|
|
|
|
for (const slave of connectedSlaves) {
|
|
|
|
|
const slaveId = slave.hardwareId?.trim();
|
|
|
|
|
const matchedTank = latestMasterRecord.tanks.find(tank => tank.tankhardwareId === slaveId);
|
|
|
|
|
|
|
|
|
|
if (matchedTank && matchedTank.date) {
|
|
|
|
|
const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata");
|
|
|
|
|
const loraDiffInMinutes = now.diff(tankTime, "minutes");
|
|
|
|
|
|
|
|
|
|
if (loraDiffInMinutes > 1) {
|
|
|
|
|
await Insensors.updateOne(
|
|
|
|
|
{ hardwareId: slaveId },
|
|
|
|
|
{ $set: { lastTicketRaisedAt: formattedNow } }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Support.updateOne(
|
|
|
|
|
{ supportId: "AWHYSU64" },
|
|
|
|
|
{ $set: { lastTicketRaisedAt: formattedNow } }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Fetch updated `lastTicketRaisedAt` values
|
|
|
|
|
const updatedMasterSensor = await Insensors.findOne({ hardwareId: connected_to }).lean();
|
|
|
|
|
const updatedSupport = await Support.findOne({ supportId: "AWHYSU64" }).lean();
|
|
|
|
|
console.log("updatedMasterSensor",updatedMasterSensor)
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
customer,
|
|
|
|
|
lastTicketRaisedAt: {
|
|
|
|
|
masterSensor: updatedMasterSensor?.lastTicketRaisedAt || null,
|
|
|
|
|
support: updatedSupport?.lastTicketRaisedAt || null
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error raising ticket:", error);
|
|
|
|
|
return reply.code(500).send({ error: "Internal server error" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.raiseATicketSlave = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId, connected_to, tankHardwareId } = req.params; // Now tankHardwareId from params
|
|
|
|
|