master^2
Bhaskar 4 months ago
parent 6b0bfc9c7e
commit 889aae3965

@ -4034,7 +4034,7 @@ cron.schedule("* * * * *", async () => {
// OR there are disconnected slaves and none of them are active
const shouldRaiseTicket =
(masterIsDisconnected && !masterAlreadyActive) ||
( !anySlaveAlreadyActive);
(disconnectedSlaves.length > 0 && !anySlaveAlreadyActive);
if (shouldRaiseTicket) {
console.log(`⚠️ Raising ticket for master: ${hardwareId}`);
@ -5532,6 +5532,8 @@ exports.getRemoveConnectedMastersWithSlaves = async (req, reply) => {
}
};
// const moment = require("moment-timezone");
exports.getDisconnectedCustomerDetails = async (req, reply) => {
try {
const { supportId } = req.params;
@ -5565,7 +5567,7 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
}
const hardwareIdsArray = Array.from(allHardwareIds);
console.log("hardwareIdsArray",hardwareIdsArray)
// 4. Find disconnected insensors using connected_to match
const disconnectedSensors = await Insensors.find({
connected_to: { $in: hardwareIdsArray },
@ -5576,14 +5578,33 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
return reply.code(404).send({ message: "No disconnected issues found" });
}
// 5. Get unique customerIds from disconnected sensors
const customerIds = [...new Set(disconnectedSensors.map(s => s.customerId))];
// 4.5 Exclude customers who have any sensor with support_issue_status = "active"
const disconnectedCustomerIds = [...new Set(disconnectedSensors.map(s => s.customerId))];
const activeSensors = await Insensors.find({
customerId: { $in: disconnectedCustomerIds },
support_issue_status: "inactive"
}).lean();
const activeCustomerIds = new Set(activeSensors.map(s => s.customerId));
// Filter disconnectedSensors to exclude customers with active sensors
const filteredDisconnectedSensors = disconnectedSensors.filter(
s => !activeCustomerIds.has(s.customerId)
);
if (filteredDisconnectedSensors.length === 0) {
return reply.code(404).send({ message: "No disconnected issues found after filtering active sensors" });
}
// 5. Get unique customerIds from filtered disconnected sensors
const customerIds = [...new Set(filteredDisconnectedSensors.map(s => s.customerId))];
const customers = await User.find({ customerId: { $in: customerIds } }).lean();
// 6. For each customer, calculate total unique hardwareIds involved in their disconnected sensors
const customerHardwareMap = {};
for (const sensor of disconnectedSensors) {
for (const sensor of filteredDisconnectedSensors) {
const custId = sensor.customerId;
if (!customerHardwareMap[custId]) {
customerHardwareMap[custId] = new Set();
@ -5604,12 +5625,9 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
) {
customerHardwareMap[custId].add(issue.hardwareId);
}
console.log("allIssueHardwareIds",allIssueHardwareIds)
}
}
// 7. Build final response
const response = [];
@ -5622,12 +5640,9 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
const issueHw = issue.hardwareId?.trim().toLowerCase();
const hardwareIds = issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || [];
const allIds = [issueHw, ...hardwareIds];
console.log("allIds",allIds)
console.log("Array.from(hardwareIdSet).some(hw => allIds.includes(hw?.trim().toLowerCase()))",Array.from(hardwareIdSet).some(hw => allIds.includes(hw?.trim().toLowerCase())))
return Array.from(hardwareIdSet).some(hw => allIds.includes(hw?.trim().toLowerCase()));
});
//console.log("relatedIssues",relatedIssues)
let latestIssueTime = null;
for (const issue of relatedIssues) {
if (issue.lastTicketRaisedAt) {
@ -5691,6 +5706,7 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
// exports.getDisconnectedCustomerDetails = async (req, reply) => {
// try {
// const { supportId } = req.params;

Loading…
Cancel
Save