diff --git a/apps/authentication/api/v1/serializers/serializer.py b/apps/authentication/api/v1/serializers/serializer.py index 59e827d..d18f830 100644 --- a/apps/authentication/api/v1/serializers/serializer.py +++ b/apps/authentication/api/v1/serializers/serializer.py @@ -5,7 +5,7 @@ from django.db.models import Q from rest_framework import serializers from apps.authentication.exceptions import UserExistException, OrganizationNationalUniqueIDException, \ - OrganizationTypeRepeatableException + OrganizationTypeRepeatableException, BankAccountExistException from apps.authentication.models import ( User, City, @@ -51,6 +51,14 @@ class BankAccountSerializer(serializers.ModelSerializer): model = BankAccountInformation fields = '__all__' + def validate(self, attrs): + account = attrs['account'] + card = attrs['card'] + sheba = attrs['sheba'] + + if self.Meta.model.objects.filter(Q(account=account) | Q(card=card) | Q(sheba=sheba)).exists(): + raise BankAccountExistException() + def update(self, instance, validated_data): """ update user bank account information """ instance.name = validated_data.get('name', instance.name) diff --git a/apps/authentication/exceptions.py b/apps/authentication/exceptions.py index 127a190..b6e8f7b 100644 --- a/apps/authentication/exceptions.py +++ b/apps/authentication/exceptions.py @@ -11,6 +11,12 @@ class TokenBlackListedException(APIException): default_code = 'unauthorized' +class BankAccountExistException(APIException): + status_code = status.HTTP_403_FORBIDDEN + default_detail = "اطلاعات بانکی وارد شده از قبل ثبت شده است" # noqa + default_code = "اطلاعات بانکی وارد شده از قبل ثبت شده است" # noqa + + class OrganizationBankAccountException(APIException): """ if organization does not have bank account """ diff --git a/apps/authentication/models.py b/apps/authentication/models.py index a1573f3..14f7825 100644 --- a/apps/authentication/models.py +++ b/apps/authentication/models.py @@ -195,9 +195,9 @@ class BankAccountInformation(BaseModel): ) account_type = models.CharField(max_length=10, default='USR') name = models.CharField(max_length=150, null=True) - card = models.CharField(max_length=25, null=True, unique=True) - account = models.CharField(max_length=25, null=True, unique=True) - sheba = models.CharField(max_length=30, null=True, unique=True) + card = models.CharField(max_length=25, null=True) + account = models.CharField(max_length=25, null=True) + sheba = models.CharField(max_length=30, null=True) def __str__(self): return f'{self.name}-{self.card}'