Model brokers
components.payment_gateway.subcomponents.authorizations.models.brokers.authorization_request ¶
AuthorizationRequestModelBroker ¶
Bases: BaseModelBroker
SQL model broker for AuthorizationRequest model.
See: https://www.notion.so/alaninsurance/SQL-Model-Brokers-90876b7e739c4195a6a2a4106139ed38?pvs=4 ⧉
find_authorization_request_by_external_id
classmethod
¶
Get an authorization request by its provider-specific external ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
PaymentServiceProvider
|
The provider of the authorization request. |
required |
external_id
|
str
|
The external ID of the authorization request. |
required |
Returns:
| Type | Description |
|---|---|
AuthorizationRequest | None
|
The authorization request if found, else None. |
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/authorization_request.py
upsert_authorization_request
classmethod
¶
upsert_authorization_request(
session,
/,
*,
status,
authorization_metadata,
provider,
external_id,
)
Upsert an authorization request.
If an authorization request with the same provider and external ID does not exist, a new one is created and returned. If it does exist, the status is updated and the old status is returned along with the updated authorization request (the other fields are left untouched).
Possible status transitions in this case are:
- if the old status is active and the new status is approved or declined, the new status is set to complete
- if the old status is approved or declined and the new status is active, the new status is set to complete
- otherwise, the status is unchanged
Note
Although the upsert itself is atomic, the status update is not
because of limitations with on_conflict_do_update (we cannot
update the status conditionally based on the old status as required
by the transition rules expressed above). However this is OK because
our transition rules are idempotent, so there's no risk of ABA
problem here.
Which brings another issue about knowing whether a create or update
occurred. on_conflict_do_update provides no direct way of knowing
that, other than comparing the before and after values. We cannot
know the old value if it's also updated either, so we have to do it
in two steps and perform the transition programmatically.
A robust way of telling between create and update cases is to force the model's UUIID at creation time and comparing it with the actual value returned by the statement. If they are the same, it means that the row was just created. If they are different, it means that the row was updated, because this value will never change.
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/authorization_request.py
components.payment_gateway.subcomponents.authorizations.models.brokers.expense_category ¶
ExpenseCategoryModelBroker ¶
Bases: BaseModelBroker
SQL model broker for ExpenseCategory model.
See: https://www.notion.so/alaninsurance/SQL-Model-Brokers-90876b7e739c4195a6a2a4106139ed38?pvs=4 ⧉
create_expense_category
classmethod
¶
Create an Expense Category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
Machine-readable code of the category. |
required |
name
|
str
|
Human-readable name of the category. |
required |
description
|
str | None
|
Description of the category. |
None
|
reference
|
str | None
|
Application-specific reference to the category. |
None
|
Returns:
| Type | Description |
|---|---|
ExpenseCategory
|
The newly created Expense Category. |
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/expense_category.py
get_expense_category
classmethod
¶
Get an Expense Category by its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID
|
ID of the Expense Category. |
required |
Returns:
| Type | Description |
|---|---|
ExpenseCategory
|
The matched Expense Category. |
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/expense_category.py
get_expense_category_id_by_code
classmethod
¶
Get Expense Category ID by its code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
Expense Category code. |
required |
Returns:
| Type | Description |
|---|---|
UUID
|
Expense Category ID. |
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/expense_category.py
list_expense_category_ids_by_codes
classmethod
¶
Get Expense Category IDs by codes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
codes
|
list[str]
|
List of Expense Category codes. |
required |
Returns:
| Type | Description |
|---|---|
list[UUID]
|
List of Expense Category IDs. |
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/expense_category.py
terminate_expense_category
classmethod
¶
Terminate an Expense Category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expense_category_id
|
UUID
|
ID of the Expense Category to terminate. |
required |
Returns:
| Type | Description |
|---|---|
ExpenseCategory
|
The terminated Expense Category. |
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/expense_category.py
components.payment_gateway.subcomponents.authorizations.models.brokers.expense_tracker ¶
ExpenseTrackerModelBroker ¶
Bases: BaseModelBroker
SQL model broker for ExpenseTracker model.
See: https://www.notion.so/alaninsurance/SQL-Model-Brokers-90876b7e739c4195a6a2a4106139ed38?pvs=4 ⧉
Note: all methods are designed to be fast and atomic to be used in a real-time context. This comes with tradeoffs. Any change must be carefully evaluated.
check_and_update_expensed_credits
classmethod
¶
Update expensed credits for all Lines of Credit by given amount if all have enough available credits, else do nothing. This method is atomic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines_of_credit_ids
|
list[UUID]
|
list of Line of Credit IDs to check and update |
required |
amount
|
int
|
amount to update expensed credits. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if all Lines of Credit had enough available credits, else False (including if no matching rows have been found) |
Note
This won't refresh the model instances, so you have to do it explicitly if for some reason you need the updated values after calling the method (which we don't in the case for which it was designed).
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/expense_tracker.py
create_expense_tracker
classmethod
¶
Create a new ExpenseTracker for a Line of Credit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line_of_credit_id
|
UUID
|
Line of Credit ID. |
required |
credits_limit
|
int
|
Credits limit for the Line of Credit (in minor units). |
0
|
expensed_credits
|
int
|
Expensed credits for the Line of Credit (in minor units). |
0
|
Returns:
| Type | Description |
|---|---|
ExpenseTracker
|
The newly created Expense Tracker. |
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/expense_tracker.py
get_expense_tracker_by_line_of_credit_id
classmethod
¶
Get an Expense Tracker by Line of Credit ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line_of_credit_id
|
UUID
|
Line of Credit ID. |
required |
Returns:
| Type | Description |
|---|---|
ExpenseTracker
|
The matched Expense Tracker. |
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/expense_tracker.py
reset_credits
classmethod
¶
Reset the credits limit and expensed credits of a Line of Credit with provided values. This method is atomic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line_of_credit_id
|
UUID
|
Line of Credit ID. |
required |
credits_limit
|
int
|
New credits limit (in minor units). |
required |
expensed_credits
|
int
|
New expensed credits (in minor units). |
required |
Note
This won't refresh the model instances, so you have to do it explicitly if for some reason you need the updated values after calling the method (which we don't in the case for which it was designed).
Warning
This method doesn't check if the Line of Credit exists.
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/expense_tracker.py
set_credits_limit
classmethod
¶
Set credit limits for a specific Line of Credit by given amount unconditionally. This method is atomic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line_of_credit_id
|
UUID
|
Line of Credit ID to check and update |
required |
credits_limit
|
int
|
New credits limit (in minor units). |
required |
Note
This won't refresh the model instances, so you have to do it explicitly if for some reason you need the updated values after calling the method (which we don't in the case for which it was designed).
Warning
This method doesn't check if the Lines of Credit exist.
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/expense_tracker.py
update_expensed_credits
classmethod
¶
Update expensed credits for all Lines of Credit by given amount unconditionally. This method is atomic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines_of_credit_ids
|
list[UUID]
|
list of Line of Credit IDs to check and update |
required |
amount
|
int
|
amount to update expensed credits. |
required |
Note
This won't refresh the model instances, so you have to do it explicitly if for some reason you need the updated values after calling the method (which we don't in the case for which it was designed).
Warning
This method doesn't check if the Lines of Credit exist.
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/expense_tracker.py
components.payment_gateway.subcomponents.authorizations.models.brokers.line_of_credit ¶
LineOfCreditModelBroker ¶
Bases: BaseModelBroker
SQL model broker for LineOfCredit model.
See: https://www.notion.so/alaninsurance/SQL-Model-Brokers-90876b7e739c4195a6a2a4106139ed38?pvs=4 ⧉
create_line_of_credit
classmethod
¶
Create a Line of Credit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner_type
|
str
|
Application-specific type of owner for the Line of Credit. |
required |
owner_ref
|
UUID
|
Application-specific reference to the owner of the Line of Credit. |
required |
expense_category_id
|
UUID
|
ID of the Expense Category the Line of Credit belongs to. |
required |
Returns:
| Type | Description |
|---|---|
LineOfCredit
|
The newly created Line of Credit. |
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/line_of_credit.py
find_line_of_credit_id_for_owner_and_expense_category_id
classmethod
¶
find_line_of_credit_id_for_owner_and_expense_category_id(
session,
/,
*,
owner_type,
owner_ref,
expense_category_id,
)
Get Line of Credit ID for a specific owner and Expense Category ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner_type
|
str
|
Application-specific type of owner for the Line of Credit. |
required |
owner_ref
|
UUID
|
Application-specific reference to the owner of the Line of Credit. |
required |
expense_category_id
|
UUID
|
Expense Category IDs to resolve. |
required |
Returns:
| Type | Description |
|---|---|
UUID | None
|
Line of Credit ID or None if none found. |
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/line_of_credit.py
get_line_of_credit
classmethod
¶
Get a Line of Credit by its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
UUID
|
ID of the Line of Credit. |
required |
Returns:
| Type | Description |
|---|---|
LineOfCredit
|
The matched Line of Credit. |
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/line_of_credit.py
list_line_of_credit_ids_for_owner_and_expense_category_ids
classmethod
¶
list_line_of_credit_ids_for_owner_and_expense_category_ids(
session,
/,
*,
owner_type,
owner_ref,
expense_category_ids,
)
Get Line of Credit IDs for a specific owner and a list of Expense Category IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner_type
|
str
|
Application-specific type of owner for the Line of Credit. |
required |
owner_ref
|
UUID
|
Application-specific reference to the owner of the Line of Credit. |
required |
expense_category_ids
|
list[UUID]
|
List of Expense Category IDs to resolve. |
required |
Returns:
| Type | Description |
|---|---|
list[UUID]
|
List of Line of Credit IDs. |
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/line_of_credit.py
list_lines_of_credit_for_owner
classmethod
¶
Get all Lines of Credit for a specific owner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner_type
|
str
|
Application-specific type of owner for the Line of Credit. |
required |
owner_ref
|
UUID
|
Application-specific reference to the owner of the Line of Credit. |
required |
Returns:
| Type | Description |
|---|---|
list[LineOfCredit]
|
List of Line of Credit IDs. |
Source code in components/payment_gateway/subcomponents/authorizations/models/brokers/line_of_credit.py
terminate_line_of_credit
classmethod
¶
Terminate a Line of Credit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line_of_credit_id
|
UUID
|
ID of the Line of Credit to terminate. |
required |
Returns:
| Type | Description |
|---|---|
LineOfCredit
|
The terminated Line of Credit. |