Skip to content

Commit f1364f0

Browse files
committed
WIP
1 parent 69bf5e6 commit f1364f0

File tree

1 file changed

+105
-88
lines changed

1 file changed

+105
-88
lines changed

dbschema/project.gel

Lines changed: 105 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ module default {
135135
when (exists __new__.mouStart and exists __new__.mouEnd)
136136
do (
137137
with
138-
insertedFinancialReports := Project::insert_financial_reports(__new__,
138+
insertedFinancialReports := Project::create_financial_reports(__new__,
139139
<array<default::FinancialReport>>{}),
140140
insertedNarrativeAndProgressReports := Project::insert_narrative_and_progress_reports(__new__,
141141
<array<default::PeriodicReport>>{}),
@@ -202,9 +202,6 @@ module default {
202202
select allReports
203203
filter not exists .reportFile
204204
)
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))
208205
select
209206
# start or end date was deleted - delete all reports
210207
if not exists __new__.mouStart or not exists __new__.mouEnd then (
@@ -218,14 +215,27 @@ module default {
218215
union (
219216
delete report
220217
)
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
225-
union (
226-
delete report
227-
filter report.period not in (monthlyReportRanges union quarterlyReportRanges)
228-
)
218+
# start date is moved forward (contraction) - delete all reports that are not in the new range
219+
) else if (__new__.mouStart > __old__.mouStart) ?? false then (
220+
for report in allReportsForDeletion
221+
union (
222+
delete report
223+
filter report.period not in (monthlyReportRanges union quarterlyReportRanges)
224+
)
225+
# end date is moved backward (contraction) - delete all reports that are not in the new range
226+
# and add an additional report period range
227+
) else if (__new__.mouEnd < __old__.mouEnd) ?? false then (
228+
with
229+
deletedReports := (
230+
for report in allReportsForDeletion
231+
union (
232+
delete report
233+
filter report.period not in (monthlyReportRanges union quarterlyReportRanges)
234+
)),
235+
additionalReportPeriodRange := (select range(<cal::local_date>newMouEnd, <cal::local_date>newMouEnd, inc_upper := true)),
236+
financialReports := Project::insert_financial_reports(__new__, array_agg(additionalReportPeriodRange)),
237+
narrativeReports := Project::insert_narrative_reports(__new__, array_agg(additionalReportPeriodRange))
238+
select Project::insert_progress_reports(__new__, array_agg(additionalReportPeriodRange))
229239
# financial report period changes - delete all financial reports and insert new ones
230240
) else if __old__.financialReportPeriod ?!= __new__.financialReportPeriod then (
231241
with
@@ -234,12 +244,11 @@ module default {
234244
union (
235245
delete report
236246
)),
237-
insertedFinancialReports := Project::insert_financial_reports(__new__, array_agg(financialReports))
238-
select insertedFinancialReports
247+
select Project::create_financial_reports(__new__, array_agg(financialReports))
239248
# start or end dates otherwise change (expansion) - insert all new reports
240249
) else if newMouStart ?!= oldMouStart or newMouEnd ?!= oldMouEnd then (
241250
with
242-
insertedFinancialReports := Project::insert_financial_reports(__new__, array_agg(financialReports))
251+
insertedFinancialReports := Project::create_financial_reports(__new__, array_agg(financialReports))
243252
select Project::insert_narrative_and_progress_reports(__new__, array_agg(narrativeAndProgressReports))
244253
# nothing changes
245254
) else (
@@ -335,12 +344,12 @@ module Project {
335344
}
336345

337346
# 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
347+
# and creates a single additional range that is bound by the given end date, with the upper
348+
# bound inclusive
339349
function create_periodic_report_ranges(startDate: cal::local_date, endDate: cal::local_date,
340350
monthInterval: str) -> set of range<cal::local_date>
341351
using (
342352
with
343-
# debug := default::log_it("In create_periodic_report_ranges - input", <json>array_agg({<str>startDate, <str>endDate, monthInterval})),
344353
reportingPeriod := range(<cal::local_date>startDate, <cal::local_date>endDate),
345354
reportPeriodStartDates := range_unpack(reportingPeriod, <cal::date_duration>(monthInterval ++ ' month')),
346355
reportPeriodRanges := (
@@ -351,11 +360,8 @@ module Project {
351360
select range(<cal::local_date>firstDayOfMonth, <cal::local_date>firstDayOfNextPeriod)
352361
)
353362
),
354-
additionalReportPeriodRange := (
355-
select range(<cal::local_date>endDate, <cal::local_date>endDate, inc_upper := true)
356-
)
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))
363+
additionalReportPeriodRange := (select range(<cal::local_date>endDate, <cal::local_date>endDate, inc_upper := true)),
364+
debug := default::log_it("In create_periodic_report_ranges", <json>array_agg(additionalReportPeriodRange)),
359365
select reportPeriodRanges union additionalReportPeriodRange
360366
);
361367

@@ -372,9 +378,6 @@ module Project {
372378
assert_exists(newProject.mouStart), assert_exists(newProject.mouEnd), monthInterval
373379
),
374380
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))
378381
select
379382
if exists existingReports then (
380383
with
@@ -386,47 +389,104 @@ module Project {
386389
select report.period
387390
)
388391
),
389-
# debug := default::log_it("In determine_requested_report_periods - something", <json>array_agg(something)),
390392
select (something)
391393
) else (
392394
select distinctRequestedReportPeriods
393395
)
394396
)
395397
);
396-
}
398+
}
397399

