From 912e705b7b35dc6fd1d317b005855582683375aa Mon Sep 17 00:00:00 2001 From: Jonatas Oliveira Date: Sun, 6 Oct 2024 16:51:53 +0200 Subject: [PATCH] chore: change order to task send commission --- app/cart/tasks.py | 45 +++++++++++++++++++++++----------------- app/payment/services.py | 5 +++-- app/payment/tasks.py | 2 +- app/report/repository.py | 15 +++++++------- 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/app/cart/tasks.py b/app/cart/tasks.py index 75c4a202..520110c0 100644 --- a/app/cart/tasks.py +++ b/app/cart/tasks.py @@ -123,15 +123,18 @@ async def checkout( authorization=payment_response.authorization_code, bootstrap=bootstrap, ) + _payment_id = payment_id gateway_payment_id = payment_response.authorization_code + logger.debug(f'PAyment Response {payment_response}') cart.payment_intent = payment_response.id + logger.debug(f'Payment Intent {cart.payment_intent}') authorization = payment_response.authorization_code payment_accept = bootstrap.payment.accept_payment( payment_gateway=cart.gateway_provider, payment_id=cart.payment_intent, ) - logger.info(f'Payment response: {payment_accept}') + logger.debug(f'Payment response: {payment_accept}') # TODO: check if order is in inventory for decrease await decrease_inventory( @@ -162,6 +165,28 @@ async def checkout( send_mail=True, bootstrap=bootstrap, ) + logger.debug('Debug comission credit card') + logger.debug(f'Order Id {order_id}') + logger.debug(f'Affiliate {affiliate_id}') + logger.debug(f'subtotal {cart.subtotal}') + logger.debug(f'Coupon {coupon}') + logger.debug(f'payment_Id {_payment_id}') + logger.debug(f'payment_Id 2 {payment_id}') + if all([order_id, affiliate_id, coupon, _payment_id]): + logger.debug('Credit Card task start') + await bootstrap.message.broker.publish( + { + 'user_id': affiliate_id, + 'order_id': order_id, + 'subtotal': cart.subtotal, + 'coupon_id': coupon.coupon_id, + 'commission_percentage': coupon.commission_percentage, + 'payment_id': _payment_id, + }, + queue=RabbitQueue('sales_commission'), + ) + else: + logger.debug('Not commission') await bootstrap.message.broker.publish( { 'mail_to': user['email'], @@ -169,24 +194,6 @@ async def checkout( }, queue=RabbitQueue('notification_order_paid'), ) - logger.debug('Debug comission credit card') - logger.debug(f'Order Id {order_id}') - logger.debug(f'Affiliate {affiliate_id}') - logger.debug(f'subtotal {cart.subtotal}') - logger.debug(f'Coupon {coupon}') - logger.debug(f'payment_Id {payment_id}') - if all([order_id, affiliate_id, coupon, payment_id]): - await bootstrap.message.broker.publish( - { - 'user_id': affiliate_id, - 'order_id': order_id, - 'subtotal': cart.subtotal, - 'coupon_id': coupon.coupon_id, - 'commission_percentage': coupon.commission_percentage, - 'payment_id': payment_id, - }, - queue=RabbitQueue('sales_commission'), - ) logger.info( f'Checkout cart {cart_uuid} with payment {payment_id} processed with success', ) diff --git a/app/payment/services.py b/app/payment/services.py index 6b80c73d..d0084f58 100644 --- a/app/payment/services.py +++ b/app/payment/services.py @@ -19,6 +19,7 @@ async def update_payment( """Update payment.""" order_id, user = None, None logger.debug(f'Data Payment {payment_data}') + logger.debug(f'Data Payment ID {payment_data.data.id}') payment = bootstrap.payment.get_payment_status( payment_id=payment_data.data.id, payment_gateway='MERCADOPAGO', @@ -44,7 +45,7 @@ async def update_payment( logger.debug(f'PAyment DB {payment_db}') await report_repository.update_payment_commissions( paid_status=True, - payment_id=payment_db.payment_id, + payment_id=payment_db[0].payment_id, db=session, cancelled_status=False, ) @@ -58,7 +59,7 @@ async def update_payment( if payment['status'] == 'cancelled': await report_repository.update_payment_commissions( paid_status=False, - payment_id=payment_db.payment_id, + payment_id=payment_db[0].payment_id, db=session, cancelled_status=True, ) diff --git a/app/payment/tasks.py b/app/payment/tasks.py index a824b443..145c72d0 100644 --- a/app/payment/tasks.py +++ b/app/payment/tasks.py @@ -46,7 +46,7 @@ async def update_payment( bootstrap: Any, ): """Update payment status.""" - await bootstrap.payment_uow.uow_update_payment( + return await bootstrap.payment_uow.uow_update_payment( payment_id, payment_status=payment_status, authorization=authorization, diff --git a/app/report/repository.py b/app/report/repository.py index 8b279279..cbd99092 100644 --- a/app/report/repository.py +++ b/app/report/repository.py @@ -23,8 +23,6 @@ async def get_user_sales_comissions( select(SalesCommissionDB) .where( SalesCommissionDB.user_id == user.user_id, - SalesCommissionDB.paid == paid, - SalesCommissionDB.released == released, ) ) commissions = await transaction.session.execute(query) @@ -49,7 +47,7 @@ def update_commissions(date_threshold: datetime, db) -> None: transaction.commit() -def update_payment_commissions( +async def update_payment_commissions( *, payment_id:int, paid_status: bool, @@ -57,19 +55,22 @@ def update_payment_commissions( cancelled_status: bool = False, ) -> None: """Update comission status.""" - with db as transaction: + dir(db) + async with db as transaction: query = select(SalesCommissionDB).where( SalesCommissionDB.payment_id == payment_id, ) - commission_db = transaction.scalar(query) + commission_db = await transaction.session.scalar(query) commission_db.paid = paid_status + commission_db.active = True 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() + commission_db.active = False + transaction.session.add(commission_db) + await transaction.session.commit() async def get_admins(transaction):