Skip to content

Commit ccd7e0e

Browse files
committed
Abstract insertion functionality
1 parent e08f699 commit ccd7e0e

File tree

1 file changed

+85
-130
lines changed

1 file changed

+85
-130
lines changed

dbschema/project.gel

Lines changed: 85 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ module default {
6060
status := Project::statusFromStep(.step);
6161
latestWorkflowEvent := (select .workflowEvents order by .at desc limit 1);
6262
workflowEvents := .<project[is Project::WorkflowEvent];
63+
6364
trigger assertMatchingLatestWorkflowEvent after insert, update for each do (
6465
assert(
6566
__new__.latestWorkflowEvent.to ?= __new__.step
@@ -130,38 +131,23 @@ module default {
130131
}
131132
);
132133

133-
trigger createPeriodicReports after insert for each do (
134+
trigger createPeriodicReports after insert for each
135+
when (
136+
exists __new__.financialReportPeriod
137+
and exists __new__.mouStart
138+
and exists __new__.mouEnd
139+
)
140+
do (
134141
with
135142
interval := (select
136143
if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3'),
137144
reportRanges := Project::create_periodic_report_ranges(
138145
__new__.mouStart,
139146
__new__.mouEnd,
140147
interval
141-
)
142-
for reportRange in reportRanges
143-
union (
144-
(insert default::FinancialReport {
145-
createdAt := datetime_of_statement(),
146-
modifiedAt := datetime_of_statement(),
147-
createdBy := assert_exists(global currentActor),
148-
modifiedBy := assert_exists(global currentActor),
149-
project := __new__,
150-
projectContext := __new__.projectContext,
151-
container := __new__,
152-
period := reportRange
153-
}),
154-
(insert default::NarrativeReport {
155-
createdAt := datetime_of_statement(),
156-
modifiedAt := datetime_of_statement(),
157-
createdBy := assert_exists(global currentActor),
158-
modifiedBy := assert_exists(global currentActor),
159-
project := __new__,
160-
projectContext := __new__.projectContext,
161-
container := __new__,
162-
period := reportRange
163-
})
164-
)
148+
),
149+
insertedReports := Project::insert_reports(array_agg(reportRanges), __new__)
150+
select insertedReports
165151
);
166152

167153
trigger addRemovePeriodicReports after update for each
@@ -192,86 +178,44 @@ module default {
192178
select existingReports
193179
filter not exists .reportFile
194180
)
195-
select if not exists __new__.mouStart or not exists __new__.mouEnd then (
181+
select
182+
# start date, end date, or financial report period were deleted
183+
if not exists __new__.mouStart
184+
or not exists __new__.mouEnd
185+
or not exists __new__.financialReportPeriod then (
196186
for report in reportsForDeletion
197187
union (
198188
delete report
199189
)
200190
)
191+
# financial report period changes, start date is moved forward, or end date is moved backward
201192
else if __old__.financialReportPeriod ?!= __new__.financialReportPeriod
202193
or (__new__.mouStart > __old__.mouStart) ?? false
203194
or (__new__.mouEnd < __old__.mouEnd) ?? false then (
204195
with
205-
requestedReportPeriodsForInsertion := (
206-
select requestedReportPeriods
207-
filter requestedReportPeriods not in existingReports.period
196+
periodsForInsertion := Project::determine_requested_report_periods(
197+
array_agg(requestedReportPeriods),
198+
array_agg(existingReports)
208199
),
209-
financialReports := (for reportPeriod in requestedReportPeriodsForInsertion
200+
insertedReports := Project::insert_reports(array_agg(periodsForInsertion), __new__),
201+
deletedReports := (for report in reportsForDeletion
210202
union (
211-
insert default::FinancialReport {
212-
createdAt := datetime_of_statement(),
213-
modifiedAt := datetime_of_statement(),
214-
createdBy := assert_exists(global currentActor),
215-
modifiedBy := assert_exists(global currentActor),
216-
project := __new__,
217-
projectContext := __new__.projectContext,
218-
container := __new__,
219-
period := reportPeriod,
220-
}
221-
)),
222-
narrativeReports := (for reportPeriod in requestedReportPeriodsForInsertion
223-
union (
224-
insert default::NarrativeReport {
225-
createdAt := datetime_of_statement(),
226-
modifiedAt := datetime_of_statement(),
227-
createdBy := assert_exists(global currentActor),
228-
modifiedBy := assert_exists(global currentActor),
229-
project := __new__,
230-
projectContext := __new__.projectContext,
231-
container := __new__,
232-
period := reportPeriod,
233-
}
203+
delete report
204+
filter report.period not in requestedReportPeriods
234205
))
235-
for report in reportsForDeletion
236-
union (
237-
delete report
238-
filter report.period not in requestedReportPeriods
239-
)
240-
)
206+
select insertedReports
207+
)
208+
# start or end dates otherwise change
241209
else if newMouStart ?!= oldMouStart
242210
or newMouEnd ?!= oldMouEnd then (
243211
with
244-
requestedReportPeriodsForInsertion := (
245-
select requestedReportPeriods
246-
filter requestedReportPeriods not in existingReports.period
247-
),
248-
financialReports := (for reportPeriod in requestedReportPeriodsForInsertion
249-
union (
250-
insert default::FinancialReport {
251-
createdAt := datetime_of_statement(),
252-
modifiedAt := datetime_of_statement(),
253-
createdBy := assert_exists(global currentActor),
254-
modifiedBy := assert_exists(global currentActor),
255-
project := __new__,
256-
projectContext := __new__.projectContext,
257-
container := __new__,
258-
period := reportPeriod,
259-
}
260-
)),
261-
narrativeReports := (for reportPeriod in requestedReportPeriodsForInsertion
262-
union (
263-
insert default::NarrativeReport {
264-
createdAt := datetime_of_statement(),
265-
modifiedAt := datetime_of_statement(),
266-
createdBy := assert_exists(global currentActor),
267-
modifiedBy := assert_exists(global currentActor),
268-
project := __new__,
269-
projectContext := __new__.projectContext,
270-
container := __new__,
271-
period := reportPeriod,
272-
}
273-
))
274-
select financialReports union narrativeReports
212+
periodsForInsertion := Project::determine_requested_report_periods(
213+
array_agg(requestedReportPeriods),
214+
array_agg(existingReports)
215+
),
216+
insertedReports := Project::insert_reports(array_agg(periodsForInsertion), __new__)
217+
select insertedReports
218+
# nothing changes
275219
) else (
276220
select <PeriodicReport>{}
277221
)
@@ -375,47 +319,58 @@ module Project {
375319
for firstDayOfMonth in reportPeriodStartDates
376320
union (
377321
with
378-
firstDayOfNextMonth := (select firstDayOfMonth + <cal::relative_duration>(monthInterval ++ ' month')),
379-
lastDayOfMonth := firstDayOfNextMonth - <cal::relative_duration>'1 day'
380-
select range(<cal::local_date>firstDayOfMonth, <cal::local_date>lastDayOfMonth)
381-
))
322+
firstDayOfNextPeriod := (select firstDayOfMonth + <cal::relative_duration>(monthInterval ++ ' month')),
323+
select range(<cal::local_date>firstDayOfMonth, <cal::local_date>firstDayOfNextPeriod)
324+
)
325+
)
382326
select reportPeriodRanges
383327
);
384328

385-
# function insertReports(requestedReportPeriods: set of range<cal::local_date>,
386-
# existingReports: array<PeriodicReport>, newProject: default::Project) -> optional str
387-
# using (
388-
# with
389-
# requestedReportPeriodsForInsertion := (
390-
# select requestedReportPeriods
391-
# filter requestedReportPeriods not in existingReports.period
392-
# ),
393-
# financialReports := (for reportPeriod in requestedReportPeriodsForInsertion
394-
# union (
395-
# insert default::FinancialReport {
396-
# createdAt := datetime_of_statement(),
397-
# modifiedAt := datetime_of_statement(),
398-
# createdBy := assert_exists(global default::currentActor),
399-
# modifiedBy := assert_exists(global default::currentActor),
400-
# project := __new__,
401-
# projectContext := __new__.projectContext,
402-
# container := __new__,
403-
# period := reportPeriod,
404-
# }
405-
# )),
406-
# narrativeReports := (for reportPeriod in requestedReportPeriodsForInsertion
407-
# union (
408-
# insert default::NarrativeReport {
409-
# createdAt := datetime_of_statement(),
410-
# modifiedAt := datetime_of_statement(),
411-
# createdBy := assert_exists(global default::currentActor),
412-
# modifiedBy := assert_exists(global default::currentActor),
413-
# project := __new__,
414-
# projectContext := __new__.projectContext,
415-
# container := __new__,
416-
# period := reportPeriod,
417-
# }
418-
# ))
419-
# select financialReports union narrativeReports
420-
# )
329+
function determine_requested_report_periods(requestedReportPeriods: array<range<cal::local_date>>,
330+
existingReports: array<default::PeriodicReport>) -> set of range<cal::local_date>
331+
using (
332+
with
333+
distinctRequestedReportPeriods := distinct array_unpack(requestedReportPeriods),
334+
periodsForInsertion := (
335+
select distinctRequestedReportPeriods
336+
filter distinctRequestedReportPeriods not in (
337+
for report in array_unpack(existingReports)
338+
select report.period
339+
)
340+
)
341+
select periodsForInsertion
342+
);
343+
344+
function insert_reports(requestedReportPeriodsForInsertion: array<range<cal::local_date>>,
345+
newProject: default::Project) -> set of default::PeriodicReport
346+
using (
347+
with
348+
financialReports := (for reportPeriod in array_unpack(requestedReportPeriodsForInsertion)
349+
union (
350+
insert default::FinancialReport {
351+
createdAt := datetime_of_statement(),
352+
modifiedAt := datetime_of_statement(),
353+
createdBy := assert_exists(global default::currentActor),
354+
modifiedBy := assert_exists(global default::currentActor),
355+
project := newProject,
356+
projectContext := newProject.projectContext,
357+
container := newProject,
358+
period := reportPeriod,
359+
}
360+
)),
361+
narrativeReports := (for reportPeriod in array_unpack(requestedReportPeriodsForInsertion)
362+
union (
363+
insert default::NarrativeReport {
364+
createdAt := datetime_of_statement(),
365+
modifiedAt := datetime_of_statement(),
366+
createdBy := assert_exists(global default::currentActor),
367+
modifiedBy := assert_exists(global default::currentActor),
368+
project := newProject,
369+
projectContext := newProject.projectContext,
370+
container := newProject,
371+
period := reportPeriod,
372+
}
373+
))
374+
select financialReports union narrativeReports
375+
);
421376
}

0 commit comments

Comments
 (0)