diff --git a/apps/authentication/migrations/0050_organization_service_area.py b/apps/authentication/migrations/0050_organization_service_area.py new file mode 100644 index 0000000..d180777 --- /dev/null +++ b/apps/authentication/migrations/0050_organization_service_area.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0 on 2025-12-22 11:59 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('authentication', '0049_alter_bankaccountinformation_account_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='organization', + name='service_area', + field=models.ManyToManyField(related_name='service_area', to='authentication.city'), + ), + ] diff --git a/apps/herd/web/api/v1/serializers.py b/apps/herd/web/api/v1/serializers.py index a9fb02f..9efa53a 100644 --- a/apps/herd/web/api/v1/serializers.py +++ b/apps/herd/web/api/v1/serializers.py @@ -74,13 +74,32 @@ class RancherOrganizationLinkSerializer(serializers.ModelSerializer): rancher = attrs['rancher'] organization = attrs['organization'] - if self.Meta.model.objects.filter(rancher=rancher, organization=organization).exists(): - raise HerdException(message="دامدار برای تعاونی مد نظر از قبل وجود دارد", status_code=403) # noqa + if not self.instance: + if self.Meta.model.objects.filter(rancher=rancher, organization=organization).exists(): + raise HerdException(message="دامدار برای تعاونی مد نظر از قبل وجود دارد", status_code=403) # noqa + + elif self.Meta.model.objects.filter( + rancher=rancher, + organization__parent_organization=organization.parent_organization + ).exists(): + raise HerdException( + message="دامدار نمیتواند زیر مجموعه دو تعاونی با اتحادیه یکسان باشند", # noqa + status_code=403 + ) if self.instance: - if rancher.city not in organization.service_area: + if rancher.city not in organization.service_area.all(): raise HerdException(message="دامدار در محدوده فعالیت تعاونی قرار ندارد", status_code=403) # noqa + elif self.instance.organization == organization: + raise HerdException(message="دامدار برای تعاونی مد نظر از قبل وجود دارد", status_code=403) # noqa + + elif self.instance.organization.parent_organization == organization.parent_organization: + raise HerdException( + message="دامدار نمیتواند زیر مجموعه دو تعاونی با اتحادیه یکسان باشند", # noqa + status_code=403 + ) + return attrs def to_representation(self, instance):