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

2427 internal users can transfer a facility or operation #2557

Merged
merged 53 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
c2cb573
chore: add new user role to permissions
Sepehr-Sobhani Dec 6, 2024
6e62445
chore: admin panel tweaks
Sepehr-Sobhani Dec 6, 2024
a8361b5
chore: fix type hints
Sepehr-Sobhani Dec 6, 2024
c443150
chore: add new api endpoint tag
Sepehr-Sobhani Dec 6, 2024
3d59799
chore: exclude `all` key from generic errors
Sepehr-Sobhani Dec 6, 2024
ad4ced0
chore: operation and designated operator fixture tweaks
Sepehr-Sobhani Dec 6, 2024
d62b410
chore: new fixture for cas analyst user
Sepehr-Sobhani Dec 6, 2024
b4a908d
chore: one more approved industry user admin
Sepehr-Sobhani Dec 6, 2024
6b99ddc
chore: more data model methods
Sepehr-Sobhani Dec 6, 2024
6db5193
chore: remove unused jsonschema file
Sepehr-Sobhani Dec 6, 2024
d1b51f7
chore: tweak endpoint, schema and types to fetch non paginated data a…
Sepehr-Sobhani Dec 6, 2024
e11ba88
chore: fix dashboard tile href
Sepehr-Sobhani Dec 6, 2024
e584d84
chore: renaming - using ts instead of tsx
Sepehr-Sobhani Dec 6, 2024
4599f90
chore: update transfer event fixtures
Sepehr-Sobhani Dec 6, 2024
58ce5fa
chore: update transfer event data model
Sepehr-Sobhani Dec 6, 2024
062942a
chore: add status to operation designated operator timeline table
Sepehr-Sobhani Dec 6, 2024
2816b5d
feat: add transfer event feature
Sepehr-Sobhani Dec 6, 2024
afd6221
chore: add transfer event endpoint and schema
Sepehr-Sobhani Dec 6, 2024
49a8a56
chore: handle server errors when rows is undefined
Sepehr-Sobhani Dec 6, 2024
47c7222
chore: resolve a new uuid when we have similar transfer events - caus…
Sepehr-Sobhani Dec 6, 2024
0e9fb44
chore: add the new fetch api to the index file
Sepehr-Sobhani Dec 6, 2024
e9474f4
chore: fix import
Sepehr-Sobhani Dec 6, 2024
5196f68
chore: post-rebase migration fix
Sepehr-Sobhani Dec 6, 2024
dc7d1e0
chore: fix tests after latest transfer event modifications
Sepehr-Sobhani Dec 8, 2024
f0b06bb
chore: remove v2 ref after endpoint refactor
Sepehr-Sobhani Dec 8, 2024
43b2aae
chore: transfer event is a v2 feature
Sepehr-Sobhani Dec 8, 2024
5e854ad
chore: post-rebase fix
Sepehr-Sobhani Dec 8, 2024
8ef2f99
chore: add cas analyst to endpoint tests
Sepehr-Sobhani Dec 8, 2024
aba9f4f
chore: add extra check to prevent creating wrong transfer event
Sepehr-Sobhani Dec 8, 2024
35bc6df
chore: make eslint happy
Sepehr-Sobhani Dec 8, 2024
0780dce
chore: add more baker recipes
Sepehr-Sobhani Dec 11, 2024
93826a2
chore: make test name singular
Sepehr-Sobhani Dec 11, 2024
29f4edc
chore: fix schema name
Sepehr-Sobhani Dec 11, 2024
0f2b1c4
chore: renaming tweaks
Sepehr-Sobhani Dec 11, 2024
2f22c75
test: add backend tests
Sepehr-Sobhani Dec 11, 2024
72de680
test: add frontend tests
Sepehr-Sobhani Dec 11, 2024
2d15e58
test: raise error if response is undefined
Sepehr-Sobhani Dec 11, 2024
f1ad03d
chore: renaming tweaks and cover edge cases
Sepehr-Sobhani Dec 11, 2024
ddf8676
chore: return undefined if pageData is undefined
Sepehr-Sobhani Dec 11, 2024
8c95f93
chore: fix dashboard tile allowedRules issue
Sepehr-Sobhani Dec 12, 2024
4134e7f
chore: remove leftover
Sepehr-Sobhani Dec 12, 2024
b0ee873
chore: fix vitest tests after using the session util
Sepehr-Sobhani Dec 12, 2024
2668fbf
docs: add docs about transfer events
Sepehr-Sobhani Dec 12, 2024
acbf3dd
chore: make pre-commit happy
Sepehr-Sobhani Dec 12, 2024
1f9a07a
chore: make reg1 e2e tests happy
Sepehr-Sobhani Dec 12, 2024
8c20d4d
chore: make sonarcloud happy
Sepehr-Sobhani Dec 12, 2024
20306cd
chore: remove transfer sub-dashboard for internal users
Sepehr-Sobhani Dec 13, 2024
fa1a690
chore: remove transfer sub-dashboard for internal users
Sepehr-Sobhani Dec 13, 2024
21bc2e9
chore: implement review suggestions, remove redundant endpoints and u…
Sepehr-Sobhani Dec 13, 2024
f59d3f2
chore: using ts instead of tsx
Sepehr-Sobhani Dec 13, 2024
eabb344
chore: remove unused mock
Sepehr-Sobhani Dec 13, 2024
2439d76
chore: fix post-rebase migration issue
Sepehr-Sobhani Dec 13, 2024
0a49409
chore: fix vitest
Sepehr-Sobhani Dec 14, 2024
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
1 change: 0 additions & 1 deletion bc_obps/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def reset_dashboard_data() -> None:
'common/fixtures/dashboard/administration/internal.json',
'common/fixtures/dashboard/operators/internal.json',
'common/fixtures/dashboard/registration/external.json',
'common/fixtures/dashboard/registration/internal.json',
'common/fixtures/dashboard/reporting/external.json',
'common/fixtures/dashboard/reporting/internal.json',
'common/fixtures/dashboard/coam/external.json',
Expand Down
140 changes: 88 additions & 52 deletions docs/backend/events/transfer.md
Original file line number Diff line number Diff line change
@@ -1,67 +1,103 @@
# Transfer Events
Sepehr-Sobhani marked this conversation as resolved.
Show resolved Hide resolved

