diff --git a/api/functions/decideStatus.js b/api/functions/decideStatus.js index 765cd41..c087ea5 100644 --- a/api/functions/decideStatus.js +++ b/api/functions/decideStatus.js @@ -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) => { @@ -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({ @@ -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; } }; diff --git a/api/functions/updateDbUsers.js b/api/functions/updateDbUsers.js index 82bcd7c..b448e1d 100644 --- a/api/functions/updateDbUsers.js +++ b/api/functions/updateDbUsers.js @@ -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} 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 @@ -23,10 +36,6 @@ 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"); } @@ -34,11 +43,6 @@ export const updateDbUsers = async (extractedDir, db) => { 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"); } @@ -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"); }