Skip to content

Commit ce27208

Browse files
committed
Merge branch 'develop' of https://github.com/ravikumar1002/website-backend into feat/github-created-at
2 parents 8d18645 + 4ade0b2 commit ce27208

File tree

11 files changed

+141
-8
lines changed

11 files changed

+141
-8
lines changed

controllers/auth.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ const githubAuthCallback = (req, res, next) => {
5353
github_id: user.username,
5454
github_display_name: user.displayName,
5555
github_created_at: Number((new Date(user._json.created_at).getTime() / 1000).toFixed(0)),
56+
created_at: Date.now(),
57+
updated_at: Date.now(),
5658
};
5759

5860
const { userId, incompleteUserDetails } = await users.addOrUpdate(userData);

controllers/tasks.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,12 @@ const updateTask = async (req, res) => {
256256
}
257257
await tasks.updateTask(req.body, req.params.id);
258258
if (isUserStatusEnabled && req.body.assignee) {
259+
// New Assignee Status Update
259260
await updateUserStatusOnTaskUpdate(req.body.assignee);
261+
// Old Assignee Status Update if available
262+
if (task.taskData.assigneeId) {
263+
await updateStatusOnTaskCompletion(task.taskData.assigneeId);
264+
}
260265
}
261266

262267
return res.status(204).send();

models/users.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const addOrUpdate = async (userData, userId = null) => {
3838
await userModel.doc(userId).set({
3939
...user.data(),
4040
...userData,
41+
updated_at: Date.now(),
4142
});
4243
}
4344

@@ -53,6 +54,7 @@ const addOrUpdate = async (userData, userId = null) => {
5354
isNewUser: false,
5455
userId: user.docs[0].id,
5556
incompleteUserDetails: user.docs[0].data().incompleteUserDetails,
57+
updated_at: Date.now(),
5658
};
5759
}
5860

@@ -65,7 +67,7 @@ const addOrUpdate = async (userData, userId = null) => {
6567
userData.roles = { archived: false, in_discord: false };
6668
userData.incompleteUserDetails = true;
6769
const userInfo = await userModel.add(userData);
68-
return { isNewUser: true, userId: userInfo.id, incompleteUserDetails: true };
70+
return { isNewUser: true, userId: userInfo.id, incompleteUserDetails: true, updated_at: Date.now() };
6971
} catch (err) {
7072
logger.error("Error in adding or updating user", err);
7173
throw err;

services/discordService.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const setInDiscordFalseScript = async () => {
3434
...user.roles,
3535
in_discord: false,
3636
},
37+
updated_at: Date.now(),
3738
};
3839
updateUsersPromises.push(userModel.doc(id).update(userData));
3940
});

test/fixtures/auth/githubUserInfo.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,39 @@ module.exports = () => {
6767
},
6868
},
6969
},
70+
{
71+
login: "sahsisunny",
72+
id: 70854507,
73+
node_id: "MDQ6VXNlcjcwODU0NTA3",
74+
avatar_url: "https://avatars.githubusercontent.com/u/70854507?v=4",
75+
gravatar_id: "",
76+
url: "https://api.github.com/users/sahsisunny",
77+
html_url: "https://github.com/sahsisunny",
78+
followers_url: "https://api.github.com/users/sahsisunny/followers",
79+
following_url: "https://api.github.com/users/sahsisunny/following{/other_user}",
80+
gists_url: "https://api.github.com/users/sahsisunny/gists{/gist_id}",
81+
starred_url: "https://api.github.com/users/sahsisunny/starred{/owner}{/repo}",
82+
subscriptions_url: "https://api.github.com/users/sahsisunny/subscriptions",
83+
organizations_url: "https://api.github.com/users/sahsisunny/orgs",
84+
repos_url: "https://api.github.com/users/sahsisunny/repos",
85+
events_url: "https://api.github.com/users/sahsisunny/events{/privacy}",
86+
received_events_url: "https://api.github.com/users/sahsisunny/received_events",
87+
type: "User",
88+
site_admin: false,
89+
name: "Sunny Sahsi",
90+
company: "@Real-Dev-Squad ",
91+
blog: "https://sahsisunny.netlify.app/",
92+
location: "India",
93+
email: null,
94+
hireable: true,
95+
bio: null,
96+
twitter_username: "sahsisunny",
97+
public_repos: 38,
98+
public_gists: 1,
99+
followers: 11,
100+
following: 4,
101+
created_at: "2020-09-06T16:21:38Z",
102+
updated_at: "2023-07-26T09:29:37Z",
103+
},
70104
];
71105
};

test/fixtures/user/user.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,5 +329,19 @@ module.exports = () => {
329329
twitter_id: "ramsingh123",
330330
linkedin_id: "ramsingh123",
331331
},
332+
{
333+
username: "sahsisunny",
334+
first_name: "sunny",
335+
last_name: "sahsi",
336+
github_id: githubUserInfo[1].login,
337+
github_display_name: githubUserInfo[1].name,
338+
roles: {
339+
member: true,
340+
in_discord: true,
341+
},
342+
incompleteUserDetails: false,
343+
updated_at: Date.now(),
344+
created_at: Date.now(),
345+
},
332346
];
333347
};

test/integration/auth.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe("auth", function () {
4040
expect(res.headers.location).to.equal(githubOauthURL);
4141
});
4242

