-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: develop
Are you sure you want to change the base?
[EdgeDB] - Add trigger to appropriately create financial/narrative report upon project creation #3279
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'), | ||
|
@@ -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(), | ||
|
@@ -153,7 +152,107 @@ module default { | |
period := reportRange | ||
}) | ||
) | ||
) | ||
); | ||
|
||
trigger addRemovePeriodicReports after update for each | ||
when ( | ||
__old__.mouStart != __new__.mouStart | ||
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 { | ||
|
@@ -259,4 +358,44 @@ module Project { | |
)) | ||
select reportPeriodRanges | ||
) | ||
|
||
# TODO - Toying with the idea of abstracting some of this logic in some capacity... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
# }) | ||
# ) | ||
# ) | ||
} |
There was a problem hiding this comment.
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.