first setup of pos transaction - add provider, user, organization information of pos to login

This commit is contained in:
2025-08-26 15:24:46 +03:30
parent c0b6b8ddca
commit 65c831d6a8
7 changed files with 54 additions and 6 deletions

View File

@@ -0,0 +1,17 @@
import typing
from apps.authentication.models import Organization
def get_users_of_organization(org: Organization) -> typing.Any:
""" get users of an organizations """
user_relations = org.user_organization.all()
users_list = [{
'name': f"{rel.user.first_name} {rel.user.last_name}",
'national_code': rel.user.national_code,
'mobile': rel.user.mobile,
'phone': rel.user.phone,
'address': rel.user.address
} for rel in user_relations]
return users_list

View File

@@ -10,14 +10,14 @@ class BaseModel(models.Model):
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="%(class)s_createddby",
related_name="%(class)s_createddby", # noqa
null=True,
blank=True,
)
modified_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="%(class)s_modifiedby",
related_name="%(class)s_modifiedby", # noqa
null=True,
blank=True,
)

View File

@@ -0,0 +1,18 @@
# Generated by Django 5.0 on 2025-08-26 11:52
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('pos_device', '0062_stakeholders_default'),
]
operations = [
migrations.AlterField(
model_name='device',
name='serial',
field=models.TextField(null=True, unique=True),
),
]

View File

@@ -34,7 +34,7 @@ class Device(BaseModel):
device_identity = models.CharField(max_length=25, null=True)
acceptor = models.CharField(max_length=50, null=True)
terminal = models.CharField(max_length=50, null=True)
serial = models.TextField(null=True)
serial = models.TextField(null=True, unique=True)
password = models.CharField(max_length=25, null=True)
multi_device = models.BooleanField(default=False)
server_in = models.BooleanField(default=False)

View File

@@ -1,4 +1,5 @@
from apps.pos_device.pos.api.v1.serializers.device import DeviceSerializer
from apps.authentication.services.service import get_users_of_organization
from apps.pos_device.mixins.pos_device_mixin import POSDeviceMixin
from apps.pos_device import models as pos_models
from rest_framework.permissions import AllowAny
@@ -49,6 +50,7 @@ class POSDeviceViewSet(viewsets.ModelViewSet, POSDeviceMixin):
serial = headers_data['device-serial']
sdk = headers_data['device-sdk']
psp_name = headers_data['device-provider']
# provider organization
organization = pos_models.Organization.objects.get(en_name=psp_name)
# check if device exists
@@ -60,6 +62,8 @@ class POSDeviceViewSet(viewsets.ModelViewSet, POSDeviceMixin):
# activate device
if device:
if (not device.is_activated or not device.pre_registered) and device.assigned_state:
# when device is logged in, its has an organization owner client, if not it will show error
device_owner_org = self.get_device_organization()
device.is_activated = True
device.save()
@@ -78,13 +82,18 @@ class POSDeviceViewSet(viewsets.ModelViewSet, POSDeviceMixin):
"message": "login success - session activated",
"device_identity": device.device_identity,
"serial": device.serial,
"device_owner": organization.name
"provider": organization.name,
"provider_users": get_users_of_organization(organization),
"device_owner": device_owner_org,
"device_owner_users": get_users_of_organization(device_owner_org)
}, status=status.HTTP_200_OK)
return Response({
"message": "device pre registered - unauthorized",
"device_identity": device.device_identity,
"serial": device.serial
"serial": device.serial,
"provider": organization.name,
"provider_users": get_users_of_organization(organization),
}, status=status.HTTP_401_UNAUTHORIZED)
pre_device = pos_models.Device.objects.create(
@@ -96,7 +105,9 @@ class POSDeviceViewSet(viewsets.ModelViewSet, POSDeviceMixin):
return Response({
"message": "device pre-registered",
"device_identity": pre_device.device_identity
"device_identity": pre_device.device_identity,
"provider": organization.name,
"provider_users": get_users_of_organization(organization),
}, status=status.HTTP_412_PRECONDITION_FAILED)
@action(

View File

@@ -75,6 +75,7 @@ class InventoryEntryViewSet(viewsets.ModelViewSet, DynamicSearchMixin, POSDevice
class InventoryQuotaSaleTransactionViewSet(viewsets.ModelViewSet, DynamicSearchMixin, POSDeviceMixin):
queryset = warehouse_models.InventoryQuotaSaleTransaction.objects.all()
serializer_class = warehouse_serializers.InventoryQuotaSaleTransactionSerializer
permission_classes = [AllowAny]
search_fields = [
"rancher__union_name",
"rancher__union_code",

View File

@@ -5,6 +5,7 @@ from . import api
router = DefaultRouter()
router.register(r'inventory_entry', api.InventoryEntryViewSet, basename='inventory_entry')
router.register(r'transaction', api.InventoryQuotaSaleTransactionViewSet, basename='transaction')
urlpatterns = [
path('v1/', include(router.urls))