Skip to content

Commit

Permalink
Added /tasks/assign/:userId PATCH Endpoint (#2317)
Browse files Browse the repository at this point in the history
* added one patch route for self task assignment based on skillset

* test case assertion fix

* test case assertion fix
  • Loading branch information
vikasosmium authored Dec 27, 2024
1 parent 439658f commit ff06c97
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 8 deletions.
13 changes: 12 additions & 1 deletion routes/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const { cacheResponse, invalidateCache } = require("../utils/cache");
const { ALL_TASKS } = require("../constants/cacheKeys");
const { verifyCronJob } = require("../middlewares/authorizeBot");
const { CLOUDFLARE_WORKER, CRON_JOB_HANDLER } = require("../constants/bot");
const { devFlagMiddleware } = require("../middlewares/devFlag");
const { userAuthorization } = require("../middlewares/userAuthorization");

const oldAuthorizationMiddleware = authorizeRoles([APPOWNER, SUPERUSER]);
const newAuthorizationMiddleware = authorizeAndAuthenticate(
Expand Down Expand Up @@ -64,7 +66,16 @@ router.patch(
tasks.updateTaskStatus,
assignTask
);
router.patch("/assign/self", authenticate, invalidateCache({ invalidationKeys: [ALL_TASKS] }), tasks.assignTask);
router.patch("/assign/self", authenticate, invalidateCache({ invalidationKeys: [ALL_TASKS] }), tasks.assignTask); // this route is being deprecated in favor of /assign/:userId.

router.patch(
"/assign/:userId",
authenticate,
devFlagMiddleware,
userAuthorization,
invalidateCache({ invalidationKeys: [ALL_TASKS] }),
tasks.assignTask
);

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

Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/tasks/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,23 @@ module.exports = () => {
createdAt: 1644753600,
updatedAt: 1644753600,
},
{
title: "Test task",
type: "feature",
endsOn: 1234,
startedOn: 4567,
status: "AVAILABLE",
percentCompleted: 0,
category: "FRONTEND",
level: 3,
participants: [],
completionAward: { [DINERO]: 3, [NEELAM]: 300 },
lossRate: { [DINERO]: 1 },
priority: "HIGH",
isNoteworthy: true,
assignee: false,
createdAt: 1644753600,
updatedAt: 1644753600,
},
];
};
27 changes: 27 additions & 0 deletions test/fixtures/user/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,5 +441,32 @@ module.exports = () => {
url: "https://res.cloudinary.com/realdevsquad/image/upload/v1667685133/profile/mtS4DhUvNYsKqI7oCWVB/aenklfhtjldc5ytei3ar.jpg",
},
},
{
username: "vikasinghdon",
first_name: "Vikas",
last_name: "Singh",
discordId: "yashu_yashu",
yoe: 10,
img: "./img.png",
linkedin_id: "destroyer",
github_id: "pickme",
github_display_name: "Vikas Singh",
phone: "1234567890",
email: "[email protected]",
joined_discord: "2024-07-16T18:21:09.278000+00:00",
status: "idle",
tokens: {
githubAccessToken: "githubAccessToken",
},
roles: {
super_user: false,
archived: false,
in_discord: true,
},
picture: {
publicId: "",
url: "",
},
},
];
};
111 changes: 110 additions & 1 deletion test/integration/tasks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ const { logType } = require("../../constants/logs");
const { INTERNAL_SERVER_ERROR } = require("../../constants/errorMessages");
const tasksService = require("../../services/tasks");
chai.use(chaiHttp);
const tags = require("../../models/tags");
const levels = require("../../models/levels");
const items = require("../../models/items");
const taskController = require("../../controllers/tasks");

const appOwner = userData[3];
const superUser = userData[4];
const genZUser = userData[20];
const testUser = userData[2];

let jwt, superUserJwt;
const { createProgressDocument } = require("../../models/progresses");
Expand Down Expand Up @@ -77,14 +83,40 @@ const taskData = [
},
];

const tagData = {
reason: "adding skills to users",
name: "EMBER",
type: "SKILL",
createdBy: "",
date: new Date().getTime(),
};

const itemData = {
itemId: "",
itemType: "TASK",
tagPayload: [
{
tagId: "",
levelId: "",
},
],
};

const levelData = {
name: "1",
value: 1,
};

