changed shipment_id to integers
This commit is contained in:
parent
744a365bfc
commit
d5c7e7e6f3
@ -85,17 +85,17 @@ specific_dewars3 = [dewar for dewar in dewars if dewar.id in specific_dewar_ids3
|
|||||||
|
|
||||||
shipments = [
|
shipments = [
|
||||||
Shipment(
|
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,
|
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
|
proposal_id=3, return_address_id=1, comments='Handle with care', dewars=specific_dewars1
|
||||||
),
|
),
|
||||||
Shipment(
|
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,
|
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
|
proposal_id=4, return_address_id=2, comments='Contains the one ring', dewars=specific_dewars2
|
||||||
),
|
),
|
||||||
Shipment(
|
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,
|
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
|
proposal_id=5, return_address_id=1, comments='Contains the one ring', dewars=specific_dewars3
|
||||||
),
|
),
|
||||||
|
@ -7,7 +7,7 @@ from app.calculations import calculate_number_of_pucks, calculate_number_of_samp
|
|||||||
class Shipment(Base):
|
class Shipment(Base):
|
||||||
__tablename__ = "shipments"
|
__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_name = Column(String, index=True)
|
||||||
shipment_date = Column(Date)
|
shipment_date = Column(Date)
|
||||||
shipment_status = Column(String)
|
shipment_status = Column(String)
|
||||||
|
@ -44,9 +44,7 @@ async def create_shipment(shipment: ShipmentCreate, db: Session = Depends(get_db
|
|||||||
if not (contact_person or return_address or proposal):
|
if not (contact_person or return_address or proposal):
|
||||||
raise HTTPException(status_code=404, detail="Associated entity not found")
|
raise HTTPException(status_code=404, detail="Associated entity not found")
|
||||||
|
|
||||||
shipment_id = f'SHIP-{uuid.uuid4().hex[:8].upper()}'
|
|
||||||
db_shipment = ShipmentModel(
|
db_shipment = ShipmentModel(
|
||||||
shipment_id=shipment_id,
|
|
||||||
shipment_name=shipment.shipment_name,
|
shipment_name=shipment.shipment_name,
|
||||||
shipment_date=shipment.shipment_date,
|
shipment_date=shipment.shipment_date,
|
||||||
shipment_status=shipment.shipment_status,
|
shipment_status=shipment.shipment_status,
|
||||||
|
@ -138,7 +138,7 @@ class Proposal(BaseModel):
|
|||||||
|
|
||||||
# Shipment schemas
|
# Shipment schemas
|
||||||
class Shipment(BaseModel):
|
class Shipment(BaseModel):
|
||||||
shipment_id: str
|
shipment_id: int
|
||||||
shipment_name: str
|
shipment_name: str
|
||||||
shipment_date: date
|
shipment_date: date
|
||||||
shipment_status: str
|
shipment_status: str
|
||||||
|
73
backend/app/services/shipment_processor.py
Normal file
73
backend/app/services/shipment_processor.py
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user