-
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 all commits
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 |
---|---|---|
|
@@ -119,6 +119,142 @@ module default { | |
projectContext := __new__.projectContext, | ||
} | ||
); | ||
|
||
trigger createPeriodicReports after insert for each do ( | ||
with | ||
interval := (select | ||
if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3'), | ||
reportRanges := Project::create_periodic_report_ranges( | ||
__new__.mouStart, | ||
__new__.mouEnd, | ||
interval | ||
) | ||
for reportRange in reportRanges | ||
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 := reportRange | ||
}), | ||
(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 := reportRange | ||
}) | ||
) | ||
); | ||
|
||
trigger addRemovePeriodicReports after update for each | ||
when ( | ||
__old__.mouStart ?!= __new__.mouStart | ||
or __old__.mouEnd ?!= __new__.mouEnd | ||
or __old__.financialReportPeriod ?!= __new__.financialReportPeriod | ||
) | ||
do ( | ||
with | ||
existingReports := ( | ||
select PeriodicReport | ||
filter .container.id = __old__.id | ||
), | ||
interval := ( | ||
select (if __new__.financialReportPeriod = default::ReportPeriod.Monthly then '1' else '3') | ||
), | ||
requestedReportPeriods := Project::create_periodic_report_ranges( | ||
__new__.mouStart, | ||
__new__.mouEnd, | ||
interval | ||
) | ||
select (if __old__.financialReportPeriod ?!= __new__.financialReportPeriod then ( | ||
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 - I'm getting this error here. I'm trying to avoid |
||
with | ||
reportPeriodsWithoutFiles := ( | ||
select existingReports | ||
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 existingReports.period | ||
), | ||
requestedReportPeriodsForDeletion := ( | ||
select existingReports.period | ||
filter existingReports.period not in requestedReportPeriods | ||
), | ||
applicableReportsForDeletion := ( | ||
select PeriodicReport | ||
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 report in applicableReportsForDeletion | ||
union ( | ||
delete report | ||
# filter report is typeof default::FinancialReport | ||
# or report is typeof default::NarrativeReport | ||
) | ||
)) | ||
); | ||
} | ||
|
||
abstract type TranslationProject extending Project { | ||
|
@@ -206,4 +342,62 @@ module Project { | |
on target delete allow; | ||
}; | ||
} | ||
|
||
# creates the ranges for the given start and end dates based upon the given month interval | ||
function create_periodic_report_ranges(startDate: cal::local_date, endDate: cal::local_date, | ||
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. This is wild. Just noting that I have not vetted this for accuracy. |
||
monthInterval: str) -> set of range<cal::local_date> | ||
using ( | ||
with | ||
reportingPeriod := range(<cal::local_date>startDate, <cal::local_date>endDate), | ||
reportPeriodStartDates := range_unpack(reportingPeriod, <cal::date_duration>(monthInterval ++ ' month')), | ||
reportPeriodRanges := ( | ||
for firstDayOfMonth in reportPeriodStartDates | ||
union ( | ||
with | ||
firstDayOfNextMonth := (select firstDayOfMonth + <cal::relative_duration>(monthInterval ++ ' month')), | ||
lastDayOfMonth := firstDayOfNextMonth - <cal::relative_duration>'1 day' | ||
select range(<cal::local_date>firstDayOfMonth, <cal::local_date>lastDayOfMonth) | ||
)) | ||
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.
@CarsonF - I'm wondering if this is the place where we can also handle the case in which all 3 of these values exist for the first time? We could check if they've each changed, but also that none of them are null?
On a related note, can any of these values go back to null once set?