Skip to content

Commit

Permalink
feat: add update sales commission in callback
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatasoli committed Oct 6, 2024
1 parent 509a7a0 commit 7360a46
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/infra/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 13 additions & 1 deletion app/payment/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
25 changes: 24 additions & 1 deletion app/report/repository.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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 ###

0 comments on commit 7360a46

Please sign in to comment.