ashok 4 months ago
commit 7059e799ce

@ -7322,9 +7322,73 @@ exports.getResolvedCustomerDetails = async (req, reply) => {
const customers = await User.find({ customerId: { $in: customerIds } }).lean();
const uniqueCustomerMap = {};
// for (const user of customers) {
// const cid = user.customerId;
// if (!uniqueCustomerMap[cid]) {
// uniqueCustomerMap[cid] = {
// customer: {
// customerId: cid,
// username: user.username || "",
// firstName: user.profile?.firstName || user.firstName || "",
// lastName: user.profile?.lastName || user.lastName || "",
// phone: user.phone || user.profile?.contactNumber || user.alternativeNumber || "",
// email: user.emails?.[0]?.email || user.email || "",
// phoneVerified: user.phoneVerified || false,
// address1: user.profile?.address1 || user.address1 || "",
// address2: user.profile?.address2 || user.address2 || "",
// city: user.profile?.city || user.city || "",
// state: user.profile?.state || user.state || "",
// country: user.profile?.country || user.country || "",
// zip: user.profile?.zip || "",
// notes: user.profile?.notes || "",
// latitude: user.latitude || 0,
// longitude: user.longitude || 0,
// 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",
// buildingName: user.buildingName || "",
// stripePaymentStatus: user.stripePaymentStatus || false,
// stripeSubscriptionStatus: user.stripeSubscriptionStatus || false,
// createdAt: user.createdAt,
// updatedAt: user.updatedAt
// }
// };
// }
// }
// Step 1: Build map of resolvedAt per hardwareId
const resolvedHardwareMap = {};
for (const issue of resolvedIssues) {
const resolvedAt = issue.resolvedAt;
if (issue.hardwareId) resolvedHardwareMap[issue.hardwareId] = resolvedAt;
if (Array.isArray(issue.hardwareIds)) {
issue.hardwareIds.forEach(hid => {
resolvedHardwareMap[hid] = resolvedAt;
});
}
}
for (const user of customers) {
const cid = user.customerId;
if (!uniqueCustomerMap[cid]) {
// Find all matching resolvedAt timestamps for this user's hardwareIds
const customerSensorHardwareIds = sensors
.filter(s => s.customerId === cid)
.map(s => s.hardwareId || s.tankhardwareId || s.connected_to)
.filter(Boolean);
const resolvedTimes = customerSensorHardwareIds
.map(hid => resolvedHardwareMap[hid])
.filter(Boolean)
.sort((a, b) => new Date(b) - new Date(a)); // Get most recent
uniqueCustomerMap[cid] = {
customer: {
customerId: cid,
@ -7358,12 +7422,14 @@ exports.getResolvedCustomerDetails = async (req, reply) => {
stripePaymentStatus: user.stripePaymentStatus || false,
stripeSubscriptionStatus: user.stripeSubscriptionStatus || false,
createdAt: user.createdAt,
updatedAt: user.updatedAt
updatedAt: user.updatedAt,
resolvedAt: resolvedTimes[0] || null // 🆕 Include latest resolved time if available
}
};
}
}
return reply.send({
status_code: 200,
data: Object.values(uniqueCustomerMap)

Loading…
Cancel
Save