Skip to content

Commit

Permalink
feat: move Event import to new tracker endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
kabaros committed Feb 23, 2024
1 parent 90a4073 commit 32c401b
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 24 deletions.
76 changes: 70 additions & 6 deletions src/components/JobSummary/SingleSummary/SingleSummary.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const SingleSummary = ({
status,
description,
conflicts,
validationReport,
id,
}) => (
<div>
Expand All @@ -29,7 +30,7 @@ const SingleSummary = ({
name="summary"
>
<>
{status && (
{status && description && (
<SingleStatusTable
description={description}
status={status}
Expand All @@ -47,16 +48,61 @@ const SingleSummary = ({
</TableHead>
<TableBody>
<TableRow>
<TableCell>{importCount.imported}</TableCell>
<TableCell>{importCount.deleted}</TableCell>
<TableCell>{importCount.ignored}</TableCell>
<TableCell>{importCount.updated}</TableCell>
<TableCell>{importCount.total}</TableCell>
<TableCell>
{importCount?.imported ?? '0'}
</TableCell>
<TableCell>{importCount?.deleted}</TableCell>
<TableCell>{importCount?.ignored}</TableCell>
<TableCell>{importCount?.updated}</TableCell>
<TableCell>{importCount?.total}</TableCell>
</TableRow>
</TableBody>
</Table>
</>
</FormField>
{validationReport?.errorReports && (
<FormField
label={`${i18n.t('Reports')}`}
dataTest="tracker-summary-reports"
name="tracker-reports"
>
<Table>
<TableHead>
<TableRowHead>
<TableCellHead>{i18n.t('UID')}</TableCellHead>
<TableCellHead>
{i18n.t('Error Code')}
</TableCellHead>
<TableCellHead>{i18n.t('Message')}</TableCellHead>
<TableCellHead>
{i18n.t('Tracker Type')}
</TableCellHead>
{/* <TableCellHead>{i18n.t('')}</TableCellHead> */}
</TableRowHead>
</TableHead>
<TableBody>
{validationReport.errorReports.map((c, i) => (
<TableRow
key={`job-summary-report-${c.object}-${i}`}
>
<TableCell>{c.uid}</TableCell>
<TableCell>
<a
target="_blank"
rel="noreferrer"
href="https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/tracker.html#webapi_nti_error_codes"
>
{c.warningCode ?? c.errorCode}
</a>
</TableCell>
<TableCell>{c.message}</TableCell>
<TableCell>{c.trackerType}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</FormField>
)}
{conflicts && (
<FormField
label={`${i18n.t('Conflicts')}`}
Expand Down Expand Up @@ -107,6 +153,24 @@ SingleSummary.propTypes = {
id: PropTypes.string,
importType: PropTypes.string,
status: PropTypes.string,
validationReport: PropTypes.shape({
errorReports: PropTypes.arrayOf(
PropTypes.shape({
errorCode: PropTypes.string,
message: PropTypes.string,
trackerType: PropTypes.string,
uid: PropTypes.string,
})
),
warningReports: PropTypes.arrayOf(
PropTypes.shape({
errorCode: PropTypes.string,
message: PropTypes.string,
trackerType: PropTypes.string,
uid: PropTypes.string,
})
),
}),
}

export { SingleSummary }
1 change: 1 addition & 0 deletions src/components/JobSummary/Summary/Summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const Summary = ({ summary, importType }) => {
importCount={importCount}
status={summary.status}
description={summary.description}
validationReport={summary.validationReport}
conflicts={
summary.conflicts &&
(summary.conflicts.length || null) &&
Expand Down
36 changes: 34 additions & 2 deletions src/hooks/useTasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ const jobSummaryQuery = {
},
}

const trackerEventQuery = {
events: {
resource: 'tracker/jobs/',
id: ({ taskId }) => `${taskId}`,
},
}

const trackerSummaryQuery = {
summary: {
resource: 'tracker/jobs/',
id: ({ taskId }) => `${taskId}/report`,
},
}

const defaultTasks = {
data: {},
event: {},
Expand All @@ -39,13 +53,20 @@ const createFetchEvents =
}

const newTask = { ...task }
const { events, error } = await engine.query(jobEventQuery, {
const query =
task.importType === 'TRACKER_IMPORT_JOB'
? trackerEventQuery
: jobEventQuery

const response = await engine.query(query, {
variables: {
type: task.importType,
taskId: task.id,
},
})

const { events, error } = response

if (error) {
console.error('fetchEvents error: ', error)
return
Expand Down Expand Up @@ -90,13 +111,24 @@ const createFetchEvents =
const createFetchSummary = (engine, setTasks) => async (type, id, task) => {
const newTask = { ...task }

const { summary, error } = await engine.query(jobSummaryQuery, {
// we could still keep one query here (the jobs query), but tracker provides a facade to these
// and even though this branches the logic unnecessarily, we should stick to
// trackers' endpoint for tracker imports and they could abstract some job-related details
// more details here: https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/tracker.html#webapi_nti_import_summary
const query =
task.importType === 'TRACKER_IMPORT_JOB'
? trackerSummaryQuery
: jobSummaryQuery

const response = await engine.query(query, {
variables: {
type: task.importType,
taskId: task.id,
},
})

const { summary, error } = response

if (error) {
console.error('fetchSummary error: ', error)
return
Expand Down
4 changes: 2 additions & 2 deletions src/pages/EventImport/EventImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
import {
FileUpload,
Format,
formatOptions,
defaultFormatOption,
DataElementIdScheme,
defaultDataElementIdSchemeOption,
Expand All @@ -26,6 +25,7 @@ import {
defaultOrgUnitIdSchemeOption,
ImportButtonStrip,
FormAlerts,
formatNoXmlOptions,
} from '../../components/Inputs/index.js'
import { TaskContext, getNewestTask } from '../../contexts/index.js'
import { getPrevJobDetails } from '../../utils/helper.js'
Expand Down Expand Up @@ -102,7 +102,7 @@ const EventImport = () => {
)}
/>
<Format
availableFormats={formatOptions}
availableFormats={formatNoXmlOptions}
type="import"
/>
</BasicOptions>
Expand Down
17 changes: 6 additions & 11 deletions src/pages/EventImport/form-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,18 @@ const onImport =
format,
dataElementIdScheme,
orgUnitIdScheme,
eventIdScheme,
idScheme,
} = values

// send xhr
const apiBaseUrl = `${baseUrl}/api/`
const endpoint = 'events.json'
const apiBaseUrl = `${baseUrl}/api/tracker`
const endpoint = ''
const params = [
'skipFirst=true',
`async=${isAsync}`,
`dryRun=${dryRun}`,
`importMode=${dryRun ? 'validate' : 'commit'}`,
`dataElementIdScheme=${dataElementIdScheme}`,
`orgUnitIdScheme=${orgUnitIdScheme}`,
`eventIdScheme=${eventIdScheme}`,
`idScheme=${idScheme}`,
`payloadFormat=${format}`,
].join('&')
const url = `${apiBaseUrl}${endpoint}?${params}`

Expand All @@ -36,11 +32,10 @@ const onImport =
url,
file: files[0],
format: format,
type: 'EVENT_IMPORT',
isAsync: isAsync,
type: 'TRACKER_IMPORT_JOB',
isAsync,
setProgress,
addEntry: (id, entry) =>
addTask('event', id, { ...entry, jobDetails: values }),
addEntry: (id, entry) => addTask('event', id, { ...entry, jobDetails: values })
})
return jobStartedMessage
} catch (e) {
Expand Down
9 changes: 6 additions & 3 deletions src/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ const uploadFile = ({
url,
upload: file,
type,
onResponse: ({ error, id, msg, typeReports }) => {
onResponse: (response) => {
const { error, id, msg, typeReports } = response
let entry
if (!isAsync) {
// we are done
Expand Down Expand Up @@ -149,12 +150,14 @@ const uploadFile = ({
created: new Date(),
lastUpdated: new Date(),
completed: false,
events: [msg],
events: [{ ...msg, date: new Date() }], // this is a workaround for the initial message date coming as invalid

summary: undefined,
error: false,
importType: type,
}
}

addEntry(entry.id, entry)

if (error) {
Expand All @@ -169,7 +172,7 @@ const uploadFile = ({
const response = JSON.parse(ev.target.response)
message = response.message
} catch (e2) {
message = genericErrorMessage
message = ev
}
console.error('sendFile error', message)
reject(errF(message))
Expand Down

0 comments on commit 32c401b

Please sign in to comment.