fix(backend): enrich repair tickets and add departments/designations endpoints
This commit is contained in:
parent
7632685a63
commit
65f23224c0
4 changed files with 86 additions and 13 deletions
|
|
@ -71,6 +71,24 @@ def create_user(
|
||||||
|
|
||||||
return new_user
|
return new_user
|
||||||
|
|
||||||
|
@router.get("/departments/all")
|
||||||
|
def get_all_departments(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: User = Depends(PermissionChecker("user.view"))
|
||||||
|
):
|
||||||
|
from app.models.DepartmentModel import Department
|
||||||
|
deps = db.query(Department).filter(Department.is_active == True).order_by(Department.name.asc()).all()
|
||||||
|
return [{"id": d.department_id, "name": d.name, "description": d.description} for d in deps]
|
||||||
|
|
||||||
|
@router.get("/designations/all")
|
||||||
|
def get_all_designations(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: User = Depends(PermissionChecker("user.view"))
|
||||||
|
):
|
||||||
|
from app.models.DesignationModel import Designation
|
||||||
|
desigs = db.query(Designation).filter(Designation.is_active == True).order_by(Designation.name.asc()).all()
|
||||||
|
return [{"id": d.designation_id, "name": d.name, "description": d.description} for d in desigs]
|
||||||
|
|
||||||
@router.get("/all", response_model=List[UserResponse])
|
@router.get("/all", response_model=List[UserResponse])
|
||||||
def get_all_users(
|
def get_all_users(
|
||||||
skip: int = 0,
|
skip: int = 0,
|
||||||
|
|
@ -80,7 +98,19 @@ def get_all_users(
|
||||||
):
|
):
|
||||||
users = user_repository.get_active_users(db, skip, limit)
|
users = user_repository.get_active_users(db, skip, limit)
|
||||||
from app.models.AuditLogModel import AuditLog
|
from app.models.AuditLogModel import AuditLog
|
||||||
|
from app.models.DepartmentModel import Department
|
||||||
|
from app.models.DesignationModel import Designation
|
||||||
|
from app.models.RoleModel import Role
|
||||||
|
|
||||||
|
dept_map = {d.department_id: d.name for d in db.query(Department).all()}
|
||||||
|
desig_map = {d.designation_id: d.name for d in db.query(Designation).all()}
|
||||||
|
role_map = {r.role_id: r.role_name for r in db.query(Role).all()}
|
||||||
|
|
||||||
for u in users:
|
for u in users:
|
||||||
|
u.department_name = dept_map.get(u.department_id) or (u.department.name if getattr(u, 'department', None) else None)
|
||||||
|
u.designation_name = desig_map.get(u.designation_id) or (u.designation.name if getattr(u, 'designation', None) else None)
|
||||||
|
u.role_name = role_map.get(u.role_id) or (u.role.role_name if getattr(u, 'role', None) else None)
|
||||||
|
|
||||||
latest = (
|
latest = (
|
||||||
db.query(AuditLog)
|
db.query(AuditLog)
|
||||||
.filter(AuditLog.user_id == u.user_id)
|
.filter(AuditLog.user_id == u.user_id)
|
||||||
|
|
@ -102,6 +132,9 @@ def get_user_profile(
|
||||||
user = user_repository.get_by_id(db, user_id)
|
user = user_repository.get_by_id(db, user_id)
|
||||||
if not user or user.deleted_at is not None:
|
if not user or user.deleted_at is not None:
|
||||||
raise HTTPException(status_code=404, detail="User not found")
|
raise HTTPException(status_code=404, detail="User not found")
|
||||||
|
user.department_name = user.department.name if getattr(user, 'department', None) else None
|
||||||
|
user.designation_name = user.designation.name if getattr(user, 'designation', None) else None
|
||||||
|
user.role_name = user.role.role_name if getattr(user, 'role', None) else None
|
||||||
return user
|
return user
|
||||||
|
|
||||||
@router.put("/update/{user_id}", response_model=UserResponse)
|
@router.put("/update/{user_id}", response_model=UserResponse)
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,15 @@ class CrmRepairTicketResponse(BaseModel):
|
||||||
is_warranty_claim: bool
|
is_warranty_claim: bool
|
||||||
notes: Optional[str] = None
|
notes: Optional[str] = None
|
||||||
created_at: Optional[datetime] = None
|
created_at: Optional[datetime] = None
|
||||||
|
status: Optional[str] = None
|
||||||
|
problem_description: Optional[str] = None
|
||||||
|
customer_name: Optional[str] = None
|
||||||
|
customer_phone: Optional[str] = None
|
||||||
|
brand_name: Optional[str] = None
|
||||||
|
model_name: Optional[str] = None
|
||||||
|
imei_serial: Optional[str] = None
|
||||||
|
priority: Optional[str] = "NORMAL"
|
||||||
|
sla_due_at: Optional[datetime] = None
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
from_attributes = True
|
from_attributes = True
|
||||||
|
|
|
||||||
|
|
@ -44,9 +44,12 @@ class UserResponse(BaseModel):
|
||||||
profile_image_id: Optional[str]
|
profile_image_id: Optional[str]
|
||||||
gender: Optional[str]
|
gender: Optional[str]
|
||||||
dob: Optional[date]
|
dob: Optional[date]
|
||||||
department_id: str
|
department_id: Optional[str] = None
|
||||||
designation_id: str
|
designation_id: Optional[str] = None
|
||||||
role_id: str
|
role_id: Optional[str] = None
|
||||||
|
department_name: Optional[str] = None
|
||||||
|
designation_name: Optional[str] = None
|
||||||
|
role_name: Optional[str] = None
|
||||||
manager_id: Optional[str]
|
manager_id: Optional[str]
|
||||||
is_active: bool
|
is_active: bool
|
||||||
email_verified: Optional[bool] = False
|
email_verified: Optional[bool] = False
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session, joinedload
|
||||||
from sqlalchemy import select, func
|
from sqlalchemy import select, func
|
||||||
from typing import List, Optional, Dict, Any
|
from typing import List, Optional, Dict, Any
|
||||||
import ulid
|
import ulid
|
||||||
|
|
@ -91,18 +91,43 @@ class RepairErpService:
|
||||||
)
|
)
|
||||||
return ticket
|
return ticket
|
||||||
|
|
||||||
|
def _enrich_ticket(self, t: Optional[CrmRepairTicket]) -> Optional[CrmRepairTicket]:
|
||||||
|
if not t:
|
||||||
|
return None
|
||||||
|
t.status = t.stage
|
||||||
|
t.problem_description = t.issue_description
|
||||||
|
t.priority = "NORMAL"
|
||||||
|
if getattr(t, "customer", None):
|
||||||
|
name_parts = [t.customer.first_name, t.customer.last_name or ""]
|
||||||
|
t.customer_name = " ".join(p for p in name_parts if p).strip() or "Customer"
|
||||||
|
t.customer_phone = t.customer.phone or ""
|
||||||
|
if getattr(t, "device", None):
|
||||||
|
t.brand_name = t.device.brand or ""
|
||||||
|
t.model_name = t.device.model_name or ""
|
||||||
|
t.imei_serial = t.device.imei_serial or ""
|
||||||
|
return t
|
||||||
|
|
||||||
def get_ticket(self, ticket_id: str) -> Optional[CrmRepairTicket]:
|
def get_ticket(self, ticket_id: str) -> Optional[CrmRepairTicket]:
|
||||||
stmt = select(CrmRepairTicket).where(
|
stmt = (
|
||||||
CrmRepairTicket.ticket_id == ticket_id,
|
select(CrmRepairTicket)
|
||||||
CrmRepairTicket.tenant_id == self.tenant_id,
|
.options(joinedload(CrmRepairTicket.customer), joinedload(CrmRepairTicket.device))
|
||||||
CrmRepairTicket.deleted_at.is_(None)
|
.where(
|
||||||
|
CrmRepairTicket.ticket_id == ticket_id,
|
||||||
|
CrmRepairTicket.tenant_id == self.tenant_id,
|
||||||
|
CrmRepairTicket.deleted_at.is_(None)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
return self.db.execute(stmt).scalar_one_or_none()
|
ticket = self.db.execute(stmt).scalar_one_or_none()
|
||||||
|
return self._enrich_ticket(ticket)
|
||||||
|
|
||||||
def list_tickets(self, skip: int = 0, limit: int = 50, stage: Optional[str] = None, technician_id: Optional[str] = None) -> List[CrmRepairTicket]:
|
def list_tickets(self, skip: int = 0, limit: int = 50, stage: Optional[str] = None, technician_id: Optional[str] = None) -> List[CrmRepairTicket]:
|
||||||
stmt = select(CrmRepairTicket).where(
|
stmt = (
|
||||||
CrmRepairTicket.tenant_id == self.tenant_id,
|
select(CrmRepairTicket)
|
||||||
CrmRepairTicket.deleted_at.is_(None)
|
.options(joinedload(CrmRepairTicket.customer), joinedload(CrmRepairTicket.device))
|
||||||
|
.where(
|
||||||
|
CrmRepairTicket.tenant_id == self.tenant_id,
|
||||||
|
CrmRepairTicket.deleted_at.is_(None)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
if stage:
|
if stage:
|
||||||
stmt = stmt.where(CrmRepairTicket.stage == stage)
|
stmt = stmt.where(CrmRepairTicket.stage == stage)
|
||||||
|
|
@ -110,7 +135,10 @@ class RepairErpService:
|
||||||
stmt = stmt.where(CrmRepairTicket.assigned_technician_id == technician_id)
|
stmt = stmt.where(CrmRepairTicket.assigned_technician_id == technician_id)
|
||||||
|
|
||||||
stmt = stmt.offset(skip).limit(limit).order_by(CrmRepairTicket.created_at.desc())
|
stmt = stmt.offset(skip).limit(limit).order_by(CrmRepairTicket.created_at.desc())
|
||||||
return list(self.db.execute(stmt).scalars().all())
|
tickets = list(self.db.execute(stmt).scalars().all())
|
||||||
|
for t in tickets:
|
||||||
|
self._enrich_ticket(t)
|
||||||
|
return tickets
|
||||||
|
|
||||||
def update_stage(self, ticket_id: str, data: CrmRepairTicketUpdateStage) -> Optional[CrmRepairTicket]:
|
def update_stage(self, ticket_id: str, data: CrmRepairTicketUpdateStage) -> Optional[CrmRepairTicket]:
|
||||||
ticket = self.get_ticket(ticket_id)
|
ticket = self.get_ticket(ticket_id)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue