Major refactor WIP
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from fastapi import APIRouter
|
||||
from app.api.endpoints import auth, pprs, public, aircraft, airport, local_flights, departures, arrivals, circuits, journal, overflights, public_book
|
||||
from app.api.endpoints import auth, pprs, public, aircraft, airport, local_flights, departures, arrivals, circuits, journal, overflights, public_book, movements
|
||||
|
||||
api_router = APIRouter()
|
||||
|
||||
@@ -11,6 +11,7 @@ api_router.include_router(arrivals.router, prefix="/arrivals", tags=["arrivals"]
|
||||
api_router.include_router(overflights.router, prefix="/overflights", tags=["overflights"])
|
||||
api_router.include_router(circuits.router, prefix="/circuits", tags=["circuits"])
|
||||
api_router.include_router(journal.router, prefix="/journal", tags=["journal"])
|
||||
api_router.include_router(movements.router, prefix="/movements", tags=["movements"])
|
||||
api_router.include_router(public.router, prefix="/public", tags=["public"])
|
||||
api_router.include_router(public_book.router, prefix="/public-book", tags=["public_booking"])
|
||||
api_router.include_router(aircraft.router, prefix="/aircraft", tags=["aircraft"])
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
from typing import List, Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from datetime import date
|
||||
from app.api.deps import get_db, get_current_read_user
|
||||
from app.crud.crud_movement import movement as crud_movement
|
||||
from app.schemas.movement import Movement
|
||||
from app.models.ppr import User
|
||||
from app.models.movement import MovementType
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/", response_model=List[Movement])
|
||||
async def get_movements(
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
movement_type: Optional[MovementType] = None,
|
||||
aircraft_registration: Optional[str] = None,
|
||||
date_from: Optional[date] = None,
|
||||
date_to: Optional[date] = None,
|
||||
entity_type: Optional[str] = None,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_read_user)
|
||||
):
|
||||
"""Get movement records with optional filtering"""
|
||||
movements = crud_movement.get_multi(
|
||||
db, skip=skip, limit=limit, movement_type=movement_type,
|
||||
aircraft_registration=aircraft_registration, date_from=date_from,
|
||||
date_to=date_to, entity_type=entity_type
|
||||
)
|
||||
return movements
|
||||
|
||||
|
||||
@router.get("/{movement_id}", response_model=Movement)
|
||||
async def get_movement(
|
||||
movement_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_read_user)
|
||||
):
|
||||
"""Get a specific movement record"""
|
||||
movement = crud_movement.get(db, movement_id=movement_id)
|
||||
if not movement:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Movement record not found"
|
||||
)
|
||||
return movement
|
||||
@@ -5,7 +5,11 @@ from datetime import date
|
||||
from app.api.deps import get_db, get_current_read_user, get_current_operator_user
|
||||
from app.crud.crud_ppr import ppr as crud_ppr
|
||||
from app.crud.crud_journal import journal as crud_journal
|
||||
from app.crud.crud_arrival import arrival as crud_arrival
|
||||
from app.crud.crud_departure import departure as crud_departure
|
||||
from app.schemas.ppr import PPR, PPRCreate, PPRUpdate, PPRStatus, PPRStatusUpdate, Journal
|
||||
from app.schemas.arrival import ArrivalCreate
|
||||
from app.schemas.departure import DepartureCreate
|
||||
from app.models.ppr import User
|
||||
from app.core.utils import get_client_ip
|
||||
from app.core.email import email_service
|
||||
@@ -373,4 +377,78 @@ async def get_ppr_journal(
|
||||
detail="PPR record not found"
|
||||
)
|
||||
|
||||
return crud_journal.get_by_ppr_id(db, ppr_id=ppr_id)
|
||||
return crud_journal.get_by_ppr_id(db, ppr_id=ppr_id)
|
||||
|
||||
|
||||
@router.post("/{ppr_id}/activate")
|
||||
async def activate_ppr(
|
||||
request: Request,
|
||||
ppr_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_operator_user)
|
||||
):
|
||||
"""Activate a PPR by creating BOOKED_IN arrival and (if out_to set) BOOKED_OUT departure records."""
|
||||
db_ppr = crud_ppr.get(db, ppr_id=ppr_id)
|
||||
if not db_ppr:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="PPR record not found")
|
||||
|
||||
if db_ppr.status not in (PPRStatus.NEW, PPRStatus.CONFIRMED):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"PPR cannot be activated in its current state ({db_ppr.status.value})"
|
||||
)
|
||||
|
||||
client_ip = get_client_ip(request)
|
||||
username = current_user.username
|
||||
|
||||
# Create INBOUND arrival (ADMIN submitted_via sets status to INBOUND)
|
||||
in_from = (db_ppr.in_from or "ZZZZ")[:4].upper()
|
||||
arrival_in = ArrivalCreate(
|
||||
registration=db_ppr.ac_reg,
|
||||
type=db_ppr.ac_type,
|
||||
callsign=db_ppr.ac_call,
|
||||
pob=db_ppr.pob_in,
|
||||
in_from=in_from,
|
||||
eta=db_ppr.eta,
|
||||
notes=db_ppr.notes,
|
||||
submitted_via="ADMIN"
|
||||
)
|
||||
new_arrival = crud_arrival.create(db, obj_in=arrival_in, created_by=username, submitted_via="ADMIN", user_ip=client_ip)
|
||||
|
||||
# Create PENDING departure linked to this arrival (only visible once arrival lands)
|
||||
new_departure = None
|
||||
if db_ppr.out_to:
|
||||
departure_in = DepartureCreate(
|
||||
registration=db_ppr.ac_reg,
|
||||
type=db_ppr.ac_type,
|
||||
callsign=db_ppr.ac_call,
|
||||
pob=db_ppr.pob_out if db_ppr.pob_out else db_ppr.pob_in,
|
||||
out_to=db_ppr.out_to,
|
||||
etd=db_ppr.etd,
|
||||
notes=db_ppr.notes,
|
||||
arrival_id=new_arrival.id,
|
||||
)
|
||||
new_departure = crud_departure.create(db, obj_in=departure_in, created_by=username, submitted_via="ADMIN", user_ip=client_ip)
|
||||
|
||||
# Mark PPR as ACTIVATED — removes it from Today's PPR and pending arrivals displays
|
||||
crud_ppr.update_status(db, ppr_id=ppr_id, status=PPRStatus.ACTIVATED, user=username, user_ip=client_ip)
|
||||
|
||||
# Broadcast WebSocket update
|
||||
if hasattr(request.app.state, 'connection_manager'):
|
||||
await request.app.state.connection_manager.broadcast({
|
||||
"type": "ppr_activated",
|
||||
"data": {
|
||||
"ppr_id": ppr_id,
|
||||
"arrival_id": new_arrival.id,
|
||||
"departure_id": new_departure.id if new_departure else None
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
"arrival_id": new_arrival.id,
|
||||
"departure_id": new_departure.id if new_departure else None,
|
||||
"message": (
|
||||
f"PPR activated: arrival #{new_arrival.id} created"
|
||||
+ (f", departure #{new_departure.id} queued (will appear when aircraft lands)" if new_departure else "")
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user