From d5c7e7e6f3429a155923f2d4c930812337f613b4 Mon Sep 17 00:00:00 2001 From: GotthardG <51994228+GotthardG@users.noreply.github.com> Date: Sat, 9 Nov 2024 08:11:03 +0100 Subject: [PATCH] changed shipment_id to integers --- backend/app/data/data.py | 6 +- backend/app/models.py | 2 +- backend/app/routers/shipment.py | 2 - backend/app/schemas.py | 2 +- backend/app/services/shipment_processor.py | 73 ++++++++++++++++++++++ 5 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 backend/app/services/shipment_processor.py diff --git a/backend/app/data/data.py b/backend/app/data/data.py index de3942c..0710694 100644 --- a/backend/app/data/data.py +++ b/backend/app/data/data.py @@ -85,17 +85,17 @@ specific_dewars3 = [dewar for dewar in dewars if dewar.id in specific_dewar_ids3 shipments = [ Shipment( - shipment_id="SHIPMENT001", shipment_date=datetime.strptime('2024-10-10', '%Y-%m-%d'), + shipment_id=1, shipment_date=datetime.strptime('2024-10-10', '%Y-%m-%d'), shipment_name='Shipment from Mordor', shipment_status='Delivered', contact_person_id=2, proposal_id=3, return_address_id=1, comments='Handle with care', dewars=specific_dewars1 ), Shipment( - shipment_id="SHIPMENT002", shipment_date=datetime.strptime('2024-10-24', '%Y-%m-%d'), + shipment_id=2, shipment_date=datetime.strptime('2024-10-24', '%Y-%m-%d'), shipment_name='Shipment from Mordor', shipment_status='In Transit', contact_person_id=4, proposal_id=4, return_address_id=2, comments='Contains the one ring', dewars=specific_dewars2 ), Shipment( - shipment_id="SHIPMENT003", shipment_date=datetime.strptime('2024-10-28', '%Y-%m-%d'), + shipment_id=3, shipment_date=datetime.strptime('2024-10-28', '%Y-%m-%d'), shipment_name='Shipment from Mordor', shipment_status='In Transit', contact_person_id=5, proposal_id=5, return_address_id=1, comments='Contains the one ring', dewars=specific_dewars3 ), diff --git a/backend/app/models.py b/backend/app/models.py index 114cc13..5fe540d 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -7,7 +7,7 @@ from app.calculations import calculate_number_of_pucks, calculate_number_of_samp class Shipment(Base): __tablename__ = "shipments" - shipment_id = Column(String, primary_key=True, index=True) + shipment_id = Column(Integer, primary_key=True, index=True, autoincrement=True) shipment_name = Column(String, index=True) shipment_date = Column(Date) shipment_status = Column(String) diff --git a/backend/app/routers/shipment.py b/backend/app/routers/shipment.py index de9d7ad..33fb40a 100644 --- a/backend/app/routers/shipment.py +++ b/backend/app/routers/shipment.py @@ -44,9 +44,7 @@ async def create_shipment(shipment: ShipmentCreate, db: Session = Depends(get_db if not (contact_person or return_address or proposal): raise HTTPException(status_code=404, detail="Associated entity not found") - shipment_id = f'SHIP-{uuid.uuid4().hex[:8].upper()}' db_shipment = ShipmentModel( - shipment_id=shipment_id, shipment_name=shipment.shipment_name, shipment_date=shipment.shipment_date, shipment_status=shipment.shipment_status, diff --git a/backend/app/schemas.py b/backend/app/schemas.py index 9d0170d..1d7fe81 100644 --- a/backend/app/schemas.py +++ b/backend/app/schemas.py @@ -138,7 +138,7 @@ class Proposal(BaseModel): # Shipment schemas class Shipment(BaseModel): - shipment_id: str + shipment_id: int shipment_name: str shipment_date: date shipment_status: str diff --git a/backend/app/services/shipment_processor.py b/backend/app/services/shipment_processor.py new file mode 100644 index 0000000..6b05402 --- /dev/null +++ b/backend/app/services/shipment_processor.py @@ -0,0 +1,73 @@ +# app/services/shipment_processor.py + +from sqlalchemy.orm import Session +from app.models import Shipment, Dewar, Puck, Sample +from app.schemas import ShipmentCreate, ShipmentResponse +import logging + +logger = logging.getLogger(__name__) + + +class ShipmentProcessor: + def __init__(self, db: Session): + self.db = db + + def process_shipment(self, shipment: ShipmentCreate) -> ShipmentResponse: + try: + # Creating new Shipment + new_shipment = Shipment( + shipment_name=shipment.shipment_name, + shipment_date=shipment.shipment_date, + shipment_status=shipment.shipment_status, # Adjusted to match model + # other shipment-related fields if any + ) + self.db.add(new_shipment) + self.db.commit() + self.db.refresh(new_shipment) + + # Link Dewars + for dewar_data in shipment.dewars: + dewar = Dewar( + shipment_id=new_shipment.id, + dewar_name=dewar_data.dewar_name, + tracking_number=dewar_data.tracking_number, + status=dewar_data.status, + contact_person_id=dewar_data.contact_person_id, + return_address_id=dewar_data.return_address_id, + ) + self.db.add(dewar) + self.db.commit() + self.db.refresh(dewar) + + # Link Pucks + for puck_data in dewar_data.pucks: + puck = Puck( + dewar_id=dewar.id, + puck_name=puck_data.puck_name, + puck_type=puck_data.puck_type, + puck_location_in_dewar=puck_data.puck_location_in_dewar, + ) + self.db.add(puck) + self.db.commit() + self.db.refresh(puck) + + # Link Samples + for sample_data in puck_data.samples: + sample = Sample( + puck_id=puck.id, + sample_name=sample_data.sample_name, + position=sample_data.position, + ) + self.db.add(sample) + self.db.commit() + self.db.refresh(sample) + + # Return a response wrapped in the ShipmentResponse schema + return ShipmentResponse( + shipment_id=new_shipment.id, + status="success", + message="Shipment processed successfully" + ) + except Exception as e: + logger.error(f"Error processing shipment: {str(e)}") + raise e