This document explains how transfer events are processed in the system. There are two types of transfer events: Facility
transfers and Operation transfers.
This document explains how transfer events are processed in the system, including the interaction between models,
statuses, and dates. There are two types of transfer events: **Facility transfers** and **Operation transfers**.

---

## What are Transfer Events?

Transfer events are used to manage the movement of facilities between operations or the transfer of an entire operation
to a new operator. These events can be initiated for a specific date in the future, or they can be set to take effect
immediately.
Transfer events manage the movement of facilities between operations or the transfer of an entire operation to a new
operator. These events can be initiated for a specific future date or take effect immediately.

When an internal user creates a transfer, it is stored in the **TransferEvent** model. The event's status and effective
date control when and how the transfer is processed.

---

## How do Transfer Events Work?
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest adding something about how the models, statuses, and dates all interact throughout this process. That was the most confusing part for me. I'll type up how I ~think it works for looking at the code, feel free to use some of this in the docs if it's helpful.

When an internal user creates a transfer, it goes into the TransferEvent model with a status of TO_BE_TRANSFERRED and the effective date from the form. (Provided there aren't any existing transfers that it overlaps with.) If the effective date is in the past, the transfers is immediately processed and a record is created in the timeline table with a status of ACTIVE and an empty end date. If there was already a timeline record, that one gets an end date set and the status set to TRANSFERRED. The TransferEvent model then gets the status set to COMPLETE? (When would we ever use the TRANSFERRED status in this model?)

If the effective date is in the future, nothing happens to the timeline tables. Once the date passes, the chronjob adds to the timeline tables as described above.

As a result of all this, we can always trust that the record in the timeline table that doesn't have an end_date is the active one. (We never check or filter for ACTIVE status, but checking for a null end date is the same thing.)


The system categorizes transfer events based on the entity being transferred: Facility or Operation.
### The TransferEvent Model and Statuses

- **TO_BE_TRANSFERRED:** Set when the event is created and pending processing. This status indicates the event is not
yet finalized.
- **TRANSFERRED:** Used when the event has been processed and its associated timeline updates are complete. (See notes
below on when this is used.)
- **COMPLETE:** Marks the event as fully finalized in certain contexts.

### Effective Date Behavior

- **Past or Today:** If the event's effective date is today or earlier, the system processes it immediately:
- The event's timelines are updated to reflect the transfer.
- The event's status is updated (see the “Processing a Transfer Event” section).
- **Future:** If the effective date is in the future, no immediate action is taken. A **cron job** later processes the
event when the effective date arrives.

### Interaction with Timeline Models

Timeline models record the historical and current relationships between facilities, operations, and operators. The
system ensures there is always a single active timeline record with no end date for each entity.

---

## Types of Transfer Events

### Facility Transfers

Facility transfers involve:

### Transferring Facilities
- Moving facilities from one operation to another.
- Updating both the originating and receiving operations’ timelines.

- The facility's current operation (the one it's assigned to before the transfer).
- The new operation (the one the facility will be assigned to after the transfer).
- The effective date of the transfer.
### Operation Transfers

