fixed bug with uk zip code and add retrieved timestamp for dewars at the beamline

This commit is contained in:
GotthardG 2024-12-12 11:59:42 +01:00
parent fd17c0e672
commit 6c88ff9651
5 changed files with 28 additions and 68 deletions

View File

@ -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}")

View File

@ -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]

View File

@ -78,7 +78,11 @@ const ShipmentForm: React.FC<ShipmentFormProps> = ({ sx = {}, onCancel, refreshS
const validateEmail = (email: string) => /\S+@\S+\.\S+/.test(email);
const validatePhoneNumber = (phone: string) => /^\+?[1-9]\d{1,14}$/.test(phone);
const validateZipCode = (zipcode: string) => /^\d{5}(?:[-\s]\d{4})?$/.test(zipcode);
const validateZipCode = (zipcode: string) => {
const usZipCodeRegex = /^\d{4,5}(?:[-\s]\d{4})?$/;
const ukPostCodeRegex = /^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$/i;
return usZipCodeRegex.test(zipcode) || ukPostCodeRegex.test(zipcode);
};
const isContactFormValid = () => {
const { firstname, lastname, phone_number, email } = newContactPerson;

View File

@ -51,19 +51,6 @@ const Storage: React.FC<StorageProps> = ({ name, selectedSlot, slotsData, onSele
/>
))}
</StorageWrapper>
<Button
variant="contained"
color="secondary"
onClick={() => {
const selectedSlotData = slotsData.find(slot => slot.qr_code === selectedSlot);
if (selectedSlotData) {
onRefillDewar(selectedSlotData);
} else {
alert('Please select a slot to refill its dewar.');
}
}}>
Refill Dewar
</Button>
</StorageContainer>
);
};

View File

@ -351,28 +351,6 @@ const LogisticsView: React.FC = () => {
}
};
const handleRetrieve = async () => {
if (!retrievedDewar) {
alert('No dewar selected for retrieval.');
return;
}
try {
await LogisticsService.retrieveDewarLogisticsDewarsRetrievePost({
dewar_qr_code: retrievedDewar,
location_qr_code: '',
transaction_type: 'retrieved',
});
alert(`Dewar ${retrievedDewar} retrieved successfully.`);
setRetrievedDewar(null);
fetchDewarsAndSlots();
} catch (e) {
console.error(e);
alert('Error retrieving dewar');
}
};
const handleOutgoing = async () => {
if (!dewarQr) {
alert('Scan a dewar QR code first.');
@ -451,9 +429,6 @@ const LogisticsView: React.FC = () => {
<Button variant="contained" color="secondary" onClick={() => handleRefillDewar()}>
Refill Dewar
</Button>
<Button variant="outlined" color="warning" onClick={handleRetrieve} sx={{ mb: 2 }}>
Retrieved
</Button>
</Box>
</Grid>