Enhance deletion processes with event-check validations.

Added validation to prevent deletion of shipments, dewars, pucks, or samples if they have associated events. Updated frontend components to handle and display error messages based on API responses for improved user feedback.
This commit is contained in:
GotthardG
2025-01-15 15:30:54 +01:00
parent e63af3e66d
commit 481068603b
4 changed files with 187 additions and 16 deletions

View File

@ -15,6 +15,9 @@ from app.models import (
Dewar as DewarModel,
Puck as PuckModel,
Sample as SampleModel,
LogisticsEvent,
SampleEvent,
PuckEvent,
)
from app.schemas import (
ShipmentCreate,
@ -117,9 +120,60 @@ async def create_shipment(shipment: ShipmentCreate, db: Session = Depends(get_db
@router.delete("/{shipment_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_shipment(shipment_id: int, db: Session = Depends(get_db)):
# Fetch the shipment
shipment = db.query(ShipmentModel).filter(ShipmentModel.id == shipment_id).first()
if not shipment:
raise HTTPException(status_code=404, detail="Shipment not found")
# Check associated dewars
dewars = db.query(DewarModel).filter(DewarModel.shipment_id == shipment_id).all()
if dewars:
# Ensure dewars, pucks, and samples have no associated events
for dewar in dewars:
if (
db.query(LogisticsEvent)
.filter(LogisticsEvent.dewar_id == dewar.id)
.first()
):
raise HTTPException(
status_code=400,
detail=f"Dewar {dewar.id} has associated logistics events."
f"Shipment cannot be deleted.",
)
for puck in dewar.pucks:
if db.query(PuckEvent).filter(PuckEvent.puck_id == puck.id).first():
raise HTTPException(
status_code=400,
detail=f"Puck {puck.id}"
f" in Dewar {dewar.id}"
f" has associated puck events."
f"Shipment cannot be deleted.",
)
for sample in puck.samples:
if (
db.query(SampleEvent)
.filter(SampleEvent.sample_id == sample.id)
.first()
):
raise HTTPException(
status_code=400,
detail=f"Sample {sample.id}"
f" in Puck {puck.id}"
f" has associated sample events."
f"Shipment cannot be deleted.",
)
# If no events are found, proceed to delete the shipment
for dewar in dewars:
for puck in dewar.pucks:
for sample in puck.samples:
db.delete(sample) # Delete associated samples
db.delete(puck) # Delete associated pucks
db.delete(dewar) # Delete the dewar itself
# Finally, delete the shipment
db.delete(shipment)
db.commit()
return
@ -228,19 +282,63 @@ async def add_dewar_to_shipment(
async def remove_dewar_from_shipment(
shipment_id: int, dewar_id: int, db: Session = Depends(get_db)
):
# Fetch the shipment
shipment = db.query(ShipmentModel).filter(ShipmentModel.id == shipment_id).first()
if not shipment:
raise HTTPException(status_code=404, detail="Shipment not found")
dewar_exists = any(dw.id == dewar_id for dw in shipment.dewars)
if not dewar_exists:
# Check if the dewar belongs to the shipment
dewar = (
db.query(DewarModel)
.filter(DewarModel.id == dewar_id, DewarModel.shipment_id == shipment_id)
.first()
)
if not dewar:
raise HTTPException(
status_code=404, detail=f"Dewar with ID {dewar_id} not found in shipment"
status_code=404,
detail=f"Dewar with ID {dewar_id} not found in shipment {shipment_id}",
)
# Check for logistics events associated with the dewar
logistics_event_exists = (
db.query(LogisticsEvent).filter(LogisticsEvent.dewar_id == dewar_id).first()
)
if logistics_event_exists:
raise HTTPException(
status_code=400,
detail=f"Dewar {dewar_id} has " f" logistics events. Removal not allowed.",
)
# Check associated pucks and their events
for puck in dewar.pucks:
puck_event_exists = (
db.query(PuckEvent).filter(PuckEvent.puck_id == puck.id).first()
)
if puck_event_exists:
raise HTTPException(
status_code=400,
detail=f"Puck {puck.id} in Dewar {dewar_id}"
f" has associated events. Removal not allowed.",
)
# Check associated samples and their events
for sample in puck.samples:
sample_event_exists = (
db.query(SampleEvent).filter(SampleEvent.sample_id == sample.id).first()
)
if sample_event_exists:
raise HTTPException(
status_code=400,
detail=f"Sample {sample.id} "
f"in Puck {puck.id} "
f"has associated events. Removal not allowed.",
)
# Unlink the dewar from the shipment
shipment.dewars = [dw for dw in shipment.dewars if dw.id != dewar_id]
db.commit()
db.refresh(shipment)
return shipment