Skip to content

Commit f84658d

Browse files
committed
Refactors
1 parent 0edc1d6 commit f84658d

File tree

2 files changed

+61
-56
lines changed

2 files changed

+61
-56
lines changed

src/data/ProgramEventsD2Repository.ts

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,6 @@ import { Timestamp } from "domain/entities/Date";
1212
import { getInChunks } from "./dhis2-utils";
1313
import { promiseMap } from "./dhis2-utils";
1414

15-
const eventFields = {
16-
created: true,
17-
event: true,
18-
status: true,
19-
orgUnit: true,
20-
orgUnitName: true,
21-
program: true,
22-
programStage: true,
23-
eventDate: true,
24-
dueDate: true,
25-
lastUpdated: true,
26-
trackedEntityInstance: true,
27-
dataValues: {
28-
dataElement: true,
29-
value: true,
30-
storedBy: true,
31-
providedElsewhere: true,
32-
lastUpdated: true,
33-
},
34-
} as const;
35-
36-
type Fields = typeof eventFields;
37-
38-
type Event = EventsGetResponse<Fields>["events"][number];
39-
40-
type DataValue = ProgramEvent["dataValues"][number];
41-
42-
type D2DataValue = Event["dataValues"][number];
43-
44-
const programFields = {
45-
id: true,
46-
name: true,
47-
programType: true,
48-
programStages: {
49-
id: true,
50-
name: true,
51-
programStageSections: { dataElements: { id: true } },
52-
},
53-
} as const;
54-
55-
type D2Program = SelectedPick<D2ProgramSchema, typeof programFields>;
56-
57-
type D2ProgramStage = D2Program["programStages"][number];
58-
5915
export class ProgramEventsD2Repository implements ProgramEventsRepository {
6016
constructor(private api: D2Api) {}
6117

@@ -327,3 +283,47 @@ async function importEvents(api: D2Api, events: EventToPost[], params?: EventsPo
327283
? { type: "success", message: `${events.length} posted` }
328284
: { type: "error", message: "Error posting events" };
329285
}
286+
287+
const eventFields = {
288+
created: true,
289+
event: true,
290+
status: true,
291+
orgUnit: true,
292+
orgUnitName: true,
293+
program: true,
294+
programStage: true,
295+
eventDate: true,
296+
dueDate: true,
297+
lastUpdated: true,
298+
trackedEntityInstance: true,
299+
dataValues: {
300+
dataElement: true,
301+
value: true,
302+
storedBy: true,
303+
providedElsewhere: true,
304+
lastUpdated: true,
305+
},
306+
} as const;
307+
308+
type Fields = typeof eventFields;
309+
310+
type Event = EventsGetResponse<Fields>["events"][number];
311+
312+
type DataValue = ProgramEvent["dataValues"][number];
313+
314+
type D2DataValue = Event["dataValues"][number];
315+
316+
const programFields = {
317+
id: true,
318+
name: true,
319+
programType: true,
320+
programStages: {
321+
id: true,
322+
name: true,
323+
programStageSections: { dataElements: { id: true } },
324+
},
325+
} as const;
326+
327+
type D2Program = SelectedPick<D2ProgramSchema, typeof programFields>;
328+
329+
type D2ProgramStage = D2Program["programStages"][number];

src/scripts/programs/DuplicatedProgramsSpreadsheetExport.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,10 @@ export class DuplicatedProgramsSpreadsheetExport {
99
constructor(private duplicated: DuplicatedEvents) {}
1010

1111
async export(outputFilePath: string): Async<void> {
12-
const programsById = _(this.duplicated.groups)
13-
.flatMap(group => group.events)
14-
.map(ev => ev.program)
15-
.map(program => [program.id, program] as [Id, typeof program])
16-
.fromPairs()
17-
.value();
18-
19-
const rowsByProgramId = _(this.duplicated.groups)
20-
.groupBy(group => group.events[0]?.program.id)
21-
.value();
22-
2312
const workbook = XLSX.utils.book_new();
13+
const programsById = this.getProgramsById();
2414

25-
const sheets = _(rowsByProgramId)
15+
const sheets = _(this.getRowsByProgram())
2616
.map((groups, programId) => {
2717
if (groups.length === 0) return;
2818

@@ -50,6 +40,21 @@ export class DuplicatedProgramsSpreadsheetExport {
5040
logger.info(`Save file ${outputFilePath}`);
5141
XLSX.writeFile(workbook, outputFilePath);
5242
}
43+
44+
private getRowsByProgram() {
45+
return _(this.duplicated.groups)
46+
.groupBy(group => group.events[0]?.program.id)
47+
.value();
48+
}
49+
50+
private getProgramsById() {
51+
return _(this.duplicated.groups)
52+
.flatMap(group => group.events)
53+
.map(ev => ev.program)
54+
.map(program => [program.id, program] as [Id, typeof program])
55+
.fromPairs()
56+
.value();
57+
}
5358
}
5459

5560
function getRow(event: ProgramEvent) {

0 commit comments

Comments
 (0)