Skip to content

Commit cf03a32

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

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

dbschema/project.gel

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,41 @@ module default {
129129
projectContext := __new__.projectContext,
130130
}
131131
);
132+
133+
trigger createPeriodicReportsOnInsert after insert for each do (
134+
with
135+
interval := (select
136+
if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3'),
137+
reportRanges := Project::create_periodic_report_ranges(
138+
__new__.mouStart,
139+
__new__.mouEnd,
140+
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+
#receivedDate := __new__.financialReportReceivedAt, //TODO - what is this at the project level?
154+
}),
155+
(insert default::NarrativeReport {
156+
createdAt := datetime_of_statement(),
157+
modifiedAt := datetime_of_statement(),
158+
createdBy := assert_exists(global currentActor),
159+
modifiedBy := assert_exists(global currentActor),
160+
project := __new__,
161+
projectContext := __new__.projectContext,
162+
container := __new__,
163+
period := reportRange
164+
})
165+
)
166+
)
132167
}
133168

134169
abstract type TranslationProject extending Project {
@@ -216,4 +251,22 @@ module Project {
216251
on target delete allow;
217252
};
218253
}
254+
255+
# creates the ranges for the given start and end dates based upon the given month interval
256+
function create_periodic_report_ranges(startDate: cal::local_date, endDate: cal::local_date,
257+
monthInterval: str) -> set of range<cal::local_date>
258+
using (
259+
with
260+
reportingPeriod := range(<cal::local_date>startDate, <cal::local_date>endDate),
261+
reportPeriodStartDates := range_unpack(reportingPeriod, <cal::date_duration>(monthInterval ++ ' month')),
262+
reportPeriodRanges := (
263+
for firstDayOfMonth in reportPeriodStartDates
264+
union (
265+
with
266+
firstDayOfNextMonth := (select firstDayOfMonth + <cal::relative_duration>(monthInterval ++ ' month')),
267+
lastDayOfMonth := firstDayOfNextMonth - <cal::relative_duration>'1 day'
268+
select range(<cal::local_date>firstDayOfMonth, <cal::local_date>lastDayOfMonth)
269+
))
270+
select reportPeriodRanges
271+
)
219272
}

0 commit comments

Comments
 (0)