feat(validation): complete validation editor and master data modules
This commit is contained in:
parent
2b5c765e41
commit
f73a24df13
73 changed files with 10194 additions and 0 deletions
32
validation-suite/backend/mercury/app/api/dependencies.py
Normal file
32
validation-suite/backend/mercury/app/api/dependencies.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||
from jose import JWTError, jwt
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.config import settings
|
||||
from app.db.session import get_session
|
||||
from app.models.user import User
|
||||
|
||||
bearer = HTTPBearer()
|
||||
|
||||
|
||||
def current_user(
|
||||
credentials: HTTPAuthorizationCredentials = Depends(bearer),
|
||||
session: Session = Depends(get_session),
|
||||
) -> User:
|
||||
try:
|
||||
payload = jwt.decode(
|
||||
credentials.credentials,
|
||||
settings.jwt_secret,
|
||||
algorithms=[settings.jwt_algorithm],
|
||||
)
|
||||
user_id = payload.get("sub")
|
||||
except JWTError as exc:
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token") from exc
|
||||
user = session.get(User, user_id)
|
||||
if user is None or not user.is_active:
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Inactive user")
|
||||
return user
|
||||
|
||||
24
validation-suite/backend/mercury/app/api/v1/auth.py
Normal file
24
validation-suite/backend/mercury/app/api/v1/auth.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.dependencies import current_user
|
||||
from app.db.session import get_session
|
||||
from app.models.user import User
|
||||
from app.schemas.auth import LoginRequest, TokenResponse, UserRead
|
||||
from app.services.auth_service import AuthService
|
||||
|
||||
router = APIRouter(prefix="/auth", tags=["auth"])
|
||||
|
||||
|
||||
@router.post("/login", response_model=TokenResponse)
|
||||
def login(payload: LoginRequest, session: Session = Depends(get_session)) -> TokenResponse:
|
||||
token = AuthService(session).login(payload.email, payload.password)
|
||||
return TokenResponse(access_token=token)
|
||||
|
||||
|
||||
@router.get("/me", response_model=UserRead)
|
||||
def me(user: User = Depends(current_user)) -> User:
|
||||
return user
|
||||
|
||||
216
validation-suite/backend/mercury/app/api/v1/domain.py
Normal file
216
validation-suite/backend/mercury/app/api/v1/domain.py
Normal file
|
|
@ -0,0 +1,216 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, Query, Response
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.dependencies import current_user
|
||||
from app.db.session import get_session
|
||||
from app.models.contact import Contact
|
||||
from app.models.customer import Customer
|
||||
from app.models.device import Device
|
||||
from app.models.equipment import Equipment
|
||||
from app.models.location import Location
|
||||
from app.models.validation import Validation
|
||||
from app.schemas.common import PaginatedResponse
|
||||
from app.schemas.domain import (
|
||||
ContactCreate,
|
||||
ContactRead,
|
||||
ContactUpdate,
|
||||
CustomerCreate,
|
||||
CustomerRead,
|
||||
CustomerUpdate,
|
||||
DeviceCreate,
|
||||
DeviceRead,
|
||||
DeviceUpdate,
|
||||
EquipmentCreate,
|
||||
EquipmentRead,
|
||||
EquipmentUpdate,
|
||||
LocationCreate,
|
||||
LocationRead,
|
||||
LocationUpdate,
|
||||
ValidationCreate,
|
||||
ValidationRead,
|
||||
ValidationUpdate,
|
||||
)
|
||||
from app.services.domain_service import CrudService, DomainServices
|
||||
|
||||
router = APIRouter(tags=["domain"], dependencies=[Depends(current_user)])
|
||||
|
||||
|
||||
@router.get("/dashboard")
|
||||
def dashboard(session: Session = Depends(get_session)) -> dict[str, int]:
|
||||
return {
|
||||
"customers": session.scalar(select(func.count()).select_from(Customer)) or 0,
|
||||
"locations": session.scalar(select(func.count()).select_from(Location)) or 0,
|
||||
"contacts": session.scalar(select(func.count()).select_from(Contact)) or 0,
|
||||
"devices": session.scalar(select(func.count()).select_from(Device)) or 0,
|
||||
"equipment": session.scalar(select(func.count()).select_from(Equipment)) or 0,
|
||||
"validations": session.scalar(select(func.count()).select_from(Validation)) or 0,
|
||||
}
|
||||
|
||||
|
||||
def paging(
|
||||
page: int = Query(default=1, ge=1),
|
||||
page_size: int = Query(default=20, ge=1, le=100),
|
||||
search: str | None = Query(default=None, max_length=120),
|
||||
) -> dict[str, Any]:
|
||||
return {"page": page, "page_size": page_size, "search": search}
|
||||
|
||||
|
||||
def commit_create(session: Session, service: CrudService, payload):
|
||||
item = service.create(payload.model_dump())
|
||||
session.commit()
|
||||
session.refresh(item)
|
||||
return item
|
||||
|
||||
|
||||
def commit_update(session: Session, service: CrudService, item_id: str, payload):
|
||||
item = service.update(item_id, payload.model_dump())
|
||||
session.commit()
|
||||
session.refresh(item)
|
||||
return item
|
||||
|
||||
|
||||
def commit_delete(session: Session, service: CrudService, item_id: str) -> Response:
|
||||
service.delete(item_id)
|
||||
session.commit()
|
||||
return Response(status_code=204)
|
||||
|
||||
|
||||
@router.get("/customers", response_model=PaginatedResponse[CustomerRead])
|
||||
def list_customers(params: dict = Depends(paging), session: Session = Depends(get_session)):
|
||||
return DomainServices(session).customers.list(**params)
|
||||
|
||||
|
||||
@router.post("/customers", response_model=CustomerRead, status_code=201)
|
||||
def create_customer(payload: CustomerCreate, session: Session = Depends(get_session)):
|
||||
return commit_create(session, DomainServices(session).customers, payload)
|
||||
|
||||
|
||||
@router.put("/customers/{item_id}", response_model=CustomerRead)
|
||||
def update_customer(item_id: str, payload: CustomerUpdate, session: Session = Depends(get_session)):
|
||||
return commit_update(session, DomainServices(session).customers, item_id, payload)
|
||||
|
||||
|
||||
@router.delete("/customers/{item_id}", status_code=204)
|
||||
def delete_customer(item_id: str, session: Session = Depends(get_session)):
|
||||
return commit_delete(session, DomainServices(session).customers, item_id)
|
||||
|
||||
|
||||
@router.get("/locations", response_model=PaginatedResponse[LocationRead])
|
||||
def list_locations(params: dict = Depends(paging), session: Session = Depends(get_session)):
|
||||
return DomainServices(session).locations.list(**params)
|
||||
|
||||
|
||||
@router.post("/locations", response_model=LocationRead, status_code=201)
|
||||
def create_location(payload: LocationCreate, session: Session = Depends(get_session)):
|
||||
return commit_create(session, DomainServices(session).locations, payload)
|
||||
|
||||
|
||||
@router.put("/locations/{item_id}", response_model=LocationRead)
|
||||
def update_location(item_id: str, payload: LocationUpdate, session: Session = Depends(get_session)):
|
||||
return commit_update(session, DomainServices(session).locations, item_id, payload)
|
||||
|
||||
|
||||
@router.delete("/locations/{item_id}", status_code=204)
|
||||
def delete_location(item_id: str, session: Session = Depends(get_session)):
|
||||
return commit_delete(session, DomainServices(session).locations, item_id)
|
||||
|
||||
|
||||
@router.get("/contacts", response_model=PaginatedResponse[ContactRead])
|
||||
def list_contacts(params: dict = Depends(paging), session: Session = Depends(get_session)):
|
||||
return DomainServices(session).contacts.list(**params)
|
||||
|
||||
|
||||
@router.post("/contacts", response_model=ContactRead, status_code=201)
|
||||
def create_contact(payload: ContactCreate, session: Session = Depends(get_session)):
|
||||
return commit_create(session, DomainServices(session).contacts, payload)
|
||||
|
||||
|
||||
@router.put("/contacts/{item_id}", response_model=ContactRead)
|
||||
def update_contact(item_id: str, payload: ContactUpdate, session: Session = Depends(get_session)):
|
||||
return commit_update(session, DomainServices(session).contacts, item_id, payload)
|
||||
|
||||
|
||||
@router.delete("/contacts/{item_id}", status_code=204)
|
||||
def delete_contact(item_id: str, session: Session = Depends(get_session)):
|
||||
return commit_delete(session, DomainServices(session).contacts, item_id)
|
||||
|
||||
|
||||
@router.get("/devices", response_model=PaginatedResponse[DeviceRead])
|
||||
def list_devices(params: dict = Depends(paging), session: Session = Depends(get_session)):
|
||||
return DomainServices(session).devices.list(**params)
|
||||
|
||||
|
||||
@router.post("/devices", response_model=DeviceRead, status_code=201)
|
||||
def create_device(payload: DeviceCreate, session: Session = Depends(get_session)):
|
||||
return commit_create(session, DomainServices(session).devices, payload)
|
||||
|
||||
|
||||
@router.put("/devices/{item_id}", response_model=DeviceRead)
|
||||
def update_device(item_id: str, payload: DeviceUpdate, session: Session = Depends(get_session)):
|
||||
return commit_update(session, DomainServices(session).devices, item_id, payload)
|
||||
|
||||
|
||||
@router.delete("/devices/{item_id}", status_code=204)
|
||||
def delete_device(item_id: str, session: Session = Depends(get_session)):
|
||||
return commit_delete(session, DomainServices(session).devices, item_id)
|
||||
|
||||
|
||||
@router.get("/equipment", response_model=PaginatedResponse[EquipmentRead])
|
||||
def list_equipment(params: dict = Depends(paging), session: Session = Depends(get_session)):
|
||||
return DomainServices(session).equipment.list(**params)
|
||||
|
||||
|
||||
@router.post("/equipment", response_model=EquipmentRead, status_code=201)
|
||||
def create_equipment(payload: EquipmentCreate, session: Session = Depends(get_session)):
|
||||
return commit_create(session, DomainServices(session).equipment, payload)
|
||||
|
||||
|
||||
@router.put("/equipment/{item_id}", response_model=EquipmentRead)
|
||||
def update_equipment(item_id: str, payload: EquipmentUpdate, session: Session = Depends(get_session)):
|
||||
return commit_update(session, DomainServices(session).equipment, item_id, payload)
|
||||
|
||||
|
||||
@router.delete("/equipment/{item_id}", status_code=204)
|
||||
def delete_equipment(item_id: str, session: Session = Depends(get_session)):
|
||||
return commit_delete(session, DomainServices(session).equipment, item_id)
|
||||
|
||||
|
||||
@router.get("/validations", response_model=PaginatedResponse[ValidationRead])
|
||||
def list_validations(params: dict = Depends(paging), session: Session = Depends(get_session)):
|
||||
return DomainServices(session).validations.list(**params)
|
||||
|
||||
|
||||
@router.get("/validations/next-report-number")
|
||||
def next_report_number(session: Session = Depends(get_session)) -> dict[str, str]:
|
||||
count = session.scalar(select(func.count()).select_from(Validation)) or 0
|
||||
return {"report_number": f"VAL-{count + 1:05d}"}
|
||||
|
||||
|
||||
@router.get("/validations/{item_id}", response_model=ValidationRead)
|
||||
def get_validation(item_id: str, session: Session = Depends(get_session)):
|
||||
item = DomainServices(session).validations.repository.get(item_id)
|
||||
if item is None:
|
||||
from fastapi import HTTPException
|
||||
|
||||
raise HTTPException(status_code=404, detail="Resource not found")
|
||||
return item
|
||||
|
||||
|
||||
@router.post("/validations", response_model=ValidationRead, status_code=201)
|
||||
def create_validation(payload: ValidationCreate, session: Session = Depends(get_session)):
|
||||
return commit_create(session, DomainServices(session).validations, payload)
|
||||
|
||||
|
||||
@router.put("/validations/{item_id}", response_model=ValidationRead)
|
||||
def update_validation(item_id: str, payload: ValidationUpdate, session: Session = Depends(get_session)):
|
||||
return commit_update(session, DomainServices(session).validations, item_id, payload)
|
||||
|
||||
|
||||
@router.delete("/validations/{item_id}", status_code=204)
|
||||
def delete_validation(item_id: str, session: Session = Depends(get_session)):
|
||||
return commit_delete(session, DomainServices(session).validations, item_id)
|
||||
10
validation-suite/backend/mercury/app/api/v1/router.py
Normal file
10
validation-suite/backend/mercury/app/api/v1/router.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.api.v1 import auth, domain
|
||||
|
||||
api_router = APIRouter(prefix="/api/v1")
|
||||
api_router.include_router(auth.router)
|
||||
api_router.include_router(domain.router)
|
||||
|
||||
24
validation-suite/backend/mercury/app/core/config.py
Normal file
24
validation-suite/backend/mercury/app/core/config.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from pydantic import Field
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(env_file=".env", extra="ignore")
|
||||
|
||||
app_name: str = "Validation Suite"
|
||||
environment: str = "local"
|
||||
database_url: str = Field(
|
||||
default="postgresql+psycopg://validation:validation123@postgres:5432/validation_suite",
|
||||
alias="DATABASE_URL",
|
||||
)
|
||||
jwt_secret: str = Field(default="change-me-in-production", alias="JWT_SECRET")
|
||||
jwt_algorithm: str = "HS256"
|
||||
access_token_minutes: int = 60 * 8
|
||||
cors_origins: list[str] = ["http://localhost:3000"]
|
||||
admin_email: str = Field(default="admin@schubamed.de", alias="ADMIN_EMAIL")
|
||||
admin_password: str = Field(default="ValidationSuite!2026", alias="ADMIN_PASSWORD")
|
||||
|
||||
|
||||
settings = Settings()
|
||||
23
validation-suite/backend/mercury/app/core/security.py
Normal file
23
validation-suite/backend/mercury/app/core/security.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import bcrypt
|
||||
from jose import jwt
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
return bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
|
||||
|
||||
|
||||
def verify_password(password: str, password_hash: str) -> bool:
|
||||
return bcrypt.checkpw(password.encode("utf-8"), password_hash.encode("utf-8"))
|
||||
|
||||
|
||||
def create_access_token(subject: str, role: str) -> str:
|
||||
expires_at = datetime.now(UTC) + timedelta(minutes=settings.access_token_minutes)
|
||||
payload = {"sub": subject, "role": role, "exp": expires_at}
|
||||
return jwt.encode(payload, settings.jwt_secret, algorithm=settings.jwt_algorithm)
|
||||
|
||||
31
validation-suite/backend/mercury/app/db/base.py
Normal file
31
validation-suite/backend/mercury/app/db/base.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from uuid import uuid4
|
||||
|
||||
from sqlalchemy import DateTime, MetaData, func
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||||
|
||||
convention = {
|
||||
"ix": "ix_%(column_0_label)s",
|
||||
"uq": "uq_%(table_name)s_%(column_0_name)s",
|
||||
"ck": "ck_%(table_name)s_%(constraint_name)s",
|
||||
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
|
||||
"pk": "pk_%(table_name)s",
|
||||
}
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
metadata = MetaData(naming_convention=convention)
|
||||
|
||||
|
||||
class UUIDMixin:
|
||||
id: Mapped[str] = mapped_column(primary_key=True, default=lambda: str(uuid4()))
|
||||
|
||||
|
||||
class TimestampMixin:
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
|
||||
)
|
||||
|
||||
30
validation-suite/backend/mercury/app/db/seed.py
Normal file
30
validation-suite/backend/mercury/app/db/seed.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.security import hash_password
|
||||
from app.db.session import SessionLocal
|
||||
from app.models.user import User, UserRole
|
||||
|
||||
|
||||
def seed_admin() -> None:
|
||||
with SessionLocal() as session:
|
||||
existing = session.scalar(select(User).where(User.email == settings.admin_email.lower()))
|
||||
if existing is not None:
|
||||
return
|
||||
session.add(
|
||||
User(
|
||||
email=settings.admin_email.lower(),
|
||||
full_name="Validation Suite Administrator",
|
||||
role=UserRole.admin,
|
||||
password_hash=hash_password(settings.admin_password),
|
||||
is_active=True,
|
||||
)
|
||||
)
|
||||
session.commit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
seed_admin()
|
||||
|
||||
20
validation-suite/backend/mercury/app/db/session.py
Normal file
20
validation-suite/backend/mercury/app/db/session.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Generator
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
engine = create_engine(settings.database_url, pool_pre_ping=True)
|
||||
SessionLocal = sessionmaker(bind=engine, autoflush=False, expire_on_commit=False)
|
||||
|
||||
|
||||
def get_session() -> Generator[Session, None, None]:
|
||||
session = SessionLocal()
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
23
validation-suite/backend/mercury/app/main.py
Normal file
23
validation-suite/backend/mercury/app/main.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from app.api.v1.router import api_router
|
||||
from app.core.config import settings
|
||||
|
||||
app = FastAPI(title=settings.app_name, version="0.1.0")
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=settings.cors_origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
app.include_router(api_router)
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
def health() -> dict[str, str]:
|
||||
return {"status": "ok"}
|
||||
|
||||
21
validation-suite/backend/mercury/app/models/__init__.py
Normal file
21
validation-suite/backend/mercury/app/models/__init__.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
from app.models.contact import Contact
|
||||
from app.models.customer import Customer
|
||||
from app.models.device import Device
|
||||
from app.models.document import Document
|
||||
from app.models.equipment import Equipment
|
||||
from app.models.location import Location
|
||||
from app.models.program import Program
|
||||
from app.models.user import User
|
||||
from app.models.validation import Validation
|
||||
|
||||
__all__ = [
|
||||
"Contact",
|
||||
"Customer",
|
||||
"Device",
|
||||
"Document",
|
||||
"Equipment",
|
||||
"Location",
|
||||
"Program",
|
||||
"User",
|
||||
"Validation",
|
||||
]
|
||||
19
validation-suite/backend/mercury/app/models/contact.py
Normal file
19
validation-suite/backend/mercury/app/models/contact.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import ForeignKey, String
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.db.base import Base, TimestampMixin, UUIDMixin
|
||||
|
||||
|
||||
class Contact(Base, UUIDMixin, TimestampMixin):
|
||||
__tablename__ = "contacts"
|
||||
|
||||
customer_id: Mapped[str] = mapped_column(ForeignKey("customers.id", ondelete="CASCADE"), index=True)
|
||||
full_name: Mapped[str] = mapped_column(String(160))
|
||||
function: Mapped[str | None] = mapped_column(String(120))
|
||||
email: Mapped[str | None] = mapped_column(String(255))
|
||||
phone: Mapped[str | None] = mapped_column(String(80))
|
||||
|
||||
customer: Mapped["Customer"] = relationship(back_populates="contacts")
|
||||
|
||||
32
validation-suite/backend/mercury/app/models/customer.py
Normal file
32
validation-suite/backend/mercury/app/models/customer.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import enum
|
||||
|
||||
from sqlalchemy import Enum, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.db.base import Base, TimestampMixin, UUIDMixin
|
||||
|
||||
|
||||
class CustomerType(str, enum.Enum):
|
||||
practice = "practice"
|
||||
clinic = "clinic"
|
||||
|
||||
|
||||
class Customer(Base, UUIDMixin, TimestampMixin):
|
||||
__tablename__ = "customers"
|
||||
|
||||
customer_type: Mapped[CustomerType] = mapped_column(Enum(CustomerType))
|
||||
name: Mapped[str] = mapped_column(String(220), index=True)
|
||||
street: Mapped[str | None] = mapped_column(String(220))
|
||||
postal_code: Mapped[str | None] = mapped_column(String(20))
|
||||
city: Mapped[str | None] = mapped_column(String(120))
|
||||
phone: Mapped[str | None] = mapped_column(String(80))
|
||||
email: Mapped[str | None] = mapped_column(String(255))
|
||||
hygiene_officer: Mapped[str | None] = mapped_column(String(160))
|
||||
quality_manager: Mapped[str | None] = mapped_column(String(160))
|
||||
notes: Mapped[str | None] = mapped_column(Text)
|
||||
|
||||
contacts: Mapped[list["Contact"]] = relationship(back_populates="customer", cascade="all, delete-orphan")
|
||||
locations: Mapped[list["Location"]] = relationship(back_populates="customer", cascade="all, delete-orphan")
|
||||
|
||||
29
validation-suite/backend/mercury/app/models/device.py
Normal file
29
validation-suite/backend/mercury/app/models/device.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
|
||||
from sqlalchemy import Date, ForeignKey, Integer, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.db.base import Base, TimestampMixin, UUIDMixin
|
||||
|
||||
|
||||
class Device(Base, UUIDMixin, TimestampMixin):
|
||||
__tablename__ = "devices"
|
||||
|
||||
customer_id: Mapped[str] = mapped_column(ForeignKey("customers.id"), index=True)
|
||||
location_id: Mapped[str | None] = mapped_column(ForeignKey("locations.id"), index=True)
|
||||
manufacturer: Mapped[str] = mapped_column(String(140))
|
||||
model: Mapped[str] = mapped_column(String(140))
|
||||
device_type: Mapped[str | None] = mapped_column(String(120))
|
||||
serial_number: Mapped[str] = mapped_column(String(140), unique=True, index=True)
|
||||
year_built: Mapped[int | None] = mapped_column(Integer)
|
||||
commissioned_on: Mapped[date | None] = mapped_column(Date)
|
||||
chamber_volume_liters: Mapped[int | None] = mapped_column(Integer)
|
||||
steam_generation: Mapped[str | None] = mapped_column(String(220))
|
||||
water_treatment: Mapped[str | None] = mapped_column(String(220))
|
||||
documentation: Mapped[str | None] = mapped_column(Text)
|
||||
supplier: Mapped[str | None] = mapped_column(String(180))
|
||||
|
||||
location: Mapped["Location | None"] = relationship(back_populates="devices")
|
||||
|
||||
26
validation-suite/backend/mercury/app/models/document.py
Normal file
26
validation-suite/backend/mercury/app/models/document.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import enum
|
||||
|
||||
from sqlalchemy import Enum, String
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.db.base import Base, TimestampMixin, UUIDMixin
|
||||
|
||||
|
||||
class DocumentOwnerType(str, enum.Enum):
|
||||
customer = "customer"
|
||||
device = "device"
|
||||
validation = "validation"
|
||||
equipment = "equipment"
|
||||
|
||||
|
||||
class Document(Base, UUIDMixin, TimestampMixin):
|
||||
__tablename__ = "documents"
|
||||
|
||||
owner_type: Mapped[DocumentOwnerType] = mapped_column(Enum(DocumentOwnerType), index=True)
|
||||
owner_id: Mapped[str] = mapped_column(String(80), index=True)
|
||||
filename: Mapped[str] = mapped_column(String(255))
|
||||
content_type: Mapped[str] = mapped_column(String(120))
|
||||
storage_path: Mapped[str] = mapped_column(String(500))
|
||||
|
||||
35
validation-suite/backend/mercury/app/models/equipment.py
Normal file
35
validation-suite/backend/mercury/app/models/equipment.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import enum
|
||||
from datetime import date
|
||||
|
||||
from sqlalchemy import Date, Enum, String
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.db.base import Base, TimestampMixin, UUIDMixin
|
||||
|
||||
|
||||
class EquipmentKind(str, enum.Enum):
|
||||
temperature_logger = "temperature_logger"
|
||||
pressure_logger = "pressure_logger"
|
||||
sensor = "sensor"
|
||||
|
||||
|
||||
class EquipmentStatus(str, enum.Enum):
|
||||
green = "green"
|
||||
yellow = "yellow"
|
||||
red = "red"
|
||||
|
||||
|
||||
class Equipment(Base, UUIDMixin, TimestampMixin):
|
||||
__tablename__ = "equipment"
|
||||
|
||||
kind: Mapped[EquipmentKind] = mapped_column(Enum(EquipmentKind))
|
||||
manufacturer: Mapped[str | None] = mapped_column(String(140))
|
||||
model: Mapped[str | None] = mapped_column(String(140))
|
||||
serial_number: Mapped[str] = mapped_column(String(140), unique=True, index=True)
|
||||
calibrated_on: Mapped[date | None] = mapped_column(Date)
|
||||
calibration_due_on: Mapped[date | None] = mapped_column(Date)
|
||||
certificate_document_id: Mapped[str | None] = mapped_column(String(80))
|
||||
status: Mapped[EquipmentStatus] = mapped_column(Enum(EquipmentStatus), default=EquipmentStatus.green)
|
||||
|
||||
21
validation-suite/backend/mercury/app/models/location.py
Normal file
21
validation-suite/backend/mercury/app/models/location.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import ForeignKey, String
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.db.base import Base, TimestampMixin, UUIDMixin
|
||||
|
||||
|
||||
class Location(Base, UUIDMixin, TimestampMixin):
|
||||
__tablename__ = "locations"
|
||||
|
||||
customer_id: Mapped[str] = mapped_column(ForeignKey("customers.id", ondelete="CASCADE"), index=True)
|
||||
name: Mapped[str] = mapped_column(String(160))
|
||||
street: Mapped[str | None] = mapped_column(String(220))
|
||||
postal_code: Mapped[str | None] = mapped_column(String(20))
|
||||
city: Mapped[str | None] = mapped_column(String(120))
|
||||
room: Mapped[str | None] = mapped_column(String(120))
|
||||
|
||||
customer: Mapped["Customer"] = relationship(back_populates="locations")
|
||||
devices: Mapped[list["Device"]] = relationship(back_populates="location")
|
||||
|
||||
17
validation-suite/backend/mercury/app/models/program.py
Normal file
17
validation-suite/backend/mercury/app/models/program.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import ForeignKey, Integer, String
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.db.base import Base, TimestampMixin, UUIDMixin
|
||||
|
||||
|
||||
class Program(Base, UUIDMixin, TimestampMixin):
|
||||
__tablename__ = "programs"
|
||||
|
||||
device_id: Mapped[str] = mapped_column(ForeignKey("devices.id"), index=True)
|
||||
name: Mapped[str] = mapped_column(String(160))
|
||||
temperature_celsius: Mapped[int | None] = mapped_column(Integer)
|
||||
holding_time_minutes: Mapped[int | None] = mapped_column(Integer)
|
||||
drying_time_minutes: Mapped[int | None] = mapped_column(Integer)
|
||||
|
||||
25
validation-suite/backend/mercury/app/models/user.py
Normal file
25
validation-suite/backend/mercury/app/models/user.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import enum
|
||||
|
||||
from sqlalchemy import Boolean, Enum, String
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.db.base import Base, TimestampMixin, UUIDMixin
|
||||
|
||||
|
||||
class UserRole(str, enum.Enum):
|
||||
admin = "admin"
|
||||
employee = "employee"
|
||||
auditor = "auditor"
|
||||
|
||||
|
||||
class User(Base, UUIDMixin, TimestampMixin):
|
||||
__tablename__ = "users"
|
||||
|
||||
email: Mapped[str] = mapped_column(String(255), unique=True, index=True)
|
||||
full_name: Mapped[str] = mapped_column(String(160))
|
||||
role: Mapped[UserRole] = mapped_column(Enum(UserRole), default=UserRole.employee)
|
||||
password_hash: Mapped[str] = mapped_column(String(255))
|
||||
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
||||
|
||||
49
validation-suite/backend/mercury/app/models/validation.py
Normal file
49
validation-suite/backend/mercury/app/models/validation.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import enum
|
||||
from datetime import date
|
||||
|
||||
from sqlalchemy import Date, Enum, ForeignKey, JSON, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.db.base import Base, TimestampMixin, UUIDMixin
|
||||
|
||||
|
||||
class ValidationStatus(str, enum.Enum):
|
||||
draft = "draft"
|
||||
in_progress = "in_progress"
|
||||
ready_for_report = "ready_for_report"
|
||||
completed = "completed"
|
||||
|
||||
|
||||
class Validation(Base, UUIDMixin, TimestampMixin):
|
||||
__tablename__ = "validations"
|
||||
|
||||
report_number: Mapped[str] = mapped_column(String(80), unique=True, index=True)
|
||||
customer_id: Mapped[str] = mapped_column(ForeignKey("customers.id"), index=True)
|
||||
location_id: Mapped[str | None] = mapped_column(ForeignKey("locations.id"), index=True)
|
||||
contact_id: Mapped[str | None] = mapped_column(ForeignKey("contacts.id"), index=True)
|
||||
device_id: Mapped[str | None] = mapped_column(ForeignKey("devices.id"), index=True)
|
||||
validation_type: Mapped[str] = mapped_column(String(120))
|
||||
project: Mapped[str | None] = mapped_column(String(180))
|
||||
test_location: Mapped[str | None] = mapped_column(String(180))
|
||||
examiner_name: Mapped[str | None] = mapped_column(String(180))
|
||||
participants: Mapped[str | None] = mapped_column(Text)
|
||||
operator_name: Mapped[str | None] = mapped_column(String(180))
|
||||
scheduled_on: Mapped[date | None] = mapped_column(Date)
|
||||
performed_on: Mapped[date | None] = mapped_column(Date)
|
||||
next_validation_on: Mapped[date | None] = mapped_column(Date)
|
||||
examiner_id: Mapped[str | None] = mapped_column(ForeignKey("users.id"))
|
||||
status: Mapped[ValidationStatus] = mapped_column(Enum(ValidationStatus), default=ValidationStatus.draft)
|
||||
result: Mapped[str | None] = mapped_column(String(120))
|
||||
notes: Mapped[str | None] = mapped_column(Text)
|
||||
equipment_ids: Mapped[list[str]] = mapped_column(JSON, default=list)
|
||||
environment_conditions: Mapped[dict] = mapped_column(JSON, default=dict)
|
||||
documentation_checklist: Mapped[list[dict]] = mapped_column(JSON, default=list)
|
||||
performance_checklist: Mapped[list[dict]] = mapped_column(JSON, default=list)
|
||||
programs: Mapped[list[dict]] = mapped_column(JSON, default=list)
|
||||
loading_patterns: Mapped[list[dict]] = mapped_column(JSON, default=list)
|
||||
measurement_data: Mapped[list[dict]] = mapped_column(JSON, default=list)
|
||||
drying: Mapped[dict] = mapped_column(JSON, default=dict)
|
||||
recommendations: Mapped[list[dict]] = mapped_column(JSON, default=list)
|
||||
attachments: Mapped[list[dict]] = mapped_column(JSON, default=list)
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class MeasurementSeries:
|
||||
headers: list[str]
|
||||
rows: list[dict[str, str]]
|
||||
|
||||
|
||||
class HeliosImportService:
|
||||
def import_csv(self, path: Path) -> MeasurementSeries:
|
||||
with path.open(newline="", encoding="utf-8-sig") as handle:
|
||||
reader = csv.DictReader(handle)
|
||||
return MeasurementSeries(headers=reader.fieldnames or [], rows=list(reader))
|
||||
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from weasyprint import HTML
|
||||
|
||||
|
||||
class OrionReportService:
|
||||
chapters = [
|
||||
"Deckblatt",
|
||||
"Inhaltsverzeichnis",
|
||||
"Zusammenfassung",
|
||||
"Gerät",
|
||||
"Kunde",
|
||||
"Normen",
|
||||
"Prüfmittel",
|
||||
"Programme",
|
||||
"Beladung",
|
||||
"Messungen",
|
||||
"Diagramme",
|
||||
"Empfehlungen",
|
||||
"Anlagen",
|
||||
]
|
||||
|
||||
def render_pdf(self, title: str, output_path: Path) -> Path:
|
||||
chapter_markup = "".join(f"<section><h2>{chapter}</h2></section>" for chapter in self.chapters)
|
||||
html = f"<html><body><h1>{title}</h1>{chapter_markup}</body></html>"
|
||||
HTML(string=html).write_pdf(output_path)
|
||||
return output_path
|
||||
|
||||
45
validation-suite/backend/mercury/app/repositories/base.py
Normal file
45
validation-suite/backend/mercury/app/repositories/base.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from sqlalchemy import Select, func, or_, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
ModelT = TypeVar("ModelT")
|
||||
|
||||
|
||||
class Repository(Generic[ModelT]):
|
||||
model: type[ModelT]
|
||||
|
||||
def __init__(self, session: Session) -> None:
|
||||
self.session = session
|
||||
|
||||
def get(self, item_id: str) -> ModelT | None:
|
||||
return self.session.get(self.model, item_id)
|
||||
|
||||
search_columns: tuple[str, ...] = ()
|
||||
|
||||
def _search_statement(self, search: str | None = None) -> Select[tuple[ModelT]]:
|
||||
statement: Select[tuple[ModelT]] = select(self.model)
|
||||
if search and self.search_columns:
|
||||
term = f"%{search.strip()}%"
|
||||
filters = [getattr(self.model, column).ilike(term) for column in self.search_columns]
|
||||
statement = statement.where(or_(*filters))
|
||||
return statement
|
||||
|
||||
def list(self, limit: int = 20, offset: int = 0, search: str | None = None) -> list[ModelT]:
|
||||
statement = self._search_statement(search).offset(offset).limit(limit)
|
||||
return list(self.session.scalars(statement))
|
||||
|
||||
def count(self, search: str | None = None) -> int:
|
||||
subquery = self._search_statement(search).subquery()
|
||||
return self.session.scalar(select(func.count()).select_from(subquery)) or 0
|
||||
|
||||
def add(self, item: ModelT) -> ModelT:
|
||||
self.session.add(item)
|
||||
self.session.flush()
|
||||
return item
|
||||
|
||||
def delete(self, item: ModelT) -> None:
|
||||
self.session.delete(item)
|
||||
self.session.flush()
|
||||
67
validation-suite/backend/mercury/app/repositories/domain.py
Normal file
67
validation-suite/backend/mercury/app/repositories/domain.py
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models.customer import Customer
|
||||
from app.models.device import Device
|
||||
from app.models.document import Document
|
||||
from app.models.equipment import Equipment
|
||||
from app.models.contact import Contact
|
||||
from app.models.location import Location
|
||||
from app.models.user import User
|
||||
from app.models.validation import Validation
|
||||
from app.repositories.base import Repository
|
||||
|
||||
|
||||
class UserRepository(Repository[User]):
|
||||
model = User
|
||||
|
||||
def by_email(self, email: str) -> User | None:
|
||||
return self.session.scalar(select(User).where(User.email == email.lower()))
|
||||
|
||||
|
||||
class CustomerRepository(Repository[Customer]):
|
||||
model = Customer
|
||||
search_columns = ("name", "city", "email", "phone")
|
||||
|
||||
|
||||
class LocationRepository(Repository[Location]):
|
||||
model = Location
|
||||
search_columns = ("name", "city", "room")
|
||||
|
||||
|
||||
class ContactRepository(Repository[Contact]):
|
||||
model = Contact
|
||||
search_columns = ("full_name", "function", "email", "phone")
|
||||
|
||||
|
||||
class DeviceRepository(Repository[Device]):
|
||||
model = Device
|
||||
search_columns = ("manufacturer", "model", "serial_number", "device_type")
|
||||
|
||||
|
||||
class EquipmentRepository(Repository[Equipment]):
|
||||
model = Equipment
|
||||
search_columns = ("manufacturer", "model", "serial_number")
|
||||
|
||||
|
||||
class ValidationRepository(Repository[Validation]):
|
||||
model = Validation
|
||||
|
||||
|
||||
class DocumentRepository(Repository[Document]):
|
||||
model = Document
|
||||
|
||||
|
||||
def repositories(session: Session) -> dict[str, Repository]:
|
||||
return {
|
||||
"users": UserRepository(session),
|
||||
"customers": CustomerRepository(session),
|
||||
"locations": LocationRepository(session),
|
||||
"contacts": ContactRepository(session),
|
||||
"devices": DeviceRepository(session),
|
||||
"equipment": EquipmentRepository(session),
|
||||
"validations": ValidationRepository(session),
|
||||
"documents": DocumentRepository(session),
|
||||
}
|
||||
24
validation-suite/backend/mercury/app/schemas/auth.py
Normal file
24
validation-suite/backend/mercury/app/schemas/auth.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from pydantic import BaseModel, EmailStr
|
||||
|
||||
from app.models.user import UserRole
|
||||
from app.schemas.common import EntityRead
|
||||
|
||||
|
||||
class LoginRequest(BaseModel):
|
||||
email: EmailStr
|
||||
password: str
|
||||
|
||||
|
||||
class TokenResponse(BaseModel):
|
||||
access_token: str
|
||||
token_type: str = "bearer"
|
||||
|
||||
|
||||
class UserRead(EntityRead):
|
||||
email: EmailStr
|
||||
full_name: str
|
||||
role: UserRole
|
||||
is_active: bool
|
||||
|
||||
25
validation-suite/backend/mercury/app/schemas/common.py
Normal file
25
validation-suite/backend/mercury/app/schemas/common.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class ORMModel(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
class EntityRead(ORMModel):
|
||||
id: str
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
|
||||
class PaginatedResponse(BaseModel, Generic[T]):
|
||||
items: list[T]
|
||||
total: int
|
||||
page: int
|
||||
page_size: int
|
||||
146
validation-suite/backend/mercury/app/schemas/domain.py
Normal file
146
validation-suite/backend/mercury/app/schemas/domain.py
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
|
||||
from pydantic import EmailStr, Field
|
||||
|
||||
from app.models.customer import CustomerType
|
||||
from app.models.equipment import EquipmentKind, EquipmentStatus
|
||||
from app.models.validation import ValidationStatus
|
||||
from app.schemas.common import EntityRead, ORMModel
|
||||
|
||||
|
||||
class CustomerCreate(ORMModel):
|
||||
customer_type: CustomerType
|
||||
name: str
|
||||
street: str | None = None
|
||||
postal_code: str | None = None
|
||||
city: str | None = None
|
||||
phone: str | None = None
|
||||
email: EmailStr | None = None
|
||||
hygiene_officer: str | None = None
|
||||
quality_manager: str | None = None
|
||||
notes: str | None = None
|
||||
|
||||
|
||||
class CustomerRead(CustomerCreate, EntityRead):
|
||||
pass
|
||||
|
||||
|
||||
class CustomerUpdate(CustomerCreate):
|
||||
pass
|
||||
|
||||
|
||||
class LocationCreate(ORMModel):
|
||||
customer_id: str
|
||||
name: str
|
||||
street: str | None = None
|
||||
postal_code: str | None = None
|
||||
city: str | None = None
|
||||
room: str | None = None
|
||||
|
||||
|
||||
class LocationRead(LocationCreate, EntityRead):
|
||||
pass
|
||||
|
||||
|
||||
class LocationUpdate(LocationCreate):
|
||||
pass
|
||||
|
||||
|
||||
class ContactCreate(ORMModel):
|
||||
customer_id: str
|
||||
full_name: str
|
||||
function: str | None = None
|
||||
email: EmailStr | None = None
|
||||
phone: str | None = None
|
||||
|
||||
|
||||
class ContactRead(ContactCreate, EntityRead):
|
||||
pass
|
||||
|
||||
|
||||
class ContactUpdate(ContactCreate):
|
||||
pass
|
||||
|
||||
|
||||
class DeviceCreate(ORMModel):
|
||||
customer_id: str
|
||||
location_id: str | None = None
|
||||
manufacturer: str
|
||||
model: str
|
||||
device_type: str | None = None
|
||||
serial_number: str
|
||||
year_built: int | None = None
|
||||
commissioned_on: date | None = None
|
||||
chamber_volume_liters: int | None = None
|
||||
steam_generation: str | None = None
|
||||
water_treatment: str | None = None
|
||||
documentation: str | None = None
|
||||
supplier: str | None = None
|
||||
|
||||
|
||||
class DeviceRead(DeviceCreate, EntityRead):
|
||||
pass
|
||||
|
||||
|
||||
class DeviceUpdate(DeviceCreate):
|
||||
pass
|
||||
|
||||
|
||||
class EquipmentCreate(ORMModel):
|
||||
kind: EquipmentKind
|
||||
manufacturer: str | None = None
|
||||
model: str | None = None
|
||||
serial_number: str
|
||||
calibrated_on: date | None = None
|
||||
calibration_due_on: date | None = None
|
||||
certificate_document_id: str | None = None
|
||||
status: EquipmentStatus = EquipmentStatus.green
|
||||
|
||||
|
||||
class EquipmentRead(EquipmentCreate, EntityRead):
|
||||
pass
|
||||
|
||||
|
||||
class EquipmentUpdate(EquipmentCreate):
|
||||
pass
|
||||
|
||||
|
||||
class ValidationCreate(ORMModel):
|
||||
report_number: str
|
||||
customer_id: str
|
||||
location_id: str | None = None
|
||||
contact_id: str | None = None
|
||||
device_id: str | None = None
|
||||
validation_type: str
|
||||
project: str | None = None
|
||||
test_location: str | None = None
|
||||
examiner_name: str | None = None
|
||||
participants: str | None = None
|
||||
operator_name: str | None = None
|
||||
scheduled_on: date | None = None
|
||||
performed_on: date | None = None
|
||||
next_validation_on: date | None = None
|
||||
examiner_id: str | None = None
|
||||
status: ValidationStatus = ValidationStatus.draft
|
||||
result: str | None = None
|
||||
notes: str | None = None
|
||||
equipment_ids: list[str] = Field(default_factory=list)
|
||||
environment_conditions: dict = Field(default_factory=dict)
|
||||
documentation_checklist: list[dict] = Field(default_factory=list)
|
||||
performance_checklist: list[dict] = Field(default_factory=list)
|
||||
programs: list[dict] = Field(default_factory=list)
|
||||
loading_patterns: list[dict] = Field(default_factory=list)
|
||||
measurement_data: list[dict] = Field(default_factory=list)
|
||||
drying: dict = Field(default_factory=dict)
|
||||
recommendations: list[dict] = Field(default_factory=list)
|
||||
attachments: list[dict] = Field(default_factory=list)
|
||||
|
||||
|
||||
class ValidationRead(ValidationCreate, EntityRead):
|
||||
pass
|
||||
|
||||
|
||||
class ValidationUpdate(ValidationCreate):
|
||||
pass
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.security import create_access_token, verify_password
|
||||
from app.repositories.domain import UserRepository
|
||||
|
||||
|
||||
class AuthService:
|
||||
def __init__(self, session: Session) -> None:
|
||||
self.users = UserRepository(session)
|
||||
|
||||
def login(self, email: str, password: str) -> str:
|
||||
user = self.users.by_email(email)
|
||||
if user is None or not user.is_active or not verify_password(password, user.password_hash):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid credentials",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
return create_access_token(user.id, user.role.value)
|
||||
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import TypeVar
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
|
||||
from app.models.contact import Contact
|
||||
from app.models.customer import Customer
|
||||
from app.models.device import Device
|
||||
from app.models.equipment import Equipment
|
||||
from app.models.location import Location
|
||||
from app.models.validation import Validation
|
||||
from app.repositories.base import Repository
|
||||
from app.repositories.domain import (
|
||||
ContactRepository,
|
||||
CustomerRepository,
|
||||
DeviceRepository,
|
||||
EquipmentRepository,
|
||||
LocationRepository,
|
||||
ValidationRepository,
|
||||
)
|
||||
|
||||
ModelT = TypeVar("ModelT")
|
||||
|
||||
|
||||
class CrudService:
|
||||
def __init__(self, repository: Repository[ModelT]) -> None:
|
||||
self.repository = repository
|
||||
|
||||
def list(self, page: int = 1, page_size: int = 20, search: str | None = None) -> dict:
|
||||
safe_page = max(page, 1)
|
||||
safe_page_size = min(max(page_size, 1), 100)
|
||||
offset = (safe_page - 1) * safe_page_size
|
||||
return {
|
||||
"items": self.repository.list(safe_page_size, offset, search),
|
||||
"total": self.repository.count(search),
|
||||
"page": safe_page,
|
||||
"page_size": safe_page_size,
|
||||
}
|
||||
|
||||
def create(self, data: dict) -> ModelT:
|
||||
return self.repository.add(self.repository.model(**data))
|
||||
|
||||
def update(self, item_id: str, data: dict) -> ModelT:
|
||||
item = self.repository.get(item_id)
|
||||
if item is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Resource not found")
|
||||
for key, value in data.items():
|
||||
setattr(item, key, value)
|
||||
return item
|
||||
|
||||
def delete(self, item_id: str) -> None:
|
||||
item = self.repository.get(item_id)
|
||||
if item is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Resource not found")
|
||||
self.repository.delete(item)
|
||||
|
||||
|
||||
class DomainServices:
|
||||
def __init__(self, session: Session) -> None:
|
||||
self.customers: CrudService[Customer] = CrudService(CustomerRepository(session))
|
||||
self.locations: CrudService[Location] = CrudService(LocationRepository(session))
|
||||
self.contacts: CrudService[Contact] = CrudService(ContactRepository(session))
|
||||
self.devices: CrudService[Device] = CrudService(DeviceRepository(session))
|
||||
self.equipment: CrudService[Equipment] = CrudService(EquipmentRepository(session))
|
||||
self.validations: CrudService[Validation] = CrudService(ValidationRepository(session))
|
||||
Loading…
Add table
Add a link
Reference in a new issue