Skip to content

Commit e401f9f

Browse files
committed
Isolate Neo4j specific code
1 parent d53d004 commit e401f9f

File tree

5 files changed

+95
-80
lines changed

5 files changed

+95
-80
lines changed

src/components/periodic-report/handlers/abstract-periodic-report-sync.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { type DateTimeUnit } from 'luxon';
22
import { type CalendarDate, DateInterval, type ID, type Range } from '~/common';
33
import { type ReportType } from '../dto';
4-
import { type PeriodicReportService } from '../periodic-report.service';
4+
import { type PeriodicReportRepository } from '../periodic-report.repository';
55

66
export type Intervals = [
77
updated: DateInterval | null,
88
previous: DateInterval | null,
99
];
1010

1111
export abstract class AbstractPeriodicReportSync {
12-
constructor(protected readonly periodicReports: PeriodicReportService) {}
12+
constructor(
13+
protected readonly periodicReportsRepo: PeriodicReportRepository,
14+
) {}
1315

1416
protected async sync(
1517
parent: ID,
@@ -20,9 +22,12 @@ export abstract class AbstractPeriodicReportSync {
2022
},
2123
finalAt?: CalendarDate,
2224
) {
23-
await this.periodicReports.delete(parent, type, diff.removals);
25+
if (!diff) {
26+
return;
27+
}
28+
await this.periodicReportsRepo.delete(parent, type, diff.removals);
2429

25-
await this.periodicReports.merge({
30+
await this.periodicReportsRepo.merge({
2631
type,
2732
parent,
2833
intervals: diff.additions,
@@ -31,7 +36,7 @@ export abstract class AbstractPeriodicReportSync {
3136
if (!finalAt) {
3237
return;
3338
}
34-
await this.periodicReports.mergeFinalReport(parent, type, finalAt);
39+
await this.periodicReportsRepo.mergeFinalReport(parent, type, finalAt);
3540
}
3641

3742
/**

src/components/periodic-report/handlers/sync-periodic-report-to-project.handler.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { type DateTimeUnit } from 'luxon';
22
import { type DateInterval } from '~/common';
3-
import { EventsHandler, type IEventHandler, ILogger, Logger } from '~/core';
3+
import {
4+
ConfigService,
5+
EventsHandler,
6+
type IEventHandler,
7+
ILogger,
8+
Logger,
9+
} from '~/core';
410
import { projectRange } from '../../project/dto';
511
import { ProjectUpdatedEvent } from '../../project/events';
612
import { ReportPeriod, ReportType } from '../dto';
7-
import { PeriodicReportService } from '../periodic-report.service';
13+
import { PeriodicReportRepository } from '../periodic-report.repository';
814
import {
915
AbstractPeriodicReportSync,
1016
type Intervals,
@@ -18,13 +24,18 @@ export class SyncPeriodicReportsToProjectDateRange
1824
implements IEventHandler<SubscribedEvent>
1925
{
2026
constructor(
21-
periodicReports: PeriodicReportService,
27+
periodicReportsRepo: PeriodicReportRepository,
28+
private readonly config: ConfigService,
2229
@Logger('periodic-reports:project-sync') private readonly logger: ILogger,
2330
) {
24-
super(periodicReports);
31+
super(periodicReportsRepo);
2532
}
2633

2734
async handle(event: SubscribedEvent) {
35+
if (this.config.databaseEngine === 'gel') {
36+
return;
37+
}
38+
2839
this.logger.debug('Project mutation, syncing periodic reports', {
2940
...event,
3041
event: event.constructor.name,

src/components/periodic-report/handlers/sync-progress-report-to-engagement.handler.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { Settings } from 'luxon';
22
import { DateInterval, type UnsecuredDto } from '~/common';
3-
import { EventsHandler, type IEventHandler, ILogger, Logger } from '~/core';
3+
import {
4+
ConfigService,
5+
EventsHandler,
6+
type IEventHandler,
7+
ILogger,
8+
Logger,
9+
} from '~/core';
410
import { EngagementService } from '../../engagement';
511
import { type Engagement, engagementRange } from '../../engagement/dto';
612
import {
@@ -9,7 +15,7 @@ import {
915
} from '../../engagement/events';
1016
import { ProjectUpdatedEvent } from '../../project/events';
1117
import { ReportType } from '../dto';
12-
import { PeriodicReportService } from '../periodic-report.service';
18+
import { PeriodicReportRepository } from '../periodic-report.repository';
1319
import {
1420
AbstractPeriodicReportSync,
1521
type Intervals,
@@ -30,14 +36,19 @@ export class SyncProgressReportToEngagementDateRange
3036
implements IEventHandler<SubscribedEvent>
3137
{
3238
constructor(
33-
periodicReports: PeriodicReportService,
39+
periodicReportsRepo: PeriodicReportRepository,
40+
private readonly config: ConfigService,
3441
private readonly engagements: EngagementService,
3542
@Logger('progress-report:engagement-sync') private readonly logger: ILogger,
3643
) {
37-
super(periodicReports);
44+
super(periodicReportsRepo);
3845
}
3946

4047
async handle(event: SubscribedEvent) {
48+
if (this.config.databaseEngine === 'gel') {
49+
return;
50+
}
51+
4152
// Only LanguageEngagements
4253
if (
4354
!(

src/components/periodic-report/periodic-report.repository.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import {
1313
type CalendarDate,
1414
CreationFailed,
15+
DateInterval,
1516
generateId,
1617
type ID,
1718
NotFoundException,
@@ -34,6 +35,7 @@ import {
3435
variable,
3536
type Variable,
3637
} from '~/core/database/query';
38+
import { ILogger, Logger } from '../../core';
3739
import { File } from '../file/dto';
3840
import {
3941
ProgressReport,
@@ -61,11 +63,16 @@ export class PeriodicReportRepository extends DtoRepository<
6163
>(IPeriodicReport) {
6264
constructor(
6365
private readonly progressRepo: ProgressReportExtraForPeriodicInterfaceRepository,
66+
@Logger('periodic:report:service') private readonly logger: ILogger,
6467
) {
6568
super();
6669
}
6770

6871
async merge(input: MergePeriodicReports) {
72+
if (input.intervals.length === 0) {
73+
return;
74+
}
75+
6976
try {
7077
const Report = resolveReportType(input);
7178

@@ -172,13 +179,44 @@ export class PeriodicReportRepository extends DtoRepository<
172179
'report.id as id, interval',
173180
);
174181

175-
return await query.run();
182+
const result = await query.run();
183+
184+
this.logger.info(`Merged ${input.type.toLowerCase()} reports`, {
185+
existing: input.intervals.length - result.length,
186+
new: result.length,
187+
parent: input.parent,
188+
newIntervals: result.map(({ interval }) =>
189+
DateInterval.fromObject(interval).toISO(),
190+
),
191+
});
176192
} catch (exception) {
177193
const Report = resolveReportType({ type: input.type });
178194
throw new CreationFailed(Report, exception);
179195
}
180196
}
181197

198+
async mergeFinalReport(
199+
parentId: ID,
200+
type: ReportType,
201+
at: CalendarDate,
202+
): Promise<void> {
203+
const report = await this.getFinalReport(parentId, type);
204+
205+
if (report) {
206+
if (+report.start === +at) {
207+
// no change
208+
return;
209+
}
210+
await this.update({ id: report.id, start: at, end: at });
211+
} else {
212+
await this.merge({
213+
intervals: [{ start: at, end: at }],
214+
type,
215+
parent: parentId,
216+
});
217+
}
218+
}
219+
182220
async update(
183221
changes: Omit<UpdatePeriodicReportInput, 'reportFile'> &
184222
Pick<PeriodicReport, 'start' | 'end'>,
@@ -334,7 +372,12 @@ export class PeriodicReportRepository extends DtoRepository<
334372
type: ReportType,
335373
intervals: ReadonlyArray<Range<CalendarDate | null>>,
336374
) {
337-
return await this.db
375+
intervals = intervals.filter((i) => i.start || i.end);
376+
if (intervals.length === 0) {
377+
return;
378+
}
379+
380+
const result = await this.db
338381
.query()
339382
.unwind(
340383
intervals.map((i) => ({ start: i.start, end: i.end })),
@@ -386,6 +429,8 @@ export class PeriodicReportRepository extends DtoRepository<
386429
)
387430
.return<{ count: number }>('count(report) as count')
388431
.first();
432+
433+
this.logger.info('Deleted reports', { baseNodeId, type, ...result });
389434
}
390435

391436
protected hydrate() {

src/components/periodic-report/periodic-report.service.ts

Lines changed: 8 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { Injectable } from '@nestjs/common';
22
import {
33
type CalendarDate,
4-
DateInterval,
54
type ID,
5+
NotFoundException,
66
type ObjectView,
7-
type Range,
87
type UnsecuredDto,
98
} from '~/common';
109
import { HandleIdLookup, IEventBus, ILogger, Logger } from '~/core';
@@ -15,7 +14,6 @@ import { FileService } from '../file';
1514
import { ProgressReport } from '../progress-report/dto';
1615
import {
1716
FinancialReport,
18-
type MergePeriodicReports,
1917
NarrativeReport,
2018
type PeriodicReport,
2119
type PeriodicReportListInput,
@@ -39,21 +37,6 @@ export class PeriodicReportService {
3937
private readonly repo: PeriodicReportRepository,
4038
) {}
4139

42-
async merge(input: MergePeriodicReports) {
43-
if (input.intervals.length === 0) {
44-
return;
45-
}
46-
const result = await this.repo.merge(input);
47-
this.logger.info(`Merged ${input.type.toLowerCase()} reports`, {
48-
existing: input.intervals.length - result.length,
49-
new: result.length,
50-
parent: input.parent,
51-
newIntervals: result.map(({ interval }) =>
52-
DateInterval.fromObject(interval).toISO(),
53-
),
54-
});
55-
}
56-
5740
async update(input: UpdatePeriodicReportInput) {
5841
const currentRaw = await this.repo.readOne(input.id);
5942
const current = this.secure(currentRaw);
@@ -65,21 +48,17 @@ export class PeriodicReportService {
6548
const { reportFile, ...simpleChanges } = changes;
6649

6750
const updated = this.secure(
68-
await this.repo.update(
69-
{
70-
id: current.id,
71-
start: current.start,
72-
end: current.end,
73-
...simpleChanges,
74-
},
75-
session,
76-
),
77-
session,
51+
await this.repo.update({
52+
id: current.id,
53+
start: current.start,
54+
end: current.end,
55+
...simpleChanges,
56+
}),
7857
);
7958

8059
if (reportFile) {
8160
const file = await this.files.updateDefinedFile(
82-
this.secure(current, session).reportFile,
61+
this.secure(currentRaw).reportFile,
8362
'file',
8463
reportFile,
8564
);
@@ -168,47 +147,11 @@ export class PeriodicReportService {
168147
: undefined;
169148
}
170149

171-
async delete(
172-
parent: ID,
173-
type: ReportType,
174-
intervals: ReadonlyArray<Range<CalendarDate | null>>,
175-
) {
176-
intervals = intervals.filter((i) => i.start || i.end);
177-
if (intervals.length === 0) {
178-
return;
179-
}
180-
const result = await this.repo.delete(parent, type, intervals);
181-
182-
this.logger.info('Deleted reports', { parent, type, ...result });
183-
}
184-
185150
async getFinalReport(
186151
parentId: ID,
187152
type: ReportType,
188153
): Promise<PeriodicReport | undefined> {
189154
const report = await this.repo.getFinalReport(parentId, type);
190155
return report ? this.secure(report) : undefined;
191156
}
192-
193-
async mergeFinalReport(
194-
parentId: ID,
195-
type: ReportType,
196-
at: CalendarDate,
197-
): Promise<void> {
198-
const report = await this.repo.getFinalReport(parentId, type);
199-
200-
if (report) {
201-
if (+report.start === +at) {
202-
// no change
203-
return;
204-
}
205-
await this.repo.update({ id: report.id, start: at, end: at }, session);
206-
} else {
207-
await this.merge({
208-
intervals: [{ start: at, end: at }],
209-
type,
210-
parent: parentId,
211-
});
212-
}
213-
}
214157
}

0 commit comments

Comments
 (0)