Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/GitHub duplication #1818

Merged
merged 22 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4017e99
Refactor : Modified callback api to have github user id
joyguptaa Dec 28, 2023
7dacd3e
Feat : Added API to add github_user_id to all users
joyguptaa Dec 28, 2023
8407103
Test : Added test for github_user_id script
joyguptaa Dec 28, 2023
d4ae3ac
Bug : Fixed logic while for checking github_user_id
joyguptaa Dec 28, 2023
9f0cc96
Chore : Minor Fixes
joyguptaa Jan 4, 2024
3a3d361
Refactor : Moved code to models
joyguptaa Jan 5, 2024
3a4c604
Merge branch 'develop' into feat/github-duplication
joyguptaa Jan 5, 2024
a8f3d7e
Test : Added failing mock data
joyguptaa Jan 5, 2024
483527c
Merge branch 'feat/github-duplication' of https://github.com/Real-Dev…
joyguptaa Jan 5, 2024
5787ca3
Merge branch 'develop' into feat/github-duplication
joyguptaa Jan 6, 2024
788f63d
Merge branch 'develop' into feat/github-duplication
joyguptaa Jan 7, 2024
8d70a3a
Merge branch 'develop' into feat/github-duplication
joyguptaa Jan 7, 2024
8aa5d6f
Feat : Updated API params
joyguptaa Jan 15, 2024
f1844d6
Feat : Added pagination logic to migrations API
joyguptaa Jan 15, 2024
78e6600
Merge branch 'feat/github-duplication' of https://github.com/Real-Dev…
joyguptaa Jan 15, 2024
61881fb
Merge branch 'develop' into feat/github-duplication
joyguptaa Jan 16, 2024
eded930
Test : Updated the test case
joyguptaa Jan 16, 2024
8f29087
Feat : Updated query params
joyguptaa Jan 16, 2024
8e1eb5f
Test : Updated test cases for migration api
joyguptaa Jan 16, 2024
1c3af94
Merge branch 'feat/github-duplication' of https://github.com/Real-Dev…
joyguptaa Jan 16, 2024
eacc712
Merge branch 'develop' into feat/github-duplication
joyguptaa Jan 17, 2024
86ae835
Merge branch 'develop' into feat/github-duplication
Ajeyakrishna-k Jan 18, 2024
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
Prev Previous commit
Next Next commit
Chore : Minor Fixes
  • Loading branch information
joyguptaa committed Jan 4, 2024
commit 9f0cc961087df06ae36c0f8e7cab8f01aabe9f55
13 changes: 9 additions & 4 deletions controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,12 @@ async function usersPatchHandler(req, res) {
}
}
const addGithubId = async (req, res) => {
const { action } = req.query;
if (action !== "adds-github-id") {
return res.status(400).json({
message: "Invalid action type",
});
}
const usersNotFound = [];
let countUserFound = 0;
let countUserNotFound = 0;
Expand All @@ -875,16 +881,15 @@ const addGithubId = async (req, res) => {
const batchCount = Math.ceil(totalUsers / 500);
// Create batch write operations for each batch of documents
for (let i = 0; i < batchCount; i++) {
const batchDocs = usersSnapshot.docs.slice(i * 500, (i + 1) * 500);
const batchWrite = firestore.batch();
const batchWrites = [];
for (const userDoc of batchDocs) {
for (const userDoc of usersSnapshot.docs.slice(i * 500, (i + 1) * 500)) {
const githubUsername = userDoc.data().github_id;
const username = userDoc.data().username;
const userId = userDoc.id;
batchWrite.update(userDoc.ref, { github_user_id: null });

const promise = fetch(`https://api.github.com/users/${githubUsername}`, requestOptions)
const getUserDetails = fetch(`https://api.github.com/users/${githubUsername}`, requestOptions)
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
Expand All @@ -902,7 +907,7 @@ const addGithubId = async (req, res) => {
usersNotFound.push(invalidUsers);
logger.error("An error occurred at fetch:", error);
});
batchWrites.push(promise);
batchWrites.push(getUserDetails);
}
await Promise.all(batchWrites);
await batchWrite.commit();
Expand Down
2 changes: 1 addition & 1 deletion routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ router.patch("/rejectDiff", authenticate, authorizeRoles([SUPERUSER]), users.rej
router.patch("/:userId", authenticate, authorizeRoles([SUPERUSER]), users.updateUser);
router.get("/suggestedUsers/:skillId", authenticate, authorizeRoles([SUPERUSER]), users.getSuggestedUsers);
// WARNING!! - One time Script/Route to do migration
router.post("/migrate", authenticate, authorizeRoles([SUPERUSER]), users.addGithubId);
router.post("/migrations", authenticate, authorizeRoles([SUPERUSER]), users.addGithubId);
module.exports = router;
22 changes: 20 additions & 2 deletions test/unit/models/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ describe("users", function () {
it("API should not be accessible to any regular user", function (done) {
chai
.request(app)
.post("/users/migrate")
.post("/users/migrations?action=adds-github-id")
.set("cookie", `${cookieName}=${userToken}`)
.send()
.end((err, res) => {
Expand All @@ -514,9 +514,16 @@ describe("users", function () {
for (const user of prodUsers.slice(2)) {
await addUser(user);
}
const allUsers = await chai.request(app).get("/users").set("cookie", `${cookieName}=${superUserToken}`).send();
const usersWithoutGithubId = allUsers.body.users.filter((user) => {
return !user.github_user_id;
});

expect(usersWithoutGithubId).to.not.have.length(0);

const res = await chai
.request(app)
.post("/users/migrate")
.post("/users/migrations?action=adds-github-id")
.set("cookie", `${cookieName}=${superUserToken}`)
.send();

Expand All @@ -526,6 +533,17 @@ describe("users", function () {
expect(res.body.data).to.have.property("usersUpdated").that.is.a("number");
expect(res.body.data).to.have.property("usersNotUpdated").that.is.a("number");
expect(res.body.data).to.have.property("invalidUsersDetails").that.is.an("array");

const updatedUsers = await chai
.request(app)
.get("/users")
.set("cookie", `${cookieName}=${superUserToken}`)
.send();

const updatedUsersWithoutGithubId = updatedUsers.body.users.filter((user) => {
return !user.github_user_id;
});
expect(updatedUsersWithoutGithubId).to.have.length(0);
});
});
});