diff --git a/middlewares/validators/requests.ts b/middlewares/validators/requests.ts index 2cf3b6983..e824984d8 100644 --- a/middlewares/validators/requests.ts +++ b/middlewares/validators/requests.ts @@ -84,7 +84,7 @@ export const getRequestsMiddleware = async (req: OooRequestCreateRequest, res: O id: joi.string().optional(), type: joi .string() - .valid(REQUEST_TYPE.OOO, REQUEST_TYPE.EXTENSION, REQUEST_TYPE.TASK, REQUEST_TYPE.ALL) + .valid(REQUEST_TYPE.OOO, REQUEST_TYPE.EXTENSION, REQUEST_TYPE.TASK, REQUEST_TYPE.ALL, REQUEST_TYPE.ONBOARDING) .optional(), requestedBy: joi.string().insensitive().optional(), state: joi diff --git a/models/requests.ts b/models/requests.ts index 9fda2fcbe..064eebd8c 100644 --- a/models/requests.ts +++ b/models/requests.ts @@ -71,6 +71,8 @@ export const updateRequest = async (id: string, body: any, lastModifiedBy: strin export const getRequests = async (query: any) => { let { id, type, requestedBy, state, prev, next, page, size = SIZE } = query; + const dev = query.dev === "true"; + size = parseInt(size); page = parseInt(page); try { @@ -86,11 +88,15 @@ export const getRequests = async (query: any) => { ...requestDoc.data(), }; } - - if (requestedBy) { + + if(requestedBy && dev){ + requestQuery = requestQuery.where("requestedBy", "==", requestedBy); + } + else if (requestedBy) { const requestedByUserId = await getUserId(requestedBy); requestQuery = requestQuery.where("requestedBy", "==", requestedByUserId); } + if (type) { requestQuery = requestQuery.where("type", "==", type); } diff --git a/test/integration/onboardingExtension.test.ts b/test/integration/onboardingExtension.test.ts index 454cc9688..66180ac47 100644 --- a/test/integration/onboardingExtension.test.ts +++ b/test/integration/onboardingExtension.test.ts @@ -10,7 +10,8 @@ import { REQUEST_ALREADY_PENDING, REQUEST_STATE, REQUEST_TYPE, ONBOARDING_REQUEST_CREATED_SUCCESSFULLY, - UNAUTHORIZED_TO_CREATE_ONBOARDING_EXTENSION_REQUEST + UNAUTHORIZED_TO_CREATE_ONBOARDING_EXTENSION_REQUEST, + REQUEST_FETCHED_SUCCESSFULLY } from "../../constants/requests"; const { generateToken } = require("../../test/utils/generateBotToken"); import app from "../../server"; @@ -299,4 +300,82 @@ describe("/requests Onboarding Extension", () => { .to.equal(new Date(currentDate + (body.numberOfDays*24*60*60*1000)).toDateString()); }) }) -}); \ No newline at end of file + + describe("GET /requests",() => { + const getEndpoint = "/requests"; + const username = userData[4].username; + + beforeEach(async () => { + await addUser(userData[4]); + }); + + afterEach(async () => { + await cleanDb(); + }); + + it("should return 204 content when onboarding extension request does not exist", (done) => { + requestsQuery.createRequest({ type: REQUEST_TYPE.OOO }); + chai.request(app) + .get(`${getEndpoint}?type=ONBOARDING`) + .end((err, res) => { + if (err) return done(err); + expect(res.statusCode).to.equal(204); + return done(); + }); + }); + + it("should fetch onboarding extension request by requestedBy field", (done) => { + requestsQuery.createRequest({ type: REQUEST_TYPE.ONBOARDING, requestedBy: username }); + chai.request(app) + .get(`${getEndpoint}?requestedBy=${username}&type=ONBOARDING&dev=true`) + .end((err, res) => { + if (err) return done(err); + expect(res.statusCode).to.equal(200); + expect(res.body.message).to.equal(REQUEST_FETCHED_SUCCESSFULLY); + expect(res.body.data[0].type).to.equal(REQUEST_TYPE.ONBOARDING); + expect(res.body.data[0].requestedBy).to.equal(username); + return done(); + }); + }); + + it("shopuld return 204 response when onboarding extension request does not exist for a user", (done) => { + requestsQuery.createRequest({ type: REQUEST_TYPE.OOO, requestedBy: username }); + chai.request(app) + .get(`${getEndpoint}?requestedBy=${username}&type=ONBOARDING`) + .end((err, res) => { + if (err) return done(err); + expect(res.statusCode).to.equal(204); + return done(); + }); + }); + + it("should fetch onboarding extension request by type field", (done) => { + requestsQuery.createRequest({ type: REQUEST_TYPE.ONBOARDING }); + chai.request(app) + .get(`${getEndpoint}?type=ONBOARDING`) + .end((err, res) => { + if (err) return done(err); + expect(res.statusCode).to.equal(200); + expect(res.body.message).to.equal(REQUEST_FETCHED_SUCCESSFULLY); + expect(res.body.data.length).to.equal(1); + expect(res.body.data[0].type).to.equal(REQUEST_TYPE.ONBOARDING); + return done(); + }); + }); + + it("should fetch onboarding extension request by state field", (done) => { + requestsQuery.createRequest({ type: REQUEST_TYPE.ONBOARDING, state: REQUEST_STATE.APPROVED }); + chai.request(app) + .get(`${getEndpoint}?state=APPROVED`) + .end((err, res) => { + if (err) return done(err); + expect(res.statusCode).to.equal(200); + expect(res.body.message).to.equal(REQUEST_FETCHED_SUCCESSFULLY); + expect(res.body.data.length).to.equal(1); + expect(res.body.data[0].type).to.equal(REQUEST_TYPE.ONBOARDING); + expect(res.body.data[0].state).to.equal(REQUEST_STATE.APPROVED); + return done(); + }); + }); + }); +}); diff --git a/test/integration/requests.test.ts b/test/integration/requests.test.ts index 18348df93..eed3cb3a6 100644 --- a/test/integration/requests.test.ts +++ b/test/integration/requests.test.ts @@ -356,7 +356,7 @@ describe("/requests OOO", function () { .end(function (err, res) { expect(res).to.have.status(400); expect(res.body.error).to.equal("Bad Request"); - expect(res.body.message).to.equal('"type" must be one of [OOO, EXTENSION, TASK, ALL]'); + expect(res.body.message).to.equal('"type" must be one of [OOO, EXTENSION, TASK, ALL, ONBOARDING]'); done(); }); }); diff --git a/test/unit/models/requests.test.ts b/test/unit/models/requests.test.ts index 51107008f..954024c86 100644 --- a/test/unit/models/requests.test.ts +++ b/test/unit/models/requests.test.ts @@ -130,7 +130,7 @@ describe("models/oooRequests", () => { it("Should return a list of all the requests by specific user ", async () => { const oooRequestBodyData = { ...createOooRequests, requestedBy: testUserId }; await createRequest(oooRequestBodyData); - const query = { dev: "true", requestedBy: userData[16].username }; + const query = { requestedBy: userData[16].username }; const oooRequestData = await getRequests(query); expect(oooRequestData.allRequests).to.have.lengthOf(1); expect(oooRequestData.allRequests[0].requestedBy).to.be.equal(testUserId);