Files
platform/app/services/reference_service.py
T
Арсентьев Александр a2ac226028 ервый импорт проекта
2026-08-06 00:01:51 +03:00

350 lines
13 KiB
Python

"""Reference data service."""
from typing import List, Optional
from sqlalchemy.orm import Session
from app.models.status import Status
from app.models.priority import Priority
from app.models.category import Category
from app.models.extra import Company, Location, Department
from app.core.exceptions import NotFoundException, ConflictException
class ReferenceService:
"""Service for managing reference data."""
def __init__(self, db: Session):
self.db = db
# Statuses
def get_statuses(self) -> List[Status]:
"""Get all statuses."""
return self.db.query(Status).order_by(Status.name).all()
def get_status_by_id(self, status_id: int) -> Status:
"""Get status by ID."""
status = self.db.query(Status).filter(Status.id == status_id).first()
if not status:
raise NotFoundException(f"Status with id {status_id} not found")
return status
def create_status(self, name: str, is_final: bool = False) -> Status:
"""Create new status."""
if self.db.query(Status).filter(Status.name == name).first():
raise ConflictException(f"Status '{name}' already exists")
status = Status(name=name, is_final=is_final)
self.db.add(status)
self.db.commit()
self.db.refresh(status)
return status
def update_status(self, status_id: int, name: Optional[str] = None, is_final: Optional[bool] = None) -> Status:
"""Update status."""
status = self.get_status_by_id(status_id)
if name is not None:
existing = self.db.query(Status).filter(Status.name == name, Status.id != status_id).first()
if existing:
raise ConflictException(f"Status '{name}' already exists")
status.name = name
if is_final is not None:
status.is_final = is_final
self.db.commit()
self.db.refresh(status)
return status
def delete_status(self, status_id: int) -> dict:
"""Delete status."""
status = self.get_status_by_id(status_id)
self.db.delete(status)
self.db.commit()
return {"message": "Status deleted successfully"}
# Priorities
def get_priorities(self) -> List[Priority]:
"""Get all priorities."""
return self.db.query(Priority).order_by(Priority.level).all()
def get_priority_by_id(self, priority_id: int) -> Priority:
"""Get priority by ID."""
priority = self.db.query(Priority).filter(Priority.id == priority_id).first()
if not priority:
raise NotFoundException(f"Priority with id {priority_id} not found")
return priority
def create_priority(self, name: str, level: int, default_response_time_minutes: int = 60) -> Priority:
"""Create new priority."""
if self.db.query(Priority).filter(Priority.name == name).first():
raise ConflictException(f"Priority '{name}' already exists")
priority = Priority(
name=name,
level=level,
default_response_time_minutes=default_response_time_minutes
)
self.db.add(priority)
self.db.commit()
self.db.refresh(priority)
return priority
def update_priority(
self,
priority_id: int,
name: Optional[str] = None,
level: Optional[int] = None,
default_response_time_minutes: Optional[int] = None
) -> Priority:
"""Update priority."""
priority = self.get_priority_by_id(priority_id)
if name is not None:
existing = self.db.query(Priority).filter(Priority.name == name, Priority.id != priority_id).first()
if existing:
raise ConflictException(f"Priority '{name}' already exists")
priority.name = name
if level is not None:
priority.level = level
if default_response_time_minutes is not None:
priority.default_response_time_minutes = default_response_time_minutes
self.db.commit()
self.db.refresh(priority)
return priority
def delete_priority(self, priority_id: int) -> dict:
"""Delete priority."""
priority = self.get_priority_by_id(priority_id)
self.db.delete(priority)
self.db.commit()
return {"message": "Priority deleted successfully"}
# Categories
def get_categories(self, parent_id: Optional[int] = None) -> List[Category]:
"""Get categories, optionally filtered by parent."""
query = self.db.query(Category)
if parent_id is not None:
query = query.filter(Category.parent_id == parent_id)
else:
query = query.filter(Category.parent_id.is_(None))
return query.order_by(Category.name).all()
def get_all_categories(self) -> List[Category]:
"""Get all categories flat."""
return self.db.query(Category).order_by(Category.name).all()
def get_category_by_id(self, category_id: int) -> Category:
"""Get category by ID."""
category = self.db.query(Category).filter(Category.id == category_id).first()
if not category:
raise NotFoundException(f"Category with id {category_id} not found")
return category
def create_category(self, name: str, parent_id: Optional[int] = None) -> Category:
"""Create new category."""
if self.db.query(Category).filter(Category.name == name).first():
raise ConflictException(f"Category '{name}' already exists")
if parent_id is not None:
self.get_category_by_id(parent_id)
category = Category(name=name, parent_id=parent_id)
self.db.add(category)
self.db.commit()
self.db.refresh(category)
return category
def update_category(self, category_id: int, name: Optional[str] = None, parent_id: Optional[int] = None) -> Category:
"""Update category."""
category = self.get_category_by_id(category_id)
if name is not None:
existing = self.db.query(Category).filter(Category.name == name, Category.id != category_id).first()
if existing:
raise ConflictException(f"Category '{name}' already exists")
category.name = name
if parent_id is not None:
if parent_id == category_id:
raise ConflictException("Category cannot be its own parent")
self.get_category_by_id(parent_id)
category.parent_id = parent_id
self.db.commit()
self.db.refresh(category)
return category
def delete_category(self, category_id: int) -> dict:
"""Delete category."""
category = self.get_category_by_id(category_id)
# Update child categories
self.db.query(Category).filter(Category.parent_id == category_id).update(
{"parent_id": category.parent_id}
)
self.db.delete(category)
self.db.commit()
return {"message": "Category deleted successfully"}
# Companies
def get_companies(self) -> List[Company]:
"""Get all companies."""
return self.db.query(Company).order_by(Company.name).all()
def get_company_by_id(self, company_id: int) -> Company:
"""Get company by ID."""
company = self.db.query(Company).filter(Company.id == company_id).first()
if not company:
raise NotFoundException(f"Company with id {company_id} not found")
return company
def create_company(self, name: str, address: Optional[str] = None, phone: Optional[str] = None, website: Optional[str] = None) -> Company:
"""Create new company."""
if self.db.query(Company).filter(Company.name == name).first():
raise ConflictException(f"Company '{name}' already exists")
company = Company(name=name, address=address, phone=phone, website=website)
self.db.add(company)
self.db.commit()
self.db.refresh(company)
return company
def update_company(
self,
company_id: int,
name: Optional[str] = None,
address: Optional[str] = None,
phone: Optional[str] = None,
website: Optional[str] = None
) -> Company:
"""Update company."""
company = self.get_company_by_id(company_id)
if name is not None:
existing = self.db.query(Company).filter(Company.name == name, Company.id != company_id).first()
if existing:
raise ConflictException(f"Company '{name}' already exists")
company.name = name
if address is not None:
company.address = address
if phone is not None:
company.phone = phone
if website is not None:
company.website = website
self.db.commit()
self.db.refresh(company)
return company
def delete_company(self, company_id: int) -> dict:
"""Delete company."""
company = self.get_company_by_id(company_id)
self.db.delete(company)
self.db.commit()
return {"message": "Company deleted successfully"}
# Locations
def get_locations(self) -> List[Location]:
"""Get all locations."""
return self.db.query(Location).order_by(Location.name).all()
def get_location_by_id(self, location_id: int) -> Location:
"""Get location by ID."""
location = self.db.query(Location).filter(Location.id == location_id).first()
if not location:
raise NotFoundException(f"Location with id {location_id} not found")
return location
def create_location(self, name: str, address: Optional[str] = None, phone: Optional[str] = None) -> Location:
"""Create new location."""
if self.db.query(Location).filter(Location.name == name).first():
raise ConflictException(f"Location '{name}' already exists")
location = Location(name=name, address=address, phone=phone)
self.db.add(location)
self.db.commit()
self.db.refresh(location)
return location
def update_location(
self,
location_id: int,
name: Optional[str] = None,
address: Optional[str] = None,
phone: Optional[str] = None
) -> Location:
"""Update location."""
location = self.get_location_by_id(location_id)
if name is not None:
existing = self.db.query(Location).filter(Location.name == name, Location.id != location_id).first()
if existing:
raise ConflictException(f"Location '{name}' already exists")
location.name = name
if address is not None:
location.address = address
if phone is not None:
location.phone = phone
self.db.commit()
self.db.refresh(location)
return location
def delete_location(self, location_id: int) -> dict:
"""Delete location."""
location = self.get_location_by_id(location_id)
self.db.delete(location)
self.db.commit()
return {"message": "Location deleted successfully"}
# Departments
def get_departments(self) -> List[Department]:
"""Get all departments."""
return self.db.query(Department).order_by(Department.name).all()
def get_department_by_id(self, department_id: int) -> Department:
"""Get department by ID."""
department = self.db.query(Department).filter(Department.id == department_id).first()
if not department:
raise NotFoundException(f"Department with id {department_id} not found")
return department
def create_department(self, name: str) -> Department:
"""Create new department."""
if self.db.query(Department).filter(Department.name == name).first():
raise ConflictException(f"Department '{name}' already exists")
department = Department(name=name)
self.db.add(department)
self.db.commit()
self.db.refresh(department)
return department
def update_department(self, department_id: int, name: Optional[str] = None) -> Department:
"""Update department."""
department = self.get_department_by_id(department_id)
if name is not None:
existing = self.db.query(Department).filter(Department.name == name, Department.id != department_id).first()
if existing:
raise ConflictException(f"Department '{name}' already exists")
department.name = name
self.db.commit()
self.db.refresh(department)
return department
def delete_department(self, department_id: int) -> dict:
"""Delete department."""
department = self.get_department_by_id(department_id)
self.db.delete(department)
self.db.commit()
return {"message": "Department deleted successfully"}