Skip to content
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

Remove dev feature flag for POST /extension-requests API #2150

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 64 additions & 136 deletions controllers/extensionRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,156 +16,84 @@ const { getFullName } = require("../utils/users");
* @param res {Object} - Express response object
*/
const createTaskExtensionRequest = async (req, res) => {
const dev = req.query.dev === "true";
if (dev) {
try {
let extensionBody = req.body;

let assigneeUsername = await getUsernameElseUndefined(extensionBody.assignee);
let assigneeId = extensionBody.assignee;
if (!assigneeUsername) {
assigneeId = await getUserIdElseUndefined(extensionBody.assignee);
assigneeUsername = extensionBody.assignee;
extensionBody.assignee = assigneeId;
}
try {
let extensionBody = req.body;

let assigneeUsername = await getUsernameElseUndefined(extensionBody.assignee);
let assigneeId = extensionBody.assignee;
if (!assigneeUsername) {
assigneeId = await getUserIdElseUndefined(extensionBody.assignee);
assigneeUsername = extensionBody.assignee;
extensionBody.assignee = assigneeId;
}

if (!assigneeId) {
return res.boom.badRequest("User Not Found");
}
if (!assigneeId) {
return res.boom.badRequest("User Not Found");
}

if (req.userData.id !== extensionBody.assignee && !req.userData.roles?.super_user) {
return res.boom.forbidden("Only assigned user and super user can create an extension request for this task.");
}
if (req.userData.id !== extensionBody.assignee && !req.userData.roles?.super_user) {
return res.boom.forbidden("Only assigned user and super user can create an extension request for this task.");
}

const { taskData: task } = await tasks.fetchTask(extensionBody.taskId);
if (!task) {
return res.boom.badRequest("Task Not Found");
}
if (task.assignee !== assigneeUsername) {
return res.boom.badRequest("This task is assigned to some different user.");
}
if (task.endsOn >= extensionBody.newEndsOn) {
return res.boom.badRequest("New ETA must be greater than Old ETA");
}
if (extensionBody.oldEndsOn !== task.endsOn) {
extensionBody.oldEndsOn = task.endsOn;
}
const { taskData: task } = await tasks.fetchTask(extensionBody.taskId);
if (!task) {
return res.boom.badRequest("Task Not Found");
}
if (task.assignee !== assigneeUsername) {
return res.boom.badRequest("This task is assigned to some different user.");
}
if (task.endsOn >= extensionBody.newEndsOn) {
return res.boom.badRequest("New ETA must be greater than Old ETA");
}
if (extensionBody.oldEndsOn !== task.endsOn) {
extensionBody.oldEndsOn = task.endsOn;
}

const latestExtensionRequest = await extensionRequestsQuery.fetchLatestExtensionRequest({
taskId: extensionBody.taskId,
});
const latestExtensionRequest = await extensionRequestsQuery.fetchLatestExtensionRequest({
taskId: extensionBody.taskId,
});

if (latestExtensionRequest && latestExtensionRequest.status === EXTENSION_REQUEST_STATUS.PENDING) {
return res.boom.badRequest("An extension request for this task already exists.");
}
if (latestExtensionRequest && latestExtensionRequest.status === EXTENSION_REQUEST_STATUS.PENDING) {
return res.boom.badRequest("An extension request for this task already exists.");
}

let requestNumber;
if (latestExtensionRequest && latestExtensionRequest.assigneeId === assigneeId) {
if (latestExtensionRequest.requestNumber && latestExtensionRequest.requestNumber > 0) {
requestNumber = latestExtensionRequest.requestNumber + 1;
extensionBody = { ...extensionBody, requestNumber };
} else {
extensionBody = { ...extensionBody, requestNumber: 2 };
}
let requestNumber;
if (latestExtensionRequest && latestExtensionRequest.assigneeId === assigneeId) {
if (latestExtensionRequest.requestNumber && latestExtensionRequest.requestNumber > 0) {
requestNumber = latestExtensionRequest.requestNumber + 1;
extensionBody = { ...extensionBody, requestNumber };
} else {
extensionBody = { ...extensionBody, requestNumber: 1 };
extensionBody = { ...extensionBody, requestNumber: 2 };
}

const extensionRequest = await extensionRequestsQuery.createExtensionRequest(extensionBody);
const extensionLog = {
type: "extensionRequests",
meta: {
taskId: extensionBody.taskId,
createdBy: req.userData.id,
},
body: {
extensionRequestId: extensionRequest.id,
oldEndsOn: task.endsOn,
newEndsOn: extensionBody.newEndsOn,
assignee: extensionBody.assignee,
status: EXTENSION_REQUEST_STATUS.PENDING,
},
};

await addLog(extensionLog.type, extensionLog.meta, extensionLog.body);

return res.json({
message: "Extension Request created successfully!",
extensionRequest: { ...extensionBody, id: extensionRequest.id },
});
} catch (err) {
logger.error(`Error while creating new extension request: ${err}`);
return res.boom.badImplementation(INTERNAL_SERVER_ERROR);
} else {
extensionBody = { ...extensionBody, requestNumber: 1 };
}
} else {
try {
const extensionBody = req.body;

let assigneeUsername = await getUsernameElseUndefined(extensionBody.assignee);
let assigneeId = extensionBody.assignee;
if (!assigneeUsername) {
assigneeId = await getUserIdElseUndefined(extensionBody.assignee);
assigneeUsername = extensionBody.assignee;
extensionBody.assignee = assigneeId;
}

if (!assigneeId) {
return res.boom.badRequest("User with this id or username doesn't exist.");
}

if (req.userData.id !== extensionBody.assignee && !req.userData.roles?.super_user) {
return res.boom.forbidden("Only assigned user and super user can create an extension request for this task.");
}

const { taskData: task } = await tasks.fetchTask(extensionBody.taskId);
if (!task) {
return res.boom.badRequest("Task with this id or taskid doesn't exist.");
}
if (task.assignee !== assigneeUsername) {
return res.boom.badRequest("This task is assigned to some different user");
}
if (task.endsOn >= extensionBody.newEndsOn) {
return res.boom.badRequest("The value for newEndsOn should be greater than the previous ETA");
}
if (extensionBody.oldEndsOn !== task.endsOn) {
extensionBody.oldEndsOn = task.endsOn;
}

const prevExtensionRequest = await extensionRequestsQuery.fetchExtensionRequests({
const extensionRequest = await extensionRequestsQuery.createExtensionRequest(extensionBody);
const extensionLog = {
type: "extensionRequests",
meta: {
taskId: extensionBody.taskId,
createdBy: req.userData.id,
},
body: {
extensionRequestId: extensionRequest.id,
oldEndsOn: task.endsOn,
newEndsOn: extensionBody.newEndsOn,
assignee: extensionBody.assignee,
});
if (prevExtensionRequest.length) {
return res.boom.forbidden("An extension request for this task already exists.");
}

const extensionRequest = await extensionRequestsQuery.createExtensionRequest(extensionBody);

const extensionLog = {
type: "extensionRequests",
meta: {
taskId: extensionBody.taskId,
createdBy: req.userData.id,
},
body: {
extensionRequestId: extensionRequest.id,
oldEndsOn: task.endsOn,
newEndsOn: extensionBody.newEndsOn,
assignee: extensionBody.assignee,
status: EXTENSION_REQUEST_STATUS.PENDING,
},
};
status: EXTENSION_REQUEST_STATUS.PENDING,
},
};

await addLog(extensionLog.type, extensionLog.meta, extensionLog.body);
await addLog(extensionLog.type, extensionLog.meta, extensionLog.body);

return res.json({
message: "Extension Request created successfully!",
extensionRequest: { ...extensionBody, id: extensionRequest.id },
});
} catch (err) {
logger.error(`Error while creating new extension request: ${err}`);
return res.boom.badImplementation(INTERNAL_SERVER_ERROR);
}
return res.json({
message: "Extension Request created successfully!",
extensionRequest: { ...extensionBody, id: extensionRequest.id },
});
} catch (err) {
logger.error(`Error while creating new extension request: ${err}`);
return res.boom.badImplementation(INTERNAL_SERVER_ERROR);
}
};

Expand Down
106 changes: 82 additions & 24 deletions test/integration/extensionRequests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ describe("Extension Requests", function () {
taskId2,
taskId3,
taskId4,
taskId5,
taskId6,
extensionRequestId1,
extensionRequestId2,
extensionRequestId3,
Expand Down Expand Up @@ -115,6 +117,30 @@ describe("Extension Requests", function () {
completionAward: { [DINERO]: 3, [NEELAM]: 300 },
lossRate: { [DINERO]: 1 },
},
{
title: "Task with multiple requests",
type: "feature",
endsOn: 1234,
startedOn: 4567,
status: "active",
percentCompleted: 10,
assignee: appOwner.username,
isNoteworthy: true,
completionAward: { [DINERO]: 3, [NEELAM]: 300 },
lossRate: { [DINERO]: 1 },
},
{
title: "Task with pending request",
type: "feature",
endsOn: 1234,
startedOn: 4567,
status: "active",
percentCompleted: 10,
assignee: appOwner.username,
isNoteworthy: true,
completionAward: { [DINERO]: 3, [NEELAM]: 300 },
lossRate: { [DINERO]: 1 },
},
];

// Add the active task
Expand All @@ -127,6 +153,8 @@ describe("Extension Requests", function () {
// Add the completed task
taskId3 = (await tasks.updateTask(taskData[3])).taskId;
taskId4 = (await tasks.updateTask(taskData[4])).taskId;
taskId5 = (await tasks.updateTask(taskData[5])).taskId;
taskId6 = (await tasks.updateTask(taskData[6])).taskId;

const extensionRequest = {
taskId: taskId3,
Expand Down Expand Up @@ -176,11 +204,21 @@ describe("Extension Requests", function () {
reason: "family event",
status: "PENDING",
};
const extensionRequest6 = {
taskId: taskId6,
title: "change ETA",
assignee: user.id,
oldEndsOn: 1234,
newEndsOn: 1235,
reason: "family event",
status: "PENDING",
};
extensionRequestId1 = (await extensionRequests.createExtensionRequest(extensionRequest)).id;
extensionRequestId2 = (await extensionRequests.createExtensionRequest(extensionRequest1)).id;
extensionRequestId3 = (await extensionRequests.createExtensionRequest(extensionRequest2)).id;
extensionRequestId4 = (await extensionRequests.createExtensionRequest(extensionRequest3)).id;
extensionRequestId5 = (await extensionRequests.createExtensionRequest(extensionRequest4)).id;
await extensionRequests.createExtensionRequest(extensionRequest6);
});

after(async function () {
Expand Down Expand Up @@ -333,7 +371,7 @@ describe("Extension Requests", function () {
return done(err);
}
expect(res).to.have.status(400);
expect(res.body.message).to.equal("User with this id or username doesn't exist.");
expect(res.body.message).to.equal("User Not Found");
return done();
});
});
Expand Down Expand Up @@ -387,7 +425,7 @@ describe("Extension Requests", function () {

expect(res).to.have.status(400);
expect(res.body).to.be.a("object");
expect(res.body.message).to.equal("Task with this id or taskid doesn't exist.");
expect(res.body.message).to.equal("Task Not Found");
return done();
});
});
Expand All @@ -413,7 +451,7 @@ describe("Extension Requests", function () {

expect(res).to.have.status(400);
expect(res.body).to.be.a("object");
expect(res.body.message).to.equal("This task is assigned to some different user");
expect(res.body.message).to.equal("This task is assigned to some different user.");
return done();
});
});
Expand All @@ -438,34 +476,54 @@ describe("Extension Requests", function () {
}
expect(res).to.have.status(400);
expect(res.body).to.be.a("object");
expect(res.body.message).to.equal("The value for newEndsOn should be greater than the previous ETA");
expect(res.body.message).to.equal("New ETA must be greater than Old ETA");
return done();
});
});

