From a2ff8b863ee107a631c78011ad0540c0a6a0c507 Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Wed, 22 Oct 2025 14:53:10 +0330 Subject: [PATCH] remove organization assignment logic in device and services modules --- apps/pos_device/exceptions.py | 7 +++++- .../web/api/v1/serilaizers/device.py | 23 ++++++++++++++----- apps/pos_device/web/api/v1/viewsets/device.py | 8 ++----- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/apps/pos_device/exceptions.py b/apps/pos_device/exceptions.py index a2e926c..f5f8d10 100644 --- a/apps/pos_device/exceptions.py +++ b/apps/pos_device/exceptions.py @@ -1,7 +1,12 @@ -from rest_framework.exceptions import APIException from rest_framework import status +from rest_framework.exceptions import APIException class DeviceAlreadyAssigned(APIException): status_code = status.HTTP_403_FORBIDDEN default_detail = "این دستگاه قبلا به این کلاینت تخصیص داده شده است" # noqa + + +class OrganizationDeviceNotAssigned(APIException): + status_code = status.HTTP_403_FORBIDDEN + default_detail = "این سازمان دستگاه فعالی ندارد" # noqa diff --git a/apps/pos_device/web/api/v1/serilaizers/device.py b/apps/pos_device/web/api/v1/serilaizers/device.py index b8f2d1c..ee5d96a 100644 --- a/apps/pos_device/web/api/v1/serilaizers/device.py +++ b/apps/pos_device/web/api/v1/serilaizers/device.py @@ -1,11 +1,10 @@ -from apps.product.web.api.v1.serializers.quota_distribution_serializers import QuotaDistributionSerializer -from apps.authentication.api.v1.serializers.serializer import BankAccountSerializer -from apps.pos_device.web.api.v1.serilaizers import client as client_serializer from rest_framework.serializers import ModelSerializer -from apps.pos_device import models as pos_models -from rest_framework.exceptions import APIException + +from apps.authentication.api.v1.serializers.serializer import BankAccountSerializer from apps.pos_device import exceptions as pos_exceptions -from rest_framework import status +from apps.pos_device import models as pos_models +from apps.pos_device.web.api.v1.serilaizers import client as client_serializer +from apps.product.web.api.v1.serializers.quota_distribution_serializers import QuotaDistributionSerializer class ProviderCompanySerializer(ModelSerializer): @@ -99,6 +98,18 @@ class StakeHolderShareAmountSerializer(ModelSerializer): model = pos_models.StakeHolderShareAmount fields = '__all__' + def validate(self, attrs): + stakeholders = attrs['stakeholders'] + + # check organization has pos device + assigned_organization = pos_models.Organization.objects.get( + id=attrs['distribution'].assigned_organization + ) + if not assigned_organization.has_pos: + raise pos_exceptions.OrganizationDeviceNotAssigned() + + return attrs + def to_representation(self, instance): representation = super().to_representation(instance) diff --git a/apps/pos_device/web/api/v1/viewsets/device.py b/apps/pos_device/web/api/v1/viewsets/device.py index e62202b..0186644 100644 --- a/apps/pos_device/web/api/v1/viewsets/device.py +++ b/apps/pos_device/web/api/v1/viewsets/device.py @@ -22,7 +22,6 @@ from apps.core.mixins.admin_mixin import AdminFilterMixin from apps.core.mixins.search_mixin import DynamicSearchMixin from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin from apps.pos_device import models as pos_models -from apps.pos_device.services.services import get_organization_by_stake_holder from apps.pos_device.web.api.v1.serilaizers import device as device_serializer from apps.pos_device.web.api.v1.viewsets.client import POSClientViewSet from apps.product.models import Broker @@ -238,7 +237,7 @@ class DeviceAssignmentViewSet(SoftDeleteMixin, viewsets.ModelViewSet): client_org.save() # after pos device assignment, must set owner - # as default stake holder + # as default stakeholder pos_models.StakeHolders.objects.create( assignment=assignment, device=assignment.device, @@ -381,13 +380,10 @@ class StakeHolderShareAmountViewSet(SoftDeleteMixin, viewsets.ModelViewSet, Dyna organization = get_organization_by_user(request.user) data.update({'registering_organization': organization.id}) - # set assigner and assigned organization in distribution data + # set assigner organization in distribution data assigner_organization = get_organization_by_user(request.user) data['distribution'].update({'assigner_organization': assigner_organization.id}) - assigned_organization = get_organization_by_stake_holder(id=data['stakeholders']) - data['distribution'].update({'assigned_organization': assigned_organization.id}) - # create distribution if 'distribution' in data.keys(): distribution = CustomOperations().custom_create(