Implement display artifacts, pricing integrity, draft base and publish preview bundle
This commit is contained in:
96
backend/app/services/editor_validation.py
Normal file
96
backend/app/services/editor_validation.py
Normal file
@@ -0,0 +1,96 @@
|
||||
from fastapi import HTTPException, status
|
||||
|
||||
from app.repositories.scheme_groups import list_scheme_version_groups
|
||||
from app.repositories.scheme_seats import list_scheme_version_seats
|
||||
from app.repositories.scheme_sectors import list_scheme_version_sectors
|
||||
|
||||
|
||||
async def validate_single_seat_patch_uniqueness(
|
||||
*,
|
||||
scheme_version_id: str,
|
||||
seat_record_id: str,
|
||||
new_seat_id: str | None,
|
||||
) -> None:
|
||||
if not new_seat_id:
|
||||
return
|
||||
|
||||
seats = await list_scheme_version_seats(scheme_version_id)
|
||||
for seat in seats:
|
||||
if seat.seat_id == new_seat_id and seat.seat_record_id != seat_record_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=f"seat_id already exists in draft version: {new_seat_id}",
|
||||
)
|
||||
|
||||
|
||||
async def validate_bulk_seat_patch_uniqueness(
|
||||
*,
|
||||
scheme_version_id: str,
|
||||
items: list[dict],
|
||||
) -> None:
|
||||
seats = await list_scheme_version_seats(scheme_version_id)
|
||||
existing = {seat.seat_id: seat.seat_record_id for seat in seats if seat.seat_id}
|
||||
|
||||
payload_new_ids = [item.get("seat_id") for item in items if item.get("seat_id")]
|
||||
duplicates_inside_payload = sorted(
|
||||
{
|
||||
seat_id
|
||||
for seat_id in payload_new_ids
|
||||
if payload_new_ids.count(seat_id) > 1
|
||||
}
|
||||
)
|
||||
if duplicates_inside_payload:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=f"Duplicate seat_id values inside bulk payload: {', '.join(duplicates_inside_payload)}",
|
||||
)
|
||||
|
||||
for item in items:
|
||||
new_seat_id = item.get("seat_id")
|
||||
seat_record_id = item["seat_record_id"]
|
||||
|
||||
if not new_seat_id:
|
||||
continue
|
||||
|
||||
existing_record_id = existing.get(new_seat_id)
|
||||
if existing_record_id and existing_record_id != seat_record_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=f"seat_id already exists in draft version: {new_seat_id}",
|
||||
)
|
||||
|
||||
|
||||
async def validate_sector_patch_uniqueness(
|
||||
*,
|
||||
scheme_version_id: str,
|
||||
sector_record_id: str,
|
||||
new_sector_id: str | None,
|
||||
) -> None:
|
||||
if not new_sector_id:
|
||||
return
|
||||
|
||||
sectors = await list_scheme_version_sectors(scheme_version_id)
|
||||
for sector in sectors:
|
||||
if sector.sector_id == new_sector_id and sector.sector_record_id != sector_record_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=f"sector_id already exists in draft version: {new_sector_id}",
|
||||
)
|
||||
|
||||
|
||||
async def validate_group_patch_uniqueness(
|
||||
*,
|
||||
scheme_version_id: str,
|
||||
group_record_id: str,
|
||||
new_group_id: str | None,
|
||||
) -> None:
|
||||
if not new_group_id:
|
||||
return
|
||||
|
||||
groups = await list_scheme_version_groups(scheme_version_id)
|
||||
for group in groups:
|
||||
if group.group_id == new_group_id and group.group_record_id != group_record_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=f"group_id already exists in draft version: {new_group_id}",
|
||||
)
|
||||
Reference in New Issue
Block a user