|
|
@ -2450,6 +2450,145 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
//const moment = require("moment-timezone");
|
|
|
|
//const moment = require("moment-timezone");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.raiseATicket = async (req, reply) => {
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// const { customerId, connected_to } = req.params;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (!customerId || !connected_to) {
|
|
|
|
|
|
|
|
// return reply.code(400).send({ error: "customerId and connected_to are required" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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() && s.type === "master");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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 diffInMinutesMaster = now.diff(indiaTime, "minutes");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const masterDisconnected = diffInMinutesMaster > 1 ? [{
|
|
|
|
|
|
|
|
// hardwareId: connected_to,
|
|
|
|
|
|
|
|
// masterName: masterSensor.tankName || masterSensor.tankName || "Unknown Master",
|
|
|
|
|
|
|
|
// // disconnectedAt: now.format("DD-MM-YYYY HH:mm:ss")
|
|
|
|
|
|
|
|
// }] : [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const connectedSlaves = sensors.filter(sensor =>
|
|
|
|
|
|
|
|
// sensor.connected_to?.trim() === connected_to.trim() &&
|
|
|
|
|
|
|
|
// sensor.type === "slave"
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const disconnectedSlaves = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
|
|
|
|
// disconnectedSlaves.push({
|
|
|
|
|
|
|
|
// hardwareId: connected_to,
|
|
|
|
|
|
|
|
// slaveHardwareId: slaveId,
|
|
|
|
|
|
|
|
// slaveName: slave.tankName || "Unknown Slave"
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const issuesToAdd = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (masterDisconnected.length > 0) {
|
|
|
|
|
|
|
|
// const existingGsmIssue = await Support.findOne({
|
|
|
|
|
|
|
|
// "issues.hardwareId": connected_to,
|
|
|
|
|
|
|
|
// "issues.type": "GSM Disconnected"
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (!existingGsmIssue) {
|
|
|
|
|
|
|
|
// issuesToAdd.push({
|
|
|
|
|
|
|
|
// type: "GSM Disconnected",
|
|
|
|
|
|
|
|
// hardwareId: connected_to,
|
|
|
|
|
|
|
|
// message: `Master GSM disconnected - ${connected_to}`,
|
|
|
|
|
|
|
|
// //disconnectedAt: now.format("DD-MM-YYYY HH:mm:ss")
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Check for already existing slave issues
|
|
|
|
|
|
|
|
// const newHardwareIds = [];
|
|
|
|
|
|
|
|
// const newSlaveNames = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// for (const slave of disconnectedSlaves) {
|
|
|
|
|
|
|
|
// const existingSlaveIssue = await Support.findOne({
|
|
|
|
|
|
|
|
// "issues.hardwareIds": slave.slaveHardwareId,
|
|
|
|
|
|
|
|
// "issues.masterHardwareId": connected_to,
|
|
|
|
|
|
|
|
// "issues.type": "LoRa Disconnected"
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (!existingSlaveIssue) {
|
|
|
|
|
|
|
|
// newHardwareIds.push(slave.slaveHardwareId);
|
|
|
|
|
|
|
|
// newSlaveNames.push(slave.slaveName);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (newHardwareIds.length > 0) {
|
|
|
|
|
|
|
|
// issuesToAdd.push({
|
|
|
|
|
|
|
|
// type: "LoRa Disconnected",
|
|
|
|
|
|
|
|
// masterHardwareId: connected_to,
|
|
|
|
|
|
|
|
// hardwareIds: newHardwareIds,
|
|
|
|
|
|
|
|
// slaveNames: newSlaveNames,
|
|
|
|
|
|
|
|
// message: `Slaves LoRa disconnected under master ${connected_to}`,
|
|
|
|
|
|
|
|
// // disconnectedAt: now.format("DD-MM-YYYY HH:mm:ss")
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (issuesToAdd.length > 0) {
|
|
|
|
|
|
|
|
// const supportRecord = await Support.findOne({ supportId: "AWHYSU64" });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (supportRecord) {
|
|
|
|
|
|
|
|
// await Support.findOneAndUpdate(
|
|
|
|
|
|
|
|
// { _id: supportRecord._id },
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// $push: {
|
|
|
|
|
|
|
|
// issues: { $each: issuesToAdd }
|
|
|
|
|
|
|
|
// },
|
|
|
|
|
|
|
|
// updatedAt: new Date()
|
|
|
|
|
|
|
|
// },
|
|
|
|
|
|
|
|
// { new: true }
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
// console.error("Support record not found for supportId AWHYSU64");
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return reply.send({
|
|
|
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
|
|
|
// message: "Checked connection and updated support if needed.",
|
|
|
|
|
|
|
|
// masterDisconnected,
|
|
|
|
|
|
|
|
// disconnectedSlaves
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
|
|
|
// console.error("Error raising ticket:", error);
|
|
|
|
|
|
|
|
// return reply.code(500).send({ error: "Internal server error" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
exports.raiseATicket = async (req, reply) => {
|
|
|
|
exports.raiseATicket = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { customerId, connected_to } = req.params;
|
|
|
|
const { customerId, connected_to } = req.params;
|
|
|
@ -2476,14 +2615,15 @@ exports.raiseATicket = async (req, reply) => {
|
|
|
|
return reply.code(404).send({ message: "No IoT data found for this hardwareId." });
|
|
|
|
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 now = moment.tz("Asia/Kolkata");
|
|
|
|
|
|
|
|
const indiaTime = moment.tz(latestMasterRecord.date, "Asia/Kolkata");
|
|
|
|
const diffInMinutesMaster = now.diff(indiaTime, "minutes");
|
|
|
|
const diffInMinutesMaster = now.diff(indiaTime, "minutes");
|
|
|
|
|
|
|
|
|
|
|
|
const masterDisconnected = diffInMinutesMaster > 1 ? [{
|
|
|
|
const masterDisconnected = diffInMinutesMaster > 1 ? [{
|
|
|
|
hardwareId: connected_to,
|
|
|
|
hardwareId: connected_to,
|
|
|
|
masterName: masterSensor.tankName || masterSensor.tankName || "Unknown Master",
|
|
|
|
masterName: masterSensor.tankName || "Unknown Master",
|
|
|
|
// disconnectedAt: now.format("DD-MM-YYYY HH:mm:ss")
|
|
|
|
connected_status: "disconnected",
|
|
|
|
|
|
|
|
last_seen_minutes_ago: diffInMinutesMaster
|
|
|
|
}] : [];
|
|
|
|
}] : [];
|
|
|
|
|
|
|
|
|
|
|
|
const connectedSlaves = sensors.filter(sensor =>
|
|
|
|
const connectedSlaves = sensors.filter(sensor =>
|
|
|
@ -2492,23 +2632,37 @@ exports.raiseATicket = async (req, reply) => {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const disconnectedSlaves = [];
|
|
|
|
const disconnectedSlaves = [];
|
|
|
|
|
|
|
|
const slaveStatusList = [];
|
|
|
|
|
|
|
|
|
|
|
|
for (const slave of connectedSlaves) {
|
|
|
|
for (const slave of connectedSlaves) {
|
|
|
|
const slaveId = slave.hardwareId?.trim();
|
|
|
|
const slaveId = slave.hardwareId?.trim();
|
|
|
|
const matchedTank = latestMasterRecord.tanks.find(tank => tank.tankhardwareId === slaveId);
|
|
|
|
const matchedTank = latestMasterRecord.tanks.find(tank => tank.tankhardwareId === slaveId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let isDisconnected = true;
|
|
|
|
|
|
|
|
let loraDiffInMinutes = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (matchedTank && matchedTank.date) {
|
|
|
|
if (matchedTank && matchedTank.date) {
|
|
|
|
const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata");
|
|
|
|
const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata");
|
|
|
|
const loraDiffInMinutes = now.diff(tankTime, "minutes");
|
|
|
|
loraDiffInMinutes = now.diff(tankTime, "minutes");
|
|
|
|
|
|
|
|
|
|
|
|
if (loraDiffInMinutes > 1) {
|
|
|
|
if (loraDiffInMinutes <= 1) {
|
|
|
|
disconnectedSlaves.push({
|
|
|
|
isDisconnected = false;
|
|
|
|
hardwareId: connected_to,
|
|
|
|
|
|
|
|
slaveHardwareId: slaveId,
|
|
|
|
|
|
|
|
slaveName: slave.tankName || "Unknown Slave"
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const slaveInfo = {
|
|
|
|
|
|
|
|
hardwareId: connected_to,
|
|
|
|
|
|
|
|
slaveHardwareId: slaveId,
|
|
|
|
|
|
|
|
slaveName: slave.tankName || "Unknown Slave",
|
|
|
|
|
|
|
|
connected_status: isDisconnected ? "disconnected" : "connected",
|
|
|
|
|
|
|
|
last_seen_minutes_ago: loraDiffInMinutes
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
slaveStatusList.push(slaveInfo);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isDisconnected) {
|
|
|
|
|
|
|
|
disconnectedSlaves.push(slaveInfo);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const issuesToAdd = [];
|
|
|
|
const issuesToAdd = [];
|
|
|
@ -2523,13 +2677,11 @@ exports.raiseATicket = async (req, reply) => {
|
|
|
|
issuesToAdd.push({
|
|
|
|
issuesToAdd.push({
|
|
|
|
type: "GSM Disconnected",
|
|
|
|
type: "GSM Disconnected",
|
|
|
|
hardwareId: connected_to,
|
|
|
|
hardwareId: connected_to,
|
|
|
|
message: `Master GSM disconnected - ${connected_to}`,
|
|
|
|
message: `Master GSM disconnected - ${connected_to}`
|
|
|
|
//disconnectedAt: now.format("DD-MM-YYYY HH:mm:ss")
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check for already existing slave issues
|
|
|
|
|
|
|
|
const newHardwareIds = [];
|
|
|
|
const newHardwareIds = [];
|
|
|
|
const newSlaveNames = [];
|
|
|
|
const newSlaveNames = [];
|
|
|
|
|
|
|
|
|
|
|
@ -2552,8 +2704,7 @@ exports.raiseATicket = async (req, reply) => {
|
|
|
|
masterHardwareId: connected_to,
|
|
|
|
masterHardwareId: connected_to,
|
|
|
|
hardwareIds: newHardwareIds,
|
|
|
|
hardwareIds: newHardwareIds,
|
|
|
|
slaveNames: newSlaveNames,
|
|
|
|
slaveNames: newSlaveNames,
|
|
|
|
message: `Slaves LoRa disconnected under master ${connected_to}`,
|
|
|
|
message: `Slaves LoRa disconnected under master ${connected_to}`
|
|
|
|
// disconnectedAt: now.format("DD-MM-YYYY HH:mm:ss")
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -2580,7 +2731,8 @@ exports.raiseATicket = async (req, reply) => {
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|
message: "Checked connection and updated support if needed.",
|
|
|
|
message: "Checked connection and updated support if needed.",
|
|
|
|
masterDisconnected,
|
|
|
|
masterDisconnected,
|
|
|
|
disconnectedSlaves
|
|
|
|
disconnectedSlaves,
|
|
|
|
|
|
|
|
connectedSlaves: slaveStatusList
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
} catch (error) {
|
|
|
@ -2591,7 +2743,6 @@ exports.raiseATicket = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.raiseATicketBuildingDetails = async (req, reply) => {
|
|
|
|
exports.raiseATicketBuildingDetails = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { customerId, connected_to, installationId } = req.params;
|
|
|
|
const { customerId, connected_to, installationId } = req.params;
|
|
|
@ -3055,3 +3206,79 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const bcrypt = require("bcrypt");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.createTeamMemberSupport = async (req, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const { supportId } = req.params;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const c_id = await generateTeamMemberId();
|
|
|
|
|
|
|
|
const support_teamMemberId = `AWTMSU${c_id}`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
|
|
|
name,
|
|
|
|
|
|
|
|
phone,
|
|
|
|
|
|
|
|
installationTeamMemId,
|
|
|
|
|
|
|
|
password,
|
|
|
|
|
|
|
|
email,
|
|
|
|
|
|
|
|
alternativePhone,
|
|
|
|
|
|
|
|
status
|
|
|
|
|
|
|
|
} = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!supportId || !support_teamMemberId || !name || !phone || !password) {
|
|
|
|
|
|
|
|
return reply.code(400).send({ error: "Missing required fields" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const supportRecord = await Support.findOne({ supportId });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!supportRecord) {
|
|
|
|
|
|
|
|
return reply.code(404).send({ error: "Support record not found" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const existingMember = supportRecord.team_member?.team_member.find(
|
|
|
|
|
|
|
|
member => member.phone === phone || member.support_teamMemberId === support_teamMemberId
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (existingMember) {
|
|
|
|
|
|
|
|
return reply.code(400).send({ error: "Team member with this phone or ID already exists" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const hashedPassword = await bcrypt.hash(password, 10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const newTeamMember = {
|
|
|
|
|
|
|
|
support_teamMemberId,
|
|
|
|
|
|
|
|
name,
|
|
|
|
|
|
|
|
phone,
|
|
|
|
|
|
|
|
installationTeamMemId,
|
|
|
|
|
|
|
|
password: hashedPassword,
|
|
|
|
|
|
|
|
status: status || "active",
|
|
|
|
|
|
|
|
email: email || null,
|
|
|
|
|
|
|
|
alternativePhone: alternativePhone || null,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await Support.findOneAndUpdate(
|
|
|
|
|
|
|
|
{ supportId },
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$push: {
|
|
|
|
|
|
|
|
"team_member.team_member": newTeamMember
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
$set: {
|
|
|
|
|
|
|
|
updatedAt: new Date()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{ new: true }
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
|
|
|
status_code: 200,
|
|
|
|
|
|
|
|
message: "Team member added successfully",
|
|
|
|
|
|
|
|
teamMember: newTeamMember
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
console.error("Error adding team member:", error);
|
|
|
|
|
|
|
|
return reply.code(500).send({ error: "Internal server error" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|