new feature: customer panel accsess
This commit is contained in:
1 parent
5052bde49a
commit
e983e0cc43
12 files changed
+175
-4
No files matched your search
@@ -6,10 +6,62 @@ from apps.customer.models import (
|
||||
)
|
||||
from apps.reminder.models import Reminder as ReminderModel
|
||||
from apps.workspace.models import Workspace as WorkspaceModel
|
||||
from apps.cuser.models import CuserRole as CuserRoleModel
|
||||
from rest_framework import exceptions
|
||||
from utils.get_user_workspace import get_user_workspace, get_user_workspace_uuid
|
||||
from utils.get_cuser_role import get_cuser_role
|
||||
from django.db import transaction
|
||||
from django.contrib.auth import get_user_model
|
||||
import uuid
|
||||
import random
|
||||
|
||||
CUser = get_user_model()
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def customer_panel_access(request, mobile: int, customer_id: int, status: bool) -> CuserRoleModel:
|
||||
|
||||
try:
|
||||
workspace = get_user_workspace(request)
|
||||
customer = CustomerModel.objects.get(id=customer_id, sys_ws=workspace)
|
||||
|
||||
try:
|
||||
cuser_obj = CUser.objects.get(mobile=mobile)
|
||||
except CUser.DoesNotExist:
|
||||
cuser_obj = CUser.objects.create_user(
|
||||
username=uuid.uuid4(),
|
||||
mobile=mobile,
|
||||
name_family=customer.name_family,
|
||||
password=f"{random.randint(10000, 900000)}!@#{random.randint(10000, 9000000)}",
|
||||
is_active=True,
|
||||
)
|
||||
|
||||
try:
|
||||
cuser_role_obj = CuserRoleModel.objects.get(cuser=cuser_obj, role_type_id=3)
|
||||
cuser_role_obj.customer_panel = status
|
||||
cuser_role_obj.save()
|
||||
except CuserRoleModel.DoesNotExist:
|
||||
cuser_role_obj = CuserRoleModel.objects.create(
|
||||
cuser=cuser_obj,
|
||||
role_type_id=3,
|
||||
name_family=customer.name_family,
|
||||
mobile=mobile,
|
||||
mobile_verify=True,
|
||||
is_active=True,
|
||||
customer_panel=status,
|
||||
sys_ws=workspace
|
||||
)
|
||||
|
||||
if cuser_role_obj:
|
||||
customer.cuser_role = cuser_role_obj
|
||||
customer.save()
|
||||
return cuser_role_obj
|
||||
else:
|
||||
raise Exception
|
||||
|
||||
except Exception as exp:
|
||||
message = f'Error Create or Update Customer panel accsess: {exp}'
|
||||
raise exceptions.ValidationError({'error': message})
|
||||
|
||||
|
||||
def create_responsible_group(name: str, workspace: WorkspaceModel):
|
||||
@@ -232,4 +284,5 @@ def customer_note_delete(request, customer_id: int, note_id: int) -> CustomerNot
|
||||
|
||||
except Exception as exp:
|
||||
message = f'Error Delete Customer Note: {exp}'
|
||||
raise exceptions.ValidationError({'error': message})
|
||||
raise exceptions.ValidationError({'error': message})
|
||||
|
||||
@@ -36,4 +36,7 @@ urlpatterns = [
|
||||
path('v2/create/', CustomerCreateApiV2.as_view(), name='customer_create_v2'),
|
||||
|
||||
path('form-item/', CustomerFormItemApi.as_view(), name='customer-form-item'),
|
||||
|
||||
|
||||
path('customer-panel/', CustomerPanelApi.as_view(), name='customer-panel'),
|
||||
]
|
||||
@@ -7,3 +7,4 @@ from .customer import *
|
||||
from .form_item import *
|
||||
from .customer_summary import *
|
||||
from .customer_note import *
|
||||
from .customer_panel import *
|
||||
@@ -22,6 +22,14 @@ class CustomerRetrieveApi(APIView):
|
||||
permission_classes = (IsAuthenticated, custom_permissions.WorkspaceOperatorPermission)
|
||||
|
||||
class OutputSerializer(serializers.ModelSerializer):
|
||||
cuser_role = inline_serializer(fields={
|
||||
'id': serializers.IntegerField(required=False),
|
||||
'cuser': inline_serializer(fields={
|
||||
'id': serializers.IntegerField(),
|
||||
'mobile': serializers.IntegerField(),
|
||||
}),
|
||||
'customer_panel': serializers.BooleanField(required=False)
|
||||
})
|
||||
class Meta:
|
||||
model = CustomerModel
|
||||
fields = '__all__'
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import serializers
|
||||
from apps.customer.services import customer_panel_access
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from utils import custom_permissions
|
||||
|
||||
|
||||
class CustomerPanelApi(APIView):
|
||||
|
||||
permission_classes = (IsAuthenticated, custom_permissions.WorkspaceOperatorPermission)
|
||||
|
||||
class InputSerializer(serializers.Serializer):
|
||||
mobile = serializers.IntegerField(required=True)
|
||||
customer_id = serializers.IntegerField(required=True)
|
||||
status = serializers.BooleanField(required=True)
|
||||
|
||||
def post(self, request):
|
||||
serializer = self.InputSerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
|
||||
cuser_role = customer_panel_access(request=request, **serializer.validated_data)
|
||||
|
||||
return Response(data={'cuser_role_id': cuser_role.id}, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
Reference in new issue
Block a user