Skip to content

Commit 07d8f1b

Browse files
committed
Create PeriodicReport queries
1 parent 4aeaf58 commit 07d8f1b

File tree

2 files changed

+212
-0
lines changed

2 files changed

+212
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Field, InterfaceType, ObjectType } from '@nestjs/graphql';
2+
import { simpleSwitch } from '@seedcompany/common';
23
import { keys as keysOf } from 'ts-transformer-keys';
4+
import { MergeExclusive } from 'type-fest';
35
import {
46
Calculated,
57
CalendarDate,
@@ -11,14 +13,29 @@ import {
1113
SecuredStringNullable,
1214
Sensitivity,
1315
SensitivityField,
16+
ServerException,
1417
} from '~/common';
1518
import { BaseNode as DbBaseNode } from '~/core/database/results';
1619
import { e } from '~/core/edgedb';
1720
import { RegisterResource } from '~/core/resources';
1821
import { ScopedRole } from '../../authorization/dto';
1922
import { DefinedFile } from '../../file/dto';
23+
import { ProgressReport } from '../../progress-report/dto';
2024
import { ReportType } from './report-type.enum';
2125

26+
export type AnyReport = MergeExclusive<
27+
FinancialReport,
28+
MergeExclusive<NarrativeReport, ProgressReport>
29+
>;
30+
31+
export const resolveReportType = (val: Pick<AnyReport, 'type'>) => {
32+
const type = simpleSwitch(val.type, ReportConcretes);
33+
if (!type) {
34+
throw new ServerException(`Could not resolve report type: '${val.type}'`);
35+
}
36+
return type;
37+
};
38+
2239
@RegisterResource({ db: e.PeriodicReport })
2340
@Calculated()
2441
@InterfaceType({
@@ -92,6 +109,12 @@ export class NarrativeReport extends PeriodicReport {
92109
})
93110
export class SecuredPeriodicReport extends SecuredProperty(PeriodicReport) {}
94111

112+
export const ReportConcretes = {
113+
Financial: FinancialReport,
114+
Narrative: NarrativeReport,
115+
Progress: ProgressReport,
116+
};
117+
95118
declare module '~/core/resources/map' {
96119
interface ResourceMap {
97120
PeriodicReport: typeof PeriodicReport;
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { Query } from 'cypher-query-builder';
3+
import { Without } from 'type-fest/source/merge-exclusive';
4+
import {
5+
CalendarDate,
6+
ID,
7+
PublicOf,
8+
Range,
9+
Session,
10+
UnsecuredDto,
11+
} from '~/common';
12+
import { castToEnum, e, RepoFor } from '~/core/edgedb';
13+
import { Variable } from '../../core/database/query';
14+
import { ProgressReport } from '../progress-report/dto';
15+
import {
16+
FinancialReport,
17+
IPeriodicReport,
18+
MergePeriodicReports,
19+
NarrativeReport,
20+
ReportType,
21+
resolveReportType,
22+
} from './dto';
23+
import { PeriodicReportRepository } from './periodic-report.repository';
24+
25+
@Injectable()
26+
export class PeriodicReportEdgeDBRepository
27+
extends RepoFor(IPeriodicReport, {
28+
hydrate: (periodicReport) => ({
29+
...periodicReport['*'],
30+
type: castToEnum(periodicReport.__type__.name.slice(9, -7), ReportType),
31+
reportFile: true,
32+
sensitivity: periodicReport.container.is(e.Project.ContextAware)
33+
.sensitivity,
34+
scope: false,
35+
parent: e.tuple({
36+
identity: periodicReport.id,
37+
labels: e.array_agg(e.set(periodicReport.__type__.name.slice(9, null))),
38+
properties: e.tuple({
39+
id: periodicReport.id,
40+
createdAt: periodicReport.createdAt,
41+
}),
42+
}),
43+
}),
44+
})
45+
implements PublicOf<PeriodicReportRepository>
46+
{
47+
merge(
48+
input: MergePeriodicReports,
49+
): Promise<ReadonlyArray<{ id: ID; interval: Range<CalendarDate> }>> {
50+
throw new Error('Method not implemented.');
51+
}
52+
53+
matchCurrentDue(
54+
parentId: ID | Variable,
55+
reportType: ReportType,
56+
): (query: Query) => Query {
57+
throw new Error('Method not implemented.');
58+
}
59+
60+
getByDate(
61+
parentId: ID,
62+
date: CalendarDate,
63+
reportType: ReportType,
64+
_session: Session,
65+
) {
66+
const resource = e.cast(e.Resource, e.uuid(parentId));
67+
68+
const report = e.select(e.PeriodicReport, (report) => ({
69+
filter: e.all(
70+
e.set(
71+
e.op(resource.id, '=', report.container.id),
72+
e.op(report.start, '<=', date),
73+
e.op(report.end, '>=', date),
74+
),
75+
),
76+
...report.is(resolveReportType(reportType)),
77+
}));
78+
79+
return this.db.run(report);
80+
}
81+
82+
getCurrentDue(
83+
parentId: ID,
84+
reportType: ReportType,
85+
session: Session,
86+
): Promise<
87+
| UnsecuredDto<
88+
| (Without<
89+
| (Without<FinancialReport, NarrativeReport> & NarrativeReport)
90+
| (Without<NarrativeReport, FinancialReport> & FinancialReport),
91+
ProgressReport
92+
> &
93+
ProgressReport)
94+
| (Without<
95+
ProgressReport,
96+
| (Without<FinancialReport, NarrativeReport> & NarrativeReport)
97+
| (Without<NarrativeReport, FinancialReport> & FinancialReport)
98+
> &
99+
(
100+
| (Without<FinancialReport, NarrativeReport> & NarrativeReport)
101+
| (Without<NarrativeReport, FinancialReport> & FinancialReport)
102+
))
103+
>
104+
| undefined
105+
> {
106+
throw new Error('Method not implemented.');
107+
}
108+
109+
getNextDue(
110+
parentId: ID,
111+
reportType: ReportType,
112+
session: Session,
113+
): Promise<
114+
| UnsecuredDto<
115+
| (Without<
116+
| (Without<FinancialReport, NarrativeReport> & NarrativeReport)
117+
| (Without<NarrativeReport, FinancialReport> & FinancialReport),
118+
ProgressReport
119+
> &
120+
ProgressReport)
121+
| (Without<
122+
ProgressReport,
123+
| (Without<FinancialReport, NarrativeReport> & NarrativeReport)
124+
| (Without<NarrativeReport, FinancialReport> & FinancialReport)
125+
> &
126+
(
127+
| (Without<FinancialReport, NarrativeReport> & NarrativeReport)
128+
| (Without<NarrativeReport, FinancialReport> & FinancialReport)
129+
))
130+
>
131+
| undefined
132+
> {
133+
throw new Error('Method not implemented.');
134+
}
135+
136+
getLatestReportSubmitted(
137+
parentId: ID,
138+
type: ReportType,
139+
session: Session,
140+
): Promise<
141+
| UnsecuredDto<
142+
| (Without<
143+
| (Without<FinancialReport, NarrativeReport> & NarrativeReport)
144+
| (Without<NarrativeReport, FinancialReport> & FinancialReport),
145+
ProgressReport
146+
> &
147+
ProgressReport)
148+
| (Without<
149+
ProgressReport,
150+
| (Without<FinancialReport, NarrativeReport> & NarrativeReport)
151+
| (Without<NarrativeReport, FinancialReport> & FinancialReport)
152+
> &
153+
(
154+
| (Without<FinancialReport, NarrativeReport> & NarrativeReport)
155+
| (Without<NarrativeReport, FinancialReport> & FinancialReport)
156+
))
157+
>
158+
| undefined
159+
> {
160+
throw new Error('Method not implemented.');
161+
}
162+
163+
getFinalReport(
164+
parentId: ID,
165+
type: ReportType,
166+
session: Session,
167+
): Promise<
168+
| UnsecuredDto<
169+
| (Without<
170+
| (Without<FinancialReport, NarrativeReport> & NarrativeReport)
171+
| (Without<NarrativeReport, FinancialReport> & FinancialReport),
172+
ProgressReport
173+
> &
174+
ProgressReport)
175+
| (Without<
176+
ProgressReport,
177+
| (Without<FinancialReport, NarrativeReport> & NarrativeReport)
178+
| (Without<NarrativeReport, FinancialReport> & FinancialReport)
179+
> &
180+
(
181+
| (Without<FinancialReport, NarrativeReport> & NarrativeReport)
182+
| (Without<NarrativeReport, FinancialReport> & FinancialReport)
183+
))
184+
>
185+
| undefined
186+
> {
187+
throw new Error('Method not implemented.');
188+
}
189+
}

0 commit comments

Comments
 (0)