Skip to content

Commit

Permalink
refactor: Improve user initialization and update process
Browse files Browse the repository at this point in the history
- Refactored the initializeUser function in dbUtils.ts to improve the user initialization and update process.
- Added a check for existing users before creating a new user.
- Updated the logic to handle user creation and update based on the existing user status.
- Added logging to Discord for user initialization and update in production environment.
- Removed an unused function setHackerrankProfile from dbUtils.ts.
  • Loading branch information
A91y committed Oct 24, 2024
1 parent b352d71 commit bad67a8
Showing 1 changed file with 75 additions and 17 deletions.
92 changes: 75 additions & 17 deletions src/utils/dbUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,31 @@ export const initializeUser = async (
email?: string | null
) => {
try {
await db.user.upsert({
const existingUser = await db.user.findUnique({
where: { githubId: githubId },
update: {},
create: {
githubId: githubId,
installationId: 0,
...(email
? { email: email, verifiedEmail: true, emails: [email] }
: {}),
},
});

let result;
if (!existingUser) {
result = await db.user.create({
data: {
githubId: githubId,
installationId: 0,
...(email
? { email: email, verifiedEmail: true, emails: [email] }
: {}),
},
});

if (process.env.NODE_ENV === "production") {
await logToDiscord(
`Triggered initializeUser for id: ${githubId}`,
"INFO"
);
}
} else {
result = existingUser;
}
return true;
} catch (error) {
if (process.env.NODE_ENV === "production") {
Expand Down Expand Up @@ -83,6 +97,9 @@ export const setUsername = async (id: bigint, username: UserDB) => {
...username,
},
});
if (process.env.NODE_ENV === "production") {
await logToDiscord(`Updated ${username} for id: ${id}`, "INFO");
}
return true;
} catch (error) {
if (process.env.NODE_ENV === "production") {
Expand Down Expand Up @@ -117,7 +134,11 @@ export const setGithubDevProfile = async (
totalIssues: profile.totalIssues,
},
});
await updateTotalPoints(id);
await updateTotalPoints(id).then(() => {
if (process.env.NODE_ENV === "production") {
logToDiscord(`Updated Github data for id: ${id}`, "INFO");
}
});
return true;
} catch (error) {
if (process.env.NODE_ENV === "production") {
Expand Down Expand Up @@ -189,6 +210,9 @@ export const getGithubUsername = async (id: bigint) => {
return user?.githubUsername || "";
};

/**
* Note: This function is not used anywhere in the codebase.
*/
export const setHackerrankProfile = async (id: bigint, profile: any) => {
try {
await db.hackerrankProfile.upsert({
Expand All @@ -201,7 +225,11 @@ export const setHackerrankProfile = async (id: bigint, profile: any) => {
...profile,
},
});
await updateTotalPoints(id);
await updateTotalPoints(id).then(() => {
if (process.env.NODE_ENV === "production") {
logToDiscord(`Updated Hackerrank data for id: ${id}`, "INFO");
}
});
return true;
} catch (error) {
if (process.env.NODE_ENV === "production") {
Expand Down Expand Up @@ -253,6 +281,12 @@ export const getSuperteamEarnProfile = async (id: bigint) => {
};

export const updateTotalPoints = async (id: bigint) => {
if (process.env.NODE_ENV === "production") {
await logToDiscord(
`Updating total points initialized for id: ${id}`,
"INFO"
);
}
const hackerrankProfile = await getHackerrankProfile(id);
const githubDevProfile = await getGithubDevProfile(id);
const gfgProfile = await getGFGProfile(id);
Expand Down Expand Up @@ -304,7 +338,11 @@ export const updateTotalPoints = async (id: bigint) => {
return false;
}

await db.user.update({
if (process.env.NODE_ENV === "production") {
await logToDiscord(`Updating total points triggered for id: ${id}`, "INFO");
}

return await db.user.update({
where: { githubId: id },
data: {
totalPoints: totalPoints,
Expand Down Expand Up @@ -338,7 +376,11 @@ export async function setHackerrankDatabyGithubId(
stars: stars,
},
});
await updateTotalPoints(githubId);
await updateTotalPoints(githubId).then(() => {
if (process.env.NODE_ENV === "production") {
logToDiscord(`Updated Hackerrank data for id: ${githubId}`, "INFO");
}
});
return true;
} catch (error) {
if (process.env.NODE_ENV === "production") {
Expand Down Expand Up @@ -367,7 +409,11 @@ export async function setGFGDatabyGithubId(
problemsSolved: problemsSolved,
},
});
await updateTotalPoints(githubId);
await updateTotalPoints(githubId).then(() => {
if (process.env.NODE_ENV === "production") {
logToDiscord(`Updated GFG data for id: ${githubId}`, "INFO");
}
});
return true;
} catch (error) {
if (process.env.NODE_ENV === "production") {
Expand All @@ -393,7 +439,11 @@ export async function setCodeChefDatabyGithubId(
currentRating: currentRating,
},
});
await updateTotalPoints(githubId);
await updateTotalPoints(githubId).then(() => {
if (process.env.NODE_ENV === "production") {
logToDiscord(`Updated Codechef data for id: ${githubId}`, "INFO");
}
});
return true;
} catch (error) {
if (process.env.NODE_ENV === "production") {
Expand Down Expand Up @@ -425,7 +475,11 @@ export async function setLeetCodeDatabyGithubId(
hardQues: hardQues,
},
});
await updateTotalPoints(githubId);
await updateTotalPoints(githubId).then(() => {
if (process.env.NODE_ENV === "production") {
logToDiscord(`Updated Leetcode data for id: ${githubId}`, "INFO");
}
});
return true;
} catch (error) {
if (process.env.NODE_ENV === "production") {
Expand Down Expand Up @@ -457,7 +511,11 @@ export async function setSuperteamEarnDatabyGithubId(
totalWinnings: totalWinnings,
},
});
await updateTotalPoints(githubId);
await updateTotalPoints(githubId).then(() => {
if (process.env.NODE_ENV === "production") {
logToDiscord(`Updated Superteam Earn data for id: ${githubId}`, "INFO");
}
});
return true;
} catch (error) {
if (process.env.NODE_ENV === "production") {
Expand Down

0 comments on commit bad67a8

Please sign in to comment.