Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EdgeDB] - Add trigger to appropriately create financial/narrative report upon project creation #3279

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 142 additions & 3 deletions dbschema/project.esdl
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ module default {
}
);

trigger createPeriodicReportsOnInsert after insert for each do (
trigger createPeriodicReports after insert for each do (
with
interval := (select
if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3'),
Expand All @@ -140,7 +140,6 @@ module default {
projectContext := __new__.projectContext,
container := __new__,
period := reportRange
#receivedDate := __new__.financialReportReceivedAt, //TODO - what is this at the project level?
}),
(insert default::NarrativeReport {
createdAt := datetime_of_statement(),
Expand All @@ -153,7 +152,107 @@ module default {
period := reportRange
})
)
)
);

trigger addRemovePeriodicReports after update for each
when (
__old__.mouStart != __new__.mouStart
Copy link
Member

@CarsonF CarsonF Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you want ?!= on these 3 comparisons. Since they are all optional fields. We want null on either side to count as a valid value to compare against, instead of skipping the comparison all together when null is encountered. It's a semi well known footgun that's complained about. Docs have notes on these ?-prefixed operators.

or __old__.mouEnd != __new__.mouEnd
or __old__.financialReportPeriod != __new__.financialReportPeriod
)
do (
with
existingReportPeriods := (
select FinancialReport
filter .container.id = __old__.id
).period,
interval := (
select (if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3')
),
requestedReportPeriods := Project::create_periodic_report_ranges(
__new__.mouStart,
__new__.mouEnd,
interval
)
if __old__.financialReportPeriod != __new__.financialReportPeriod (
CarsonF marked this conversation as resolved.
Show resolved Hide resolved
with
reportPeriodsWithoutFiles := (
select existingReportPeriods
filter not exists .reportFile
),
deletedReportPeriods : = (
for reportPeriod in reportPeriodsWithoutFiles
union (
delete reportPeriod
)
)
for reportPeriod in requestedReportPeriods
union (
(insert default::FinancialReport {
createdAt := datetime_of_statement(),
modifiedAt := datetime_of_statement(),
createdBy := assert_exists(global currentActor),
modifiedBy := assert_exists(global currentActor),
project := __new__,
projectContext := __new__.projectContext,
container := __new__,
period := reportPeriod
}),
(insert default::NarrativeReport {
createdAt := datetime_of_statement(),
modifiedAt := datetime_of_statement(),
createdBy := assert_exists(global currentActor),
modifiedBy := assert_exists(global currentActor),
project := __new__,
projectContext := __new__.projectContext,
container := __new__,
period := reportPeriod
})
)
) else (
with
requestedReportPeriodsForInsertion := (
select requestedReportPeriods
filter requestedReportPeriods not in existingReportPeriods
),
requestedReportPeriodsForDeletion := (
select existingReportPeriods
filter existingReportPeriods not in requestedReportPeriods
),
applicableReportPeriodsForDeletion := (
select PeriodicReport[is FinancialReport | NarrativeReport]
filter .period in requestedReportPeriodsForDeletion
and not exists .reportFile
),
insertedReportPeriods := (for reportPeriod in requestedReportPeriodsForInsertion
union (
(insert default::FinancialReport {
createdAt := datetime_of_statement(),
modifiedAt := datetime_of_statement(),
createdBy := assert_exists(global currentActor),
modifiedBy := assert_exists(global currentActor),
project := __new__,
projectContext := __new__.projectContext,
container := __new__,
period := reportPeriod
}),
(insert default::NarrativeReport {
createdAt := datetime_of_statement(),
modifiedAt := datetime_of_statement(),
createdBy := assert_exists(global currentActor),
modifiedBy := assert_exists(global currentActor),
project := __new__,
projectContext := __new__.projectContext,
container := __new__,
period := reportPeriod
})
))
for reportPeriod in applicableReportPeriodsForDeletion
union (
delete reportPeriod
)
)
);
}

abstract type TranslationProject extending Project {
Expand Down Expand Up @@ -259,4 +358,44 @@ module Project {
))
select reportPeriodRanges
)

# TODO - Toying with the idea of abstracting some of this logic in some capacity...
Copy link
Contributor Author

@bryanjnelson bryanjnelson Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CarsonF - After the trigger code settles down I think there's some opportunity for some common methods like I started here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think functions can mutate data right now. I don't really understand the limitation.

# function insertReportPeriods(existingReportPeriods: set of range<cal::local_date>,
# requestedReportPeriods: set of range<cal::local_date>) -> optional str
# using (
# with
# reportPeriodsWithoutFiles := (
# select existingReportPeriods
# filter not exists .reportFile
# ),
# deletedReportPeriods : = (
# for reportPeriod in reportPeriodsWithoutFiles
# union (
# delete reportPeriod
# )
# )
# for reportPeriod in requestedReportPeriods
# union (
# (insert default::FinancialReport {
# createdAt := datetime_of_statement(),
# modifiedAt := datetime_of_statement(),
# createdBy := assert_exists(global currentActor),
# modifiedBy := assert_exists(global currentActor),
# project := __new__,
# projectContext := __new__.projectContext,
# container := __new__,
# period := reportPeriod
# }),
# (insert default::NarrativeReport {
# createdAt := datetime_of_statement(),
# modifiedAt := datetime_of_statement(),
# createdBy := assert_exists(global currentActor),
# modifiedBy := assert_exists(global currentActor),
# project := __new__,
# projectContext := __new__.projectContext,
# container := __new__,
# period := reportPeriod
# })
# )
# )
}
Loading