feat: cancel deal bill if exists

This commit is contained in:
2025-04-16 21:59:26 +03:00
parent 4303f679b5
commit cd374569dd
2 changed files with 11 additions and 6 deletions

View File

@@ -30,6 +30,7 @@ class CreateCardBillRequest(BaseSchema):
class CancelCardBillRequest(BaseSchema):
card_id: int
force: Optional[bool] = False
# endregion

View File

@@ -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))