now associating a dewar to a slot

This commit is contained in:
GotthardG
2024-11-19 14:43:26 +01:00
parent bf46a7ff37
commit 98d6265ae1
4 changed files with 69 additions and 10 deletions

View File

@ -79,7 +79,6 @@ class Dewar(Base):
contact_person_id = Column(Integer, ForeignKey("contact_persons.id"))
shipment = relationship("Shipment", back_populates="dewars")
events = relationship("LogisticsEvent", back_populates="dewar")
return_address = relationship("Address")
contact_person = relationship("ContactPerson")
pucks = relationship("Puck", back_populates="dewar")
@ -87,6 +86,7 @@ class Dewar(Base):
dewar_type = relationship("DewarType")
dewar_serial_number = relationship("DewarSerialNumber")
slot = relationship("Slot", back_populates="dewar")
events = relationship("LogisticsEvent", back_populates="dewar")
@property
def number_of_pucks(self) -> int:
@ -147,6 +147,7 @@ class Slot(Base):
time_until_refill = Column(Integer) # store as total seconds
dewar_unique_id = Column(String, ForeignKey('dewars.unique_id'), nullable=True) # Added field
dewar = relationship("Dewar", back_populates="slot")
events = relationship("LogisticsEvent", back_populates="slot")
@property
@ -155,13 +156,14 @@ class Slot(Base):
return self.last_refill + self.time_until_refill - datetime.utcnow()
return None
class LogisticsEvent(Base):
__tablename__ = 'logistics_events'
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
dewar_id = Column(Integer, ForeignKey('dewars.id'), nullable=False)
event_type = Column(String, nullable=False)
timestamp = Column(DateTime, default=datetime.utcnow)
__tablename__ = "logistics_events"
id = Column(Integer, primary_key=True, index=True)
dewar_id = Column(Integer, ForeignKey('dewars.id')) # corrected table name
slot_id = Column(Integer, ForeignKey('slots.id')) # corrected table name
event_type = Column(String, index=True)
timestamp = Column(DateTime, default=datetime.utcnow)
action_details = Column(String)
dewar = relationship("Dewar", back_populates="events")
slot_id = Column(String, ForeignKey('slots.id'), nullable=True)
slot = relationship("Slot", back_populates="events")

View File

@ -87,4 +87,22 @@ async def scan_dewar(event_data: LogisticsEventCreate, db: Session = Depends(get
def log_event(db: Session, dewar_id: int, slot_id: int, event_type: str):
new_event = LogisticsEventModel(dewar_id=dewar_id, slot_id=slot_id, event_type=event_type)
db.add(new_event)
db.commit()
db.commit()
@router.post("/dewar/refill", response_model=dict)
async def refill_dewar(qr_code: str, db: Session = Depends(get_db)):
dewar = db.query(DewarModel).filter(DewarModel.unique_id == qr_code).first()
if not dewar:
raise HTTPException(status_code=404, detail="Dewar not found")
# Process refill
dewar.last_refill = datetime.now()
new_event = LogisticsEventModel(
dewar_id=dewar.id, slot_id=None, # No specific slot, as it's a refill event
event_type="refill",
action_details=f"{dewar.unique_id} refilled"
)
db.add(new_event)
db.commit()
return {"message": "Dewar refilled successfully"}