Skip to content

Commit 09ea41f

Browse files
committed
Bug fix for missing download history and enforcing perOuMode's organizationUnit(s) requirement
1 parent d1826f4 commit 09ea41f

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/renderer/src/i18n/en/main.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
"download": "Download",
2323
"downloadNote": "Note: Use a unique file name to prevent overwriting.",
2424
"exportGeoJSON": "Export GeoJSON",
25-
"exportCSV": "Export CSV"
25+
"exportCSV": "Export CSV",
26+
"errorNeedAtLeastOneOU": "Please select at least one organisation unit for one-by-one mode."
2627
}

src/renderer/src/i18n/fr/main.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
"download": "Télécharger",
2323
"downloadNote": "Remarque : utilisez un nom de fichier unique pour éviter tout écrasement.",
2424
"exportGeoJSON": "Exporter GeoJSON",
25-
"exportCSV": "Exporter CSV"
25+
"exportCSV": "Exporter CSV",
26+
"errorNeedAtLeastOneOU": "Veuillez sélectionner au moins une unité organisationnelle pour le mode un par un."
2627
}

src/renderer/src/pages/MainPage/index.jsx

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
triggerNotification
1414
} from '../../reducers/statusReducer'
1515
import Tooltip from '../../components/Tooltip'
16-
import { openModal } from '../../reducers/modalReducer'
16+
import { openModal, closeModal } from '../../reducers/modalReducer'
1717
import { fetchCsvData } from '../../service/useApi'
1818
import { generatePeriods } from '../../utils/dateUtils'
1919
import {
@@ -50,10 +50,13 @@ const MainPage = ({ queryDb }) => {
5050
return saveFilePath
5151
}
5252

53-
const getDownloadParameters = (layout) => {
54-
const ou = getOrganizationUnits()
55-
const elementIds = addedElements.map((element) => element.id)
56-
const elementNames = addedElements.map((element) => element.displayName)
53+
const getDownloadParameters = (layout, opts = {}) => {
54+
// opts.ouOverride is only used for saving query in per OU mode
55+
// in that case we want to save all OUs and levels in one query
56+
// instead of just the current OU or levels
57+
const ou = opts.ouOverride ?? getOrganizationUnits()
58+
const elementIds = addedElements.map((e) => e.id)
59+
const elementNames = addedElements.map((e) => e.displayName)
5760
const periods = generatePeriods(frequency, startDate, endDate)
5861

5962
return {
@@ -64,15 +67,15 @@ const MainPage = ({ queryDb }) => {
6467
pe: periods.join(';'),
6568
periods,
6669
elementIds,
67-
layout: layout, // ensure layout object is stored
70+
layout,
6871
downloadingUrl: generateDownloadingUrl(
6972
dhis2Url,
7073
ou,
7174
elementIds.join(';'),
7275
periods.join(';'),
7376
selectedCategory,
7477
'csv',
75-
layout // pass correct object
78+
layout
7679
)
7780
}
7881
}
@@ -182,6 +185,18 @@ const MainPage = ({ queryDb }) => {
182185
const handleStreamDownload = async ({ layout, perOuMode }) => {
183186
let fileStream = null
184187
try {
188+
// Validation for per OU mode
189+
const hasOUs = selectedOrgUnits.length > 0
190+
if (perOuMode && !hasOUs) {
191+
dispatch(closeModal())
192+
dispatch(
193+
triggerNotification({
194+
message: t('mainPage.errorNeedAtLeastOneOU'),
195+
type: 'error'
196+
})
197+
)
198+
return
199+
}
185200
const currentChunkingStrategy = store.getState().download.chunkingStrategy
186201
const saveFilePath = await getSaveFilePath()
187202
if (!saveFilePath) return
@@ -237,6 +252,13 @@ const MainPage = ({ queryDb }) => {
237252
)
238253
await new Promise((r) => setTimeout(r, 200))
239254
}
255+
256+
// Save query with all OUs and levels into one database entry
257+
const levelsToken = selectedOrgUnitLevels.map((l) => `LEVEL-${l}`).join(';')
258+
const ouCombined = `${levelsToken};${selectedOrgUnits.join(';')}`
259+
260+
const historyParams = getDownloadParameters(layout, { ouOverride: ouCombined })
261+
await saveQueryToDatabase(historyParams)
240262
} else {
241263
const downloadParams = getDownloadParameters(layout)
242264
const chunks = createDataChunks(
@@ -247,6 +269,8 @@ const MainPage = ({ queryDb }) => {
247269
layout
248270
)
249271
await processChunks(chunks, fileStream, downloadParams, headerState)
272+
// Save query into database
273+
await saveQueryToDatabase(downloadParams)
250274
}
251275

252276
fileStream.end()

0 commit comments

Comments
 (0)