fixed bug with uk zip code and add retrieved timestamp for dewars at the beamline
This commit is contained in:
@ -142,6 +142,7 @@ async def get_all_slots(db: Session = Depends(get_db)):
|
||||
slots_with_refill_time = []
|
||||
|
||||
for slot in slots:
|
||||
# Initialize variables for slot-related data
|
||||
time_until_refill = None
|
||||
retrievedTimestamp = None
|
||||
beamlineLocation = None
|
||||
@ -153,9 +154,9 @@ async def get_all_slots(db: Session = Depends(get_db)):
|
||||
last_refill_event = db.query(LogisticsEventModel) \
|
||||
.join(DewarModel, DewarModel.id == LogisticsEventModel.dewar_id) \
|
||||
.filter(
|
||||
DewarModel.unique_id == slot.dewar.unique_id,
|
||||
LogisticsEventModel.event_type == "refill"
|
||||
) \
|
||||
DewarModel.unique_id == slot.dewar.unique_id,
|
||||
LogisticsEventModel.event_type == "refill"
|
||||
) \
|
||||
.order_by(LogisticsEventModel.timestamp.desc()) \
|
||||
.first()
|
||||
|
||||
@ -165,42 +166,35 @@ async def get_all_slots(db: Session = Depends(get_db)):
|
||||
else:
|
||||
time_until_refill = -1
|
||||
|
||||
# Get last retrieved timestamp
|
||||
last_retrieved_event = db.query(LogisticsEventModel) \
|
||||
# Fetch the latest beamline event
|
||||
last_beamline_event = db.query(LogisticsEventModel) \
|
||||
.join(DewarModel, DewarModel.id == LogisticsEventModel.dewar_id) \
|
||||
.filter(
|
||||
DewarModel.unique_id == slot.dewar.unique_id,
|
||||
LogisticsEventModel.event_type == "retrieved"
|
||||
) \
|
||||
DewarModel.unique_id == slot.dewar.unique_id,
|
||||
LogisticsEventModel.event_type == "beamline"
|
||||
) \
|
||||
.order_by(LogisticsEventModel.timestamp.desc()) \
|
||||
.first()
|
||||
|
||||
if last_retrieved_event:
|
||||
retrievedTimestamp = last_retrieved_event.timestamp.isoformat()
|
||||
retrieved = True
|
||||
if last_beamline_event:
|
||||
# Set retrievedTimestamp to the timestamp of the beamline event
|
||||
retrievedTimestamp = last_beamline_event.timestamp.isoformat()
|
||||
|
||||
# Determine the last event excluding refills
|
||||
last_event = db.query(LogisticsEventModel) \
|
||||
.join(DewarModel, DewarModel.id == LogisticsEventModel.dewar_id) \
|
||||
.filter(
|
||||
DewarModel.unique_id == slot.dewar.unique_id,
|
||||
LogisticsEventModel.event_type != "refill"
|
||||
) \
|
||||
.order_by(LogisticsEventModel.timestamp.desc()) \
|
||||
.first()
|
||||
|
||||
if last_event:
|
||||
associated_slot = db.query(SlotModel).filter(SlotModel.id == last_event.slot_id).first()
|
||||
# Fetch the associated slot's label for beamlineLocation
|
||||
associated_slot = db.query(SlotModel).filter(SlotModel.id == last_beamline_event.slot_id).first()
|
||||
beamlineLocation = associated_slot.label if associated_slot else None
|
||||
at_beamline = last_event.event_type == "beamline"
|
||||
|
||||
# Corrected the contact_person assignment
|
||||
# Mark as being at a beamline
|
||||
at_beamline = True
|
||||
|
||||
# Correct the contact_person assignment
|
||||
contact_person = None
|
||||
if slot.dewar and slot.dewar.contact_person:
|
||||
first_name = slot.dewar.contact_person.firstname
|
||||
last_name = slot.dewar.contact_person.lastname
|
||||
contact_person = f"{first_name} {last_name}"
|
||||
|
||||
# Prepare the slot data for the response
|
||||
slot_data = SlotSchema(
|
||||
id=slot.id,
|
||||
qr_code=slot.qr_code,
|
||||
@ -212,20 +206,19 @@ async def get_all_slots(db: Session = Depends(get_db)):
|
||||
dewar_name=slot.dewar.dewar_name if slot.dewar else None,
|
||||
time_until_refill=time_until_refill,
|
||||
at_beamline=at_beamline,
|
||||
retrieved=retrieved,
|
||||
retrievedTimestamp=retrievedTimestamp,
|
||||
beamlineLocation=beamlineLocation,
|
||||
shipment_name=slot.dewar.shipment.shipment_name if slot.dewar and slot.dewar.shipment else None,
|
||||
contact_person=contact_person,
|
||||
local_contact='local contact placeholder'
|
||||
local_contact="local contact placeholder",
|
||||
)
|
||||
logger.info(f"Dewar retrieved: {retrieved}")
|
||||
logger.info(f"Dewar at: {beamlineLocation}")
|
||||
# Add updated slot data to the response list
|
||||
slots_with_refill_time.append(slot_data)
|
||||
|
||||
return slots_with_refill_time
|
||||
|
||||
|
||||
|
||||
@router.post("/dewar/refill", response_model=dict)
|
||||
async def refill_dewar(qr_code: str, db: Session = Depends(get_db)):
|
||||
logger.info(f"Refilling dewar with QR code: {qr_code}")
|
||||
|
@ -310,6 +310,7 @@ class SlotSchema(BaseModel):
|
||||
dewar_name: Optional[str]
|
||||
time_until_refill: Optional[int]
|
||||
at_beamline: Optional[bool]
|
||||
retrievedTimestamp: Optional[str]
|
||||
beamlineLocation: Optional[str]
|
||||
shipment_name: Optional[str]
|
||||
contact_person: Optional[str]
|
||||
|
Reference in New Issue
Block a user