diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 39568dc..c49a91e 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,7 +3,12 @@ + + + + + diff --git a/apps/cuser/cuser_model.py b/apps/cuser/cuser_model.py index 6d96c29..19a4bf6 100644 --- a/apps/cuser/cuser_model.py +++ b/apps/cuser/cuser_model.py @@ -43,4 +43,4 @@ class CUser(BaseModel, AbstractBaseUser, PermissionsMixin): ] def __str__(self): - return str(self.username) + return f'{self.name_family} - {self.username}' diff --git a/apps/cuser/migrations/0011_cuserrole_customer_panel.py b/apps/cuser/migrations/0011_cuserrole_customer_panel.py new file mode 100644 index 0000000..056b553 --- /dev/null +++ b/apps/cuser/migrations/0011_cuserrole_customer_panel.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.1 on 2024-01-24 06:07 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cuser', '0010_alter_cuserrole_unique_together'), + ] + + operations = [ + migrations.AddField( + model_name='cuserrole', + name='customer_panel', + field=models.BooleanField(blank=True, default=None, null=True), + ), + ] diff --git a/apps/cuser/models.py b/apps/cuser/models.py index 5864e60..ddfd217 100644 --- a/apps/cuser/models.py +++ b/apps/cuser/models.py @@ -25,6 +25,7 @@ class CuserRole(BaseModel): title = models.CharField(max_length=30, null=True, blank=True) mobile = models.PositiveBigIntegerField(null=False, blank=False) mobile_verify = models.BooleanField(default=False) + customer_panel = models.BooleanField(default=None, blank=True, null=True) sys_ws = models.ForeignKey(WorkspaceModel, on_delete=models.CASCADE, null=True, blank=True, default=None) uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True) @@ -34,7 +35,7 @@ class CuserRole(BaseModel): unique_together = ('cuser', 'role_type', 'sys_ws', ) def __str__(self): - return f'{str(self.cuser) } - { str(self.id) }' + return f'{str(self.name_family) } - { str(self.role_type.role_name) }' class SmsOpt(BaseModel): diff --git a/apps/customer/services.py b/apps/customer/services.py index 6f43e0b..09f6ea3 100644 --- a/apps/customer/services.py +++ b/apps/customer/services.py @@ -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}) \ No newline at end of file + raise exceptions.ValidationError({'error': message}) + diff --git a/apps/customer/urls.py b/apps/customer/urls.py index 01df296..1d968e5 100644 --- a/apps/customer/urls.py +++ b/apps/customer/urls.py @@ -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'), ] diff --git a/apps/customer/views/__init__.py b/apps/customer/views/__init__.py index f0aa0eb..c9dc55b 100644 --- a/apps/customer/views/__init__.py +++ b/apps/customer/views/__init__.py @@ -7,3 +7,4 @@ from .customer import * from .form_item import * from .customer_summary import * from .customer_note import * +from .customer_panel import * diff --git a/apps/customer/views/customer.py b/apps/customer/views/customer.py index f9a20c4..49632ef 100644 --- a/apps/customer/views/customer.py +++ b/apps/customer/views/customer.py @@ -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__' diff --git a/apps/customer/views/customer_panel.py b/apps/customer/views/customer_panel.py new file mode 100644 index 0000000..005440b --- /dev/null +++ b/apps/customer/views/customer_panel.py @@ -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) + + diff --git a/apps/jwt_auth/migrations/0074_alter_cuserroleloginopt_uuid.py b/apps/jwt_auth/migrations/0074_alter_cuserroleloginopt_uuid.py new file mode 100644 index 0000000..5ff2dd1 --- /dev/null +++ b/apps/jwt_auth/migrations/0074_alter_cuserroleloginopt_uuid.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.1 on 2024-01-24 06:07 + +from django.db import migrations, models +import uuid + + +class Migration(migrations.Migration): + + dependencies = [ + ('jwt_auth', '0073_alter_cuserroleloginopt_uuid'), + ] + + operations = [ + migrations.AlterField( + model_name='cuserroleloginopt', + name='uuid', + field=models.UUIDField(default=uuid.UUID('27b41338-1021-4d24-8bcc-daa29fac3415'), unique=True), + ), + ] diff --git a/apps/lead/migrations/0028_alter_lead_status.py b/apps/lead/migrations/0028_alter_lead_status.py new file mode 100644 index 0000000..703e8e7 --- /dev/null +++ b/apps/lead/migrations/0028_alter_lead_status.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.1 on 2024-01-24 06:07 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('lead', '0027_alter_lead_status'), + ] + + operations = [ + migrations.AlterField( + model_name='lead', + name='status', + field=models.CharField(blank=True, choices=[('FA', 'FAIL'), ('SU', 'OK'), ('UN', 'unknown')], default='UN', max_length=2, null=True), + ), + ] diff --git a/apps/reminder/migrations/0024_alter_reminder_type.py b/apps/reminder/migrations/0024_alter_reminder_type.py new file mode 100644 index 0000000..062db78 --- /dev/null +++ b/apps/reminder/migrations/0024_alter_reminder_type.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.1 on 2024-01-24 06:07 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('reminder', '0023_alter_reminder_type'), + ] + + operations = [ + migrations.AlterField( + model_name='reminder', + name='type', + field=models.CharField(choices=[('CU', 'CUSTOMER'), ('UN', 'UNKNOWN'), ('NO', 'NOTE'), ('LE', 'LEAD')], default='UN', max_length=2), + ), + ]