describe("Tasks", function () {
let taskId1, taskId;
let taskId1, taskId, testUserId, testUserjwt;

before(async function () {
const userId = await addUser(appOwner);
const superUserId = await addUser(superUser);
testUserId = await addUser(testUser);
jwt = authService.generateAuthToken({ userId });
superUserJwt = authService.generateAuthToken({ userId: superUserId });
testUserjwt = authService.generateAuthToken({ userId: testUserId });

// Add the active task
taskId = (await tasks.updateTask(taskData[0])).taskId;
Expand Down Expand Up @@ -1699,4 +1731,81 @@ describe("Tasks", function () {
expect(res.body.message).to.be.equal(INTERNAL_SERVER_ERROR);
});
});

describe("PATCH /tasks/assign/:userId", function () {
let taskData, genZUserJwt, genZUserId;

beforeEach(async function () {
genZUserId = await addUser(genZUser);
genZUserJwt = authService.generateAuthToken({ userId: genZUserId });
taskData = tasksData[8];
});

afterEach(async function () {
await cleanDb();
sinon.restore();
});

it("Should not assign a task to the user if they do not have status idle", async function () {
await tasks.updateTask(taskData);

const res = await chai
.request(app)
.patch(`/tasks/assign/${testUserId}?dev=true`)
.set("cookie", `${cookieName}=${testUserjwt}`)
.send();

expect(res).to.have.status(200);
expect(res.body.message).to.be.equal("Task cannot be assigned to users with active or OOO status");
});

it("Should not assign a task to the user if task doesn't exist", async function () {
await tasks.updateTask(taskData);

const res = await chai
.request(app)
.patch(`/tasks/assign/${genZUserId}?dev=true`)
.set("cookie", `${cookieName}=${genZUserJwt}`)
.send();

expect(res).to.have.status(200);
expect(res.body.message).to.be.equal("Task not found");
});

it("Should assign task to the user if their status is idle and task is available", async function () {
const taskAdd = await tasks.updateTask(taskData);
const levelAdd = await levels.addLevel(levelData);

tagData.createdBy = genZUserId;
const tagAdd = await tags.addTag(tagData);

itemData.itemId = taskAdd.taskId;
itemData.tagPayload[0].tagId = tagAdd.id;
itemData.tagPayload[0].levelId = levelAdd.id;

await items.addTagsToItem(itemData);

const res = await chai
.request(app)
.patch(`/tasks/assign/${genZUserId}?dev=true`)
.set("cookie", `${cookieName}=${genZUserJwt}`)
.send();

expect(res).to.have.status(200);
expect(res.body.message).to.be.equal("Task assigned");
});

it("Should throw an error if Firestore batch operations fail", async function () {
sinon.stub(taskController, "assignTask").rejects(new Error(INTERNAL_SERVER_ERROR));

const res = await chai
.request(app)
.patch(`/tasks/assign/${genZUserId}?dev=true`)
.set("cookie", `${cookieName}=${genZUserJwt}`)
.send();

expect(res).to.have.status(500);
expect(res.body.message).to.be.equal(INTERNAL_SERVER_ERROR);
});
});
});
4 changes: 2 additions & 2 deletions test/unit/models/tasks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ describe("tasks", function () {

it("Should update task status COMPLETED to DONE", async function () {
const res = await tasks.updateTaskStatus();
expect(res.totalTasks).to.be.equal(8);
expect(res.totalUpdatedStatus).to.be.equal(8);
expect(res.totalTasks).to.be.equal(9);
expect(res.totalUpdatedStatus).to.be.equal(9);
});

it("should throw an error if firebase batch operation fails", async function () {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/services/tasks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe("Tasks services", function () {
const res = await updateTaskStatusToDone(tasks);

expect(res).to.deep.equal({
totalUpdatedStatus: 8,
totalUpdatedStatus: 9,
totalOperationsFailed: 0,
updatedTaskDetails: taskDetails,
failedTaskDetails: [],
Expand All @@ -73,7 +73,7 @@ describe("Tasks services", function () {

expect(res).to.deep.equal({
totalUpdatedStatus: 0,
totalOperationsFailed: 8,
totalOperationsFailed: 9,
updatedTaskDetails: [],
failedTaskDetails: taskDetails,
});
Expand Down
4 changes: 2 additions & 2 deletions test/unit/services/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe("Users services", function () {

expect(res).to.deep.equal({
message: "Successfully completed batch updates",
totalUsersArchived: 20,
totalUsersArchived: 21,
totalOperationsFailed: 0,
updatedUserDetails: userDetails,
failedUserDetails: [],
Expand All @@ -82,7 +82,7 @@ describe("Users services", function () {
expect(res).to.deep.equal({
message: "Firebase batch operation failed",
totalUsersArchived: 0,
totalOperationsFailed: 20,
totalOperationsFailed: 21,
updatedUserDetails: [],
failedUserDetails: userDetails,
});
Expand Down

0 comments on commit ff06c97

Please sign in to comment.