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
15 changes: 7 additions & 8 deletions api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,18 @@ api.post("/upload", processUpload, async (req, res) => {

const processedActivity = processImportFiles(extractedDir);

const isUsersInserted = await updateDbUsers(extractedDir, db);

if (!isUsersInserted.success) {
return res.status(500).json({});
}
await updateDbUsers(extractedDir, db);

const isActivityInserted = await updateUsersActivity(processedActivity, db);

if (!isActivityInserted.success) {
return res.status(500).json({});
if (isActivityInserted) {
logger.info(
"isActivityInserted in the upload endpoint? => inserted successfully",
);
return res.status(200).json({});
}

return res.status(200).json({});
return res.status(500).json({});
} catch (error) {
logger.error(error);
return res.status(500).json({});
Expand Down
35 changes: 19 additions & 16 deletions api/functions/updateDbUsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export const updateDbUsers = async (extractedDir, db) => {
try {
// if directory passed is invalid
if (!extractedDir) {
return { success: false, message: "invalid passed directory" };
throw Error("invalid passed directory");
// return { success: false, message: "invalid passed directory" };
}

// Find users.json file in the directory
Expand All @@ -22,27 +23,32 @@ 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",
};
// 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",
};
// return {
// success: false,
// message: "users.json content is missing or invalid",
// };

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

let usersList;
try {
usersList = JSON.parse(usersFileContent);
} catch (error) {
logger.error("Error parsing users.json");
return { success: false, message: "Error in parsing users.json" };
// return { success: false, message: "Error in parsing users.json" };
throw Error("Error in parsing users.json");
}

const activeUsers = usersList.filter(
Expand All @@ -57,10 +63,7 @@ export const updateDbUsers = async (extractedDir, db) => {

if (activeUsers.length === 0) {
logger.error("active user is empty.something is wrong");
return {
success: false,
message: "active user is empty.something is wrong",
};
throw Error("error happened, active users is empty");
}

await db.query("BEGIN");
Expand Down Expand Up @@ -91,10 +94,10 @@ export const updateDbUsers = async (extractedDir, db) => {
await db.query("COMMIT");

logger.info("users inserted into database successfully");
return { success: true };
return true;
} catch (error) {
await db.query("ROLLBACK");
logger.error(error);
return { success: false };
throw error;
}
};
10 changes: 6 additions & 4 deletions api/functions/updateUsersActivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export const updateUsersActivity = async (processedActivity, db) => {
try {
if (!processedActivity || Object.keys(processedActivity).length === 0) {
logger.error("invalid or empty activity data");
return { success: false, message: "invalid or empty activity data" };

throw Error("invalid or empty activity data");
// return { success: false, message: "invalid or empty activity data" };
}

// retrieve all the users in database as they are real users
Expand Down Expand Up @@ -70,7 +72,7 @@ export const updateUsersActivity = async (processedActivity, db) => {
if (usersActivity.length === 0) {
await db.query("COMMIT");
logger.warn("no user activity was found to be instrted.");
return { success: true };
return true;
}

const insertQuery = `
Expand All @@ -94,10 +96,10 @@ export const updateUsersActivity = async (processedActivity, db) => {

await db.query("COMMIT");

return { success: true };
return true;
} catch (error) {
await db.query("ROLLBACK");
logger.debug("Error inserting user activity:", error);
return { success: false, message: error.message };
throw error;
}
};