43-
it("should redirect the user to new sign up flow if they are have incomplte user details true", async function () {
43+
it("should redirect the user to new sign up flow if they are have incomplete user details true", async function () {
4444
const redirectURL = "https://my.realdevsquad.com/new-signup";
4545
sinon.stub(passport, "authenticate").callsFake((strategy, options, callback) => {
4646
callback(null, "accessToken", githubUserInfo[0]);
@@ -55,6 +55,7 @@ describe("auth", function () {
5555
expect(res).to.have.status(302);
5656
expect(res.headers.location).to.equal(redirectURL);
5757
});
58+
5859
// same data should be return from github and same data should be added there
5960
it("should redirect the request to the goto page on successful login, if user has incomplete user details false", async function () {
6061
await addUserToDBForTest(userData[0]);
@@ -90,7 +91,7 @@ describe("auth", function () {
9091
expect(res.headers.location).to.equal(rdsUrl);
9192
});
9293

93-
it("should redirect the realdevsquad.com if non RDS URL provided, any url that is other than *.realdevsqud.com is invalid", async function () {
94+
it("should redirect the realdevsquad.com if non RDS URL provided, any url that is other than *.realdevsquad.com is invalid", async function () {
9495
await addUserToDBForTest(userData[0]);
9596
const invalidRedirectUrl = new URL("https://google.com").href;
9697
const rdsUiUrl = new URL(config.get("services.rdsUi.baseUrl")).href;

test/integration/taskBasedStatusUpdate.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,4 +537,59 @@ describe("Task Based Status Updates", function () {
537537
);
538538
});
539539
});
540+
541+
describe("PATCH Update User Status on Task Assignment by SuperUser", function () {
542+
let userId1, user2Name, superUserId, superUserJwt, taskArr;
543+
const reqBody = {};
544+
545+
beforeEach(async function () {
546+
userId1 = await addUser(userData[6]);
547+
superUserId = await addUser(userData[4]);
548+
superUserJwt = authService.generateAuthToken({ userId: superUserId });
549+
await addUser(userData[0]);
550+
user2Name = userData[0].username;
551+
taskArr = allTasks();
552+
const sampleTask1 = taskArr[0];
553+
sampleTask1.assignee = userId1;
554+
sampleTask1.createdBy = superUserId;
555+
await firestore.collection("tasks").doc("taskid123").set(sampleTask1);
556+
const statusData = generateStatusDataForState(userId1, userState.ACTIVE);
557+
await firestore.collection("usersStatus").doc("userStatusDoc001").set(statusData);
558+
});
559+
560+
afterEach(async function () {
561+
await cleanDb();
562+
});
563+
564+
it("Update the old assignee status to IDLE on task reassignment if no tasks is in progress in their name", async function () {
565+
reqBody.assignee = user2Name;
566+
const res = await chai
567+
.request(app)
568+
.patch(`/tasks/taskid123?userStatusFlag=true`)
569+
.set("cookie", `${cookieName}=${superUserJwt}`)
570+
.send(reqBody);
571+
expect(res.status).to.equal(204);
572+
const userStatus002Data = (await userStatusModel.doc("userStatusDoc001").get()).data();
573+
expect(userStatus002Data).to.have.keys(["userId", "currentStatus"]);
574+
expect(userStatus002Data.currentStatus.state).to.equal(userState.IDLE);
575+
});
576+
577+
it("Should maintain the old assignee status to ACTIVE on task reassignment if another task is in progress in their name", async function () {
578+
const sampleTask2 = taskArr[1];
579+
sampleTask2.assignee = userId1;
580+
sampleTask2.createdBy = superUserId;
581+
await firestore.collection("tasks").doc("taskid234").set(sampleTask2);
582+
583+
reqBody.assignee = user2Name;
584+
const res = await chai
585+
.request(app)
586+
.patch(`/tasks/taskid123?userStatusFlag=true`)
587+
.set("cookie", `${cookieName}=${superUserJwt}`)
588+
.send(reqBody);
589+
expect(res.status).to.equal(204);
590+
const userStatus002Data = (await userStatusModel.doc("userStatusDoc001").get()).data();
591+
expect(userStatus002Data).to.have.keys(["userId", "currentStatus"]);
592+
expect(userStatus002Data.currentStatus.state).to.equal(userState.ACTIVE);
593+
});
594+
});
540595
});

test/unit/models/users.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,18 @@ describe("users", function () {
9292
expect(user.last_name).to.equal(userData.last_name);
9393
expect(userExists).to.equal(true);
9494
});
95+
96+
it("It should have created_At and updated_At fields", async function () {
97+
const userData = userDataArray[14];
98+
await users.addOrUpdate(userData);
99+
const githubUsername = "sahsisunny";
100+
const { user, userExists } = await users.fetchUser({ githubUsername });
101+
expect(user).to.haveOwnProperty("created_at");
102+
expect(user).to.haveOwnProperty("updated_at");
103+
expect(userExists).to.equal(true);
104+
});
95105
});
106+
96107
describe("user image verification", function () {
97108
let userId, discordId, profileImageUrl, discordImageUrl;
98109
beforeEach(async function () {

test/unit/services/discordService.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe("Discord services", function () {
2626
const updatedUsers = await fetchAllUsers();
2727
updatedUsers.forEach((user) => {
2828
expect(user.roles.in_discord).to.be.equal(false);
29+
expect(user.updated_at).to.be.a("number");
2930
});
3031
});
3132
});

0 commit comments

Comments
 (0)