Skip to content

Commit e08f699

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

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
@@ -130,7 +130,7 @@ module default {
130130
}
131131
);
132132

133-
trigger createPeriodicReportsOnInsert after insert for each do (
133+
trigger createPeriodicReports after insert for each do (
134134
with
135135
interval := (select
136136
if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3'),
@@ -150,7 +150,6 @@ module default {
150150
projectContext := __new__.projectContext,
151151
container := __new__,
152152
period := reportRange
153-
#receivedDate := __new__.financialReportReceivedAt, //TODO - what is this at the project level?
154153
}),
155154
(insert default::NarrativeReport {
156155
createdAt := datetime_of_statement(),
@@ -163,7 +162,120 @@ module default {
163162
period := reportRange
164163
})
165164
)
166-
)
165+
);
166+
167+
trigger addRemovePeriodicReports after update for each
168+
when (
169+
__old__.mouStart ?!= __new__.mouStart
170+
or __old__.mouEnd ?!= __new__.mouEnd
171+
or __old__.financialReportPeriod ?!= __new__.financialReportPeriod
172+
)
173+
do (
174+
with
175+
newMouStart := __new__.mouStart,
176+
oldMouStart := __old__.mouStart,
177+
newMouEnd := __new__.mouEnd,
178+
oldMouEnd := __old__.mouEnd,
179+
existingReports := (
180+
select FinancialReport union NarrativeReport
181+
filter .container.id = __old__.id
182+
),
183+
interval := (
184+
select (if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3')
185+
),
186+
requestedReportPeriods := Project::create_periodic_report_ranges(
187+
__new__.mouStart,
188+
__new__.mouEnd,
189+
interval
190+
),
191+
reportsForDeletion := (
192+
select existingReports
193+
filter not exists .reportFile
194+
)
195+
select if not exists __new__.mouStart or not exists __new__.mouEnd then (
196+
for report in reportsForDeletion
197+
union (
198+
delete report
199+
)
200+
)
201+
else if __old__.financialReportPeriod ?!= __new__.financialReportPeriod
202+
or (__new__.mouStart > __old__.mouStart) ?? false
203+
or (__new__.mouEnd < __old__.mouEnd) ?? false then (
204+
with
205+
requestedReportPeriodsForInsertion := (
206+
select requestedReportPeriods
207+
filter requestedReportPeriods not in existingReports.period
208+
),
209+
financialReports := (for reportPeriod in requestedReportPeriodsForInsertion
210+
union (
211+
insert default::FinancialReport {
212+
createdAt := datetime_of_statement(),
213+
modifiedAt := datetime_of_statement(),
214+
createdBy := assert_exists(global currentActor),
215+
modifiedBy := assert_exists(global currentActor),
216+
project := __new__,
217+
projectContext := __new__.projectContext,
218+
container := __new__,
219+
period := reportPeriod,
220+
}
221+
)),
222+
narrativeReports := (for reportPeriod in requestedReportPeriodsForInsertion
223+
union (
224+
insert default::NarrativeReport {
225+
createdAt := datetime_of_statement(),
226+
modifiedAt := datetime_of_statement(),
227+
createdBy := assert_exists(global currentActor),
228+
modifiedBy := assert_exists(global currentActor),
229+
project := __new__,
230+
projectContext := __new__.projectContext,
231+
container := __new__,
232+
period := reportPeriod,
233+
}
234+
))
235+
for report in reportsForDeletion
236+
union (
237+
delete report
238+
filter report.period not in requestedReportPeriods
239+
)
240+
)
241+
else if newMouStart ?!= oldMouStart
242+
or newMouEnd ?!= oldMouEnd then (
243+
with
244+
requestedReportPeriodsForInsertion := (
245+
select requestedReportPeriods
246+
filter requestedReportPeriods not in existingReports.period
247+
),
248+
financialReports := (for reportPeriod in requestedReportPeriodsForInsertion
249+
union (
250+
insert default::FinancialReport {
251+
createdAt := datetime_of_statement(),
252+
modifiedAt := datetime_of_statement(),
253+
createdBy := assert_exists(global currentActor),
254+
modifiedBy := assert_exists(global currentActor),
255+
project := __new__,
256+
projectContext := __new__.projectContext,
257+
container := __new__,
258+
period := reportPeriod,
259+
}
260+
)),
261+
narrativeReports := (for reportPeriod in requestedReportPeriodsForInsertion
262+
union (
263+
insert default::NarrativeReport {
264+
createdAt := datetime_of_statement(),
265+
modifiedAt := datetime_of_statement(),
266+
createdBy := assert_exists(global currentActor),
267+
modifiedBy := assert_exists(global currentActor),
268+
project := __new__,
269+
projectContext := __new__.projectContext,
270+
container := __new__,
271+
period := reportPeriod,
272+
}
273+
))
274+
select financialReports union narrativeReports
275+
) else (
276+
select <PeriodicReport>{}
277+
)
278+
);
167279
}
168280

169281
abstract type TranslationProject extending Project {
@@ -268,5 +380,42 @@ module Project {
268380
select range(<cal::local_date>firstDayOfMonth, <cal::local_date>lastDayOfMonth)
269381
))
270382
select reportPeriodRanges
271-
)
383+
);
384+
385+
# function insertReports(requestedReportPeriods: set of range<cal::local_date>,
386+
# existingReports: array<PeriodicReport>, newProject: default::Project) -> optional str
387+
# using (
388+
# with
389+
# requestedReportPeriodsForInsertion := (
390+
# select requestedReportPeriods
391+
# filter requestedReportPeriods not in existingReports.period
392+
# ),
393+
# financialReports := (for reportPeriod in requestedReportPeriodsForInsertion
394+
# union (
395+
# insert default::FinancialReport {
396+
# createdAt := datetime_of_statement(),
397+
# modifiedAt := datetime_of_statement(),
398+
# createdBy := assert_exists(global default::currentActor),
399+
# modifiedBy := assert_exists(global default::currentActor),
400+
# project := __new__,
401+
# projectContext := __new__.projectContext,
402+
# container := __new__,
403+
# period := reportPeriod,
404+
# }
405+
# )),
406+
# narrativeReports := (for reportPeriod in requestedReportPeriodsForInsertion
407+
# union (
408+
# insert default::NarrativeReport {
409+
# createdAt := datetime_of_statement(),
410+
# modifiedAt := datetime_of_statement(),
411+
# createdBy := assert_exists(global default::currentActor),
412+
# modifiedBy := assert_exists(global default::currentActor),
413+
# project := __new__,
414+
# projectContext := __new__.projectContext,
415+
# container := __new__,
416+
# period := reportPeriod,
417+
# }
418+
# ))
419+
# select financialReports union narrativeReports
420+
# )
272421
}

0 commit comments

Comments
 (0)