Skip to content

Commit f594e65

Browse files
committed
Adds addRemovePeriodicReports trigger in Project schema
- Handles periodic report date ranges expanding and shrinking - Handles periodic report frequency changing
1 parent 712f624 commit f594e65

File tree

1 file changed

+150
-2
lines changed

1 file changed

+150
-2
lines changed

dbschema/project.gel

Lines changed: 150 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ module default {
122122
}
123123
);
124124

125-
trigger createPeriodicReportsOnInsert after insert for each do (
125+
trigger createPeriodicReports after insert for each do (
126126
with
127127
interval := (select
128128
if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3'),
@@ -142,7 +142,6 @@ module default {
142142
projectContext := __new__.projectContext,
143143
container := __new__,
144144
period := reportRange
145-
#receivedDate := __new__.financialReportReceivedAt, //TODO - what is this at the project level?
146145
}),
147146
(insert default::NarrativeReport {
148147
createdAt := datetime_of_statement(),
@@ -155,6 +154,115 @@ module default {
155154
period := reportRange
156155
})
157156
)
157+
);
158+
159+
trigger addRemovePeriodicReports after update for each
160+
when (
161+
__old__.mouStart ?!= __new__.mouStart
162+
or __old__.mouEnd ?!= __new__.mouEnd
163+
or __old__.financialReportPeriod ?!= __new__.financialReportPeriod
164+
)
165+
do (
166+
with
167+
existingReports := (
168+
select PeriodicReport
169+
filter .container.id = __old__.id
170+
),
171+
interval := (
172+
select (if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3')
173+
),
174+
requestedReportPeriods := Project::create_periodic_report_ranges(
175+
__new__.mouStart,
176+
__new__.mouEnd,
177+
interval
178+
),
179+
result := (if __old__.financialReportPeriod ?!= __new__.financialReportPeriod then (
180+
with
181+
reportPeriodsWithoutFiles := (
182+
select existingReports
183+
filter not exists .reportFile
184+
),
185+
deletedReportPeriods := (
186+
for reportPeriod in reportPeriodsWithoutFiles
187+
union (
188+
delete reportPeriod
189+
)
190+
),
191+
financialReports := (for reportPeriod in requestedReportPeriods
192+
union (
193+
insert default::FinancialReport {
194+
createdAt := datetime_of_statement(),
195+
modifiedAt := datetime_of_statement(),
196+
createdBy := assert_exists(global currentActor),
197+
modifiedBy := assert_exists(global currentActor),
198+
project := __new__,
199+
projectContext := __new__.projectContext,
200+
container := __new__,
201+
period := reportPeriod,
202+
}
203+
)),
204+
narrativeReports := (for reportPeriod in requestedReportPeriods
205+
union (
206+
insert default::NarrativeReport {
207+
createdAt := datetime_of_statement(),
208+
modifiedAt := datetime_of_statement(),
209+
createdBy := assert_exists(global currentActor),
210+
modifiedBy := assert_exists(global currentActor),
211+
project := __new__,
212+
projectContext := __new__.projectContext,
213+
container := __new__,
214+
period := reportPeriod,
215+
}
216+
))
217+
select financialReports union narrativeReports
218+
) else (
219+
with
220+
requestedReportPeriodsForInsertion := (
221+
select requestedReportPeriods
222+
filter requestedReportPeriods not in existingReports.period
223+
),
224+
requestedReportPeriodsForDeletion := (
225+
select existingReports.period
226+
filter existingReports.period not in requestedReportPeriods
227+
),
228+
applicableReportsForDeletion := (
229+
select FinancialReport union NarrativeReport
230+
filter .period in requestedReportPeriodsForDeletion
231+
and not exists .reportFile
232+
),
233+
financialReports := (for reportPeriod in requestedReportPeriods
234+
union (
235+
insert default::FinancialReport {
236+
createdAt := datetime_of_statement(),
237+
modifiedAt := datetime_of_statement(),
238+
createdBy := assert_exists(global currentActor),
239+
modifiedBy := assert_exists(global currentActor),
240+
project := __new__,
241+
projectContext := __new__.projectContext,
242+
container := __new__,
243+
period := reportPeriod,
244+
}
245+
)),
246+
narrativeReports := (for reportPeriod in requestedReportPeriods
247+
union (
248+
insert default::NarrativeReport {
249+
createdAt := datetime_of_statement(),
250+
modifiedAt := datetime_of_statement(),
251+
createdBy := assert_exists(global currentActor),
252+
modifiedBy := assert_exists(global currentActor),
253+
project := __new__,
254+
projectContext := __new__.projectContext,
255+
container := __new__,
256+
period := reportPeriod,
257+
}
258+
)),
259+
deletedReports := (for report in applicableReportsForDeletion
260+
union (
261+
delete report
262+
))
263+
select financialReports union narrativeReports
264+
))
265+
select result
158266
)
159267
}
160268

@@ -261,4 +369,44 @@ module Project {
261369
))
262370
select reportPeriodRanges
263371
)
372+
373+
# TODO - Toying with the idea of abstracting some of this logic in some capacity...
374+
# function insertReportPeriods(existingReportPeriods: set of range<cal::local_date>,
375+
# requestedReportPeriods: set of range<cal::local_date>) -> optional str
376+
# using (
377+
# with
378+
# reportPeriodsWithoutFiles := (
379+
# select existingReportPeriods
380+
# filter not exists .reportFile
381+
# ),
382+
# deletedReportPeriods : = (
383+
# for reportPeriod in reportPeriodsWithoutFiles
384+
# union (
385+
# delete reportPeriod
386+
# )
387+
# )
388+
# for reportPeriod in requestedReportPeriods
389+
# union (
390+
# (insert default::FinancialReport {
391+
# createdAt := datetime_of_statement(),
392+
# modifiedAt := datetime_of_statement(),
393+
# createdBy := assert_exists(global currentActor),
394+
# modifiedBy := assert_exists(global currentActor),
395+
# project := __new__,
396+
# projectContext := __new__.projectContext,
397+
# container := __new__,
398+
# period := reportPeriod
399+
# }),
400+
# (insert default::NarrativeReport {
401+
# createdAt := datetime_of_statement(),
402+
# modifiedAt := datetime_of_statement(),
403+
# createdBy := assert_exists(global currentActor),
404+
# modifiedBy := assert_exists(global currentActor),
405+
# project := __new__,
406+
# projectContext := __new__.projectContext,
407+
# container := __new__,
408+
# period := reportPeriod
409+
# })
410+
# )
411+
# )
264412
}

0 commit comments

Comments
 (0)