From 1798c480f611fd47aa4c4dddc03d278e4861b943 Mon Sep 17 00:00:00 2001 From: GotthardG <51994228+GotthardG@users.noreply.github.com> Date: Mon, 2 Dec 2024 13:36:48 +0100 Subject: [PATCH] outgoing by scanning the outgoing qr code --- backend/app/routers/logistics.py | 34 +++++++++++++++++++++++++++----- backend/app/schemas.py | 8 ++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/backend/app/routers/logistics.py b/backend/app/routers/logistics.py index d22d771..af1b3ed 100644 --- a/backend/app/routers/logistics.py +++ b/backend/app/routers/logistics.py @@ -79,39 +79,63 @@ async def scan_dewar(event_data: LogisticsEventCreate, db: Session = Depends(get location_qr_code = event_data.location_qr_code transaction_type = event_data.transaction_type + # Validate Dewar QR Code if not dewar_qr_code or not dewar_qr_code.strip(): logger.error("Dewar QR Code is null or empty") raise HTTPException(status_code=422, detail="Dewar QR Code cannot be null or empty") + # Retrieve the Dewar dewar = db.query(DewarModel).filter(DewarModel.unique_id == dewar_qr_code).first() if not dewar: + logger.error("Dewar not found") raise HTTPException(status_code=404, detail="Dewar not found") - slot = db.query(SlotModel).filter(SlotModel.qr_code == location_qr_code).first() + # Check for Outgoing QR Codes and set transaction type + if location_qr_code in ["Outgoing X10-SA", "Outgoing X06-SA"]: + transaction_type = 'outgoing' + # Retrieve the Slot associated with the Dewar (for outgoing) + slot = None + if transaction_type == 'outgoing': + slot = db.query(SlotModel).filter(SlotModel.dewar_unique_id == dewar.unique_id).first() + if not slot: + logger.error(f"No slot associated with dewar for outgoing: {dewar_qr_code}") + raise HTTPException(status_code=404, detail="No slot associated with dewar for outgoing") + + # Incoming Logic if transaction_type == 'incoming': + slot = db.query(SlotModel).filter(SlotModel.qr_code == location_qr_code).first() if not slot or slot.occupied: - logger.error(f"Slot not found or already occupied: {slot}") + logger.error(f"Slot not found or already occupied: {location_qr_code}") raise HTTPException(status_code=400, detail="Slot not found or already occupied") slot.dewar_unique_id = dewar.unique_id slot.occupied = True elif transaction_type == 'outgoing': - if not slot or not slot.occupied or slot.dewar_unique_id != dewar.unique_id: - logger.error(f"Slot not found or dewar not associated with slot: {slot}") - raise HTTPException(status_code=400, detail="Slot not found or dewar not associated with slot") + if not slot.occupied or slot.dewar_unique_id != dewar.unique_id: + logger.error(f"Slot not valid for outgoing: {location_qr_code}") + raise HTTPException(status_code=400, detail="Dewar not associated with the slot for outgoing") slot.dewar_unique_id = None slot.occupied = False elif transaction_type == 'beamline': + slot = db.query(SlotModel).filter(SlotModel.qr_code == location_qr_code).first() if not slot: logger.error(f"Beamline location not found: {location_qr_code}") raise HTTPException(status_code=400, detail="Beamline location not found") dewar.beamline_location = location_qr_code logger.info(f"Dewar {dewar_qr_code} assigned to beamline {location_qr_code}") + # Log the event log_event(db, dewar.id, slot.id if slot else None, transaction_type) db.commit() + logger.info( + f"Transaction completed: {transaction_type} for dewar {dewar_qr_code} in slot {slot.qr_code if slot else 'N/A'}") + return {"message": "Status updated successfully"} + + + + @router.get("/slots", response_model=List[SlotSchema]) async def get_all_slots(db: Session = Depends(get_db)): slots = db.query(SlotModel).options(joinedload(SlotModel.dewar)).all() diff --git a/backend/app/schemas.py b/backend/app/schemas.py index af1df69..919fa39 100644 --- a/backend/app/schemas.py +++ b/backend/app/schemas.py @@ -4,6 +4,14 @@ from pydantic import BaseModel, EmailStr, constr, Field from datetime import date +class loginToken(BaseModel): + access_token: str + token_type: str + +class loginData(BaseModel): + username: str + pgroups: List[int] + class DewarTypeBase(BaseModel): dewar_type: str