|
|
@ -3823,6 +3823,137 @@ exports.raiseATicketSlave = async (req, reply) => {
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getDisconnectedIssuesBySupportId = async (req, reply) => {
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// const { supportId } = req.params;
|
|
|
|
|
|
|
|
// if (!supportId) {
|
|
|
|
|
|
|
|
// return reply.code(400).send({ error: "supportId is required" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const supportRecord = await Support.findOne({ supportId }).lean();
|
|
|
|
|
|
|
|
// if (!supportRecord) {
|
|
|
|
|
|
|
|
// return reply.code(404).send({ message: "No support record found for this supportId" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const allIssues = supportRecord.issues || [];
|
|
|
|
|
|
|
|
// const hardwareSet = new Set();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Collect hardwareId and masterHardwareId
|
|
|
|
|
|
|
|
// for (const issue of allIssues) {
|
|
|
|
|
|
|
|
// if (issue.hardwareId) hardwareSet.add(issue.hardwareId);
|
|
|
|
|
|
|
|
// if (issue.masterHardwareId) hardwareSet.add(issue.masterHardwareId);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const hardwareIds = [...hardwareSet];
|
|
|
|
|
|
|
|
// const sensors = await Insensors.find({
|
|
|
|
|
|
|
|
// $or: [
|
|
|
|
|
|
|
|
// { hardwareId: { $in: hardwareIds } },
|
|
|
|
|
|
|
|
// { tankhardwareId: { $in: hardwareIds } }
|
|
|
|
|
|
|
|
// ]
|
|
|
|
|
|
|
|
// }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const sensorMap = {};
|
|
|
|
|
|
|
|
// for (const sensor of sensors) {
|
|
|
|
|
|
|
|
// if (sensor.hardwareId) sensorMap[sensor.hardwareId] = sensor;
|
|
|
|
|
|
|
|
// if (sensor.tankhardwareId) sensorMap[sensor.tankhardwareId] = sensor;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const customerId = supportRecord.customerId;
|
|
|
|
|
|
|
|
// const orders = await Order.find({ customerId }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const orderMap = {};
|
|
|
|
|
|
|
|
// for (const order of orders) {
|
|
|
|
|
|
|
|
// (order.master_connections || []).forEach(conn => {
|
|
|
|
|
|
|
|
// if (conn.hardwareId) {
|
|
|
|
|
|
|
|
// orderMap[conn.hardwareId] = {
|
|
|
|
|
|
|
|
// masterName: conn.master_name || null,
|
|
|
|
|
|
|
|
// location: conn.location || null
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// console.log("order",orderMap)
|
|
|
|
|
|
|
|
// const masterMap = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// for (const issue of allIssues) {
|
|
|
|
|
|
|
|
// const masterId = issue.masterHardwareId || issue.hardwareId;
|
|
|
|
|
|
|
|
// const masterSensor = sensorMap[masterId];
|
|
|
|
|
|
|
|
// if (!masterSensor || masterSensor.type !== "master") continue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // If master not already added
|
|
|
|
|
|
|
|
// if (!masterMap[masterSensor.hardwareId]) {
|
|
|
|
|
|
|
|
// const enriched = orderMap[masterSensor.hardwareId] || {};
|
|
|
|
|
|
|
|
// masterMap[masterSensor.hardwareId] = {
|
|
|
|
|
|
|
|
// hardwareId: masterSensor.hardwareId,
|
|
|
|
|
|
|
|
// masterName: enriched.masterName || masterSensor.masterName || "",
|
|
|
|
|
|
|
|
// location: enriched.location || masterSensor.location || "",
|
|
|
|
|
|
|
|
// type: "master",
|
|
|
|
|
|
|
|
// connected_status: masterSensor.connected_status,
|
|
|
|
|
|
|
|
// gsm_last_check_time: masterSensor.gsm_last_check_time,
|
|
|
|
|
|
|
|
// gsm_last_disconnect_time: masterSensor.gsm_last_disconnect_time,
|
|
|
|
|
|
|
|
// connected_gsm_date: masterSensor.connected_gsm_date,
|
|
|
|
|
|
|
|
// connected_gsm_time: masterSensor.connected_gsm_time,
|
|
|
|
|
|
|
|
// connected_lora_date: masterSensor.connected_lora_date,
|
|
|
|
|
|
|
|
// connected_lora_time: masterSensor.connected_lora_time,
|
|
|
|
|
|
|
|
// support_gsm_last_check_time: masterSensor.support_gsm_last_check_time,
|
|
|
|
|
|
|
|
// support_lora_last_check_time: masterSensor.support_lora_last_check_time,
|
|
|
|
|
|
|
|
// team_member_support_gsm_last_check_time: masterSensor.team_member_support_gsm_last_check_time,
|
|
|
|
|
|
|
|
// team_member_support_lora_last_check_time: masterSensor.team_member_support_lora_last_check_time,
|
|
|
|
|
|
|
|
// connected_slave_count: 0,
|
|
|
|
|
|
|
|
// connected_slaves: []
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const master = masterMap[masterSensor.hardwareId];
|
|
|
|
|
|
|
|
// console.log("master",master)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // ✅ Always fetch slaves using connected_to
|
|
|
|
|
|
|
|
// const connectedSlaves = await Insensors.find({
|
|
|
|
|
|
|
|
// connected_to: masterSensor.hardwareId,
|
|
|
|
|
|
|
|
// type: "slave"
|
|
|
|
|
|
|
|
// }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// for (const slave of connectedSlaves) {
|
|
|
|
|
|
|
|
// // Only include disconnected slaves
|
|
|
|
|
|
|
|
// if (slave.connected_status !== "disconnected") continue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// master.connected_slaves.push({
|
|
|
|
|
|
|
|
// hardwareId: slave.tankhardwareId || slave.hardwareId,
|
|
|
|
|
|
|
|
// tankName: slave.tankName || "",
|
|
|
|
|
|
|
|
// location: slave.location || "",
|
|
|
|
|
|
|
|
// connected_status: slave.connected_status,
|
|
|
|
|
|
|
|
// connected_lora_time: slave.connected_lora_time,
|
|
|
|
|
|
|
|
// connected_lora_date: slave.connected_lora_date,
|
|
|
|
|
|
|
|
// lora_last_check_time: slave.lora_last_check_time,
|
|
|
|
|
|
|
|
// lora_last_disconnect_time: slave.lora_last_disconnect_time,
|
|
|
|
|
|
|
|
// connected_to: slave.connected_to,
|
|
|
|
|
|
|
|
// masterName: master.masterName,
|
|
|
|
|
|
|
|
// type: "slave",
|
|
|
|
|
|
|
|
// typeOfWater: slave.typeOfWater || "",
|
|
|
|
|
|
|
|
// tankHeight: slave.tankHeight,
|
|
|
|
|
|
|
|
// support_lora_last_check_time: slave.support_lora_last_check_time,
|
|
|
|
|
|
|
|
// //team_member_support_gsm_last_check_time: masterSensor.team_member_support_gsm_last_check_time,
|
|
|
|
|
|
|
|
// team_member_support_lora_last_check_time: slave.team_member_support_lora_last_check_time,
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// master.connected_slave_count++;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return reply.send({
|
|
|
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
|
|
|
// supportId,
|
|
|
|
|
|
|
|
// totalMasters: Object.keys(masterMap).length,
|
|
|
|
|
|
|
|
// disconnectedIssues: Object.values(masterMap)
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
|
|
|
// console.error("Error fetching disconnected issues:", error);
|
|
|
|
|
|
|
|
// return reply.code(500).send({ error: "Internal server error" });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getDisconnectedIssuesBySupportId = async (req, reply) => {
|
|
|
|
exports.getDisconnectedIssuesBySupportId = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { supportId } = req.params;
|
|
|
|
const { supportId } = req.params;
|
|
|
@ -3872,7 +4003,7 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log("order",orderMap)
|
|
|
|
|
|
|
|
const masterMap = {};
|
|
|
|
const masterMap = {};
|
|
|
|
|
|
|
|
|
|
|
|
for (const issue of allIssues) {
|
|
|
|
for (const issue of allIssues) {
|
|
|
@ -3905,18 +4036,15 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const master = masterMap[masterSensor.hardwareId];
|
|
|
|
const master = masterMap[masterSensor.hardwareId];
|
|
|
|
console.log("master",master)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ Always fetch slaves using connected_to
|
|
|
|
// ✅ Always fetch slaves using connected_to, even if currently connected
|
|
|
|
const connectedSlaves = await Insensors.find({
|
|
|
|
const connectedSlaves = await Insensors.find({
|
|
|
|
connected_to: masterSensor.hardwareId,
|
|
|
|
connected_to: masterSensor.hardwareId,
|
|
|
|
type: "slave"
|
|
|
|
type: "slave"
|
|
|
|
}).lean();
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
|
|
for (const slave of connectedSlaves) {
|
|
|
|
for (const slave of connectedSlaves) {
|
|
|
|
// Only include disconnected slaves
|
|
|
|
// ✅ Do not skip slaves based on current connection status
|
|
|
|
if (slave.connected_status !== "disconnected") continue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
master.connected_slaves.push({
|
|
|
|
master.connected_slaves.push({
|
|
|
|
hardwareId: slave.tankhardwareId || slave.hardwareId,
|
|
|
|
hardwareId: slave.tankhardwareId || slave.hardwareId,
|
|
|
|
tankName: slave.tankName || "",
|
|
|
|
tankName: slave.tankName || "",
|
|
|
@ -3932,8 +4060,7 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => {
|
|
|
|
typeOfWater: slave.typeOfWater || "",
|
|
|
|
typeOfWater: slave.typeOfWater || "",
|
|
|
|
tankHeight: slave.tankHeight,
|
|
|
|
tankHeight: slave.tankHeight,
|
|
|
|
support_lora_last_check_time: slave.support_lora_last_check_time,
|
|
|
|
support_lora_last_check_time: slave.support_lora_last_check_time,
|
|
|
|
//team_member_support_gsm_last_check_time: masterSensor.team_member_support_gsm_last_check_time,
|
|
|
|
team_member_support_lora_last_check_time: slave.team_member_support_lora_last_check_time
|
|
|
|
team_member_support_lora_last_check_time: slave.team_member_support_lora_last_check_time,
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
master.connected_slave_count++;
|
|
|
|
master.connected_slave_count++;
|
|
|
@ -3953,8 +4080,6 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getDisconnectedCustomerDetails = async (req, reply) => {
|
|
|
|
exports.getDisconnectedCustomerDetails = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { supportId } = req.params;
|
|
|
|
const { supportId } = req.params;
|
|
|
|