Skip to content

Commit ff06c97

Browse files
authored
Added /tasks/assign/:userId PATCH Endpoint (#2317)
* added one patch route for self task assignment based on skillset * test case assertion fix * test case assertion fix
1 parent 439658f commit ff06c97

File tree

7 files changed

+173
-8
lines changed

7 files changed

+173
-8
lines changed

routes/tasks.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const { cacheResponse, invalidateCache } = require("../utils/cache");
1717
const { ALL_TASKS } = require("../constants/cacheKeys");
1818
const { verifyCronJob } = require("../middlewares/authorizeBot");
1919
const { CLOUDFLARE_WORKER, CRON_JOB_HANDLER } = require("../constants/bot");
20+
const { devFlagMiddleware } = require("../middlewares/devFlag");
21+
const { userAuthorization } = require("../middlewares/userAuthorization");
2022

2123
const oldAuthorizationMiddleware = authorizeRoles([APPOWNER, SUPERUSER]);
2224
const newAuthorizationMiddleware = authorizeAndAuthenticate(
@@ -64,7 +66,16 @@ router.patch(
6466
tasks.updateTaskStatus,
6567
assignTask
6668
);
67-
router.patch("/assign/self", authenticate, invalidateCache({ invalidationKeys: [ALL_TASKS] }), tasks.assignTask);
69+
router.patch("/assign/self", authenticate, invalidateCache({ invalidationKeys: [ALL_TASKS] }), tasks.assignTask); // this route is being deprecated in favor of /assign/:userId.
70+
71+
router.patch(
72+
"/assign/:userId",
73+
authenticate,
74+
devFlagMiddleware,
75+
userAuthorization,
76+
invalidateCache({ invalidationKeys: [ALL_TASKS] }),
77+
tasks.assignTask
78+
);
6879

6980
router.get("/users/discord", verifyCronJob, getUsersValidator, tasks.getUsersHandler);
7081

test/fixtures/tasks/tasks.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,23 @@ module.exports = () => {
140140
createdAt: 1644753600,
141141
updatedAt: 1644753600,
142142
},
143+
{
144+
title: "Test task",
145+
type: "feature",
146+
endsOn: 1234,
147+
startedOn: 4567,
148+
status: "AVAILABLE",
149+
percentCompleted: 0,
150+
category: "FRONTEND",
151+
level: 3,
152+
participants: [],
153+
completionAward: { [DINERO]: 3, [NEELAM]: 300 },
154+
lossRate: { [DINERO]: 1 },
155+
priority: "HIGH",
156+
isNoteworthy: true,
157+
assignee: false,
158+
createdAt: 1644753600,
159+
updatedAt: 1644753600,
160+
},
143161
];
144162
};

test/fixtures/user/user.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,5 +441,32 @@ module.exports = () => {
441441
url: "https://res.cloudinary.com/realdevsquad/image/upload/v1667685133/profile/mtS4DhUvNYsKqI7oCWVB/aenklfhtjldc5ytei3ar.jpg",
442442
},
443443
},
444+
{
445+
username: "vikasinghdon",
446+
first_name: "Vikas",
447+
last_name: "Singh",
448+
discordId: "yashu_yashu",
449+
yoe: 10,
450+
img: "./img.png",
451+
linkedin_id: "destroyer",
452+
github_id: "pickme",
453+
github_display_name: "Vikas Singh",
454+
phone: "1234567890",
455+
456+
joined_discord: "2024-07-16T18:21:09.278000+00:00",
457+
status: "idle",
458+
tokens: {
459+
githubAccessToken: "githubAccessToken",
460+
},
461+
roles: {
462+
super_user: false,
463+
archived: false,
464+
in_discord: true,
465+
},
466+
picture: {
467+
publicId: "",
468+
url: "",
469+
},
470+
},
444471
];
445472
};

test/integration/tasks.test.js

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ const { logType } = require("../../constants/logs");
2828
const { INTERNAL_SERVER_ERROR } = require("../../constants/errorMessages");
2929
const tasksService = require("../../services/tasks");
3030
chai.use(chaiHttp);
31+
const tags = require("../../models/tags");
32+
const levels = require("../../models/levels");
33+
const items = require("../../models/items");
34+
const taskController = require("../../controllers/tasks");
3135

3236
const appOwner = userData[3];
3337
const superUser = userData[4];
38+
const genZUser = userData[20];
39+
const testUser = userData[2];
3440

3541
let jwt, superUserJwt;
3642
const { createProgressDocument } = require("../../models/progresses");
@@ -77,14 +83,40 @@ const taskData = [
7783
},
7884
];
7985

