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,16 +2753,12 @@ 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);
return {
// hardwareId: sensor.hardwareId,
// masterName: sensor.masterName,
// location: sensor.location,
// disconnectedAt: sensor.disconnectedAt || new Date().toISOString(),
customer: user ? {
// 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 || "",
@ -2779,7 +2775,7 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
notes: user.profile?.notes || "",
latitude: user.latitude,
longitude: user.longitude,
fcmIds: user.fcmIds || [],
fcmIds: (user.fcmIds || []).filter(fcm => typeof fcm === "string" && fcm.startsWith("d")),
installationId: user.installationId || "",
notificationPreferences: {
allowNotifications: user.allowNotifications || false,
@ -2794,9 +2790,12 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
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