forked from ITISFoundation/osparc-simcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ webserver API: exposes payment methods (🗃️) (ITISFoundation#4747)
- Loading branch information
Showing
27 changed files
with
1,526 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...rc/simcore_postgres_database/migration/versions/624a029738b8_new_payments_method_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
"""New payments_method table | ||
Revision ID: 624a029738b8 | ||
Revises: e7b3d381efe4 | ||
Create Date: 2023-09-13 15:05:41.094403+00:00 | ||
""" | ||
from typing import Final | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "624a029738b8" | ||
down_revision = "e7b3d381efe4" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
# auto-update modified | ||
# TRIGGERS ------------------------ | ||
_TABLE_NAME: Final[str] = "payments_methods" | ||
_TRIGGER_NAME: Final[str] = "trigger_auto_update" # NOTE: scoped on table | ||
_PROCEDURE_NAME: Final[ | ||
str | ||
] = f"{_TABLE_NAME}_auto_update_modified()" # NOTE: scoped on database | ||
modified_timestamp_trigger = sa.DDL( | ||
f""" | ||
DROP TRIGGER IF EXISTS {_TRIGGER_NAME} on {_TABLE_NAME}; | ||
CREATE TRIGGER {_TRIGGER_NAME} | ||
BEFORE INSERT OR UPDATE ON {_TABLE_NAME} | ||
FOR EACH ROW EXECUTE PROCEDURE {_PROCEDURE_NAME}; | ||
""" | ||
) | ||
|
||
# PROCEDURES ------------------------ | ||
update_modified_timestamp_procedure = sa.DDL( | ||
f""" | ||
CREATE OR REPLACE FUNCTION {_PROCEDURE_NAME} | ||
RETURNS TRIGGER AS $$ | ||
BEGIN | ||
NEW.modified := current_timestamp; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
""" | ||
) | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"payments_methods", | ||
sa.Column("payment_method_id", sa.String(), nullable=False), | ||
sa.Column("user_id", sa.BigInteger(), nullable=False), | ||
sa.Column("wallet_id", sa.BigInteger(), nullable=False), | ||
sa.Column("initiated_at", sa.DateTime(timezone=True), nullable=False), | ||
sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True), | ||
sa.Column( | ||
"state", | ||
sa.Enum( | ||
"PENDING", | ||
"SUCCESS", | ||
"FAILED", | ||
"CANCELED", | ||
name="initpromptackflowstate", | ||
), | ||
nullable=False, | ||
server_default="PENDING", | ||
), | ||
sa.Column("state_message", sa.Text(), nullable=True), | ||
sa.Column( | ||
"created", | ||
sa.DateTime(timezone=True), | ||
server_default=sa.text("now()"), | ||
nullable=False, | ||
), | ||
sa.Column( | ||
"modified", | ||
sa.DateTime(timezone=True), | ||
server_default=sa.text("now()"), | ||
nullable=False, | ||
), | ||
sa.PrimaryKeyConstraint("payment_method_id"), | ||
) | ||
op.create_index( | ||
op.f("ix_payments_methods_user_id"), | ||
"payments_methods", | ||
["user_id"], | ||
unique=False, | ||
) | ||
op.create_index( | ||
op.f("ix_payments_methods_wallet_id"), | ||
"payments_methods", | ||
["wallet_id"], | ||
unique=False, | ||
) | ||
# ### end Alembic commands ### | ||
|
||
# custom | ||
op.execute(update_modified_timestamp_procedure) | ||
op.execute(modified_timestamp_trigger) | ||
|
||
|
||
def downgrade(): | ||
# custom | ||
op.execute(f"DROP TRIGGER IF EXISTS {_TRIGGER_NAME} on {_TABLE_NAME};") | ||
op.execute(f"DROP FUNCTION {_PROCEDURE_NAME};") | ||
|
||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index(op.f("ix_payments_methods_wallet_id"), table_name="payments_methods") | ||
op.drop_index(op.f("ix_payments_methods_user_id"), table_name="payments_methods") | ||
op.drop_table("payments_methods") | ||
# ### end Alembic commands ### |
89 changes: 89 additions & 0 deletions
89
packages/postgres-database/src/simcore_postgres_database/models/payments_methods.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import enum | ||
|
||
import sqlalchemy as sa | ||
|
||
from ._common import ( | ||
column_created_datetime, | ||
column_modified_datetime, | ||
register_modified_datetime_auto_update_trigger, | ||
) | ||
from .base import metadata | ||
|
||
|
||
@enum.unique | ||
class InitPromptAckFlowState(str, enum.Enum): | ||
PENDING = "PENDING" # initiated | ||
SUCCESS = "SUCCESS" # completed (ack) with success | ||
FAILED = "FAILED" # failed | ||
CANCELED = "CANCELED" # explicitly aborted by user | ||
|
||
|
||
# | ||
# NOTE: | ||
# - This table was designed to work in an isolated database. For that reason | ||
# we do not use ForeignKeys to establish relations with other tables (e.g. user_id). | ||
# - Payment methods are owned by a user and associated to a wallet. When the same CC is added | ||
# in the framework by different users, the gateway will produce different payment_method_id for each | ||
# of them (VERIFY assumption) | ||
# - A payment method is unique, i.e. only one per wallet and user. For the moment, we intentially avoid the | ||
# possibility of associating a payment method to more than one wallet to avoid complexity | ||
# | ||
payments_methods = sa.Table( | ||
"payments_methods", | ||
metadata, | ||
sa.Column( | ||
"payment_method_id", | ||
sa.String, | ||
nullable=False, | ||
primary_key=True, | ||
doc="Unique identifier of the payment method provided by payment gateway", | ||
), | ||
sa.Column( | ||
"user_id", | ||
sa.BigInteger, | ||
nullable=False, | ||
doc="Unique identifier of the user", | ||
index=True, | ||
), | ||
sa.Column( | ||
"wallet_id", | ||
sa.BigInteger, | ||
nullable=False, | ||
doc="Unique identifier to the wallet owned by the user", | ||
index=True, | ||
), | ||
# | ||
# States of Init-Prompt-Ack flow | ||
# | ||
sa.Column( | ||
"initiated_at", | ||
sa.DateTime(timezone=True), | ||
nullable=False, | ||
doc="Timestamps init step of the flow", | ||
), | ||
sa.Column( | ||
"completed_at", | ||
sa.DateTime(timezone=True), | ||
nullable=True, | ||
doc="Timestamps ack step of the flow", | ||
), | ||
sa.Column( | ||
"state", | ||
sa.Enum(InitPromptAckFlowState), | ||
nullable=False, | ||
default=InitPromptAckFlowState.PENDING, | ||
doc="Current state of this row in the flow ", | ||
), | ||
sa.Column( | ||
"state_message", | ||
sa.Text, | ||
nullable=True, | ||
doc="State message to with details on the state e.g. failure messages", | ||
), | ||
# time-stamps | ||
column_created_datetime(timezone=True), | ||
column_modified_datetime(timezone=True), | ||
) | ||
|
||
|
||
register_modified_datetime_auto_update_trigger(payments_methods) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.