Skip to content

Commit 7b57924

Browse files
committed
update response data for migrate function
1 parent ed2dfc3 commit 7b57924

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

controllers/users.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -598,43 +598,50 @@ const filterUsers = async (req, res) => {
598598
// one time script function to perform the migration - adding github_user_id field to the document
599599
const migrate = async (req, res) => {
600600
const usersNotFound = [];
601+
let countUserFound = 0;
601602
let countUserNotFound = 0;
602603
try {
603604
// Fetch user data from GitHub API for each document in the users collection
604605
// divided by 500 because firestore api guarantee that we can process in batch of 500.
605606
const usersSnapshot = await firestore.collection("users").get();
606-
const batchCount = Math.ceil(usersSnapshot.docs.length / 500);
607+
const totalUsers = usersSnapshot.docs.length;
608+
const batchCount = Math.ceil(totalUsers / 500);
607609
// Create batch write operations for each batch of documents
608610
for (let i = 0; i < batchCount; i++) {
609611
const batchDocs = usersSnapshot.docs.slice(i * 500, (i + 1) * 500);
610612
const batchWrite = firestore.batch();
611613
const batchWrites = [];
612614
for (const userDoc of batchDocs) {
613615
const githubUsername = userDoc.data().github_id;
614-
const userName = userDoc.data().github_display_name;
616+
const username = userDoc.data().username;
617+
const userId = userDoc.id;
615618
batchWrite.update(userDoc.ref, { github_user_id: null });
616619
batchWrites.push(
617620
axios
618621
.get(`https://api.github.com/users/${githubUsername}`, {
619622
headers: {
620623
"Content-Type": "application/json",
621-
auth: `Bearer <Auth Token>`,
624+
},
625+
auth: {
626+
username: config.get("githubOauth.clientId"),
627+
password: config.get("githubOauth.clientSecret"),
622628
},
623629
})
624630
.then((response) => {
625631
const githubUserId = response.data.id;
626632
batchWrite.update(userDoc.ref, { github_user_id: `${githubUserId}` });
633+
countUserFound++;
627634
})
628635
.catch((error) => {
629636
if (error.response && error.response.status === 404) {
630637
countUserNotFound++;
631638
const invalidUsers = {
632-
name: `${userName}`,
633-
username: `${githubUsername}`,
639+
userId: `${userId}`,
640+
username: `${username}`,
641+
githubUsername: `${githubUsername}`,
634642
};
635643
usersNotFound.push(invalidUsers);
636644
} else {
637-
// Other error occurred
638645
logger.error("An error occurred at axios.get:", error);
639646
}
640647
})
@@ -645,10 +652,12 @@ const migrate = async (req, res) => {
645652
}
646653

647654
return res.status(200).json({
648-
message: "All Users github_user_id added successfully",
655+
message: "Result of migration",
649656
data: {
650-
invalidUsers: usersNotFound,
651-
totalCount: countUserNotFound,
657+
totalUsers: totalUsers,
658+
usersUpdated: countUserFound,
659+
usersNotUpdated: countUserNotFound,
660+
invalidUsersDetails: usersNotFound,
652661
},
653662
});
654663
} catch (error) {

0 commit comments

Comments
 (0)