Skip to content

Commit 00d7391

Browse files
committed
Refactor to account for progess reports in triggers
1 parent 5f7e68d commit 00d7391

File tree

1 file changed

+125
-63
lines changed

1 file changed

+125
-63
lines changed

dbschema/project.gel

Lines changed: 125 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,33 @@ module default {
124124
);
125125

126126
trigger createPeriodicReports after insert for each
127-
when (
128-
exists __new__.financialReportPeriod
129-
and exists __new__.mouStart
130-
and exists __new__.mouEnd
131-
)
132-
do (
127+
when (exists __new__.mouStart and exists __new__.mouEnd)
128+
do (
133129
with
134-
interval := (select
135-
if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3'),
136130
reportRanges := Project::create_periodic_report_ranges(
137131
__new__.mouStart,
138132
__new__.mouEnd,
139-
interval
133+
'3'
134+
),
135+
insertedFinancialReports := (
136+
select
137+
if __new__.financialReportPeriod = default::ReportPeriod.Monthly then (
138+
with
139+
financialReportRanges := Project::create_periodic_report_ranges(
140+
__new__.mouStart,
141+
__new__.mouEnd,
142+
'1'
143+
),
144+
financialReports := Project::insert_financial_reports(array_agg(financialReportRanges), __new__)
145+
select financialReports
146+
) else if __new__.financialReportPeriod = default::ReportPeriod.Quarterly then (
147+
select Project::insert_financial_reports(array_agg(reportRanges), __new__)
148+
) else (
149+
select <default::FinancialReport>{}
150+
)
140151
),
141-
insertedReports := Project::insert_reports(array_agg(reportRanges), __new__)
142-
select insertedReports
152+
insertedNarrativeAndProgressReports := Project::insert_narrative_and_progress_reports(array_agg(reportRanges), __new__)
153+
select insertedFinancialReports union insertedNarrativeAndProgressReports
143154
);
144155

145156
trigger addRemovePeriodicReports after update for each
@@ -154,59 +165,69 @@ module default {
154165
oldMouStart := __old__.mouStart,
155166
newMouEnd := __new__.mouEnd,
156167
oldMouEnd := __old__.mouEnd,
157-
existingReports := (
158-
select FinancialReport union NarrativeReport
159-
filter .container.id = __old__.id
160-
),
161-
interval := (
162-
select (if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3')
163-
),
164-
requestedReportPeriods := Project::create_periodic_report_ranges(
168+
monthlyReportRanges := Project::create_periodic_report_ranges(
165169
__new__.mouStart,
166170
__new__.mouEnd,
167-
interval
171+
'1'
172+
),
173+
quarterlyReportRanges := Project::create_periodic_report_ranges(
174+
__new__.mouStart,
175+
__new__.mouEnd,
176+
'3'
177+
),
178+
financialReports := (
179+
select default::FinancialReport
180+
filter .container.id = __old__.id
181+
),
182+
financialReportsForDeletion := (
183+
select financialReports
184+
filter not exists .reportFile
185+
),
186+
narrativeAndProgressReports := (
187+
select NarrativeReport union ProgressReport
188+
filter .container.id = __old__.id
168189
),
169-
reportsForDeletion := (
170-
select existingReports
190+
narrativeAndProgressReportsForDeletion := (
191+
select narrativeAndProgressReports
171192
filter not exists .reportFile
172-
)
193+
)
173194
select
174-
# start date, end date, or financial report period were deleted
175-
if not exists __new__.mouStart
176-
or not exists __new__.mouEnd
177-
or not exists __new__.financialReportPeriod then (
178-
for report in reportsForDeletion
195+
# start date or end date were deleted - delete all reports
196+
if not exists __new__.mouStart or not exists __new__.mouEnd then (
197+
for report in financialReportsForDeletion union narrativeAndProgressReportsForDeletion
179198
union (
180-
delete report
199+
delete report
181200
)
182-
)
183-
# financial report period changes, start date is moved forward, or end date is moved backward
184-
else if __old__.financialReportPeriod ?!= __new__.financialReportPeriod
185-
or (__new__.mouStart > __old__.mouStart) ?? false
186-
or (__new__.mouEnd < __old__.mouEnd) ?? false then (
187-
with
188-
periodsForInsertion := Project::determine_requested_report_periods(
189-
array_agg(requestedReportPeriods),
190-
array_agg(existingReports)
191-
),
192-
insertedReports := Project::insert_reports(array_agg(periodsForInsertion), __new__),
193-
deletedReports := (for report in reportsForDeletion
201+
# financial report period was deleted - delete financial reports
202+
) else if not exists __new__.financialReportPeriod then (
203+
for report in financialReportsForDeletion
204+
union (
205+
delete report
206+
)
207+
# start date is moved forward or end date is moved backward (contraction) - delete all
208+
# reports that are not in the new range
209+
) else if (__new__.mouStart > __old__.mouStart) ?? false or (__new__.mouEnd < __old__.mouEnd) ?? false then (
210+
for report in financialReportsForDeletion union narrativeAndProgressReportsForDeletion
194211
union (
195212
delete report
196-
filter report.period not in requestedReportPeriods
197-
))
198-
select insertedReports
199-
)
200-
# start or end dates otherwise change
201-
else if newMouStart ?!= oldMouStart
202-
or newMouEnd ?!= oldMouEnd then (
213+
filter report.period not in monthlyReportRanges union quarterlyReportRanges
214+
)
215+
# financial report period changes - delete all financial reports and insert new ones
216+
) else if __old__.financialReportPeriod ?!= __new__.financialReportPeriod then (
217+
with
218+
insertedFinancialReports := Project::insert_financial_reports(__new__, array_agg(financialReports),
219+
array_agg(monthlyReportRanges), array_agg(quarterlyReportRanges))
220+
for report in financialReportsForDeletion
221+
union (
222+
delete report
223+
)
224+
# start or end dates otherwise change (expansion) - insert all new reports
225+
) else if newMouStart ?!= oldMouStart or newMouEnd ?!= oldMouEnd then (
203226
with
204-
periodsForInsertion := Project::determine_requested_report_periods(
205-
array_agg(requestedReportPeriods),
206-
array_agg(existingReports)
207-
),
208-
insertedReports := Project::insert_reports(array_agg(periodsForInsertion), __new__)
209-
select insertedReports
227+
insertedFinancialReports := Project::insert_financial_reports(__new__, array_agg(financialReports),
228+
array_agg(monthlyReportRanges), array_agg(quarterlyReportRanges))
229+
select Project::insert_narrative_and_progress_reports(__new__, array_agg(quarterlyReportRanges),
230+
array_agg(narrativeAndProgressReports))
210231
# nothing changes
211232
) else (
212233
select <PeriodicReport>{}
@@ -300,7 +321,8 @@ module Project {
300321
};
301322
}
302323

303-
# creates the ranges for the given start and end dates based upon the given month interval
324+
# creates the ranges for the given start and end dates based upon the given month interval,
325+
# appending one additional range bound by matching end dates
304326
function create_periodic_report_ranges(startDate: cal::local_date, endDate: cal::local_date,
305327
monthInterval: str) -> set of range<cal::local_date>
306328
using (
@@ -333,16 +355,21 @@ module Project {
333355
select report.period
334356
)
335357
)
336-
select periodsForInsertion
358+
select periodsForInsertion
337359
);
338360

339-
function insert_reports(requestedReportPeriodsForInsertion: array<range<cal::local_date>>,
340-
newProject: default::Project) -> set of default::PeriodicReport
361+
function insert_narrative_and_progress_reports(newProject: default::Project,
362+
quarterlyReportRanges: array<range<cal::local_date>>,
363+
narrativeAndProgressReports: array<default::PeriodicReport>) -> set of default::PeriodicReport
341364
using (
342365
with
343-
financialReports := (for reportPeriod in array_unpack(requestedReportPeriodsForInsertion)
366+
periodsForInsertion := Project::determine_requested_report_periods(
367+
quarterlyReportRanges,
368+
narrativeAndProgressReports
369+
),
370+
narrativeReports := (for reportPeriod in periodsForInsertion
344371
union (
345-
insert default::FinancialReport {
372+
insert default::NarrativeReport {
346373
createdAt := datetime_of_statement(),
347374
modifiedAt := datetime_of_statement(),
348375
createdBy := assert_exists(global default::currentActor),
@@ -353,9 +380,9 @@ module Project {
353380
period := reportPeriod,
354381
}
355382
)),
356-
narrativeReports := (for reportPeriod in array_unpack(requestedReportPeriodsForInsertion)
383+
progressReports := (for reportPeriod in periodsForInsertion
357384
union (
358-
insert default::NarrativeReport {
385+
insert default::ProgressReport {
359386
createdAt := datetime_of_statement(),
360387
modifiedAt := datetime_of_statement(),
361388
createdBy := assert_exists(global default::currentActor),
@@ -366,6 +393,41 @@ module Project {
366393
period := reportPeriod,
367394
}
368395
))
369-
select financialReports union narrativeReports
396+
select narrativeReports union progressReports
370397
);
398+
399+
function insert_financial_reports(newProject: default::Project,
400+
financialReports: array<default::FinancialReport>,
401+
monthlyReportRanges: array<range<cal::local_date>>,
402+
quarterlyReportRanges: array<range<cal::local_date>>) -> set of default::PeriodicReport
403+
using (
404+
with
405+
periodsForInsertion := (
406+
select
407+
if newProject.financialReportPeriod = default::ReportPeriod.Monthly then (
408+
select Project::determine_requested_report_periods(
409+
monthlyReportRanges,
410+
financialReports
411+
)
412+
) else (
413+
select Project::determine_requested_report_periods(
414+
quarterlyReportRanges,
415+
financialReports
416+
)
417+
)
418+
)
419+
select (for reportPeriod in periodsForInsertion
420+
union (
421+
insert default::FinancialReport {
422+
createdAt := datetime_of_statement(),
423+
modifiedAt := datetime_of_statement(),
424+
createdBy := assert_exists(global default::currentActor),
425+
modifiedBy := assert_exists(global default::currentActor),
426+
project := newProject,
427+
projectContext := newProject.projectContext,
428+
container := newProject,
429+
period := reportPeriod,
430+
}
431+
))
432+
);
371433
}

0 commit comments

Comments
 (0)