Skip to content
Open
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
5 changes: 5 additions & 0 deletions cypress/integration/EndpointParamChecks/ExportEvent.feature
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,8 @@ Feature: The user should be able to export events
Given the "inclusion" input is set to "CHILDREN"
When the export form is submitted
Then the download request is sent with the right parameters

Scenario: The export request fails
Given the event export request will fail
When the export form is submitted
Then a warning alert is shown with the error message
26 changes: 26 additions & 0 deletions cypress/integration/EndpointParamChecks/ExportEvent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const orgUnitsRootApi =
/\/organisationUnits\?filter=level:eq:1&fields=id,path,displayName,children::isNotEmpty&paging=false/
const programsApi = /\/programs\?/
const programStagesApi = /\/programs\/[a-zA-Z0-9]+/
const eventsApi = /\/api\/tracker\/events/

Before(() => {
cy.server()
Expand All @@ -30,6 +31,11 @@ Before(() => {
url: programStagesApi,
fixture: 'programStages',
}).as('programStagesXHR')

cy.intercept(eventsApi, {
statusCode: 200,
body: '{}',
}).as('downloadXHR')
})

Given('the user is on the event export page', () => {
Expand Down Expand Up @@ -72,6 +78,26 @@ When('the export form is submitted', () => {
win.locationAssign = locationAssignStub
cy.get('[data-test="input-export-submit"]').click()
})
cy.wait('@downloadXHR')
})

Given('the event export request will fail', () => {
cy.intercept(eventsApi, {
statusCode: 409,
body: {
httpStatus: 'Conflict',
httpStatusCode: 409,
status: 'ERROR',
message: 'Could not find an id for CODE on Data Element.',
},
}).as('downloadXHR')
})

Then('a warning alert is shown with the error message', () => {
cy.get('[data-test="input-form-alerts"]').should(
'contain',
'Could not find an id for CODE on Data Element.'
)
})

Then('the download request is sent with the right parameters', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/pages/EventExport/EventExport.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
IdScheme,
defaultIdSchemeOption,
formatNoXmlOptions,
FormAlerts,
} from '../../components/Inputs/index.js'
import { jsDateToISO8601 } from '../../utils/helper.js'
import { onExport, validate } from './form-helper.js'
Expand Down Expand Up @@ -88,8 +89,9 @@ const EventExport = () => {
validate={validate}
subscription={{
values: true,
submitError: true,
}}
render={({ handleSubmit, form, values }) => (
render={({ handleSubmit, form, values, submitError }) => (
<form onSubmit={handleSubmit}>
<BasicOptions>
<OrgUnitTree multiSelect={false} />
Expand Down Expand Up @@ -121,6 +123,7 @@ const EventExport = () => {
label={i18n.t('Export events')}
disabled={!exportEnabled}
/>
<FormAlerts alerts={submitError} />
</form>
)}
/>
Expand Down
48 changes: 42 additions & 6 deletions src/pages/EventExport/form-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@ import {
DATE_AFTER_VALIDATOR,
} from '../../components/DatePicker/DatePickerField.jsx'
import { ALL_VALUE } from '../../hooks/useProgramStages.js'
import { locationAssign, pathToId } from '../../utils/helper.js'
import { FORM_ERROR } from '../../utils/final-form.js'
import {
genericErrorMessage,
locationAssign,
pathToId,
} from '../../utils/helper.js'

const exportErrorAlert = (message) => ({
[FORM_ERROR]: [
{
id: `event-export-error-${Date.now()}`,
warning: true,
message,
},
],
})

const onExport = (baseUrl, setExportEnabled) => (values) => {
const onExport = (baseUrl, setExportEnabled) => async (values) => {
setExportEnabled(false)

const {
Expand Down Expand Up @@ -44,11 +59,32 @@ const onExport = (baseUrl, setExportEnabled) => (values) => {
.filter((s) => s != '')
.join('&')
const url = `${apiBaseUrl}${endpoint}.${endpointExtension}?${downloadUrlParams}`
locationAssign(url)
setExportEnabled(true)

// log for debugging purposes
console.log('event-export:', { url, params: downloadUrlParams })
try {
const response = await fetch(url, { credentials: 'include' })

if (!response.ok) {
let message = genericErrorMessage
try {
const body = await response.json()
message = body.message || message
} catch (e) {
// response body wasn't JSON, fall back to the generic message
console.error('event-export: failed to parse error response', e)
}
return exportErrorAlert(message)
}

locationAssign(url)

// log for debugging purposes
console.log('event-export:', { url, params: downloadUrlParams })
} catch (e) {
console.error('event-export: request failed', e)
return exportErrorAlert(genericErrorMessage)
} finally {
setExportEnabled(true)
}
}

const validate = (values) => ({
Expand Down
Loading