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

update frontend to use POST instead of GET to match backend api for c… #33801

Merged
merged 2 commits into from
Jan 6, 2025
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
54 changes: 17 additions & 37 deletions src/applications/caregivers/actions/fetchFacilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,13 @@ const formatAddress = address => {
};
};

const formatQueryParams = ({
lat,
long,
radius,
page,
perPage,
facilityIds,
}) => {
const formatFacilityIdParams = () => {
let facilityIdParams = '';
if (facilityIds.length > 0) {
facilityIdParams = `facilityIds=${facilityIds.join(',')}`;
}

return facilityIdParams;
};

const params = [
lat ? `lat=${lat}` : null,
long ? `long=${long}` : null,
radius ? `radius=${radius}` : null,
page ? `page=${page}` : null,
perPage ? `per_page=${perPage}` : null,
formatFacilityIdParams() || null,
];

let filteredParams = params.filter(Boolean);
if (filteredParams.length > 0) {
filteredParams = `&${filteredParams.join('&')}`;
const formatFacilityIds = facilityIds => {
let facilityIdList = '';
if (facilityIds.length > 0) {
facilityIdList = `${facilityIds.join(',')}`;
}

return filteredParams;
return facilityIdList;
};

export const fetchFacilities = async ({
Expand All @@ -58,22 +33,27 @@ export const fetchFacilities = async ({
page = null,
perPage = null,
facilityIds = [],
type = 'health',
}) => {
const baseUrl = `${
const url = `${
environment.API_URL
}/v0/caregivers_assistance_claims/facilities?type=health`;
}/v0/caregivers_assistance_claims/facilities`;

const queryParams = formatQueryParams({
const body = {
type,
lat,
long,
radius,
page,
perPage,
facilityIds,
});
facilityIds: formatFacilityIds(facilityIds),
};

const requestUrl = `${baseUrl}${queryParams}`;
const fetchRequest = apiRequest(requestUrl);
const fetchRequest = apiRequest(url, {
method: 'POST',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' },
});

return fetchRequest
.then(response => {
Expand Down
4 changes: 2 additions & 2 deletions src/applications/caregivers/tests/e2e/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const setupPerTest = () => {
cy.intercept('GET', '/v0/feature_toggles?*', featureToggles);
cy.intercept('POST', 'v0/form1010cg/attachments', mockUpload);
cy.intercept(
'GET',
'/v0/caregivers_assistance_claims/facilities?*',
'POST',
'/v0/caregivers_assistance_claims/facilities',
mockFacilities,
).as('getFacilities');
cy.intercept('POST', '/v0/caregivers_assistance_claims', mockSubmission);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
import content from '../../../locales/en/content.json';

describe('CG fetchFacilities action', () => {
const method = 'POST';
const headers = { 'Content-Type': 'application/json' };
const lat = 1;
const long = 2;
const perPage = 5;
Expand All @@ -31,30 +33,70 @@ describe('CG fetchFacilities action', () => {
it('calls correct url when all params are passed', async () => {
await fetchFacilities({ long, lat, perPage, radius, page, facilityIds });

const expectedBody = JSON.stringify({
type: 'health',
lat,
long,
radius,
page,
perPage,
facilityIds: `${facilityIds[0]},${facilityIds[1]}`,
});

const expectedUrl = `${
environment.API_URL
}/v0/caregivers_assistance_claims/facilities?type=health&lat=${lat}&long=${long}&radius=${radius}&page=${page}&per_page=${perPage}&facilityIds=${
facilityIds[0]
},${facilityIds[1]}`;
sinon.assert.calledWith(apiRequestStub, expectedUrl);
}/v0/caregivers_assistance_claims/facilities`;
sinon.assert.calledWith(apiRequestStub, expectedUrl, {
method,
headers,
body: expectedBody,
});
});

it('calls url without params when no params are passed', async () => {
await fetchFacilities({});

const expectedBody = JSON.stringify({
type: 'health',
lat: null,
long: null,
radius: null,
page: null,
perPage: null,
facilityIds: '',
});

const expectedUrl = `${
environment.API_URL
}/v0/caregivers_assistance_claims/facilities?type=health`;
sinon.assert.calledWith(apiRequestStub, expectedUrl);
}/v0/caregivers_assistance_claims/facilities`;
sinon.assert.calledWith(apiRequestStub, expectedUrl, {
method,
headers,
body: expectedBody,
});
});

it('formats facility ids correctly when only one facility id', async () => {
await fetchFacilities({ facilityIds: ['1'] });

const expectedBody = JSON.stringify({
type: 'health',
lat: null,
long: null,
radius: null,
page: null,
perPage: null,
facilityIds: '1',
});

const expectedUrl = `${
environment.API_URL
}/v0/caregivers_assistance_claims/facilities?type=health&facilityIds=1`;
sinon.assert.calledWith(apiRequestStub, expectedUrl);
}/v0/caregivers_assistance_claims/facilities`;
sinon.assert.calledWith(apiRequestStub, expectedUrl, {
method,
headers,
body: expectedBody,
});
});

it('formats facility addresses', async () => {
Expand Down
Loading