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 all commits
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
194 changes: 194 additions & 0 deletions dbschema/project.esdl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

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?

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 (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@CarsonF - I'm getting this error here.

Window_and_project_esdl_—_cord-api-v3

I'm trying to avoid ProgressReports here since that's a different flow. Any ideas on the best way to handle this?

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 {
Expand Down Expand Up @@ -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,
Copy link
Member

Choose a reason for hiding this comment

The 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...
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