From 06f881cef3881a311a2708bdfdceb88ffce3fdcf Mon Sep 17 00:00:00 2001 From: Cizz22 Date: Wed, 18 Dec 2024 15:26:51 +0700 Subject: [PATCH] add --- src/overhaul_history/model.py | 18 ++++++++++++++++++ src/overhaul_history/service.py | 19 +++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/overhaul_history/model.py b/src/overhaul_history/model.py index 2103cd3..98b0bb9 100644 --- a/src/overhaul_history/model.py +++ b/src/overhaul_history/model.py @@ -1,9 +1,11 @@ from sqlalchemy import UUID, Column, DateTime, Float, ForeignKey, Integer, String +from sqlalchemy.orm import relationship from src.database.core import Base from src.models import DefaultMixin from .enums import OverhaulStatus + class OverhaulHistory(Base, DefaultMixin): __tablename__ = "oh_tr_overhaul_history" @@ -15,3 +17,19 @@ class OverhaulHistory(Base, DefaultMixin): status = Column(String, nullable=False, default=OverhaulStatus.PLANNED) maximo_id = Column(String, nullable=True, comment="Id From MAXIMO regarding overhaul schedule") + + equipments = relationship("OverhaulHistoryEquip", + back_populates="overhaul_history", + cascade="all, delete-orphan") + + +class OverhaulHistoryEquip(Base, DefaultMixin): + __tablename__ = "oh_tr_overhaul_history_equip" + + assetnum = Column(String(10), nullable=False) + overhaul_history_id = Column( + UUID(as_uuid=True), ForeignKey("oh_tr_overhaul_history.id"), nullable=False) + + # Relationship to OverhaulHistory + overhaul_history = relationship("OverhaulHistory", + back_populates="equipments") diff --git a/src/overhaul_history/service.py b/src/overhaul_history/service.py index b019d6a..b5397e3 100644 --- a/src/overhaul_history/service.py +++ b/src/overhaul_history/service.py @@ -6,13 +6,14 @@ from sqlalchemy import Select, Delete, and_ from src.maximo.service import MaximoDataMapper from src.overhaul_history.enums import OverhaulStatus from src.overhaul_history.utils import determine_overhaul_status -from .model import OverhaulHistory +from .model import OverhaulHistory, OverhaulHistoryEquip from .schema import OverhaulHistoryRead, OverhaulHistoryCreate from typing import Optional from src.database.core import DbSession from src.auth.service import CurrentUser from src.scope.service import get_by_scope_name +from src.scope_equipment.service import get_by_scope_name as scope_equipment_by_scope_name async def get(*, db_session: DbSession, overhaul_history_id: str) -> Optional[OverhaulHistory]: @@ -47,7 +48,7 @@ async def start_overhaul(*, db_session: DbSession, maximo_data: dict): ) status, status_reason = await determine_overhaul_status(maximo_data) - scope = await get_by_scope_name("A") + scope = await get_by_scope_name(db_session=db_session, scope_name="A") overhaul = OverhaulHistory( scope_id=scope.id, @@ -57,6 +58,20 @@ async def start_overhaul(*, db_session: DbSession, maximo_data: dict): maximo_id=maximo_id, status=status ) + # Get equipment list asynchronously + scope_equipment = await scope_equipment_by_scope_name( + db_session=db_session, + scope_name="A" + ) + + # Create equipment instances + equipments = [OverhaulHistoryEquip(assetnum=eq.assetnum) + for eq in scope_equipment] + + # Assign equipment to overhaul + overhaul.equipments = equipments + + # Get All Equipment db_session.add(overhaul) await db_session.commit()