Skip to content

Commit cf4bbc4

Browse files
committed
Add EdgeDB trigger to appropriately create financial/narrative reports upon project creation
1 parent e65866f commit cf4bbc4

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

dbschema/project.esdl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,41 @@ module default {
119119
projectContext := __new__.projectContext,
120120
}
121121
);
122+
123+
trigger createPeriodicReportsOnInsert after insert for each do (
124+
with
125+
interval := (select
126+
if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3'),
127+
reportRanges := Project::create_periodic_report_ranges(
128+
__new__.mouStart,
129+
__new__.mouEnd,
130+
interval
131+
)
132+
for reportRange in reportRanges
133+
union (
134+
(insert default::FinancialReport {
135+
createdAt := datetime_of_statement(),
136+
modifiedAt := datetime_of_statement(),
137+
createdBy := assert_exists(global currentActor),
138+
modifiedBy := assert_exists(global currentActor),
139+
project := __new__,
140+
projectContext := __new__.projectContext,
141+
container := __new__,
142+
period := reportRange
143+
#receivedDate := __new__.financialReportReceivedAt, //TODO - what is this at the project level?
144+
}),
145+
(insert default::NarrativeReport {
146+
createdAt := datetime_of_statement(),
147+
modifiedAt := datetime_of_statement(),
148+
createdBy := assert_exists(global currentActor),
149+
modifiedBy := assert_exists(global currentActor),
150+
project := __new__,
151+
projectContext := __new__.projectContext,
152+
container := __new__,
153+
period := reportRange
154+
})
155+
)
156+
)
122157
}
123158

124159
abstract type TranslationProject extending Project {
@@ -206,4 +241,22 @@ module Project {
206241
on target delete allow;
207242
};
208243
}
244+
245+
# creates the ranges for the given start and end dates based upon the given month interval
246+
function create_periodic_report_ranges(startDate: cal::local_date, endDate: cal::local_date,
247+
monthInterval: str) -> set of range<cal::local_date>
248+
using (
249+
with
250+
reportingPeriod := range(<cal::local_date>startDate, <cal::local_date>endDate),
251+
reportPeriodStartDates := range_unpack(reportingPeriod, <cal::date_duration>(monthInterval ++ ' month')),
252+
reportPeriodRanges := (
253+
for firstDayOfMonth in reportPeriodStartDates
254+
union (
255+
with
256+
firstDayOfNextMonth := (select firstDayOfMonth + <cal::relative_duration>(monthInterval ++ ' month')),
257+
lastDayOfMonth := firstDayOfNextMonth - <cal::relative_duration>'1 day'
258+
select range(<cal::local_date>firstDayOfMonth, <cal::local_date>lastDayOfMonth)
259+
))
260+
select reportPeriodRanges
261+
)
209262
}

0 commit comments

Comments
 (0)