Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions api/functions/decideStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { decideScore } from "./decideScore.js";
* @returns {Object} An object containing the success status and the user's activity status.
* @returns {boolean} return.success - Whether the operation was successful.
* @returns {string} return.status - The status of the user: "inactive", "low", "medium", or "high", or an error message if the process failed.
* @throws {Error} If the user activity could not be aggregated or if there is an issue with the score calculation.

*/
export const decideStatus = async (configTable, userId, userActivity) => {
Expand All @@ -35,10 +36,8 @@ export const decideStatus = async (configTable, userId, userActivity) => {

// @todo updated version of decideScore should be replaced when it was merged
if (!aggregatedActivity.success) {
return {
success: false,
status: "activity has not been fetched correctly",
};
logger.error("activity has not been fetched correctly");
throw Error("activity has not been fetched correctly");
}

const score = decideScore({
Expand All @@ -59,6 +58,6 @@ export const decideStatus = async (configTable, userId, userActivity) => {
}
} catch (error) {
logger.error(error);
return { success: false, status: "unknown error happened" };
throw error;
}
};
35 changes: 19 additions & 16 deletions api/functions/updateDbUsers.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import logger from "../utils/logger.js";

/**
* Updates the users in the database by processing the users.json file from the extracted directory.
* Filters out invalid users, and performs a batch upsert into the 'all_users' table.
* Updates the database with active users from a provided directory of JSON data.
*
* @param {Array} extractedDir - An array of directory entries (files and folders) extracted from a zip.
* @param {Object} db - The database client used to interact with the database.
* @returns {Object} - Returns an object indicating success or failure with a message.
* The function performs the following:
* - Verifies the validity of the directory and contents of `users.json`.
* - Filters out active users based on specific criteria.
* - Performs a batch insert or update of user records into the `all_users` table.
* - If any step fails, the function will throw an error and log relevant messages.
*
* @param {Array} extractedDir - Array of directory entries, each containing a `name` and `content` property.
* @param {Object} db - Database client to execute queries (typically a `pg` client).
*
* @returns {Promise<boolean>} Resolves with `true` if users were successfully inserted into the database.
*
* @throws {Error} If any of the following occur:
* - Invalid or missing `extractedDir`.
* - Missing or invalid `users.json` file.
* - Invalid or missing `users.json` content.
* - Error parsing the `users.json` file.
* - No active users found.
* - Any database operation failure, which will trigger a rollback.
*/
export const updateDbUsers = async (extractedDir, db) => {
try {
// if directory passed is invalid
if (!extractedDir) {
throw Error("invalid passed directory");
// return { success: false, message: "invalid passed directory" };
}

// Find users.json file in the directory
Expand All @@ -23,22 +36,13 @@ export const updateDbUsers = async (extractedDir, db) => {

if (!usersFileEntry) {
logger.error("users json file not found in the directory");
// return {
// success: false,
// message: "users json file not found in the directory",
// };

throw Error("users json file not found in the directory");
}

const usersFileContent = usersFileEntry.content;

if (!usersFileContent) {
// return {
// success: false,
// message: "users.json content is missing or invalid",
// };

throw Error("users.json content is missing or invalid");
}

Expand All @@ -47,7 +51,6 @@ export const updateDbUsers = async (extractedDir, db) => {
usersList = JSON.parse(usersFileContent);
} catch (error) {
logger.error("Error parsing users.json");
// return { success: false, message: "Error in parsing users.json" };
throw Error("Error in parsing users.json");
}

Expand Down