ashok 3 months ago
commit 0d7de09be7

@ -6969,6 +6969,184 @@ exports.getRemoveConnectedMastersWithSlaves = async (req, reply) => {
// }; // };
// exports.getDisconnectedCustomerDetails = 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 unresolvedIssues = supportRecord.issues?.filter(
// (issue) => issue.resolved === false && issue.movedToCategory === false
// ) || [];
// const existingCategorizedHardwareIds = new Set();
// (supportRecord.categorizedIssues || []).forEach(issue => {
// if (issue.hardwareId) existingCategorizedHardwareIds.add(issue.hardwareId.trim().toLowerCase());
// if (Array.isArray(issue.hardwareIds)) {
// issue.hardwareIds.forEach(id => {
// if (typeof id === "string") existingCategorizedHardwareIds.add(id.trim().toLowerCase());
// });
// }
// });
// const hardwareIdsArray = new Set();
// unresolvedIssues.forEach((issue) => {
// if (issue.hardwareId) hardwareIdsArray.add(issue.hardwareId.trim());
// if (Array.isArray(issue.hardwareIds)) {
// issue.hardwareIds.forEach((id) => {
// if (typeof id === "string") hardwareIdsArray.add(id.trim());
// });
// }
// });
// const allHardwareIds = [...hardwareIdsArray];
// // const disconnectedSensorsRaw = await Insensors.find({
// // connected_status: "disconnected",
// // $or: [
// // { connected_to: { $in: allHardwareIds } },
// // { hardwareId: { $in: allHardwareIds } },
// // { tankhardwareId: { $in: allHardwareIds } }
// // ]
// // }).lean();
// const disconnectedSensorsRaw = await Insensors.find({
// $or: [
// { connected_to: { $in: allHardwareIds } },
// { hardwareId: { $in: allHardwareIds } },
// { tankhardwareId: { $in: allHardwareIds } }
// ]
// }).lean();
// const disconnectedSensors = disconnectedSensorsRaw.filter(sensor => {
// const ids = [
// sensor.hardwareId?.trim().toLowerCase(),
// sensor.connected_to?.trim().toLowerCase(),
// sensor.tankhardwareId?.trim().toLowerCase()
// ];
// return !ids.some(id => existingCategorizedHardwareIds.has(id));
// });
// const customerHardwareMap = {};
// for (const sensor of disconnectedSensors) {
// const custId = sensor.customerId;
// if (!customerHardwareMap[custId]) {
// customerHardwareMap[custId] = new Set();
// }
// const sensorHw = sensor.tankhardwareId?.trim().toLowerCase();
// const sensorConnected = sensor.connected_to?.trim().toLowerCase();
// const sensorHardwareId = sensor.hardwareId?.trim().toLowerCase();
// for (const issue of unresolvedIssues) {
// const allIssueHardwareIds = [
// ...(issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []),
// issue.hardwareId?.trim().toLowerCase()
// ];
// const isCategorizedMatch = [sensorHw, sensorConnected, sensorHardwareId].some(id =>
// id && existingCategorizedHardwareIds.has(id)
// );
// if (isCategorizedMatch) continue;
// if (
// (sensorHw && allIssueHardwareIds.includes(sensorHw)) ||
// (sensorConnected && allIssueHardwareIds.includes(sensorConnected)) ||
// (sensorHardwareId && allIssueHardwareIds.includes(sensorHardwareId))
// ) {
// for (const hw of allIssueHardwareIds) {
// if (hw && !existingCategorizedHardwareIds.has(hw)) {
// customerHardwareMap[custId].add(hw);
// }
// }
// }
// }
// }
// const customerDetails = await User.find({
// customerId: { $in: Object.keys(customerHardwareMap) }
// }).lean();
// const customerResults = customerDetails.map((customer) => {
// const customerId = customer.customerId;
// const affectedHardwareSet = customerHardwareMap[customerId] || new Set();
// const affectedLowerSet = new Set([...affectedHardwareSet].map(id => id.toLowerCase().trim()));
// const disconnectedSensorsForCustomer = disconnectedSensors.filter(
// s => s.customerId === customerId && s.connected_status === "disconnected"
// );
// const disconnectedSlaves = disconnectedSensorsForCustomer.filter(s => s.type === 'slave');
// const disconnectedMasters = disconnectedSensorsForCustomer.filter(s => s.type === 'master');
// console.log("disconnectedMasters",disconnectedMasters)
// const uniqueDisconnectedHardwareIds = new Set([
// ...disconnectedMasters.map(s => s.hardwareId?.trim()),
// ...disconnectedSlaves.map(s => s.tankhardwareId?.trim() || s.hardwareId?.trim())
// ].filter(Boolean));
// const disconnectedCount = disconnectedMasters.length + disconnectedSlaves.length;
// const customerIssues = unresolvedIssues.filter(issue => {
// const allIssueHardwareIds = [
// ...(issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []),
// issue.hardwareId?.trim().toLowerCase()
// ].filter(Boolean);
// return allIssueHardwareIds.some(hw => affectedLowerSet.has(hw));
// });
// const lastTicketRaisedAt = customerIssues.reduce((latest, issue) => {
// const issueTime = new Date(issue.lastTicketRaisedAt);
// if (!isNaN(issueTime)) {
// return (!latest || issueTime > new Date(latest)) ? issue.lastTicketRaisedAt : latest;
// }
// return latest;
// }, null);
// return {
// customerId: customer.customerId,
// buildingName: customer.buildingName || "",
// location: customer.location || "",
// username: customer.username || "",
// firstName: customer.profile?.firstName || "",
// lastName: customer.profile?.lastName || "",
// phone: customer.phone || customer.profile?.contactNumber || "",
// email: customer.emails?.[0]?.email || "",
// phoneVerified: customer.phoneVerified || false,
// address1: customer.profile?.address1 || "",
// address2: customer.profile?.address2 || "",
// city: customer.profile?.city || "",
// latitude: customer.latitude,
// longitude: customer.longitude,
// totalHardwareIdsCount: uniqueDisconnectedHardwareIds.size,
// hardwareIds: [...uniqueDisconnectedHardwareIds],
// lastTicketRaisedAt: lastTicketRaisedAt || null,
// disconnectedCount
// };
// });
// return reply.code(200).send({
// success: true,
// totalCustomers: customerResults.length,
// customers: customerResults
// });
// } catch (error) {
// console.error("Error in getDisconnectedCustomerDetails:", error);
// return reply.code(500).send({
// success: false,
// message: "Internal Server Error"
// });
// }
// };
exports.getDisconnectedCustomerDetails = async (req, reply) => { exports.getDisconnectedCustomerDetails = async (req, reply) => {
try { try {
const { supportId } = req.params; const { supportId } = req.params;
@ -7008,23 +7186,15 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
const allHardwareIds = [...hardwareIdsArray]; const allHardwareIds = [...hardwareIdsArray];
// const disconnectedSensorsRaw = await Insensors.find({
// connected_status: "disconnected",
// $or: [
// { connected_to: { $in: allHardwareIds } },
// { hardwareId: { $in: allHardwareIds } },
// { tankhardwareId: { $in: allHardwareIds } }
// ]
// }).lean();
const disconnectedSensorsRaw = await Insensors.find({ const disconnectedSensorsRaw = await Insensors.find({
connected_status: "disconnected",
$or: [ $or: [
{ connected_to: { $in: allHardwareIds } }, { connected_to: { $in: allHardwareIds } },
{ hardwareId: { $in: allHardwareIds } }, { hardwareId: { $in: allHardwareIds } },
{ tankhardwareId: { $in: allHardwareIds } } { tankhardwareId: { $in: allHardwareIds } }
] ]
}).lean(); }).lean();
const disconnectedSensors = disconnectedSensorsRaw.filter(sensor => { const disconnectedSensors = disconnectedSensorsRaw.filter(sensor => {
const ids = [ const ids = [
sensor.hardwareId?.trim().toLowerCase(), sensor.hardwareId?.trim().toLowerCase(),
@ -7070,28 +7240,28 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
} }
} }
const customerDetails = await User.find({ const customerIds = Object.keys(customerHardwareMap);
customerId: { $in: Object.keys(customerHardwareMap) } const [customerDetails, allDisconnectedSensors] = await Promise.all([
}).lean(); User.find({ customerId: { $in: customerIds } }).lean(),
Insensors.find({ customerId: { $in: customerIds }, connected_status: "disconnected" }).lean()
]);
const customerResults = customerDetails.map((customer) => { const customerResults = customerDetails.map((customer) => {
const customerId = customer.customerId; const customerId = customer.customerId;
const affectedHardwareSet = customerHardwareMap[customerId] || new Set(); const affectedHardwareSet = customerHardwareMap[customerId] || new Set();
const affectedLowerSet = new Set([...affectedHardwareSet].map(id => id.toLowerCase().trim())); const affectedLowerSet = new Set([...affectedHardwareSet].map(id => id.toLowerCase().trim()));
const disconnectedSensorsForCustomer = disconnectedSensors.filter( const disconnectedSensorsForCustomer = allDisconnectedSensors.filter(
s => s.customerId === customerId && s.connected_status === "disconnected" s => s.customerId === customerId &&
(s.type === "master" || s.type === "slave") &&
s.connected_status === "disconnected"
); );
const disconnectedSlaves = disconnectedSensorsForCustomer.filter(s => s.type === 'slave'); const uniqueDisconnectedHardwareIds = new Set();
const disconnectedMasters = disconnectedSensorsForCustomer.filter(s => s.type === 'master'); for (const sensor of disconnectedSensorsForCustomer) {
console.log("disconnectedMasters",disconnectedMasters) const hw = sensor.tankhardwareId || sensor.hardwareId || sensor.connected_to;
const uniqueDisconnectedHardwareIds = new Set([ if (hw) uniqueDisconnectedHardwareIds.add(hw.trim());
...disconnectedMasters.map(s => s.hardwareId?.trim()), }
...disconnectedSlaves.map(s => s.tankhardwareId?.trim() || s.hardwareId?.trim())
].filter(Boolean));
const disconnectedCount = disconnectedMasters.length + disconnectedSlaves.length;
const customerIssues = unresolvedIssues.filter(issue => { const customerIssues = unresolvedIssues.filter(issue => {
const allIssueHardwareIds = [ const allIssueHardwareIds = [
@ -7110,7 +7280,7 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
}, null); }, null);
return { return {
customerId: customer.customerId, customerId,
buildingName: customer.buildingName || "", buildingName: customer.buildingName || "",
location: customer.location || "", location: customer.location || "",
username: customer.username || "", username: customer.username || "",
@ -7127,7 +7297,7 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
totalHardwareIdsCount: uniqueDisconnectedHardwareIds.size, totalHardwareIdsCount: uniqueDisconnectedHardwareIds.size,
hardwareIds: [...uniqueDisconnectedHardwareIds], hardwareIds: [...uniqueDisconnectedHardwareIds],
lastTicketRaisedAt: lastTicketRaisedAt || null, lastTicketRaisedAt: lastTicketRaisedAt || null,
disconnectedCount disconnectedCount: disconnectedSensorsForCustomer.length // ✅ Added disconnected count
}; };
}); });
@ -7149,8 +7319,6 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
exports.getDisconnectedCustomerDetailsByTeamMemberId = async (req, reply) => { exports.getDisconnectedCustomerDetailsByTeamMemberId = async (req, reply) => {
try { try {
const { support_teamMemberId } = req.params; const { support_teamMemberId } = req.params;

Loading…
Cancel
Save