feat: adds deployment records for seismographs.
This commit is contained in:
@@ -9,7 +9,8 @@ import httpx
|
||||
import os
|
||||
|
||||
from backend.database import get_db
|
||||
from backend.models import RosterUnit, IgnoredUnit, Emitter, UnitHistory, UserPreferences
|
||||
from backend.models import RosterUnit, IgnoredUnit, Emitter, UnitHistory, UserPreferences, DeploymentRecord
|
||||
import uuid
|
||||
from backend.services.slmm_sync import sync_slm_to_slmm
|
||||
|
||||
router = APIRouter(prefix="/api/roster", tags=["roster-edit"])
|
||||
@@ -27,6 +28,38 @@ def get_calibration_interval(db: Session) -> int:
|
||||
SLMM_BASE_URL = os.getenv("SLMM_BASE_URL", "http://localhost:8100")
|
||||
|
||||
|
||||
def sync_deployment_record(db: Session, unit: RosterUnit, new_deployed: bool):
|
||||
"""
|
||||
Keep DeploymentRecord in sync with the deployed flag.
|
||||
|
||||
deployed True → open a new DeploymentRecord if none is already open.
|
||||
deployed False → close the active DeploymentRecord by setting actual_removal_date = today.
|
||||
"""
|
||||
if new_deployed:
|
||||
existing = db.query(DeploymentRecord).filter(
|
||||
DeploymentRecord.unit_id == unit.id,
|
||||
DeploymentRecord.actual_removal_date == None
|
||||
).first()
|
||||
if not existing:
|
||||
record = DeploymentRecord(
|
||||
id=str(uuid.uuid4()),
|
||||
unit_id=unit.id,
|
||||
project_ref=unit.project_id or None,
|
||||
deployed_date=date.today(),
|
||||
created_at=datetime.utcnow(),
|
||||
updated_at=datetime.utcnow(),
|
||||
)
|
||||
db.add(record)
|
||||
else:
|
||||
active = db.query(DeploymentRecord).filter(
|
||||
DeploymentRecord.unit_id == unit.id,
|
||||
DeploymentRecord.actual_removal_date == None
|
||||
).first()
|
||||
if active:
|
||||
active.actual_removal_date = date.today()
|
||||
active.updated_at = datetime.utcnow()
|
||||
|
||||
|
||||
def record_history(db: Session, unit_id: str, change_type: str, field_name: str = None,
|
||||
old_value: str = None, new_value: str = None, source: str = "manual", notes: str = None):
|
||||
"""Helper function to record a change in unit history"""
|
||||
@@ -679,6 +712,7 @@ async def edit_roster_unit(
|
||||
status_text = "deployed" if deployed else "benched"
|
||||
old_status_text = "deployed" if old_deployed else "benched"
|
||||
record_history(db, unit_id, "deployed_change", "deployed", old_status_text, status_text, "manual")
|
||||
sync_deployment_record(db, unit, deployed_bool)
|
||||
|
||||
if old_retired != retired:
|
||||
status_text = "retired" if retired else "active"
|
||||
@@ -795,6 +829,7 @@ async def set_deployed(unit_id: str, deployed: bool = Form(...), db: Session = D
|
||||
new_value=status_text,
|
||||
source="manual"
|
||||
)
|
||||
sync_deployment_record(db, unit, deployed)
|
||||
|
||||
db.commit()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user