master^2
Bhaskar 5 months ago
parent 3440a62ce0
commit 46ec234fb6

@ -2736,7 +2736,7 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
return reply.code(404).send({ message: "No hardware IDs found in issues" }); return reply.code(404).send({ message: "No hardware IDs found in issues" });
} }
// 3. Find disconnected Insensors (matching hardwareId or connected_to) // 3. Find disconnected Insensors
const disconnectedSensors = await Insensors.find({ const disconnectedSensors = await Insensors.find({
$or: [ $or: [
{ hardwareId: { $in: hardwareIds } }, { hardwareId: { $in: hardwareIds } },
@ -2753,16 +2753,12 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
const customerIds = [...new Set(disconnectedSensors.map(s => s.customerId))]; const customerIds = [...new Set(disconnectedSensors.map(s => s.customerId))];
const customers = await User.find({ customerId: { $in: customerIds } }).lean(); const customers = await User.find({ customerId: { $in: customerIds } }).lean();
// 5. Build enriched response // 5. Map unique customers
const response = disconnectedSensors.map(sensor => { const uniqueCustomerMap = {};
const user = customers.find(u => u.customerId === sensor.customerId); for (const user of customers) {
if (!uniqueCustomerMap[user.customerId]) {
return { uniqueCustomerMap[user.customerId] = {
// hardwareId: sensor.hardwareId, customer: {
// masterName: sensor.masterName,
// location: sensor.location,
// disconnectedAt: sensor.disconnectedAt || new Date().toISOString(),
customer: user ? {
customerId: user.customerId, customerId: user.customerId,
username: user.username || "", username: user.username || "",
firstName: user.profile?.firstName || "", firstName: user.profile?.firstName || "",
@ -2779,7 +2775,7 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
notes: user.profile?.notes || "", notes: user.profile?.notes || "",
latitude: user.latitude, latitude: user.latitude,
longitude: user.longitude, longitude: user.longitude,
fcmIds: user.fcmIds || [], fcmIds: (user.fcmIds || []).filter(fcm => typeof fcm === "string" && fcm.startsWith("d")),
installationId: user.installationId || "", installationId: user.installationId || "",
notificationPreferences: { notificationPreferences: {
allowNotifications: user.allowNotifications || false, allowNotifications: user.allowNotifications || false,
@ -2794,9 +2790,12 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
stripeSubscriptionStatus: user.stripeSubscriptionStatus || false, stripeSubscriptionStatus: user.stripeSubscriptionStatus || false,
createdAt: user.createdAt, createdAt: user.createdAt,
updatedAt: user.updatedAt updatedAt: user.updatedAt
} : null }
}; };
}); }
}
const response = Object.values(uniqueCustomerMap);
return reply.send({ return reply.send({
status_code: 200, status_code: 200,
@ -2810,3 +2809,5 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
}; };

Loading…
Cancel
Save