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

1664 update roster csv download endpoint #1665

Merged
merged 2 commits into from
Sep 16, 2021
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
1 change: 1 addition & 0 deletions src/App/tests/AppNoBackend.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ 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;
}
);
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);
Expand Down
17 changes: 14 additions & 3 deletions src/containers/pages/MDAPoint/ClientListView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -60,21 +60,23 @@ const filesArraySelector = makeFilesArraySelector();

/** interface to describe props for ClientListView component */
export interface ClientListViewProps {
baseURL: string;
fetchFilesActionCreator: typeof fetchFiles;
files: File[];
serviceClass: typeof OpenSRPService;
clientLabel: string;
}
/** default props for ClientListView component */
export const defaultClientListViewProps: ClientListViewProps = {
baseURL: OPENSRP_API_BASE_URL.replace('rest/', ''),
clientLabel: CLIENT_LABEL,
fetchFilesActionCreator: fetchFiles,
files: [],
serviceClass: OpenSRPService,
};

export const ClientListView = (props: ClientListViewProps & RouteComponentProps) => {
const { location, files, clientLabel } = props;
const { location, files, clientLabel, baseURL } = props;

const [loading, setLoading] = useState(false);

Expand All @@ -100,14 +102,23 @@ export const ClientListView = (props: ClientListViewProps & RouteComponentProps)
{
Cell: (fileObj: Cell<File>) => {
const original = fileObj.row.original;
const urlParams = {
'dynamic-media-directory': true,
};
return (
<span key={original.identifier}>
{fileObj.value} &nbsp;
<button
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})`}
Expand Down
24 changes: 24 additions & 0 deletions src/containers/pages/MDAPoint/ClientListView/tests/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
];
45 changes: 45 additions & 0 deletions src/containers/pages/MDAPoint/ClientListView/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');
Expand Down Expand Up @@ -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(
<Provider store={store}>
<Router history={history}>
<ConnectedClientListView {...props} />
</Router>
</Provider>
);
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',
},
]);
});
});