From 6791938bd90ba1d52735b716a3593ea92976071d Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Wed, 5 Nov 2025 17:16:54 +0330 Subject: [PATCH] validation of organization bank accounts --- .../api/v1/serializers/serializer.py | 19 ++++++++++++++++--- apps/authentication/exceptions.py | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/apps/authentication/api/v1/serializers/serializer.py b/apps/authentication/api/v1/serializers/serializer.py index 70238fd..c6548bb 100644 --- a/apps/authentication/api/v1/serializers/serializer.py +++ b/apps/authentication/api/v1/serializers/serializer.py @@ -5,7 +5,8 @@ from django.db.models import Q from rest_framework import serializers from apps.authentication.exceptions import UserExistException, OrganizationNationalUniqueIDException, \ - OrganizationTypeRepeatableException, BankAccountExistException + OrganizationTypeRepeatableException, BankAccountExistException, BankAccountNumberAccountException, \ + BankAccountCardException, BankAccountShebaException from apps.authentication.models import ( User, City, @@ -56,9 +57,21 @@ class BankAccountSerializer(serializers.ModelSerializer): card = attrs['card'] sheba = attrs['sheba'] - if self.Meta.model.objects.filter(Q(account=account) | Q(card=card) | Q(sheba=sheba)).exists(): - raise BankAccountExistException() + if not self.instance: + if self.Meta.model.objects.filter(Q(account=account) | Q(card=card) | Q(sheba=sheba)).exists(): + raise BankAccountExistException() + elif self.instance: + if self.instance.account != account: + if self.Meta.model.objects.filter(Q(account=account)).exists(): + raise BankAccountNumberAccountException() + elif self.instance.card != card: + if self.Meta.model.objects.filter(Q(card=card)).exists(): + raise BankAccountCardException() + + elif self.instance.sheba != sheba: + if self.Meta.model.objects.filter(Q(sheba=sheba)).exists(): + raise BankAccountShebaException() return attrs def update(self, instance, validated_data): diff --git a/apps/authentication/exceptions.py b/apps/authentication/exceptions.py index b6e8f7b..f39ee58 100644 --- a/apps/authentication/exceptions.py +++ b/apps/authentication/exceptions.py @@ -17,6 +17,24 @@ class BankAccountExistException(APIException): default_code = "اطلاعات بانکی وارد شده از قبل ثبت شده است" # noqa +class BankAccountCardException(APIException): + status_code = status.HTTP_403_FORBIDDEN + default_detail = "شماره کارت از قبل ثبت شده است" # noqa + default_code = "شماره کارت از قبل ثبت شده است" # noqa + + +class BankAccountShebaException(APIException): + status_code = status.HTTP_403_FORBIDDEN + default_detail = "شماره شبا از قبل ثبت شده است" # noqa + default_code = "شماره شبا از قبل ثبت شده است" # noqa + + +class BankAccountNumberAccountException(APIException): + status_code = status.HTTP_403_FORBIDDEN + default_detail = "شماره حساب از قبل ثبت شده است" # noqa + default_code = "شماره حساب از قبل ثبت شده است" # noqa + + class OrganizationBankAccountException(APIException): """ if organization does not have bank account """