Skip to content

Commit

Permalink
chore: change order to task send commission
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatasoli committed Oct 6, 2024
1 parent 6a469ed commit 912e705
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
45 changes: 26 additions & 19 deletions app/cart/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -162,31 +165,35 @@ 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'],
'order_id': order_id if order_id else '',
},
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',
)
Expand Down
5 changes: 3 additions & 2 deletions app/payment/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand Down
2 changes: 1 addition & 1 deletion app/payment/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 8 additions & 7 deletions app/report/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -49,27 +47,30 @@ 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,
db,
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):
Expand Down

0 comments on commit 912e705

Please sign in to comment.