Skip to content

Commit d1bf281

Browse files
committed
Add addRemovePeriodicReports trigger in Project schema
- Handles periodic report date ranges expanding and shrinking - Handles periodic report frequency changing
1 parent 4e9bff7 commit d1bf281

File tree

1 file changed

+153
-4
lines changed

1 file changed

+153
-4
lines changed

dbschema/project.gel

Lines changed: 153 additions & 4 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,7 +154,120 @@ module default {
155154
period := reportRange
156155
})
157156
)
158-
)
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+
newMouStart := __new__.mouStart,
168+
oldMouStart := __old__.mouStart,
169+
newMouEnd := __new__.mouEnd,
170+
oldMouEnd := __old__.mouEnd,
171+
existingReports := (
172+
select FinancialReport union NarrativeReport
173+
filter .container.id = __old__.id
174+
),
175+
interval := (
176+
select (if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3')
177+
),
178+
requestedReportPeriods := Project::create_periodic_report_ranges(
179+
__new__.mouStart,
180+
__new__.mouEnd,
181+
interval
182+
),
183+
reportsForDeletion := (
184+
select existingReports
185+
filter not exists .reportFile
186+
)
187+
select if not exists __new__.mouStart or not exists __new__.mouEnd then (
188+
for report in reportsForDeletion
189+
union (
190+
delete report
191+
)
192+
)
193+
else if __old__.financialReportPeriod ?!= __new__.financialReportPeriod
194+
or (__new__.mouStart > __old__.mouStart) ?? false
195+
or (__new__.mouEnd < __old__.mouEnd) ?? false then (
196+
with
197+
requestedReportPeriodsForInsertion := (
198+
select requestedReportPeriods
199+
filter requestedReportPeriods not in existingReports.period
200+
),
201+
financialReports := (for reportPeriod in requestedReportPeriodsForInsertion
202+
union (
203+
insert default::FinancialReport {
204+
createdAt := datetime_of_statement(),
205+
modifiedAt := datetime_of_statement(),
206+
createdBy := assert_exists(global currentActor),
207+
modifiedBy := assert_exists(global currentActor),
208+
project := __new__,
209+
projectContext := __new__.projectContext,
210+
container := __new__,
211+
period := reportPeriod,
212+
}
213+
)),
214+
narrativeReports := (for reportPeriod in requestedReportPeriodsForInsertion
215+
union (
216+
insert default::NarrativeReport {
217+
createdAt := datetime_of_statement(),
218+
modifiedAt := datetime_of_statement(),
219+
createdBy := assert_exists(global currentActor),
220+
modifiedBy := assert_exists(global currentActor),
221+
project := __new__,
222+
projectContext := __new__.projectContext,
223+
container := __new__,
224+
period := reportPeriod,
225+
}
226+
))
227+
for report in reportsForDeletion
228+
union (
229+
delete report
230+
filter report.period not in requestedReportPeriods
231+
)
232+
)
233+
else if newMouStart ?!= oldMouStart
234+
or newMouEnd ?!= oldMouEnd then (
235+
with
236+
requestedReportPeriodsForInsertion := (
237+
select requestedReportPeriods
238+
filter requestedReportPeriods not in existingReports.period
239+
),
240+
financialReports := (for reportPeriod in requestedReportPeriodsForInsertion
241+
union (
242+
insert default::FinancialReport {
243+
createdAt := datetime_of_statement(),
244+
modifiedAt := datetime_of_statement(),
245+
createdBy := assert_exists(global currentActor),
246+
modifiedBy := assert_exists(global currentActor),
247+
project := __new__,
248+
projectContext := __new__.projectContext,
249+
container := __new__,
250+
period := reportPeriod,
251+
}
252+
)),
253+
narrativeReports := (for reportPeriod in requestedReportPeriodsForInsertion
254+
union (
255+
insert default::NarrativeReport {
256+
createdAt := datetime_of_statement(),
257+
modifiedAt := datetime_of_statement(),
258+
createdBy := assert_exists(global currentActor),
259+
modifiedBy := assert_exists(global currentActor),
260+
project := __new__,
261+
projectContext := __new__.projectContext,
262+
container := __new__,
263+
period := reportPeriod,
264+
}
265+
))
266+
select financialReports union narrativeReports
267+
) else (
268+
select <PeriodicReport>{}
269+
)
270+
);
159271
}
160272

161273
abstract type TranslationProject extending Project {
@@ -260,5 +372,42 @@ module Project {
260372
select range(<cal::local_date>firstDayOfMonth, <cal::local_date>lastDayOfMonth)
261373
))
262374
select reportPeriodRanges
263-
)
375+
);
376+
377+
# function insertReports(requestedReportPeriods: set of range<cal::local_date>,
378+
# existingReports: array<PeriodicReport>, newProject: default::Project) -> optional str
379+
# using (
380+
# with
381+
# requestedReportPeriodsForInsertion := (
382+
# select requestedReportPeriods
383+
# filter requestedReportPeriods not in existingReports.period
384+
# ),
385+
# financialReports := (for reportPeriod in requestedReportPeriodsForInsertion
386+
# union (
387+
# insert default::FinancialReport {
388+
# createdAt := datetime_of_statement(),
389+
# modifiedAt := datetime_of_statement(),
390+
# createdBy := assert_exists(global default::currentActor),
391+
# modifiedBy := assert_exists(global default::currentActor),
392+
# project := __new__,
393+
# projectContext := __new__.projectContext,
394+
# container := __new__,
395+
# period := reportPeriod,
396+
# }
397+
# )),
398+
# narrativeReports := (for reportPeriod in requestedReportPeriodsForInsertion
399+
# union (
400+
# insert default::NarrativeReport {
401+
# createdAt := datetime_of_statement(),
402+
# modifiedAt := datetime_of_statement(),
403+
# createdBy := assert_exists(global default::currentActor),
404+
# modifiedBy := assert_exists(global default::currentActor),
405+
# project := __new__,
406+
# projectContext := __new__.projectContext,
407+
# container := __new__,
408+
# period := reportPeriod,
409+
# }
410+
# ))
411+
# select financialReports union narrativeReports
412+
# )
264413
}

0 commit comments

Comments
 (0)