fix(customs-brokers): update VU and personnel for multi-tenancy support

This commit is contained in:
Galindo97
2026-02-24 17:41:40 -06:00
parent a52602dedf
commit 980c913d46
6 changed files with 124 additions and 65 deletions

View File

@@ -43,7 +43,7 @@ class CustomsBrokerResponseDTO(CustomsBrokerBaseDTO):
broker_key: str
tenant_id: int
company_id: int
vu: Optional["CustomsBrokerVUCreateDTO"] = None
vu: Optional["CustomsBrokerVUResponseDTO"] = None
class Config:
from_attributes = True
@@ -95,6 +95,11 @@ class CustomsBrokerVUCreateDTO(BaseModel):
doda_web_service_access_key: Optional[str] = None
doda_fiel_access_key: Optional[str] = None
doda_xml_files_path: Optional[str] = None
tenant_id: Optional[int] = None
company_id: Optional[int] = None
class CustomsBrokerVUResponseDTO(CustomsBrokerVUCreateDTO):
customs_broker_id: int
class Config:
from_attributes = True
@@ -112,6 +117,8 @@ class CustomsBrokerPersonnelDTO(BaseModel):
last_name: Optional[str] = None
middle_name: Optional[str] = None
email: Optional[str] = None
tenant_id: Optional[int] = None
company_id: Optional[int] = None
class Config:
from_attributes = True

View File

@@ -62,7 +62,7 @@ def update_customs_broker(
@router.put(
"/customs-broker-vu/{broker_key}",
response_model=dto.CustomsBrokerVUCreateDTO,
response_model=dto.CustomsBrokerVUResponseDTO,
)
def update_customs_broker_vu(
broker_key: str,
@@ -77,7 +77,7 @@ def update_customs_broker_vu(
if not broker:
raise HTTPException(status_code=404, detail="Customs Broker not found")
updated_vu = services.CustomsBrokerVUService.update_vu(db, broker_key, vu_data)
updated_vu = services.CustomsBrokerVUService.update_vu(db, broker_key, vu_data, tenant_id, company_id)
if not updated_vu:
raise HTTPException(status_code=404, detail="Customs Broker VU not found")
return updated_vu
@@ -102,7 +102,7 @@ def update_customs_broker_personnel(
raise HTTPException(status_code=404, detail="Customs Broker not found")
updated_personnel = services.CustomsBrokerPersonnelService.update_personnel(
db, broker_key, line, personnel_data
db, broker_key, line, personnel_data, tenant_id, company_id
)
if not updated_personnel:
raise HTTPException(

View File

@@ -90,9 +90,17 @@ class CustomsBrokerVUService:
return new_vu
@staticmethod
def update_vu(db: Session, broker_key: str, vu_data: dto.CustomsBrokerVUCreateDTO):
def update_vu(db: Session, broker_key: str, vu_data: dto.CustomsBrokerVUCreateDTO, tenant_id: int, company_id: int):
# We need the custom broker ID to insert a new VU
broker = db.query(models.CustomsBroker).filter(models.CustomsBroker.broker_key == broker_key).first()
broker = (
db.query(models.CustomsBroker)
.filter(
models.CustomsBroker.broker_key == broker_key,
models.CustomsBroker.tenant_id == tenant_id,
models.CustomsBroker.company_id == company_id,
)
.first()
)
if not broker:
return None
@@ -108,6 +116,8 @@ class CustomsBrokerVUService:
else:
# Create new
new_vu_data = vu_data.model_dump()
new_vu_data["tenant_id"] = tenant_id
new_vu_data["company_id"] = company_id
new_vu = models.CustomsBrokerVU(customs_broker_id=broker.id, **new_vu_data)
db.add(new_vu)
db.commit()
@@ -125,24 +135,36 @@ class CustomsBrokerVUService:
class CustomsBrokerPersonnelService:
@staticmethod
def get_by_broker_key_and_line(db: Session, broker_key: str, line: int):
def get_by_broker_key_and_line(db: Session, broker_key: str, line: int, tenant_id: int, company_id: int):
return (
db.query(models.CustomsBrokerPersonnel)
.join(models.CustomsBroker)
.filter(
models.CustomsBroker.broker_key == broker_key,
models.CustomsBrokerPersonnel.line == line,
models.CustomsBroker.tenant_id == tenant_id,
models.CustomsBroker.company_id == company_id,
)
.first()
)
@staticmethod
def create_personnel(db: Session, broker_key: str, personnel_data: dto.CustomsBrokerPersonnelDTO):
broker = db.query(models.CustomsBroker).filter(models.CustomsBroker.broker_key == broker_key).first()
def create_personnel(db: Session, broker_key: str, personnel_data: dto.CustomsBrokerPersonnelDTO, tenant_id: int, company_id: int):
broker = (
db.query(models.CustomsBroker)
.filter(
models.CustomsBroker.broker_key == broker_key,
models.CustomsBroker.tenant_id == tenant_id,
models.CustomsBroker.company_id == company_id,
)
.first()
)
if not broker:
return None
new_personnel_data = personnel_data.model_dump()
new_personnel_data["tenant_id"] = tenant_id
new_personnel_data["company_id"] = company_id
new_personnel = models.CustomsBrokerPersonnel(customs_broker_id=broker.id, **new_personnel_data)
db.add(new_personnel)
db.commit()
@@ -155,16 +177,22 @@ class CustomsBrokerPersonnelService:
broker_key: str,
line: int,
personnel_data: dto.CustomsBrokerPersonnelDTO,
tenant_id: int,
company_id: int,
):
personnel = CustomsBrokerPersonnelService.get_by_broker_key_and_line(
db, broker_key, line
db, broker_key, line, tenant_id, company_id
)
if personnel:
for key, value in personnel_data.model_dump(exclude_unset=True).items():
setattr(personnel, key, value)
db.commit()
db.refresh(personnel)
return personnel
return personnel
else:
return CustomsBrokerPersonnelService.create_personnel(
db, broker_key, personnel_data, tenant_id, company_id
)
@staticmethod
def delete_personnel(db: Session, broker_key: str, line: int):