98 lines
3.4 KiB
Python
98 lines
3.4 KiB
Python
import ulid
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy import select
|
|
from typing import Optional, List
|
|
from fastapi import HTTPException, status
|
|
|
|
from app.models.ServiceModel import ServiceJob, ServiceJobIntake, ServiceJobInspection, ServiceJobMedia, ServiceJobEvent
|
|
from app.schemas.ServiceSchema import ServiceJobIntakeCreate, ServiceJobInspectionCreate
|
|
|
|
class InspectionService:
|
|
def create_device_intake(
|
|
self, db: Session, job_id: str, staff_id: str, data: ServiceJobIntakeCreate
|
|
) -> ServiceJobIntake:
|
|
job = db.get(ServiceJob, job_id)
|
|
if not job:
|
|
raise HTTPException(status_code=404, detail="Service job not found")
|
|
|
|
# Create intake record
|
|
intake = ServiceJobIntake(
|
|
intake_id=str(ulid.ULID()),
|
|
service_job_id=job_id,
|
|
received_by=staff_id,
|
|
power_status=data.power_status,
|
|
screen_condition=data.screen_condition,
|
|
body_condition=data.body_condition,
|
|
back_condition=data.back_condition,
|
|
camera_condition=data.camera_condition,
|
|
accessories=data.accessories,
|
|
customer_notes=data.customer_notes,
|
|
technician_notes=data.technician_notes,
|
|
)
|
|
db.add(intake)
|
|
|
|
# Log Event & Update Job status
|
|
job.status = "DEVICE_INTAKE"
|
|
event = ServiceJobEvent(
|
|
event_id=str(ulid.ULID()),
|
|
job_id=job_id,
|
|
event_type="DEVICE_INTAKE",
|
|
performed_by=staff_id,
|
|
notes="Device physical condition checked and logged during intake."
|
|
)
|
|
db.add(event)
|
|
db.commit()
|
|
db.refresh(intake)
|
|
return intake
|
|
|
|
def submit_inspection(
|
|
self, db: Session, job_id: str, technician_id: str, data: ServiceJobInspectionCreate
|
|
) -> ServiceJobInspection:
|
|
job = db.get(ServiceJob, job_id)
|
|
if not job:
|
|
raise HTTPException(status_code=404, detail="Service job not found")
|
|
|
|
# Create inspection record
|
|
inspection = ServiceJobInspection(
|
|
inspection_id=str(ulid.ULID()),
|
|
service_job_id=job_id,
|
|
technician_id=technician_id,
|
|
result=data.result,
|
|
customer_report=data.customer_report,
|
|
confirmed_damage=data.confirmed_damage,
|
|
additional_damage=data.additional_damage,
|
|
notes=data.notes,
|
|
)
|
|
db.add(inspection)
|
|
|
|
# Log Event & Update Job status
|
|
job.status = "INSPECTION_COMPLETED"
|
|
event = ServiceJobEvent(
|
|
event_id=str(ulid.ULID()),
|
|
job_id=job_id,
|
|
event_type="INSPECTION_COMPLETED",
|
|
performed_by=technician_id,
|
|
notes=f"Technician inspection complete. Result: {data.result}."
|
|
)
|
|
db.add(event)
|
|
db.commit()
|
|
db.refresh(inspection)
|
|
return inspection
|
|
|
|
def upload_job_media(
|
|
self, db: Session, job_id: str, category: str, file_id: str
|
|
) -> ServiceJobMedia:
|
|
job = db.get(ServiceJob, job_id)
|
|
if not job:
|
|
raise HTTPException(status_code=404, detail="Service job not found")
|
|
|
|
media = ServiceJobMedia(
|
|
media_id=str(ulid.ULID()),
|
|
service_job_id=job_id,
|
|
category=category,
|
|
file_id=file_id
|
|
)
|
|
db.add(media)
|
|
db.commit()
|
|
db.refresh(media)
|
|
return media
|