### Transferring Operations
Operation transfers involve:

- The Operation's current operator (the one it's assigned to before the transfer).
- The operation being transferred.
- The new operator that will take over the operation.
- The effective date of the transfer.
- Moving an operation from one operator to another.
- Updating both the originating and receiving operators’ timelines.

---

## Processing Transfer Events

The system processes transfer events differently depending on their effective date:

- **Today or Past Effective Date:** If a transfer event's effective date is today or in the past, the transfer is
processed immediately upon creation.
- **Future Effective Date:** Transfer events with a future effective date are handled by a scheduled background job (
cron job). This job runs periodically and utilizes the `process_due_transfer_events` service to identify and process
any transfer events that have become due (effective date has arrived).

## Processing a Transfer Event

Once a transfer event is identified for processing (either immediately or by the cron job), the system performs the
following actions specific to the transfer type:

### Facility Transfer

1. **For each facility in the transfer:**
- Identify the current timeline record linking the facility to its original operation using the
`FacilityDesignatedOperationTimelineService.get_current_timeline` service.
- If a current timeline exists, update its end date and status to reflect the transfer using the
`FacilityDesignatedOperationTimelineService.set_timeline_status_and_end_date` service. The new status will be set
to `TRANSFERRED` and the end date will be set to the transfer's effective date.
- Create a new timeline record using the
`FacilityDesignatedOperationTimelineDataAccessService.create_facility_designated_operation_timeline` service. This
new record links the facility to the new operation, sets the start date to the transfer's effective date, and sets
the status to `ACTIVE`.

### Operation Transfer

1. Identify the current timeline record linking the operation to its original operator using the
`OperationDesignatedOperatorTimelineService.get_current_timeline` service.
2. If a current timeline exists, update its end date and status to reflect the transfer using the
`OperationDesignatedOperatorTimelineService.set_timeline_status_and_end_date` service. The new status will be set to
`TRANSFERRED` and the end date will be set to the transfer's effective date.
3. Create a new timeline record using the
`OperationDesignatedOperatorTimelineDataAccessService.create_operation_designated_operator_timeline` service. This
new record links the operation to the new operator, sets the start date to the transfer's effective date, and sets
the status to `ACTIVE`.
### General Workflow

1. **Validation:** The system ensures no overlapping active transfer events for the same entities.
2. **Timeline Updates:**
- For facilities: The current timeline's status is updated to `TRANSFERRED`, and its end date is set.
- For operations: The same update is applied to the operation's timeline.
- New timelines are created for the receiving operation/operator, with a status of `ACTIVE` and no end date.
3. **Status Update:** The transfer event's status is set to `TRANSFERRED` once the timeline updates are complete.

### Immediate vs. Scheduled Processing

- **Immediate:** For past or present effective dates, timelines are updated immediately.
- **Scheduled:** For future effective dates, a background job periodically processes due events.

### Timeline Integrity

- At any given time, the timeline record with no `end_date` represents the current, active state of the entity.
- The `ACTIVE` status in timeline records reinforces this, but null `end_date` is the definitive check for activeness.

---

### Detailed Example: Facility Transfers

1. **Validation:** Ensures no overlapping transfer events for the facility or operation.
2. **Timeline Update:**
- The facility's current timeline (linking it to its current operation) is updated with:
- `end_date`: The transfer's effective date.
- `status`: `TRANSFERRED`.
- A new timeline record is created with:
- `start_date`: The transfer's effective date.
- `status`: `ACTIVE`.
- The new operation association.
3. **TransferEvent Status:** Updated to `TRANSFERRED` once processing is complete.

### Detailed Example: Operation Transfers

1. **Validation:** Ensures no overlapping transfer events for the operation or operator.
2. **Timeline Update:**
- The operation's current timeline (linking it to its current operator) is updated similarly to facilities.
- A new timeline record is created for the new operator.
3. **TransferEvent Status:** Updated to `TRANSFERRED`.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is great, Sepehr, thanks for including so much detail and the examples!