From 0d414b1cb4d8246c39f682db3eb71fc3a539336b Mon Sep 17 00:00:00 2001 From: Samarpan Harit Date: Thu, 12 Sep 2024 02:27:02 +0530 Subject: [PATCH] chore: remove feature flag for GET /extension-requests/self API --- controllers/extensionRequests.js | 77 ++++++--------- test/integration/extensionRequests.test.js | 99 +++++++++++++++++++ .../multilpleExtensionRequest.test.js | 95 ------------------ 3 files changed, 130 insertions(+), 141 deletions(-) diff --git a/controllers/extensionRequests.js b/controllers/extensionRequests.js index 06bbcca76..121f00843 100644 --- a/controllers/extensionRequests.js +++ b/controllers/extensionRequests.js @@ -217,64 +217,49 @@ const getExtensionRequest = async (req, res) => { * @param res {Object} - Express response object */ const getSelfExtensionRequests = async (req, res) => { - const dev = req.query.dev === "true"; - try { const { id: userId } = req.userData; const { taskId, status } = req.query; - if (dev) { - if (userId) { - let allExtensionRequests; - if (taskId) { - const latestExtensionRequest = await extensionRequestsQuery.fetchLatestExtensionRequest({ - taskId, - }); - - if (latestExtensionRequest && latestExtensionRequest.assigneeId !== userId) { - allExtensionRequests = []; - } else { - // Add reviewer's name if status is not PENDING - if (latestExtensionRequest.status === "APPROVED" || latestExtensionRequest.status === "DENIED") { - const logs = await logsQuery.fetchLogs( - { "meta.extensionRequestId": latestExtensionRequest.id, limit: 1 }, - "extensionRequests" - ); - - if ( - logs.length === 1 && - logs[0]?.meta?.userId && - (logs[0]?.body?.status === "APPROVED" || logs[0]?.body?.status === "DENIED") // Make sure log is only related to status change - ) { - const superUserId = logs[0].meta.userId; - const name = await getFullName(superUserId); - latestExtensionRequest.reviewedBy = `${name?.first_name} ${name?.last_name}`; - latestExtensionRequest.reviewedAt = logs[0].timestamp._seconds; - } + if (userId) { + let allExtensionRequests; + if (taskId) { + const latestExtensionRequest = await extensionRequestsQuery.fetchLatestExtensionRequest({ + taskId, + }); + + if (latestExtensionRequest && latestExtensionRequest.assigneeId !== userId) { + allExtensionRequests = []; + } else { + // Add reviewer's name if status is not PENDING + if (latestExtensionRequest.status === "APPROVED" || latestExtensionRequest.status === "DENIED") { + const logs = await logsQuery.fetchLogs( + { "meta.extensionRequestId": latestExtensionRequest.id, limit: 1 }, + "extensionRequests" + ); + + if ( + logs.length === 1 && + logs[0]?.meta?.userId && + (logs[0]?.body?.status === "APPROVED" || logs[0]?.body?.status === "DENIED") // Make sure log is only related to status change + ) { + const superUserId = logs[0].meta.userId; + const name = await getFullName(superUserId); + latestExtensionRequest.reviewedBy = `${name?.first_name} ${name?.last_name}`; + latestExtensionRequest.reviewedAt = logs[0].timestamp._seconds; } - allExtensionRequests = [latestExtensionRequest]; } - } else { - allExtensionRequests = await extensionRequestsQuery.fetchExtensionRequests({ - assignee: userId, - status: status || undefined, - }); + allExtensionRequests = [latestExtensionRequest]; } - return res.json({ message: "Extension Requests returned successfully!", allExtensionRequests }); } else { - return res.boom.notFound("User doesn't exist"); - } - } else { - if (userId) { - const allExtensionRequests = await extensionRequestsQuery.fetchExtensionRequests({ - taskId, + allExtensionRequests = await extensionRequestsQuery.fetchExtensionRequests({ assignee: userId, status: status || undefined, }); - return res.json({ message: "Extension Requests returned successfully!", allExtensionRequests }); - } else { - return res.boom.notFound("User doesn't exist"); } + return res.json({ message: "Extension Requests returned successfully!", allExtensionRequests }); + } else { + return res.boom.notFound("User doesn't exist"); } } catch (error) { logger.error(`Error while fetching extension requests: ${error}`); diff --git a/test/integration/extensionRequests.test.js b/test/integration/extensionRequests.test.js index 9a1915299..eac6e7eb0 100644 --- a/test/integration/extensionRequests.test.js +++ b/test/integration/extensionRequests.test.js @@ -30,6 +30,7 @@ describe("Extension Requests", function () { taskId2, taskId3, taskId4, + taskId5, extensionRequestId1, extensionRequestId2, extensionRequestId3, @@ -115,6 +116,22 @@ describe("Extension Requests", function () { completionAward: { [DINERO]: 3, [NEELAM]: 300 }, lossRate: { [DINERO]: 1 }, }, + { + title: "Test task 5", + purpose: "To Test mocha", + featureUrl: "", + type: "group", + links: ["test1"], + endsOn: 1234, + startedOn: 54321, + status: "active", + percentCompleted: 10, + dependsOn: ["d12", "d23"], + isNoteworthy: false, + assignee: appOwner.username, + completionAward: { [DINERO]: 3, [NEELAM]: 300 }, + lossRate: { [DINERO]: 1 }, + }, ]; // Add the active task @@ -127,6 +144,7 @@ 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; const extensionRequest = { taskId: taskId3, @@ -176,11 +194,22 @@ describe("Extension Requests", function () { reason: "family event", status: "PENDING", }; + const extensionRequest5 = { + taskId: taskId5, + title: "change ETA", + assignee: user.id, + oldEndsOn: 1234, + newEndsOn: 1235, + reason: "family event", + status: "APPROVED", + }; + 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(extensionRequest5); }); after(async function () { @@ -192,6 +221,14 @@ describe("Extension Requests", function () { }); describe("GET /extension-requests/self", function () { + // beforeEach(function () { + // fetchLatestExtensionRequestStub = sinon.stub(extensionRequests, "fetchLatestExtensionRequest"); + // }); + + // afterEach(function () { + // fetchLatestExtensionRequestStub.restore(); + // }); + it("should return success response and extension request of the authenticated user", function (done) { chai .request(app) @@ -235,6 +272,68 @@ describe("Extension Requests", function () { }); }); + it("should return success response and an empty array of extensionRequest if assignee is not same as latest one", function (done) { + // fetchLatestExtensionRequestStub.returns({ + // taskId: taskId5, + // title: "change ETA", + // assignee: "mayur", + // oldEndsOn: 1234, + // newEndsOn: 1237, + // reason: "family event", + // status: "APPROVED", + // requestNumber: 5, + // userId: "ajdf", + // }); + chai + .request(app) + .get(`/extension-requests/self`) + .query({ taskId: taskId5 }) + .set("cookie", `${cookieName}=${appOwnerjwt}`) + .end((err, res) => { + if (err) { + return done(err); + } + + expect(res).to.have.status(200); + expect(res.body).to.be.a("object"); + expect(res.body.message).to.be.equal("Extension Requests returned successfully!"); + expect(res.body.allExtensionRequests).to.be.a("array").with.lengthOf(0); + return done(); + }); + }); + + it("should return success response and a single latestExtensionRequest if assignee same as latest one", function (done) { + // fetchLatestExtensionRequestStub.returns({ + // taskId: taskId5, + // title: "change ETA", + // assigneeId: user.id, + // assignee: "mayur", + // oldEndsOn: 1234, + // newEndsOn: 1237, + // reason: "family event", + // status: "APPROVED", + // requestNumber: 5, + // userId: user.id, + // id: "12234", + // }); + chai + .request(app) + .get(`/extension-requests/self`) + .query({ taskId: taskId2 }) + .set("cookie", `${cookieName}=${appOwnerjwt}`) + .end((err, res) => { + if (err) { + return done(err); + } + + expect(res).to.have.status(200); + expect(res.body).to.be.a("object"); + expect(res.body.message).to.be.equal("Extension Requests returned successfully!"); + expect(res.body.allExtensionRequests).to.be.a("array").with.lengthOf(1); + return done(); + }); + }); + it("Should return 401 if not logged in", function (done) { chai .request(app) diff --git a/test/integration/multilpleExtensionRequest.test.js b/test/integration/multilpleExtensionRequest.test.js index 75a08996e..5124dd558 100644 --- a/test/integration/multilpleExtensionRequest.test.js +++ b/test/integration/multilpleExtensionRequest.test.js @@ -280,99 +280,4 @@ describe("Multiple Extension Requests", function () { expect(res.body.message).to.equal("New ETA must be greater than Old ETA"); }); }); - - let fetchLatestExtensionRequestStub; - - describe("GET /extension-requests/self **when dev flag is true**", function () { - beforeEach(function () { - fetchLatestExtensionRequestStub = sinon.stub(extensionRequests, "fetchLatestExtensionRequest"); - }); - - afterEach(function () { - fetchLatestExtensionRequestStub.restore(); - }); - - it("Dev-flag true->should return success response and an empty array of extensionRequest if assignee is not same as latest one", function (done) { - fetchLatestExtensionRequestStub.returns({ - taskId: taskId2, - title: "change ETA", - assignee: "mayur", - oldEndsOn: 1234, - newEndsOn: 1237, - reason: "family event", - status: "APPROVED", - requestNumber: 5, - userId: "ajdf", - }); - chai - .request(app) - .get(`/extension-requests/self`) - .query({ taskId: taskId2, dev: "true" }) - .set("cookie", `${cookieName}=${userJWT}`) - .end((err, res) => { - if (err) { - return done(err); - } - - expect(res).to.have.status(200); - expect(res.body).to.be.a("object"); - expect(res.body.message).to.be.equal("Extension Requests returned successfully!"); - expect(res.body.allExtensionRequests).to.be.a("array").with.lengthOf(0); - return done(); - }); - }); - - it("Dev-flag true->should return success response and a single latestExtensionRequest if assignee same as latest one", function (done) { - fetchLatestExtensionRequestStub.returns({ - taskId: taskId2, - title: "change ETA", - assigneeId: user.id, - assignee: "mayur", - oldEndsOn: 1234, - newEndsOn: 1237, - reason: "family event", - status: "APPROVED", - requestNumber: 5, - userId: user.id, - id: "12234", - }); - chai - .request(app) - .get(`/extension-requests/self`) - .query({ taskId: taskId2, dev: "true" }) - .set("cookie", `${cookieName}=${userJWT}`) - .end((err, res) => { - if (err) { - return done(err); - } - - expect(res).to.have.status(200); - expect(res.body).to.be.a("object"); - expect(res.body.message).to.be.equal("Extension Requests returned successfully!"); - expect(res.body.allExtensionRequests).to.be.a("array").with.lengthOf(1); - return done(); - }); - }); - - it("Dev-flag true->it should return 401 if not logged in", function (done) { - chai - .request(app) - .get("/extension-requests/self") - .end((err, res) => { - if (err) { - return done(); - } - - expect(res).to.have.status(401); - expect(res.body).to.be.an("object"); - expect(res.body).to.eql({ - statusCode: 401, - error: "Unauthorized", - message: "Unauthenticated User", - }); - - return done(); - }); - }); - }); });