-
Notifications
You must be signed in to change notification settings - Fork 273
GitHub duplication Fixed and Migration #1044
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
base: develop
Are you sure you want to change the base?
Changes from 28 commits
5941bc5
5aad16d
7358662
0c2e8ce
0ee25fd
d54f16a
4e5aff3
9afcbd0
0fbfc9a
cc487d9
a5ca925
f01f146
558dd2d
adaf3cb
8d8232c
e88520b
2e0838b
65f4545
fcac418
a45e5c8
63cf7d0
45b2f3b
9ee96fc
0fb5eca
0d60558
7c6d7aa
e1e57a9
a481fb6
66b6e07
b25554d
75d682f
aa0f615
fa249fc
241df6a
88aaabd
7353c58
e56cd15
2bdff72
d59ae9e
d1b213c
eccbbd5
9c0fa42
f61ff70
7bcdb26
348ce2b
5247952
468b057
1f04c76
8e9414e
421c736
3d11b0b
20f0b3e
34f235c
ae3bfd8
9569688
5484294
d20932d
6d309e3
240ccb9
d0ed63c
c3bd1db
2dc68a5
e9a0819
ed2dfc3
7b57924
97f0959
0dfda95
b2c933e
8158773
31ad08a
708d57e
7f79193
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
const axios = require("axios"); | ||
prakashchoudhary07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const firestore = require("../utils/firestore"); | ||
const chaincodeQuery = require("../models/chaincodes"); | ||
const userQuery = require("../models/users"); | ||
const profileDiffsQuery = require("../models/profileDiffs"); | ||
|
@@ -462,6 +464,46 @@ const filterUsers = async (req, res) => { | |
} | ||
}; | ||
|
||
// one time script function to perform the migration - adding github_user_id field to the document | ||
const migrate = async (req, res) => { | ||
RitikJaiswal75 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
try { | ||
// Fetch user data from GitHub API for each document in the users collection | ||
// divided by 500 because firestore api guarantee that we can process in batch of 500. | ||
const usersSnapshot = await firestore.collection("users").get(); | ||
const batchCount = Math.ceil(usersSnapshot.docs.length / 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) { | ||
const githubUsername = userDoc.data().github_id; | ||
batchWrite.update(userDoc.ref, { github_user_id: null }); | ||
batchWrites.push( | ||
axios | ||
.get(`https://api.github.com/users/${githubUsername}`) | ||
prakashchoudhary07 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
shubham-y marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
.then((response) => { | ||
const githubUserId = response.data.id; | ||
batchWrite.update(userDoc.ref, { github_user_id: `${githubUserId}` }); | ||
}) | ||
.catch((error) => { | ||
logger.error(error); | ||
shubham-y marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}) | ||
); | ||
} | ||
await Promise.all(batchWrites); | ||
heyrandhir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await batchWrite.commit(); | ||
} | ||
|
||
return res.send({ | ||
shubham-y marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
statusCode: 201, | ||
message: "All Users github_user_id added successfully", | ||
}); | ||
} catch (error) { | ||
return res.status(500).send("Internal server error"); | ||
shubham-y marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
}; | ||
|
||
module.exports = { | ||
verifyUser, | ||
generateChaincode, | ||
|
@@ -481,4 +523,5 @@ module.exports = { | |
addDefaultArchivedRole, | ||
getUserSkills, | ||
filterUsers, | ||
migrate, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const githubUserInfo = require("../auth/githubUserInfo")(); | ||
|
||
/** | ||
* User info for GitHub auth response | ||
* Multiple responses can be added to the array if required | ||
* | ||
* @return {Object} | ||
*/ | ||
module.exports = () => { | ||
return [ | ||
{ | ||
username: "ankur", | ||
first_name: "Ankur", | ||
last_name: "Narkhede", | ||
yoe: 0, | ||
img: "./img.png", | ||
linkedin_id: "ankurnarkhede", | ||
github_id: githubUserInfo[0].username, | ||
github_display_name: githubUserInfo[0].displayName, | ||
isMember: true, | ||
phone: "1234567890", | ||
email: "[email protected]", | ||
roles: { | ||
member: true, | ||
}, | ||
tokens: { | ||
githubAccessToken: "githubAccessToken", | ||
}, | ||
status: "active", | ||
profileURL: "https://abcde.com", | ||
picture: { | ||
publicId: "profile/mtS4DhUvNYsKqI7oCWVB/aenklfhtjldc5ytei3ar", | ||
url: "https://res.cloudinary.com/realdevsquad/image/upload/v1667685133/profile/mtS4DhUvNYsKqI7oCWVB/aenklfhtjldc5ytei3ar.jpg", | ||
}, | ||
incompleteUserDetails: false, | ||
}, | ||
]; | ||
}; | ||
shubhamsinghbundela marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
const chai = require("chai"); | ||
const { expect } = chai; | ||
const chaiHttp = require("chai-http"); | ||
|
||
shubham-y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const firestore = require("../../utils/firestore"); | ||
const app = require("../../server"); | ||
const authService = require("../../services/authService"); | ||
|
@@ -13,7 +12,6 @@ const userData = require("../fixtures/user/user")(); | |
const profileDiffData = require("../fixtures/profileDiffs/profileDiffs")(); | ||
const superUser = userData[4]; | ||
const searchParamValues = require("../fixtures/user/search")(); | ||
|
||
const config = require("config"); | ||
const joinData = require("../fixtures/user/join"); | ||
const { | ||
|
@@ -25,7 +23,6 @@ const userStatusModel = require("../../models/userStatus"); | |
|
||
const cookieName = config.get("userToken.cookieName"); | ||
chai.use(chaiHttp); | ||
|
||
describe("Users", function () { | ||
let jwt; | ||
let superUserId; | ||
|
@@ -1040,4 +1037,61 @@ describe("Users", function () { | |
}); | ||
}); | ||
}); | ||
|
||
describe("POST /users/migrate", function () { | ||
beforeEach(async function () { | ||
superUserId = await addUser(superUser); | ||
superUserAuthToken = authService.generateAuthToken({ userId: superUserId }); | ||
}); | ||
shubham-y marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
afterEach(async function () { | ||
await cleanDb(); | ||
}); | ||
shubham-y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
it("Should return 401 when user is unauthorize", function (done) { | ||
chai | ||
.request(app) | ||
.post("/users/migrate") | ||
.end((err, res) => { | ||
if (err) { | ||
return done(); | ||
} | ||
expect(res).to.have.status(401); | ||
expect(res.body.message).to.equal("Unauthenticated User"); | ||
return done(); | ||
}); | ||
shubham-y marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}); | ||
it("Should update the user", async function () { | ||
|
||
const response1 = await chai | ||
shubham-y marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
.request(app) | ||
.post(`/users/migrate`) | ||
shubham-y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.set("Cookie", `${cookieName}=${superUserAuthToken}`); | ||
expect(response1).to.have.status(200); | ||
expect(response1.body).to.eql({ | ||
statusCode: 201, | ||
message: `All Users github_user_id added successfully`, | ||
}); | ||
const response2 = await chai.request(app).get(`/users`).set("cookie", `${cookieName}=${superUserAuthToken}`); | ||
shubham-y marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
expect(response2).to.have.status(200); | ||
response2.body.users.forEach((document) => { | ||
expect(document).to.have.property(`github_user_id`); | ||
iamYashSinha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
}); | ||
it("Should return unauthorized error when not logged in", function (done) { | ||
chai | ||
.request(app) | ||
.post(`/users/migrate`) | ||
.end((err, res) => { | ||
if (err) { | ||
return done(err); | ||
} | ||
expect(res).to.have.status(401); | ||
expect(res.body).to.eql({ | ||
statusCode: 401, | ||
error: "Unauthorized", | ||
message: "Unauthenticated User", | ||
}); | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.