Skip to content

Commit c6d7120

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

File tree

1 file changed

+142
-3
lines changed

1 file changed

+142
-3
lines changed

dbschema/project.esdl

Lines changed: 142 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ module default {
120120
}
121121
);
122122

123-
trigger createPeriodicReportsOnInsert after insert for each do (
123+
trigger createPeriodicReports after insert for each do (
124124
with
125125
interval := (select
126126
if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3'),
@@ -140,7 +140,6 @@ module default {
140140
projectContext := __new__.projectContext,
141141
container := __new__,
142142
period := reportRange
143-
#receivedDate := __new__.financialReportReceivedAt, //TODO - what is this at the project level?
144143
}),
145144
(insert default::NarrativeReport {
146145
createdAt := datetime_of_statement(),
@@ -153,7 +152,107 @@ module default {
153152
period := reportRange
154153
})
155154
)
156-
)
155+
);
156+
157+
trigger addRemovePeriodicReports after update for each
158+
when (
159+
__old__.mouStart != __new__.mouStart
160+
or __old__.mouEnd != __new__.mouEnd
161+
or __old__.financialReportPeriod != __new__.financialReportPeriod
162+
)
163+
do (
164+
with
165+
existingReportPeriods := (
166+
select FinancialReport
167+
filter .container.id = __old__.id
168+
).period,
169+
interval := (
170+
select (if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3')
171+
),
172+
requestedReportPeriods := Project::create_periodic_report_ranges(
173+
__new__.mouStart,
174+
__new__.mouEnd,
175+
interval
176+
)
177+
if __old__.financialReportPeriod != __new__.financialReportPeriod (
178+
with
179+
reportPeriodsWithoutFiles := (
180+
select existingReportPeriods
181+
filter not exists .reportFile
182+
),
183+
deletedReportPeriods : = (
184+
for reportPeriod in reportPeriodsWithoutFiles
185+
union (
186+
delete reportPeriod
187+
)
188+
)
189+
for reportPeriod in requestedReportPeriods
190+
union (
191+
(insert default::FinancialReport {
192+
createdAt := datetime_of_statement(),
193+
modifiedAt := datetime_of_statement(),
194+
createdBy := assert_exists(global currentActor),
195+
modifiedBy := assert_exists(global currentActor),
196+
project := __new__,
197+
projectContext := __new__.projectContext,
198+
container := __new__,
199+
period := reportPeriod
200+
}),
201+
(insert default::NarrativeReport {
202+
createdAt := datetime_of_statement(),
203+
modifiedAt := datetime_of_statement(),
204+
createdBy := assert_exists(global currentActor),
205+
modifiedBy := assert_exists(global currentActor),
206+
project := __new__,
207+
projectContext := __new__.projectContext,
208+
container := __new__,
209+
period := reportPeriod
210+
})
211+
)
212+
) else (
213+
with
214+
requestedReportPeriodsForInsertion := (
215+
select requestedReportPeriods
216+
filter requestedReportPeriods not in existingReportPeriods
217+
),
218+
requestedReportPeriodsForDeletion := (
219+
select existingReportPeriods
220+
filter existingReportPeriods not in requestedReportPeriods
221+
),
222+
applicableReportPeriodsForDeletion := (
223+
select PeriodicReport[is FinancialReport | NarrativeReport]
224+
filter .period in requestedReportPeriodsForDeletion
225+
and not exists .reportFile
226+
),
227+
insertedReportPeriods := (for reportPeriod in requestedReportPeriodsForInsertion
228+
union (
229+
(insert default::FinancialReport {
230+
createdAt := datetime_of_statement(),
231+
modifiedAt := datetime_of_statement(),
232+
createdBy := assert_exists(global currentActor),
233+
modifiedBy := assert_exists(global currentActor),
234+
project := __new__,
235+
projectContext := __new__.projectContext,
236+
container := __new__,
237+
period := reportPeriod
238+
}),
239+
(insert default::NarrativeReport {
240+
createdAt := datetime_of_statement(),
241+
modifiedAt := datetime_of_statement(),
242+
createdBy := assert_exists(global currentActor),
243+
modifiedBy := assert_exists(global currentActor),
244+
project := __new__,
245+
projectContext := __new__.projectContext,
246+
container := __new__,
247+
period := reportPeriod
248+
})
249+
))
250+
for reportPeriod in applicableReportPeriodsForDeletion
251+
union (
252+
delete reportPeriod
253+
)
254+
)
255+
);
157256
}
158257

159258
abstract type TranslationProject extending Project {
@@ -259,4 +358,44 @@ module Project {
259358
))
260359
select reportPeriodRanges
261360
)
361+
362+
# TODO - Toying with the idea of abstracting some of this logic in some capacity...
363+
# function insertReportPeriods(existingReportPeriods: set of range<cal::local_date>,
364+
# requestedReportPeriods: set of range<cal::local_date>) -> optional str
365+
# using (
366+
# with
367+
# reportPeriodsWithoutFiles := (
368+
# select existingReportPeriods
369+
# filter not exists .reportFile
370+
# ),
371+
# deletedReportPeriods : = (
372+
# for reportPeriod in reportPeriodsWithoutFiles
373+
# union (
374+
# delete reportPeriod
375+
# )
376+
# )
377+
# for reportPeriod in requestedReportPeriods
378+
# union (
379+
# (insert default::FinancialReport {
380+
# createdAt := datetime_of_statement(),
381+
# modifiedAt := datetime_of_statement(),
382+
# createdBy := assert_exists(global currentActor),
383+
# modifiedBy := assert_exists(global currentActor),
384+
# project := __new__,
385+
# projectContext := __new__.projectContext,
386+
# container := __new__,
387+
# period := reportPeriod
388+
# }),
389+
# (insert default::NarrativeReport {
390+
# createdAt := datetime_of_statement(),
391+
# modifiedAt := datetime_of_statement(),
392+
# createdBy := assert_exists(global currentActor),
393+
# modifiedBy := assert_exists(global currentActor),
394+
# project := __new__,
395+
# projectContext := __new__.projectContext,
396+
# container := __new__,
397+
# period := reportPeriod
398+
# })
399+
# )
400+
# )
262401
}

0 commit comments

Comments
 (0)