it("Should return fail response if extension request for a task already exists", function (done) {
chai
it("should create a new extension request when no previous extension request exists and make the requestNumber to 1", async function () {
const requestData = {
taskId: taskId5,
title: "change ETA",
assignee: appOwner.id,
oldEndsOn: 1234,
newEndsOn: 1235,
reason: "family event",
status: "PENDING",
};

const res = await chai
.request(app)
.post("/extension-requests")
.set("cookie", `${cookieName}=${appOwnerjwt}`)
.send({
taskId: taskId3,
title: "change ETA",
assignee: appOwner.id,
oldEndsOn: 1234,
newEndsOn: 1235,
reason: "family event",
status: "PENDING",
})
.end((err, res) => {
if (err) {
return done(err);
}
expect(res).to.have.status(403);
expect(res.body).to.be.a("object");
expect(res.body.message).to.equal("An extension request for this task already exists.");
return done();
});
.send(requestData);

expect(res).to.have.status(200);
expect(res.body.message).to.equal("Extension Request created successfully!");

expect(res.body.extensionRequest.requestNumber).to.be.equal(1);
expect(res.body.extensionRequest).to.be.an("object");
});

it("should handle the case when a previous extension request is pending so api should not allow and throw a proper message", async function () {
const requestData = {
taskId: taskId6,
title: "change ETA",
assignee: appOwner.id,
oldEndsOn: 1235,
newEndsOn: 1236,
reason: "family event",
status: "PENDING",
};

const res = await chai
.request(app)
.post("/extension-requests")
.set("cookie", `${cookieName}=${appOwnerjwt}`)
.send(requestData);

expect(res).to.have.status(400);
expect(res.body.message).to.equal("An extension request for this task already exists.");
});

it("Should return success response after adding the extension request and also there should be a log for the same", function (done) {
Expand Down
Loading
Loading