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:
@ -30,6 +30,9 @@ from app.models import (
|
||||
Sample as SampleModel,
|
||||
DewarType as DewarTypeModel,
|
||||
DewarSerialNumber as DewarSerialNumberModel,
|
||||
LogisticsEvent,
|
||||
PuckEvent,
|
||||
SampleEvent,
|
||||
)
|
||||
from app.dependencies import get_db
|
||||
import qrcode
|
||||
@ -505,12 +508,55 @@ async def update_dewar(
|
||||
|
||||
@router.delete("/{dewar_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_dewar(dewar_id: int, db: Session = Depends(get_db)):
|
||||
# Fetch the Dewar from the database
|
||||
dewar = db.query(DewarModel).filter(DewarModel.id == dewar_id).first()
|
||||
|
||||
if not dewar:
|
||||
raise HTTPException(status_code=404, detail="Dewar not found")
|
||||
|
||||
db.delete(dewar)
|
||||
# Check for associated logistics events
|
||||
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 associated logistics events."
|
||||
f"Deletion 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} "
|
||||
f"associated with this Dewar has events."
|
||||
f"Deletion 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"associated with Puck {puck.id} has events."
|
||||
f" Deletion not allowed.",
|
||||
)
|
||||
|
||||
# Perform cascade deletion: Delete samples, pucks, and the dewar
|
||||
for puck in dewar.pucks:
|
||||
for sample in puck.samples:
|
||||
db.delete(sample) # Delete associated samples
|
||||
db.delete(puck) # Delete associated puck
|
||||
db.delete(dewar) # Finally, delete the dewar itself
|
||||
|
||||
db.commit()
|
||||
return
|
||||
|
||||
|
Reference in New Issue
Block a user