From 7360a46e1c44071f6a318c2a715c9122712bfec4 Mon Sep 17 00:00:00 2001 From: Jonatas Oliveira Date: Sun, 6 Oct 2024 15:21:34 +0200 Subject: [PATCH] feat: add update sales commission in callback --- app/infra/models.py | 2 ++ app/payment/services.py | 14 +++++++- app/report/repository.py | 25 ++++++++++++++- ...dd_cancelled_status_in_sales_commission.py | 32 +++++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/1dba810f2de3_add_cancelled_status_in_sales_commission.py diff --git a/app/infra/models.py b/app/infra/models.py index c72213c0..fd12521c 100644 --- a/app/infra/models.py +++ b/app/infra/models.py @@ -389,6 +389,8 @@ class SalesCommissionDB(Base): released: Mapped[bool] = mapped_column(default=False) paid: Mapped[bool] = mapped_column(default=False) active: Mapped[bool] = mapped_column(default=False) + cancelled: Mapped[bool] = mapped_column(default=False, server_default='0') + cancelled_at: Mapped[datetime | None] # Virtual relationship fields user: Mapped['UserDB'] = relationship( diff --git a/app/payment/services.py b/app/payment/services.py index e0547e2f..8ae01a4e 100644 --- a/app/payment/services.py +++ b/app/payment/services.py @@ -10,7 +10,7 @@ from typing import Any from loguru import logger from app.payment import repository - +from app.report import repository as report_repository async def update_payment( payment_data: PaymentNotification, @@ -39,6 +39,12 @@ async def update_payment( ) order_id = payment_db[0].order_id if payment['status'] == 'approved' or payment['status'] == 'authorized': + await report_repository.update_payment_commissions( + paid_status=True, + payment_id=payment_db.payment_id, + db=session, + cancelled_status=False, + ) await bootstrap.message.broker.publish( { 'mail_to': user.email, @@ -47,6 +53,12 @@ async def update_payment( queue=RabbitQueue('notification_order_paid'), ) if payment['status'] == 'cancelled': + await report_repository.update_payment_commissions( + paid_status=False, + payment_id=payment_db.payment_id, + db=session, + cancelled_status=True, + ) await bootstrap.message.broker.publish( { 'mail_to': user.email, diff --git a/app/report/repository.py b/app/report/repository.py index 9b5fa29e..8b279279 100644 --- a/app/report/repository.py +++ b/app/report/repository.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime, UTC from app.entities.product import ProductInDB from app.entities.user import UserInDB @@ -49,6 +49,29 @@ def update_commissions(date_threshold: datetime, db) -> None: transaction.commit() +def update_payment_commissions( + *, + payment_id:int, + paid_status: bool, + db, + cancelled_status: bool = False, +) -> None: + """Update comission status.""" + with db as transaction: + query = select(SalesCommissionDB).where( + SalesCommissionDB.payment_id == payment_id, + ) + commission_db = transaction.scalar(query) + commission_db.paid = paid_status + if cancelled_status: + today = datetime.now(tz=UTC) + commission_db.cancelled_at = cancelled_status + commission_db.cancelled_at = today + commission_db.paid = False + transaction.add(commission_db) + transaction.commit() + + async def get_admins(transaction): """Get list of admins.""" async with transaction: diff --git a/migrations/versions/1dba810f2de3_add_cancelled_status_in_sales_commission.py b/migrations/versions/1dba810f2de3_add_cancelled_status_in_sales_commission.py new file mode 100644 index 00000000..789c73b9 --- /dev/null +++ b/migrations/versions/1dba810f2de3_add_cancelled_status_in_sales_commission.py @@ -0,0 +1,32 @@ +"""Add cancelled status in sales commission + +Revision ID: 1dba810f2de3 +Revises: 46ba4b2c93ac +Create Date: 2024-10-06 15:16:19.236795 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '1dba810f2de3' +down_revision: Union[str, None] = '46ba4b2c93ac' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('sales_commission', sa.Column('cancelled', sa.Boolean(), server_default='0', nullable=False)) + op.add_column('sales_commission', sa.Column('cancelled_at', sa.DateTime(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('sales_commission', 'cancelled_at') + op.drop_column('sales_commission', 'cancelled') + # ### end Alembic commands ###