Skip to content

Commit 69bf5e6

Browse files
committed
Refactor to account for progess reports in triggers
1 parent 01bdb5a commit 69bf5e6

File tree

1 file changed

+211
-98
lines changed

1 file changed

+211
-98
lines changed

dbschema/project.gel

Lines changed: 211 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,14 @@ module default {
132132
);
133133

134134
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 (
135+
when (exists __new__.mouStart and exists __new__.mouEnd)
136+
do (
141137
with
142-
interval := (select
143-
if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3'),
144-
reportRanges := Project::create_periodic_report_ranges(
145-
__new__.mouStart,
146-
__new__.mouEnd,
147-
interval
148-
),
149-
insertedReports := Project::insert_reports(array_agg(reportRanges), __new__)
150-
select insertedReports
138+
insertedFinancialReports := Project::insert_financial_reports(__new__,
139+
<array<default::FinancialReport>>{}),
140+
insertedNarrativeAndProgressReports := Project::insert_narrative_and_progress_reports(__new__,
141+
<array<default::PeriodicReport>>{}),
142+
select insertedFinancialReports union insertedNarrativeAndProgressReports
151143
);
152144

153145
trigger addRemovePeriodicReports after update for each
@@ -162,59 +154,93 @@ module default {
162154
oldMouStart := __old__.mouStart,
163155
newMouEnd := __new__.mouEnd,
164156
oldMouEnd := __old__.mouEnd,
165-
existingReports := (
166-
select FinancialReport union NarrativeReport
167-
filter .container.id = __old__.id
157+
monthlyReportRanges := (
158+
select
159+
if not exists __new__.mouStart or not exists __new__.mouEnd then (
160+
select <range<cal::local_date>>{}
161+
) else (
162+
Project::create_periodic_report_ranges(
163+
assert_exists(__new__.mouStart),
164+
assert_exists(__new__.mouEnd),
165+
'1'
166+
)
167+
)
168+
),
169+
quarterlyReportRanges := (
170+
select
171+
if not exists __new__.mouStart or not exists __new__.mouEnd then (
172+
select <range<cal::local_date>>{}
173+
) else (
174+
Project::create_periodic_report_ranges(
175+
assert_exists(__new__.mouStart),
176+
assert_exists(__new__.mouEnd),
177+
'3'
178+
)
179+
)
168180
),
169-
interval := (
170-
select (if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3')
181+
financialReports := (
182+
select default::FinancialReport
183+
filter .container.id = __old__.id
171184
),
172-
requestedReportPeriods := Project::create_periodic_report_ranges(
173-
__new__.mouStart,
174-
__new__.mouEnd,
175-
interval
185+
allReports := (
186+
select PeriodicReport
187+
filter .container.id = __old__.id
176188
),
177-
reportsForDeletion := (
178-
select existingReports
189+
narrativeAndProgressReports := (
190+
select PeriodicReport
191+
filter .container.id = __old__.id
192+
and .id not in (
193+
for report in financialReports
194+
select report.id
195+
)
196+
),
197+
financialReportsForDeletion := (
198+
select financialReports
179199
filter not exists .reportFile
180-
)
200+
),
201+
allReportsForDeletion := (
202+
select allReports
203+
filter not exists .reportFile
204+
)
205+
# debug := default::log_it("In addRemovePeriodicReports - monthlyReportRanges", <json>array_agg(monthlyReportRanges)),
206+
# debugTwo := default::log_it("In addRemovePeriodicReports - quarterlyReportRanges", <json>array_agg(quarterlyReportRanges)),
207+
# debugThree := default::log_it("In addRemovePeriodicReports - unioned", <json>array_agg(monthlyReportRanges union quarterlyReportRanges))
181208
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 (
186-
for report in reportsForDeletion
209+
# start or end date was deleted - delete all reports
210+
if not exists __new__.mouStart or not exists __new__.mouEnd then (
211+
for report in allReportsForDeletion
187212
union (
188-
delete report
213+
delete report
189214
)
190-
)
191-
# financial report period changes, start date is moved forward, or end date is moved backward
192-
else if __old__.financialReportPeriod ?!= __new__.financialReportPeriod
193-
or (__new__.mouStart > __old__.mouStart) ?? false
194-
or (__new__.mouEnd < __old__.mouEnd) ?? false then (
195-
with
196-
periodsForInsertion := Project::determine_requested_report_periods(
197-
array_agg(requestedReportPeriods),
198-
array_agg(existingReports)
199-
),
200-
insertedReports := Project::insert_reports(array_agg(periodsForInsertion), __new__),
201-
deletedReports := (for report in reportsForDeletion
215+
# financial report period was deleted - delete financial reports
216+
) else if not exists __new__.financialReportPeriod then (
217+
for report in financialReportsForDeletion
218+
union (
219+
delete report
220+
)
221+
# start date is moved forward or end date is moved backward (contraction) - delete all
222+
# reports that are not in the new range
223+
) else if (__new__.mouStart > __old__.mouStart) ?? false or (__new__.mouEnd < __old__.mouEnd) ?? false then (
224+
for report in allReportsForDeletion
202225
union (
203226
delete report
204-
filter report.period not in requestedReportPeriods
205-
))
206-
select insertedReports
207-
)
208-
# start or end dates otherwise change
209-
else if newMouStart ?!= oldMouStart
210-
or newMouEnd ?!= oldMouEnd then (
227+
filter report.period not in (monthlyReportRanges union quarterlyReportRanges)
228+
)
229+
# financial report period changes - delete all financial reports and insert new ones
230+
) else if __old__.financialReportPeriod ?!= __new__.financialReportPeriod then (
231+
with
232+
deletedFinancialReports := (
233+
for report in financialReportsForDeletion
234+
union (
235+
delete report
236+
)),
237+
insertedFinancialReports := Project::insert_financial_reports(__new__, array_agg(financialReports))
238+
select insertedFinancialReports
239+
# start or end dates otherwise change (expansion) - insert all new reports
240+
) else if newMouStart ?!= oldMouStart or newMouEnd ?!= oldMouEnd then (
211241
with
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
242+
insertedFinancialReports := Project::insert_financial_reports(__new__, array_agg(financialReports))
243+
select Project::insert_narrative_and_progress_reports(__new__, array_agg(narrativeAndProgressReports))
218244
# nothing changes
219245
) else (
220246
select <PeriodicReport>{}
@@ -308,11 +334,13 @@ module Project {
308334
};
309335
}
310336

311-
# creates the ranges for the given start and end dates based upon the given month interval
337+
# creates the ranges for the given start and end dates based upon the given month interval,
338+
# appending one additional range bound by matching end dates
312339
function create_periodic_report_ranges(startDate: cal::local_date, endDate: cal::local_date,
313340
monthInterval: str) -> set of range<cal::local_date>
314341
using (
315342
with
343+
# debug := default::log_it("In create_periodic_report_ranges - input", <json>array_agg({<str>startDate, <str>endDate, monthInterval})),
316344
reportingPeriod := range(<cal::local_date>startDate, <cal::local_date>endDate),
317345
reportPeriodStartDates := range_unpack(reportingPeriod, <cal::date_duration>(monthInterval ++ ' month')),
318346
reportPeriodRanges := (
@@ -326,54 +354,139 @@ module Project {
326354
additionalReportPeriodRange := (
327355
select range(<cal::local_date>endDate, <cal::local_date>endDate, inc_upper := true)
328356
)
357+
# debugTwo := default::log_it("In create_periodic_report_ranges - reportPeriodRanges", <json>array_agg(reportPeriodRanges)),
358+
# debugThree := default::log_it("In create_periodic_report_ranges - additionalReportPeriodRange", <json>array_agg(additionalReportPeriodRange))
329359
select reportPeriodRanges union additionalReportPeriodRange
330360
);
331361

332-
function determine_requested_report_periods(requestedReportPeriods: array<range<cal::local_date>>,
333-
existingReports: array<default::PeriodicReport>) -> set of range<cal::local_date>
362+
function determine_requested_report_periods(monthInterval: str, newProject: default::Project,
363+
existingReports: optional array<default::PeriodicReport>) -> set of range<cal::local_date> {
364+
volatility := 'Modifying';
334365
using (
335-
with
336-
distinctRequestedReportPeriods := distinct array_unpack(requestedReportPeriods),
337-
periodsForInsertion := (
366+
select
367+
if not exists newProject.mouStart or not exists newProject.mouEnd or not exists monthInterval then (
368+
select <range<cal::local_date>>{}
369+
) else (
370+
with
371+
requestedReportPeriods := Project::create_periodic_report_ranges(
372+
assert_exists(newProject.mouStart), assert_exists(newProject.mouEnd), monthInterval
373+
),
374+
distinctRequestedReportPeriods := distinct requestedReportPeriods,
375+
# debug := default::log_it("In determine_requested_report_periods - interval", <json>monthInterval),
376+
# debugTwo := default::log_it("In determine_requested_report_periods - existingReports", <json>existingReports),
377+
# debugThree := default::log_it("In determine_requested_report_periods - distinctRequestedReportPeriods", <json>array_agg(distinctRequestedReportPeriods))
378+
select
379+
if exists existingReports then (
380+
with
381+
something := (
382+
select distinctRequestedReportPeriods
383+
# filter out report periods that already exist in current reports
384+
filter distinctRequestedReportPeriods not in (
385+
for report in array_unpack(existingReports)
386+
select report.period
387+
)
388+
),
389+
# debug := default::log_it("In determine_requested_report_periods - something", <json>array_agg(something)),
390+
select (something)
391+
) else (
338392
select distinctRequestedReportPeriods
339-
filter distinctRequestedReportPeriods not in (
340-
for report in array_unpack(existingReports)
341-
select report.period
342-
)
343393
)
344-
select periodsForInsertion
345-
);
394+
)
395+
);
396+
}
346397

347-
function insert_reports(requestedReportPeriodsForInsertion: array<range<cal::local_date>>,
348-
newProject: default::Project) -> set of default::PeriodicReport
398+
function insert_narrative_and_progress_reports(newProject: default::Project,
399+
narrativeAndProgressReports: optional array<default::PeriodicReport>) -> set of default::PeriodicReport
349400
using (
350401
with
351-
financialReports := (for reportPeriod in array_unpack(requestedReportPeriodsForInsertion)
352-
union (
353-
insert default::FinancialReport {
354-
createdAt := datetime_of_statement(),
355-
modifiedAt := datetime_of_statement(),
356-
createdBy := assert_exists(global default::currentActor),
357-
modifiedBy := assert_exists(global default::currentActor),
358-
project := newProject,
359-
projectContext := newProject.projectContext,
360-
container := newProject,
361-
period := reportPeriod,
362-
}
363-
)),
364-
narrativeReports := (for reportPeriod in array_unpack(requestedReportPeriodsForInsertion)
365-
union (
366-
insert default::NarrativeReport {
367-
createdAt := datetime_of_statement(),
368-
modifiedAt := datetime_of_statement(),
369-
createdBy := assert_exists(global default::currentActor),
370-
modifiedBy := assert_exists(global default::currentActor),
371-
project := newProject,
372-
projectContext := newProject.projectContext,
373-
container := newProject,
374-
period := reportPeriod,
402+
# debug := default::log_it("In insert_narrative_and_progress_reports", <json>{}),
403+
periodsForInsertion := Project::determine_requested_report_periods(
404+
'3',
405+
newProject,
406+
narrativeAndProgressReports
407+
),
408+
narrativeReports := (
409+
for reportPeriod in periodsForInsertion
410+
union (
411+
insert default::NarrativeReport {
412+
createdAt := datetime_of_statement(),
413+
modifiedAt := datetime_of_statement(),
414+
createdBy := assert_exists(global default::currentActor),
415+
modifiedBy := assert_exists(global default::currentActor),
416+
project := newProject,
417+
projectContext := newProject.projectContext,
418+
container := newProject,
419+
period := reportPeriod,
420+
}
421+
)
422+
),
423+
projectWithEngagements := (
424+
select newProject {
425+
engagements := .<project[is default::LanguageEngagement]
375426
}
376-
))
377-
select financialReports union narrativeReports
427+
),
428+
progressReports := (
429+
for reportPeriod in periodsForInsertion
430+
union (
431+
for engagement in projectWithEngagements.engagements
432+
union (
433+
insert default::ProgressReport {
434+
createdAt := datetime_of_statement(),
435+
modifiedAt := datetime_of_statement(),
436+
createdBy := assert_exists(global default::currentActor),
437+
modifiedBy := assert_exists(global default::currentActor),
438+
project := newProject,
439+
projectContext := newProject.projectContext,
440+
engagement := engagement,
441+
container := engagement,
442+
period := reportPeriod,
443+
}
444+
)
445+
)
446+
)
447+
select narrativeReports
448+
);
449+
450+
function insert_financial_reports(newProject: default::Project,
451+
financialReports: optional array<default::FinancialReport>) -> set of default::FinancialReport
452+
using (
453+
select
454+
if exists newProject.financialReportPeriod then (
455+
with
456+
periodsForInsertion := (
457+
select
458+
if newProject.financialReportPeriod ?= default::ReportPeriod.Monthly then (
459+
select Project::determine_requested_report_periods(
460+
'1',
461+
newProject,
462+
financialReports
463+
)
464+
) else (
465+
select Project::determine_requested_report_periods(
466+
'3',
467+
newProject,
468+
financialReports
469+
)
470+
)
471+
)
472+
# debug := default::log_it("In insert_financial_reports - periodsForInsertion", <json>array_agg(periodsForInsertion)),
473+
select (
474+
for reportPeriod in periodsForInsertion
475+
union (
476+
insert default::FinancialReport {
477+
createdAt := datetime_of_statement(),
478+
modifiedAt := datetime_of_statement(),
479+
createdBy := assert_exists(global default::currentActor),
480+
modifiedBy := assert_exists(global default::currentActor),
481+
project := newProject,
482+
projectContext := newProject.projectContext,
483+
container := newProject,
484+
period := reportPeriod,
485+
}
486+
)
487+
)
488+
) else (
489+
select <default::FinancialReport>{}
490+
)
378491
);
379492
}

0 commit comments

Comments
 (0)