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" });
}
// 3. Find disconnected Insensors (matching hardwareId or connected_to)
// 3. Find disconnected Insensors
const disconnectedSensors = await Insensors.find({
$or: [
{ hardwareId: { $in: hardwareIds } },
@ -2753,50 +2753,49 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
const customerIds = [...new Set(disconnectedSensors.map(s => s.customerId))];
const customers = await User.find({ customerId: { $in: customerIds } }).lean();
// 5. Build enriched response
const response = disconnectedSensors.map(sensor => {
const user = customers.find(u => u.customerId === sensor.customerId);
// 5. Map unique customers
const uniqueCustomerMap = {};
for (const user of customers) {
if (!uniqueCustomerMap[user.customerId]) {
uniqueCustomerMap[user.customerId] = {
customer: {
customerId: user.customerId,
username: user.username || "",
firstName: user.profile?.firstName || "",
lastName: user.profile?.lastName || "",
phone: user.phone || user.profile?.contactNumber || "",
email: user.emails?.[0]?.email || "",
phoneVerified: user.phoneVerified || false,
address1: user.profile?.address1 || "",
address2: user.profile?.address2 || "",
city: user.profile?.city || "",
state: user.profile?.state || "",
country: user.profile?.country || "",
zip: user.profile?.zip || "",
notes: user.profile?.notes || "",
latitude: user.latitude,
longitude: user.longitude,
fcmIds: (user.fcmIds || []).filter(fcm => typeof fcm === "string" && fcm.startsWith("d")),
installationId: user.installationId || "",
notificationPreferences: {
allowNotifications: user.allowNotifications || false,
automaticStartAndStopNotify: user.automaticStartAndStopNotify || false,
manualStartAndStopNotify: user.manualStartAndStopNotify || false,
criticalLowWaterAlert: user.criticalLowWaterAlert || false,
lowWaterAlert: user.lowWaterAlert || false,
notificationPreference: user.notificationPreference || "never"
},
surveyStatus: user.survey_status || "pending",
stripePaymentStatus: user.stripePaymentStatus || false,
stripeSubscriptionStatus: user.stripeSubscriptionStatus || false,
createdAt: user.createdAt,
updatedAt: user.updatedAt
}
};
}
}
return {
// hardwareId: sensor.hardwareId,
// masterName: sensor.masterName,
// location: sensor.location,
// disconnectedAt: sensor.disconnectedAt || new Date().toISOString(),
customer: user ? {
customerId: user.customerId,
username: user.username || "",
firstName: user.profile?.firstName || "",
lastName: user.profile?.lastName || "",
phone: user.phone || user.profile?.contactNumber || "",
email: user.emails?.[0]?.email || "",
phoneVerified: user.phoneVerified || false,
address1: user.profile?.address1 || "",
address2: user.profile?.address2 || "",
city: user.profile?.city || "",
state: user.profile?.state || "",
country: user.profile?.country || "",
zip: user.profile?.zip || "",
notes: user.profile?.notes || "",
latitude: user.latitude,
longitude: user.longitude,
fcmIds: user.fcmIds || [],
installationId: user.installationId || "",
notificationPreferences: {
allowNotifications: user.allowNotifications || false,
automaticStartAndStopNotify: user.automaticStartAndStopNotify || false,
manualStartAndStopNotify: user.manualStartAndStopNotify || false,
criticalLowWaterAlert: user.criticalLowWaterAlert || false,
lowWaterAlert: user.lowWaterAlert || false,
notificationPreference: user.notificationPreference || "never"
},
surveyStatus: user.survey_status || "pending",
stripePaymentStatus: user.stripePaymentStatus || false,
stripeSubscriptionStatus: user.stripeSubscriptionStatus || false,
createdAt: user.createdAt,
updatedAt: user.updatedAt
} : null
};
});
const response = Object.values(uniqueCustomerMap);
return reply.send({
status_code: 200,
@ -2810,3 +2809,5 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
};

Loading…
Cancel
Save