Skip to content

Commit

Permalink
fix: Add support to fetch onboarding extension by requestedBy field
Browse files Browse the repository at this point in the history
- Add requestedBy field in the fetch query
- Add tests to verify the chanes
  • Loading branch information
pankajjs committed Jan 7, 2025
1 parent 07b8720 commit a292d17
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 5 deletions.
2 changes: 1 addition & 1 deletion middlewares/validators/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion models/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const getRequests = async (query: any) => {

if (requestedBy) {
const requestedByUserId = await getUserId(requestedBy);
requestQuery = requestQuery.where("requestedBy", "==", requestedByUserId);
requestQuery = requestQuery.where("requestedBy", "in", [requestedByUserId, requestedBy]);
}
if (type) {
requestQuery = requestQuery.where("type", "==", type);
Expand Down
83 changes: 81 additions & 2 deletions test/integration/onboardingExtension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -299,4 +300,82 @@ describe("/requests Onboarding Extension", () => {
.to.equal(new Date(currentDate + (body.numberOfDays*24*60*60*1000)).toDateString());
})
})
});

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`)
.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();
});
});
});
});
2 changes: 1 addition & 1 deletion test/integration/requests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down

0 comments on commit a292d17

Please sign in to comment.