diff --git a/src/App/tests/AppNoBackend.test.tsx b/src/App/tests/AppNoBackend.test.tsx index 3c4823989d..6209c1c8af 100644 --- a/src/App/tests/AppNoBackend.test.tsx +++ b/src/App/tests/AppNoBackend.test.tsx @@ -15,6 +15,7 @@ jest.mock('../../configs/env', () => ({ ',' ), HIDDEN_PLAN_STATUSES: ['retired'], + OPENSRP_API_BASE_URL: 'https://test.smartregister.org/opensrp/rest/', SUPERSET_DYNAMIC_MDA_REPORTING_JURISDICTIONS_DATA_SLICES: '1338', SUPERSET_IRS_LITE_REPORTING_JURISDICTIONS_DATA_SLICES: '11, 12', SUPERSET_IRS_MOPUP_REPORTING_JURISDICTIONS_DATA_SLICES: '0', diff --git a/src/constants.tsx b/src/constants.tsx index 69eaff8ffe..d2cfe1dc87 100644 --- a/src/constants.tsx +++ b/src/constants.tsx @@ -171,7 +171,7 @@ export const OPENSRP_LOCATIONS_BY_PLAN = 'plans/findLocationNames'; export const OPENSRP_FILE_UPLOAD_HISTORY_ENDPOINT = 'upload/history'; export const OPENSRP_EVENT_PARAM_VALUE = 'Child Registration'; export const OPENSRP_UPLOAD_ENDPOINT = 'upload'; -export const OPENSRP_UPLOAD_DOWNLOAD_ENDPOINT = 'upload/download'; +export const OPENSRP_UPLOAD_DOWNLOAD_ENDPOINT = 'multimedia/media'; export const EVENT_NAME_PARAM = 'event_name'; export const LOCATION_ID_PARAM = 'location_id'; export const TEAM_ID_PARAM = 'team_id'; diff --git a/src/containers/pages/MDAPoint/ClientListView/helpers/serviceHooks.ts b/src/containers/pages/MDAPoint/ClientListView/helpers/serviceHooks.ts index 83e8a23022..cd8f818674 100644 --- a/src/containers/pages/MDAPoint/ClientListView/helpers/serviceHooks.ts +++ b/src/containers/pages/MDAPoint/ClientListView/helpers/serviceHooks.ts @@ -82,9 +82,13 @@ export const handleDownload = async ( name: string, endpoint: string, params?: Dictionary, + baseURL?: string, serviceClass: any = OpenSRPService ) => { - const downloadService = new serviceClass(endpoint); + let downloadService = new serviceClass(endpoint); + if (baseURL) { + downloadService = new serviceClass(endpoint, baseURL); + } downloadService .readFile(id, params) .then((res: typeof Blob) => { diff --git a/src/containers/pages/MDAPoint/ClientListView/helpers/tests/serviceHooks.test.tsx b/src/containers/pages/MDAPoint/ClientListView/helpers/tests/serviceHooks.test.tsx index a0271f6ca5..1644bee16d 100644 --- a/src/containers/pages/MDAPoint/ClientListView/helpers/tests/serviceHooks.test.tsx +++ b/src/containers/pages/MDAPoint/ClientListView/helpers/tests/serviceHooks.test.tsx @@ -78,7 +78,7 @@ describe('src/containers/pages/ClientListView/helpers/servicehooks', () => { }; }); - handleDownload('123', 'student', OPENSRP_UPLOAD_DOWNLOAD_ENDPOINT, undefined, mockClass).catch( + handleDownload('123', 'student', OPENSRP_UPLOAD_DOWNLOAD_ENDPOINT, {}, '', mockClass).catch( e => { throw e; } @@ -86,7 +86,7 @@ describe('src/containers/pages/ClientListView/helpers/servicehooks', () => { await flushPromises(); // calls the correct endpoint - expect(mockClass).toHaveBeenCalledWith('upload/download'); + expect(mockClass).toHaveBeenCalledWith(OPENSRP_UPLOAD_DOWNLOAD_ENDPOINT); // Uses the correct service method expect(mockReadFile).toHaveBeenCalledTimes(1); diff --git a/src/containers/pages/MDAPoint/ClientListView/index.tsx b/src/containers/pages/MDAPoint/ClientListView/index.tsx index ff33ff445f..265fc76fab 100644 --- a/src/containers/pages/MDAPoint/ClientListView/index.tsx +++ b/src/containers/pages/MDAPoint/ClientListView/index.tsx @@ -19,7 +19,7 @@ import { renderInFilterFactory, } from '../../../../components/Table/DrillDownFilters/utils'; import { NoDataComponent } from '../../../../components/Table/NoDataComponent'; -import { CLIENT_LABEL } from '../../../../configs/env'; +import { CLIENT_LABEL, OPENSRP_API_BASE_URL } from '../../../../configs/env'; import { ADD_NEW_CSV, CLIENTS_TITLE, @@ -60,6 +60,7 @@ const filesArraySelector = makeFilesArraySelector(); /** interface to describe props for ClientListView component */ export interface ClientListViewProps { + baseURL: string; fetchFilesActionCreator: typeof fetchFiles; files: File[]; serviceClass: typeof OpenSRPService; @@ -67,6 +68,7 @@ export interface ClientListViewProps { } /** default props for ClientListView component */ export const defaultClientListViewProps: ClientListViewProps = { + baseURL: OPENSRP_API_BASE_URL.replace('rest/', ''), clientLabel: CLIENT_LABEL, fetchFilesActionCreator: fetchFiles, files: [], @@ -74,7 +76,7 @@ export const defaultClientListViewProps: ClientListViewProps = { }; export const ClientListView = (props: ClientListViewProps & RouteComponentProps) => { - const { location, files, clientLabel } = props; + const { location, files, clientLabel, baseURL } = props; const [loading, setLoading] = useState(false); @@ -100,6 +102,9 @@ export const ClientListView = (props: ClientListViewProps & RouteComponentProps) { Cell: (fileObj: Cell) => { const original = fileObj.row.original; + const urlParams = { + 'dynamic-media-directory': true, + }; return ( {fileObj.value}   @@ -107,7 +112,13 @@ export const ClientListView = (props: ClientListViewProps & RouteComponentProps) className="btn btn-link" // tslint:disable-next-line: jsx-no-lambda onClick={() => - handleDownload(original.url, original.fileName, OPENSRP_UPLOAD_DOWNLOAD_ENDPOINT) + handleDownload( + original.identifier, + original.fileName, + OPENSRP_UPLOAD_DOWNLOAD_ENDPOINT, + urlParams, + baseURL + ) } > {`(${DOWNLOAD})`} diff --git a/src/containers/pages/MDAPoint/ClientListView/tests/fixtures.ts b/src/containers/pages/MDAPoint/ClientListView/tests/fixtures.ts index 8fc37ccc58..bc2a010ee6 100644 --- a/src/containers/pages/MDAPoint/ClientListView/tests/fixtures.ts +++ b/src/containers/pages/MDAPoint/ClientListView/tests/fixtures.ts @@ -83,3 +83,27 @@ export const files = [ 'https://user-images.githubusercontent.com/12836913/81139056-3be46680-8f19-11ea-92f8-fb1ab7877626.png', }, ]; + +export const files2 = [ + { + fileName: 'Enhletjeni cc - Child roster test QA.csv', + identifier: '8bfee034-f172-4c3c-afd7-aeba3f72acd5', + providerID: '301faf1d-6cfb-4ed1-997c-46b44146ab05', + uploadDate: 'Sep 14, 2021, 10:42:42 AM', + url: '8bfee034-f172-4c3c-afd7-aeba3f72acd5.csv', + }, + { + fileName: 'Test Lomahasha - Emafusini Nazarene D - Child roster test QA.csv', + identifier: '08b8b86b-0a74-494a-93fb-fd6a5f188d01', + providerID: '301faf1d-6cfb-4ed1-997c-46b44146ab05', + uploadDate: 'Sep 7, 2021, 9:05:21 AM', + url: '08b8b86b-0a74-494a-93fb-fd6a5f188d01.csv', + }, + { + fileName: 'reuben_client_upload_sigcineni_catholic.csv', + identifier: '1b0f7197-5dee-4c5b-a939-2957d0d403c1', + providerID: '301faf1d-6cfb-4ed1-997c-46b44146ab05', + uploadDate: 'Aug 31, 2021, 12:40:41 PM', + url: '1b0f7197-5dee-4c5b-a939-2957d0d403c1.csv', + }, +]; diff --git a/src/containers/pages/MDAPoint/ClientListView/tests/index.test.tsx b/src/containers/pages/MDAPoint/ClientListView/tests/index.test.tsx index af904f2a5d..8bd1e1f0d4 100644 --- a/src/containers/pages/MDAPoint/ClientListView/tests/index.test.tsx +++ b/src/containers/pages/MDAPoint/ClientListView/tests/index.test.tsx @@ -8,6 +8,7 @@ import { Helmet } from 'react-helmet'; import { Provider } from 'react-redux'; import { Router } from 'react-router'; import ConnectedClientListView, { ClientListView } from '../'; +import { OPENSRP_API_BASE_URL } from '../../../../../configs/env'; import { STUDENTS_TITLE } from '../../../../../configs/lang'; import { QUERY_PARAM_TITLE } from '../../../../../constants'; import store from '../../../../../store'; @@ -17,6 +18,10 @@ import reducer, { removeFilesAction, } from '../../../../../store/ducks/opensrp/clientfiles'; import * as fixtures from './fixtures'; + +/* tslint:disable-next-line no-var-requires */ +const fetch = require('jest-fetch-mock'); + reducerRegistry.register(reducerName, reducer); const history = createBrowserHistory(); jest.mock('../../../../../configs/env'); @@ -231,4 +236,44 @@ describe('containers/pages/MDAPoints/ClientListView', () => { expect(wrapper.find('DrillDownTable').props().data).toEqual([fixtures.files[2]]); wrapper.unmount(); }); + + it('CSV download works', async () => { + store.dispatch(fetchFiles(fixtures.files2)); + const mock: any = jest.fn(); + const props = { + baseURL: OPENSRP_API_BASE_URL.replace('rest/', ''), + history, + location: mock, + match: mock, + }; + const wrapper = mount( + + + + + + ); + await act(async () => { + await flushPromises(); + wrapper.update(); + }); + wrapper + .find('.btn-link') + .at(0) + .simulate('click'); + await act(async () => { + await flushPromises(); + }); + expect(fetch.mock.calls[1]).toEqual([ + 'https://test.smartregister.org/opensrp/multimedia/media/8bfee034-f172-4c3c-afd7-aeba3f72acd5?dynamic-media-directory=true', + { + headers: { + accept: 'application/json', + authorization: 'Bearer null', + 'content-type': 'application/json;charset=UTF-8', + }, + method: 'GET', + }, + ]); + }); });