86+
const tagData = {
87+
reason: "adding skills to users",
88+
name: "EMBER",
89+
type: "SKILL",
90+
createdBy: "",
91+
date: new Date().getTime(),
92+
};
93+
94+
const itemData = {
95+
itemId: "",
96+
itemType: "TASK",
97+
tagPayload: [
98+
{
99+
tagId: "",
100+
levelId: "",
101+
},
102+
],
103+
};
104+
105+
const levelData = {
106+
name: "1",
107+
value: 1,
108+
};
109+
80110
describe("Tasks", function () {
81-
let taskId1, taskId;
111+
let taskId1, taskId, testUserId, testUserjwt;
82112

83113
before(async function () {
84114
const userId = await addUser(appOwner);
85115
const superUserId = await addUser(superUser);
116+
testUserId = await addUser(testUser);
86117
jwt = authService.generateAuthToken({ userId });
87118
superUserJwt = authService.generateAuthToken({ userId: superUserId });
119+
testUserjwt = authService.generateAuthToken({ userId: testUserId });
88120

89121
// Add the active task
90122
taskId = (await tasks.updateTask(taskData[0])).taskId;
@@ -1699,4 +1731,81 @@ describe("Tasks", function () {
16991731
expect(res.body.message).to.be.equal(INTERNAL_SERVER_ERROR);
17001732
});
17011733
});
1734+
1735+
describe("PATCH /tasks/assign/:userId", function () {
1736+
let taskData, genZUserJwt, genZUserId;
1737+
1738+
beforeEach(async function () {
1739+
genZUserId = await addUser(genZUser);
1740+
genZUserJwt = authService.generateAuthToken({ userId: genZUserId });
1741+
taskData = tasksData[8];
1742+
});
1743+
1744+
afterEach(async function () {
1745+
await cleanDb();
1746+
sinon.restore();
1747+
});
1748+
1749+
it("Should not assign a task to the user if they do not have status idle", async function () {
1750+
await tasks.updateTask(taskData);
1751+
1752+
const res = await chai
1753+
.request(app)
1754+
.patch(`/tasks/assign/${testUserId}?dev=true`)
1755+
.set("cookie", `${cookieName}=${testUserjwt}`)
1756+
.send();
1757+
1758+
expect(res).to.have.status(200);
1759+
expect(res.body.message).to.be.equal("Task cannot be assigned to users with active or OOO status");
1760+
});
1761+
1762+
it("Should not assign a task to the user if task doesn't exist", async function () {
1763+
await tasks.updateTask(taskData);
1764+
1765+
const res = await chai
1766+
.request(app)
1767+
.patch(`/tasks/assign/${genZUserId}?dev=true`)
1768+
.set("cookie", `${cookieName}=${genZUserJwt}`)
1769+
.send();
1770+
1771+
expect(res).to.have.status(200);
1772+
expect(res.body.message).to.be.equal("Task not found");
1773+
});
1774+
1775+
it("Should assign task to the user if their status is idle and task is available", async function () {
1776+
const taskAdd = await tasks.updateTask(taskData);
1777+
const levelAdd = await levels.addLevel(levelData);
1778+
1779+
tagData.createdBy = genZUserId;
1780+
const tagAdd = await tags.addTag(tagData);
1781+
1782+
itemData.itemId = taskAdd.taskId;
1783+
itemData.tagPayload[0].tagId = tagAdd.id;
1784+
itemData.tagPayload[0].levelId = levelAdd.id;
1785+
1786+
await items.addTagsToItem(itemData);
1787+
1788+
const res = await chai
1789+
.request(app)
1790+
.patch(`/tasks/assign/${genZUserId}?dev=true`)
1791+
.set("cookie", `${cookieName}=${genZUserJwt}`)
1792+
.send();
1793+
1794+
expect(res).to.have.status(200);
1795+
expect(res.body.message).to.be.equal("Task assigned");
1796+
});
1797+
1798+
it("Should throw an error if Firestore batch operations fail", async function () {
1799+
sinon.stub(taskController, "assignTask").rejects(new Error(INTERNAL_SERVER_ERROR));
1800+
1801+
const res = await chai
1802+
.request(app)
1803+
.patch(`/tasks/assign/${genZUserId}?dev=true`)
1804+
.set("cookie", `${cookieName}=${genZUserJwt}`)
1805+
.send();
1806+
1807+
expect(res).to.have.status(500);
1808+
expect(res.body.message).to.be.equal(INTERNAL_SERVER_ERROR);
1809+
});
1810+
});
17021811
});

test/unit/models/tasks.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ describe("tasks", function () {
336336

337337
it("Should update task status COMPLETED to DONE", async function () {
338338
const res = await tasks.updateTaskStatus();
339-
expect(res.totalTasks).to.be.equal(8);
340-
expect(res.totalUpdatedStatus).to.be.equal(8);
339+
expect(res.totalTasks).to.be.equal(9);
340+
expect(res.totalUpdatedStatus).to.be.equal(9);
341341
});
342342

343343
it("should throw an error if firebase batch operation fails", async function () {

test/unit/services/tasks.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe("Tasks services", function () {
5353
const res = await updateTaskStatusToDone(tasks);
5454

5555
expect(res).to.deep.equal({
56-
totalUpdatedStatus: 8,
56+
totalUpdatedStatus: 9,
5757
totalOperationsFailed: 0,
5858
updatedTaskDetails: taskDetails,
5959
failedTaskDetails: [],
@@ -73,7 +73,7 @@ describe("Tasks services", function () {
7373

7474
expect(res).to.deep.equal({
7575
totalUpdatedStatus: 0,
76-
totalOperationsFailed: 8,
76+
totalOperationsFailed: 9,
7777
updatedTaskDetails: [],
7878
failedTaskDetails: taskDetails,
7979
});

test/unit/services/users.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe("Users services", function () {
6161

6262
expect(res).to.deep.equal({
6363
message: "Successfully completed batch updates",
64-
totalUsersArchived: 20,
64+
totalUsersArchived: 21,
6565
totalOperationsFailed: 0,
6666
updatedUserDetails: userDetails,
6767
failedUserDetails: [],
@@ -82,7 +82,7 @@ describe("Users services", function () {
8282
expect(res).to.deep.equal({
8383
message: "Firebase batch operation failed",
8484
totalUsersArchived: 0,
85-
totalOperationsFailed: 20,
85+
totalOperationsFailed: 21,
8686
updatedUserDetails: [],
8787
failedUserDetails: userDetails,
8888
});

0 commit comments

Comments
 (0)