Skip to content
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
92 changes: 91 additions & 1 deletion src/api/protectedApiClient.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,99 @@
import AppAxiosInstance from '../auth/axios';

export interface ProtectedApiClient {
readonly makeReservation: (blockId: number, teamId?: number) => Promise<void>;
readonly completeReservation: (
blockId: number,
teamId?: number,
) => Promise<void>;
readonly releaseReservation: (blockId: number) => Promise<void>;
readonly uncompleteReservation: (blockId: number) => Promise<void>;
readonly markReservationForQa: (blockId: number) => Promise<void>;
readonly passReservationQa: (blockId: number) => Promise<void>;
readonly failReservationQa: (blockId: number) => Promise<void>;
readonly changePassword: (request: {
currentPassword: string;
newPassword: string;
}) => Promise<void>;
}

enum ProtectedApiClientRoutes {
export enum ProtectedApiClientRoutes {
MAKE_RESERVATION = '/api/v1/protected/reservations/reserve',
COMPLETE_RESERVATION = '/api/v1/protected/reservations/complete',
RELEASE_RESERVATION = '/api/v1/protected/reservations/release',
CHANGE_PASSWORD = '/api/v1/protected/user/change_password',
}

export enum AdminApiClientRoutes {
UNCOMPLETE_RESERVATION = '/api/v1/protected/reservations/uncomplete',
MARK_RESERVATION_FOR_QA = '/api/v1/protected/reservations/qa',
PASS_RESERVATION_QA = '/api/v1/protected/reservations/pass_qa',
FAIL_RESERVATION_QA = '/api/v1/protected/reservations/fail_qa',
}

const makeReservation = (blockId: number, teamId?: number): Promise<void> => {
return AppAxiosInstance.post(ProtectedApiClientRoutes.MAKE_RESERVATION, {
block_id: blockId,
team_id: teamId,
})
.then((r) => r.data)
.catch((e) => e);
};

const completeReservation = (
blockId: number,
teamId?: number,
): Promise<void> => {
return AppAxiosInstance.post(ProtectedApiClientRoutes.COMPLETE_RESERVATION, {
block_id: blockId,
team_id: teamId,
})
.then((r) => r.data)
.catch((e) => e);
};

const releaseReservation = (blockId: number): Promise<void> => {
return AppAxiosInstance.post(ProtectedApiClientRoutes.RELEASE_RESERVATION, {
block_id: blockId,
})
.then((r) => r.data)
.catch((e) => e);
};

// Admin routes

const uncompleteReservation = (blockId: number): Promise<void> => {
return AppAxiosInstance.post(AdminApiClientRoutes.UNCOMPLETE_RESERVATION, {
block_id: blockId,
})
.then((r) => r.data)
.catch((e) => e);
};

const markReservationForQa = (blockId: number): Promise<void> => {
return AppAxiosInstance.post(AdminApiClientRoutes.MARK_RESERVATION_FOR_QA, {
block_id: blockId,
})
.then((r) => r.data)
.catch((e) => e);
};

const passReservationQa = (blockId: number): Promise<void> => {
return AppAxiosInstance.post(AdminApiClientRoutes.PASS_RESERVATION_QA, {
block_id: blockId,
})
.then((r) => r.data)
.catch((e) => e);
};

const failReservationQa = (blockId: number): Promise<void> => {
return AppAxiosInstance.post(AdminApiClientRoutes.FAIL_RESERVATION_QA, {
block_id: blockId,
})
.then((r) => r.data)
.catch((e) => e);
};

const changePassword = (request: {
currentPassword: string;
newPassword: string;
Expand All @@ -24,6 +107,13 @@ const changePassword = (request: {
};

const Client: ProtectedApiClient = Object.freeze({
makeReservation,
completeReservation,
releaseReservation,
uncompleteReservation,
markReservationForQa,
passReservationQa,
failReservationQa,
changePassword,
});

Expand Down
135 changes: 135 additions & 0 deletions src/api/test/protectedApiClient.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import nock from 'nock';
import ProtectedApiClient, {
ProtectedApiClientRoutes,
AdminApiClientRoutes,
} from '../protectedApiClient';

const BASE_URL = 'http://localhost';

// TODO: handle invalid cases
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a ticket for this on Monday on the bugs/tech debt section? Don't want this to get lost. Or finish it as part of this PR.


describe('Protected Client Tests', () => {
describe('makeReservation', () => {
it('makes the right request with team', async () => {
const response = '';

nock(BASE_URL)
.post(ProtectedApiClientRoutes.MAKE_RESERVATION)
.reply(200, response);

const result = await ProtectedApiClient.makeReservation(1, 1);

expect(result).toEqual(response);
});

it('makes the right request without team', async () => {
const response = '';

nock(BASE_URL)
.post(ProtectedApiClientRoutes.MAKE_RESERVATION)
.reply(200, response);

const result = await ProtectedApiClient.makeReservation(1);

expect(result).toEqual(response);
});
});

describe('completeReservation', () => {
it('makes the right request with team', async () => {
const response = '';

nock(BASE_URL)
.post(ProtectedApiClientRoutes.COMPLETE_RESERVATION)
.reply(200, response);

const result = await ProtectedApiClient.completeReservation(2, 2);

expect(result).toEqual(response);
});

it('makes the right request without team', async () => {
const response = '';

nock(BASE_URL)
.post(ProtectedApiClientRoutes.COMPLETE_RESERVATION)
.reply(200, response);

const result = await ProtectedApiClient.completeReservation(2);

expect(result).toEqual(response);
});
});

describe('releaseReservation', () => {
it('makes the right request', async () => {
const response = '';

nock(BASE_URL)
.post(ProtectedApiClientRoutes.RELEASE_RESERVATION)
.reply(200, response);

const result = await ProtectedApiClient.releaseReservation(2);

expect(result).toEqual(response);
});
});
});

describe('Admin Protected Client Routes', () => {
describe('uncompleteReservation', () => {
it('makes the right request', async () => {
const response = '';

nock(BASE_URL)
.post(AdminApiClientRoutes.UNCOMPLETE_RESERVATION)
.reply(200, response);

const result = await ProtectedApiClient.uncompleteReservation(5);

expect(result).toEqual(response);
});
});

describe('markReservationForQa', () => {
it('makes the right request', async () => {
const response = '';

nock(BASE_URL)
.post(AdminApiClientRoutes.MARK_RESERVATION_FOR_QA)
.reply(200, response);

const result = await ProtectedApiClient.markReservationForQa(6);

expect(result).toEqual(response);
});
});

describe('passReservationQa', () => {
it('makes the right request', async () => {
const response = '';

nock(BASE_URL)
.post(AdminApiClientRoutes.PASS_RESERVATION_QA)
.reply(200, response);

const result = await ProtectedApiClient.passReservationQa(6);

expect(result).toEqual(response);
});
});

describe('failReservationQa', () => {
it('makes the right request', async () => {
const response = '';

nock(BASE_URL)
.post(AdminApiClientRoutes.FAIL_RESERVATION_QA)
.reply(200, response);

const result = await ProtectedApiClient.failReservationQa(7);

expect(result).toEqual(response);
});
});
});