67 lines
2.4 KiB
Python
67 lines
2.4 KiB
Python
from typing import Optional
|
|
|
|
from fastapi import APIRouter, Query, Request, Response
|
|
|
|
from app.modules.shipmondo.backend.service import shipmondo_service
|
|
from app.modules.shipmondo.models.schemas import (
|
|
ShipmondoBookingCreate,
|
|
ShipmondoBookingListResponse,
|
|
ShipmondoBookingResponse,
|
|
ShipmondoBookingSubmitResponse,
|
|
ShipmondoProductListResponse,
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
def _user_id_from_request(request: Request) -> Optional[int]:
|
|
raw_user_id = getattr(request.state, "user_id", None)
|
|
try:
|
|
return int(raw_user_id) if raw_user_id is not None else None
|
|
except (TypeError, ValueError):
|
|
return None
|
|
|
|
|
|
@router.get("/shipmondo/config")
|
|
async def shipmondo_config() -> dict:
|
|
return {
|
|
"enabled": shipmondo_service.enabled,
|
|
"configured": shipmondo_service.configured,
|
|
"read_only": shipmondo_service.read_only,
|
|
"dry_run": shipmondo_service.dry_run,
|
|
}
|
|
|
|
|
|
@router.get("/shipmondo/products", response_model=ShipmondoProductListResponse)
|
|
async def list_products(country_code: str = Query(default="DK", min_length=2, max_length=2)):
|
|
return {"items": await shipmondo_service.list_products(country_code)}
|
|
|
|
|
|
@router.post("/shipmondo/bookings", response_model=ShipmondoBookingResponse)
|
|
async def create_booking(payload: ShipmondoBookingCreate, request: Request):
|
|
return shipmondo_service.create_booking_draft(payload, _user_id_from_request(request))
|
|
|
|
|
|
@router.get("/shipmondo/bookings", response_model=ShipmondoBookingListResponse)
|
|
async def list_bookings(case_id: Optional[int] = Query(default=None, gt=0)):
|
|
return {"items": shipmondo_service.list_bookings(case_id)}
|
|
|
|
|
|
@router.get("/shipmondo/bookings/{booking_ref}", response_model=ShipmondoBookingResponse)
|
|
async def get_booking(booking_ref: str):
|
|
return shipmondo_service.get_booking(booking_ref)
|
|
|
|
|
|
@router.post("/shipmondo/bookings/{booking_ref}/submit", response_model=ShipmondoBookingSubmitResponse)
|
|
async def submit_booking(booking_ref: str, request: Request):
|
|
return await shipmondo_service.submit_booking(booking_ref, _user_id_from_request(request))
|
|
|
|
|
|
@router.get("/shipmondo/bookings/{booking_ref}/label")
|
|
async def download_label(booking_ref: str):
|
|
return Response(
|
|
content=shipmondo_service.get_label_pdf(booking_ref),
|
|
media_type="application/pdf",
|
|
headers={"Content-Disposition": f'inline; filename="{booking_ref}.pdf"'},
|
|
)
|