From cd374569dd917c55286db375219ee3a4d763e831 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 16 Apr 2025 21:59:26 +0300 Subject: [PATCH] feat: cancel deal bill if exists --- schemas/billing.py | 1 + services/billing.py | 16 ++++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/schemas/billing.py b/schemas/billing.py index de8cafa..d1adf8a 100644 --- a/schemas/billing.py +++ b/schemas/billing.py @@ -30,6 +30,7 @@ class CreateCardBillRequest(BaseSchema): class CancelCardBillRequest(BaseSchema): card_id: int + force: Optional[bool] = False # endregion diff --git a/services/billing.py b/services/billing.py index c1e7c0f..945c2dd 100644 --- a/services/billing.py +++ b/services/billing.py @@ -180,8 +180,12 @@ class BillingService(BaseService): ) ) create_bill_response = await billing_client.create(create_bill_request) + if not create_bill_response.ok and 'Request already exists' in create_bill_response.message: + await self.cancel_card_billing(user, CancelCardBillRequest(card_id=request.card_id, force=True)) + return await self.create_card_billing(user, request) if not create_bill_response.ok: - return CreateCardBillResponse(ok=create_bill_response.ok, message=create_bill_response.message or 'Неизвестная ошибка') + return CreateCardBillResponse(ok=create_bill_response.ok, + message=create_bill_response.message or 'Неизвестная ошибка') if basic_card.group: await self.create_group_bill_request(basic_card.group) @@ -212,22 +216,22 @@ class BillingService(BaseService): if card.group: bill = await self._get_group_bill_by_id(card.group.id) - if not bill: + if not bill and not request.force: return CancelCardBillResponse(ok=False, message='Заявка не найдена') billing_client = BillingClient(backend.config.BILLING_API_KEY) response = await billing_client.delete(DeleteBillRequestSchema(listener_transaction_id=card.group.id)) else: bill = await self._get_card_bill_by_id(request.card_id) - if not bill: + if not bill and not request.force: return CancelCardBillResponse(ok=False, message='Заявка не найдена') billing_client = BillingClient(backend.config.BILLING_API_KEY) response = await billing_client.delete(DeleteBillRequestSchema(listener_transaction_id=request.card_id)) if not response.ok: return CancelCardBillResponse(ok=False, message='Ошибка') - - await self.session.delete(bill) - await self.session.commit() + if bill: + await self.session.delete(bill) + await self.session.commit() return CancelCardBillResponse(ok=True, message='Заявка успешно отозвана') except Exception as e: return CancelCardBillResponse(ok=False, message=str(e))