398400
function insert_narrative_and_progress_reports(newProject: default::Project,
399401
narrativeAndProgressReports: optional array<default::PeriodicReport>) -> set of default::PeriodicReport
400402
using (
401403
with
402-
# debug := default::log_it("In insert_narrative_and_progress_reports", <json>{}),
403404
periodsForInsertion := Project::determine_requested_report_periods(
404405
'3',
405406
newProject,
406407
narrativeAndProgressReports
407408
),
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-
),
409+
narrativeReports := Project::insert_narrative_reports(newProject, array_agg(periodsForInsertion)),
410+
progressReports := Project::insert_progress_reports(newProject, array_agg(periodsForInsertion))
411+
select narrativeReports
412+
);
413+
414+
function create_financial_reports(newProject: default::Project,
415+
financialReports: optional array<default::FinancialReport>) -> set of default::FinancialReport
416+
using (
417+
select
418+
if exists newProject.financialReportPeriod then (
419+
with
420+
periodsForInsertion := (
421+
select
422+
if newProject.financialReportPeriod ?= default::ReportPeriod.Monthly then (
423+
select Project::determine_requested_report_periods(
424+
'1',
425+
newProject,
426+
financialReports
427+
)
428+
) else (
429+
select Project::determine_requested_report_periods(
430+
'3',
431+
newProject,
432+
financialReports
433+
)
434+
)
435+
),
436+
project := newProject,
437+
select Project::insert_financial_reports(project, array_agg(periodsForInsertion))
438+
) else (
439+
select <default::FinancialReport>{}
440+
)
441+
);
442+
443+
function insert_financial_reports(newProject: default::Project,
444+
periodsForInsertion: array<range<cal::local_date>>) -> set of default::FinancialReport
445+
using (
446+
for reportPeriod in array_unpack(periodsForInsertion)
447+
union (
448+
insert default::FinancialReport {
449+
createdAt := datetime_of_statement(),
450+
modifiedAt := datetime_of_statement(),
451+
createdBy := assert_exists(global default::currentActor),
452+
modifiedBy := assert_exists(global default::currentActor),
453+
project := newProject,
454+
projectContext := newProject.projectContext,
455+
container := newProject,
456+
period := reportPeriod,
457+
}
458+
)
459+
);
460+
461+
function insert_narrative_reports(newProject: default::Project,
462+
periodsForInsertion: array<range<cal::local_date>>) -> set of default::NarrativeReport
463+
using (
464+
for reportPeriod in array_unpack(periodsForInsertion)
465+
union (
466+
insert default::NarrativeReport {
467+
createdAt := datetime_of_statement(),
468+
modifiedAt := datetime_of_statement(),
469+
createdBy := assert_exists(global default::currentActor),
470+
modifiedBy := assert_exists(global default::currentActor),
471+
project := newProject,
472+
projectContext := newProject.projectContext,
473+
container := newProject,
474+
period := reportPeriod,
475+
}
476+
)
477+
);
478+
479+
function insert_progress_reports(newProject: default::Project,
480+
periodsForInsertion: array<range<cal::local_date>>) -> set of default::ProgressReport
481+
using (
482+
with
423483
projectWithEngagements := (
424484
select newProject {
425485
engagements := .<project[is default::LanguageEngagement]
426486
}
427487
),
428488
progressReports := (
429-
for reportPeriod in periodsForInsertion
489+
for reportPeriod in array_unpack(periodsForInsertion)
430490
union (
431491
for engagement in projectWithEngagements.engagements
432492
union (
@@ -444,49 +504,6 @@ module Project {
444504
)
445505
)
446506
)
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-
)
507+
select progressReports
491508
);
492509
}

0 commit comments

Comments
 (0)