Skip to content

Commit 5be235c

Browse files
committed
Abstract insertion functionality
1 parent da2a768 commit 5be235c

File tree

1 file changed

+55
-101
lines changed

1 file changed

+55
-101
lines changed

dbschema/project.gel

Lines changed: 55 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ module default {
184184
select existingReports
185185
filter not exists .reportFile
186186
)
187-
select if not exists __new__.mouStart or not exists __new__.mouEnd then (
187+
select if not exists __new__.mouStart or not exists __new__.mouEnd then (
188188
for report in reportsForDeletion
189189
union (
190190
delete report
@@ -194,76 +194,25 @@ module default {
194194
or (__new__.mouStart > __old__.mouStart) ?? false
195195
or (__new__.mouEnd < __old__.mouEnd) ?? false then (
196196
with
197-
requestedReportPeriodsForInsertion := (
198-
select requestedReportPeriods
199-
filter requestedReportPeriods not in existingReports.period
200-
),
201-
financialReports := (for reportPeriod in requestedReportPeriodsForInsertion
197+
insertedReports := Project::insertReports(
198+
array_agg(requestedReportPeriods),
199+
array_agg(existingReports),
200+
__new__),
201+
deletedReports := (for report in reportsForDeletion
202202
union (
203-
insert default::FinancialReport {
204-
createdAt := datetime_of_statement(),
205-
modifiedAt := datetime_of_statement(),
206-
createdBy := assert_exists(global currentActor),
207-
modifiedBy := assert_exists(global currentActor),
208-
project := __new__,
209-
projectContext := __new__.projectContext,
210-
container := __new__,
211-
period := reportPeriod,
212-
}
213-
)),
214-
narrativeReports := (for reportPeriod in requestedReportPeriodsForInsertion
215-
union (
216-
insert default::NarrativeReport {
217-
createdAt := datetime_of_statement(),
218-
modifiedAt := datetime_of_statement(),
219-
createdBy := assert_exists(global currentActor),
220-
modifiedBy := assert_exists(global currentActor),
221-
project := __new__,
222-
projectContext := __new__.projectContext,
223-
container := __new__,
224-
period := reportPeriod,
225-
}
203+
delete report
204+
filter report.period not in requestedReportPeriods
226205
))
227-
for report in reportsForDeletion
228-
union (
229-
delete report
230-
filter report.period not in requestedReportPeriods
231-
)
206+
select insertedReports
232207
)
233208
else if newMouStart ?!= oldMouStart
234209
or newMouEnd ?!= oldMouEnd then (
235210
with
236-
requestedReportPeriodsForInsertion := (
237-
select requestedReportPeriods
238-
filter requestedReportPeriods not in existingReports.period
239-
),
240-
financialReports := (for reportPeriod in requestedReportPeriodsForInsertion
241-
union (
242-
insert default::FinancialReport {
243-
createdAt := datetime_of_statement(),
244-
modifiedAt := datetime_of_statement(),
245-
createdBy := assert_exists(global currentActor),
246-
modifiedBy := assert_exists(global currentActor),
247-
project := __new__,
248-
projectContext := __new__.projectContext,
249-
container := __new__,
250-
period := reportPeriod,
251-
}
252-
)),
253-
narrativeReports := (for reportPeriod in requestedReportPeriodsForInsertion
254-
union (
255-
insert default::NarrativeReport {
256-
createdAt := datetime_of_statement(),
257-
modifiedAt := datetime_of_statement(),
258-
createdBy := assert_exists(global currentActor),
259-
modifiedBy := assert_exists(global currentActor),
260-
project := __new__,
261-
projectContext := __new__.projectContext,
262-
container := __new__,
263-
period := reportPeriod,
264-
}
265-
))
266-
select financialReports union narrativeReports
211+
insertedReports := Project::insertReports(
212+
array_agg(requestedReportPeriods),
213+
array_agg(existingReports),
214+
__new__)
215+
select insertedReports
267216
) else (
268217
select <PeriodicReport>{}
269218
)
@@ -374,40 +323,45 @@ module Project {
374323
select reportPeriodRanges
375324
);
376325

377-
# function insertReports(requestedReportPeriods: set of range<cal::local_date>,
378-
# existingReports: array<PeriodicReport>, newProject: default::Project) -> optional str
379-
# using (
380-
# with
381-
# requestedReportPeriodsForInsertion := (
382-
# select requestedReportPeriods
383-
# filter requestedReportPeriods not in existingReports.period
384-
# ),
385-
# financialReports := (for reportPeriod in requestedReportPeriodsForInsertion
386-
# union (
387-
# insert default::FinancialReport {
388-
# createdAt := datetime_of_statement(),
389-
# modifiedAt := datetime_of_statement(),
390-
# createdBy := assert_exists(global default::currentActor),
391-
# modifiedBy := assert_exists(global default::currentActor),
392-
# project := __new__,
393-
# projectContext := __new__.projectContext,
394-
# container := __new__,
395-
# period := reportPeriod,
396-
# }
397-
# )),
398-
# narrativeReports := (for reportPeriod in requestedReportPeriodsForInsertion
399-
# union (
400-
# insert default::NarrativeReport {
401-
# createdAt := datetime_of_statement(),
402-
# modifiedAt := datetime_of_statement(),
403-
# createdBy := assert_exists(global default::currentActor),
404-
# modifiedBy := assert_exists(global default::currentActor),
405-
# project := __new__,
406-
# projectContext := __new__.projectContext,
407-
# container := __new__,
408-
# period := reportPeriod,
409-
# }
410-
# ))
411-
# select financialReports union narrativeReports
412-
# )
326+
function insertReports(requestedReportPeriods: array<range<cal::local_date>>,
327+
existingReports: array<default::PeriodicReport>,
328+
newProject: default::Project) -> set of default::PeriodicReport
329+
using (
330+
with
331+
distinctRequestedReportPeriods := distinct array_unpack(requestedReportPeriods),
332+
requestedReportPeriodsForInsertion := (
333+
select distinctRequestedReportPeriods
334+
filter distinctRequestedReportPeriods not in (
335+
for report in array_unpack(existingReports)
336+
select report.period
337+
)
338+
),
339+
financialReports := (for reportPeriod in requestedReportPeriodsForInsertion
340+
union (
341+
insert default::FinancialReport {
342+
createdAt := datetime_of_statement(),
343+
modifiedAt := datetime_of_statement(),
344+
createdBy := assert_exists(global default::currentActor),
345+
modifiedBy := assert_exists(global default::currentActor),
346+
project := newProject,
347+
projectContext := newProject.projectContext,
348+
container := newProject,
349+
period := reportPeriod,
350+
}
351+
)),
352+
narrativeReports := (for reportPeriod in requestedReportPeriodsForInsertion
353+
union (
354+
insert default::NarrativeReport {
355+
createdAt := datetime_of_statement(),
356+
modifiedAt := datetime_of_statement(),
357+
createdBy := assert_exists(global default::currentActor),
358+
modifiedBy := assert_exists(global default::currentActor),
359+
project := newProject,
360+
projectContext := newProject.projectContext,
361+
container := newProject,
362+
period := reportPeriod,
363+
}
364+
))
365+
select financialReports union narrativeReports
366+
);
413367
}

0 commit comments

Comments
 (0)