from sqlalchemy.orm import Session from sqlalchemy import select from typing import List, Optional import ulid from app.models.SystemConfigModel import CrmLocation, DeviceTypeRegistry, UserLocationMapping from app.schemas.SystemConfig import CrmLocationCreate, DeviceTypeCreate, UserLocationAssign class SystemConfigService: def __init__(self, db: Session, tenant_id: str = "tenant_default"): self.db = db self.tenant_id = tenant_id # --- LOCATIONS MANAGER --- def create_location(self, data: CrmLocationCreate) -> CrmLocation: location = CrmLocation( location_id=str(ulid.ULID()), tenant_id=self.tenant_id, name=data.name, code=data.code.upper(), address=data.address, city=data.city, state=data.state, pincode=data.pincode, phone=data.phone, email=data.email, is_primary=data.is_primary or False, is_central_warehouse=data.is_central_warehouse or False, is_active=data.is_active if data.is_active is not None else True ) self.db.add(location) self.db.commit() self.db.refresh(location) return location def list_locations(self) -> List[CrmLocation]: stmt = select(CrmLocation).where(CrmLocation.tenant_id == self.tenant_id, CrmLocation.deleted_at.is_(None)) return list(self.db.execute(stmt).scalars().all()) # --- DYNAMIC DEVICE TYPES MASTER --- def create_device_type(self, data: DeviceTypeCreate) -> DeviceTypeRegistry: # Support both simple (name+code) and extended (category+brand+device_name) modes effective_name = data.name or data.device_name or f"{data.brand or ''} {data.category or ''}".strip() or "Unknown" device = DeviceTypeRegistry( device_type_id=str(ulid.ULID()), tenant_id=self.tenant_id, name=effective_name, code=data.code.upper(), icon_name=data.icon_name or "Smartphone", display_order=data.display_order or 0, is_active=data.is_active if data.is_active is not None else True, category=data.category, brand=data.brand, device_name=data.device_name, ) self.db.add(device) self.db.commit() self.db.refresh(device) return device def list_device_types(self) -> List[DeviceTypeRegistry]: stmt = select(DeviceTypeRegistry).where( DeviceTypeRegistry.tenant_id == self.tenant_id, DeviceTypeRegistry.deleted_at.is_(None) ).order_by(DeviceTypeRegistry.display_order.asc()) results = list(self.db.execute(stmt).scalars().all()) if not results: # Seed default dynamic device types defaults = [ DeviceTypeCreate(name="Mobile", code="MOBILE", icon_name="Smartphone", display_order=1), DeviceTypeCreate(name="Tablet", code="TABLET", icon_name="Tablet", display_order=2), DeviceTypeCreate(name="Laptop", code="LAPTOP", icon_name="Laptop", display_order=3), DeviceTypeCreate(name="Watch", code="WATCH", icon_name="Watch", display_order=4), DeviceTypeCreate(name="Drone", code="DRONE", icon_name="Camera", display_order=5), ] for d in defaults: self.create_device_type(d) results = list(self.db.execute(stmt).scalars().all()) return results # --- USER LOCATION MAPPINGS --- def assign_user_locations(self, data: UserLocationAssign) -> List[str]: # Delete existing mappings existing = self.db.execute( select(UserLocationMapping).where(UserLocationMapping.user_id == data.user_id) ).scalars().all() for m in existing: self.db.delete(m) target_ids = data.location_ids if data.is_all_locations: locs = self.list_locations() target_ids = [l.location_id for l in locs] primary_id = data.primary_location_id or (target_ids[0] if target_ids else None) for loc_id in target_ids: mapping = UserLocationMapping( user_id=data.user_id, location_id=loc_id, is_primary=(loc_id == primary_id) ) self.db.add(mapping) self.db.commit() return target_ids