ifixkart-backend/Backend/app/repositories/ServiceRepository.py

42 lines
2.1 KiB
Python

from sqlalchemy.orm import Session
from sqlalchemy import select
from typing import List, Optional
from app.repositories.base_repository import BaseRepository
from app.models.ServiceModel import ServiceJob, ServiceCatalog, ServiceAppointment, ServiceJobAssignment
from app.models.CustomerDeviceModel import CustomerDevice
class ServiceRepository(BaseRepository[ServiceJob]):
def __init__(self):
super().__init__(ServiceJob)
def get_by_job_no(self, db: Session, job_no: str) -> Optional[ServiceJob]:
stmt = select(ServiceJob).where(ServiceJob.job_no == job_no)
return db.execute(stmt).scalar_one_or_none()
def get_customer_jobs(self, db: Session, customer_id: str) -> List[ServiceJob]:
stmt = select(ServiceJob).where(ServiceJob.customer_id == customer_id).order_by(ServiceJob.created_at.desc())
return list(db.execute(stmt).scalars().all())
def get_catalog_service(self, db: Session, service_id: str) -> Optional[ServiceCatalog]:
return db.get(ServiceCatalog, service_id)
def get_all_catalog_services(self, db: Session, active_only: bool = True) -> List[ServiceCatalog]:
stmt = select(ServiceCatalog)
if active_only:
stmt = stmt.where(ServiceCatalog.active == True)
return list(db.execute(stmt).scalars().all())
def get_appointment(self, db: Session, appointment_id: str) -> Optional[ServiceAppointment]:
return db.get(ServiceAppointment, appointment_id)
def get_active_appointments_in_range(self, db: Session, start_time, end_time) -> List[ServiceAppointment]:
stmt = select(ServiceAppointment).where(
ServiceAppointment.scheduled_start < end_time,
ServiceAppointment.scheduled_end > start_time,
ServiceAppointment.status.notin_(["CANCELLED", "EXPIRED"])
)
return list(db.execute(stmt).scalars().all())
def get_customer_devices(self, db: Session, customer_id: str) -> List[CustomerDevice]:
stmt = select(CustomerDevice).where(CustomerDevice.customer_id == customer_id)
return list(db.execute(stmt).scalars().all())