From b907f64657f4ae01e390a177dbb6148997f19bd1 Mon Sep 17 00:00:00 2001 From: baalmart Date: Sat, 14 Dec 2024 12:58:06 +0300 Subject: [PATCH] ensure that logging is consistent with the new group-specific preference management --- .../bin/jobs/preferences-log-job.js | 45 ++++++++++++++----- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/src/auth-service/bin/jobs/preferences-log-job.js b/src/auth-service/bin/jobs/preferences-log-job.js index 1f49e455d2..5c2738ff4f 100644 --- a/src/auth-service/bin/jobs/preferences-log-job.js +++ b/src/auth-service/bin/jobs/preferences-log-job.js @@ -1,4 +1,5 @@ const cron = require("node-cron"); +const mongoose = require("mongoose"); const UserModel = require("@models/User"); const PreferenceModel = require("@models/Preference"); const constants = require("@config/constants"); @@ -13,25 +14,41 @@ const logUserPreferences = async () => { try { const batchSize = 100; let skip = 0; - let totalCountWithoutSelectedSites = 0; // To keep track of total count - let totalUsersProcessed = 0; // To keep track of total users processed + let totalCountWithoutSelectedSites = 0; + let totalUsersProcessed = 0; + + // Use the default group ID + const defaultGroupId = mongoose.Types.ObjectId(constants.DEFAULT_GROUP); while (true) { + // Fetch users with their group_roles const users = await UserModel("airqo") .find() .limit(batchSize) .skip(skip) - .select("_id email") + .select("_id email group_roles") .lean(); if (users.length === 0) { break; } - // Fetch existing preferences for users in batch - const userIds = users.map((user) => user._id); + // Filter users who are members of the default group + const validUsers = users.filter( + (user) => + user.group_roles && + user.group_roles.some( + (role) => role.group.toString() === defaultGroupId.toString() + ) + ); + + // Fetch existing preferences for valid users in the default group + const userIds = validUsers.map((user) => user._id); const preferences = await PreferenceModel("airqo") - .find({ user_id: { $in: userIds } }) + .find({ + user_id: { $in: userIds }, + group_id: defaultGroupId, + }) .select("_id user_id selected_sites") .lean(); @@ -40,31 +57,33 @@ const logUserPreferences = async () => { preferencesMap.set(pref.user_id.toString(), pref); }); - // Collect IDs of users without selected_sites - const usersWithoutSelectedSites = users.filter((user) => { + // Collect IDs of users without selected_sites in the default group + const usersWithoutSelectedSites = validUsers.filter((user) => { const preference = preferencesMap.get(user._id.toString()); return !preference || isEmpty(preference.selected_sites); }); // Aggregate results totalCountWithoutSelectedSites += usersWithoutSelectedSites.length; - totalUsersProcessed += users.length; // Increment total processed users + totalUsersProcessed += validUsers.length; skip += batchSize; } // Log the aggregated results once after processing all users - if (totalUsersProcessed > 0 && totalCountWithoutSelectedSites > 0) { + if (totalUsersProcessed > 0) { const percentageWithoutSelectedSites = ( (totalCountWithoutSelectedSites / totalUsersProcessed) * 100 ).toFixed(2); logger.info( - `💔💔 Total count of users without any Customised Locations: ${totalCountWithoutSelectedSites}, which is ${percentageWithoutSelectedSites}% of all Analytics users.` + `💔💔 Total count of users without Customised Locations in the default group: ${totalCountWithoutSelectedSites}, which is ${percentageWithoutSelectedSites}% of processed users.` ); } else { - logger.info(`😎🎉✅ All Analytics users have Customised Locations.`); + logger.info( + `😎🎉✅ No users processed or all users have Customised Locations.` + ); } } catch (error) { logger.error(`🐛🐛 Error in logUserPreferences: ${stringify(error)}`); @@ -76,3 +95,5 @@ cron.schedule(schedule, logUserPreferences, { scheduled: true, timezone: "Africa/Nairobi", }); + +module.exports = { logUserPreferences };