Refactor and enhance CRUD operations for Seal, Trailer, Transporter, Vehicle, and Customs Broker modules
- Updated SealService to support tenant and company filtering with pagination and enhanced CRUD methods. - Refactored TrailerService to include tenant and company support, added filtering capabilities, and improved CRUD methods. - Introduced TenantCRUDRoutes for Trailer and Transporter routes to streamline API endpoint creation and management. - Enhanced TransporterService with tenant and company filtering, pagination, and improved CRUD operations. - Added Customs Broker module with DTOs, models, services, and routes for managing customs broker data. - Implemented CRUD operations for Customs Broker, including personnel and VU management. - Improved data validation and descriptions in DTOs for better API documentation.
This commit is contained in:
@@ -1,39 +1,110 @@
|
||||
"""
|
||||
Service layer for Packages (GBultos).
|
||||
"""
|
||||
|
||||
from typing import Optional, Tuple, List, Dict, Any
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from . import dto, models
|
||||
|
||||
|
||||
class GBultoService:
|
||||
"""
|
||||
Service layer for GBultos.
|
||||
"""
|
||||
class PackageService:
|
||||
"""Service for Package CRUD operations with tenant support"""
|
||||
|
||||
@staticmethod
|
||||
def get_bulto_by_code(db: Session, code: str):
|
||||
return db.query(models.Package).filter(models.Package.CODE == code).first()
|
||||
def get_all(
|
||||
db: Session,
|
||||
tenant_id: int,
|
||||
company_id: int,
|
||||
skip: int = 0,
|
||||
limit: int = 50,
|
||||
filters: Optional[Dict[str, Any]] = None,
|
||||
) -> Tuple[List[models.Package], int]:
|
||||
"""Get all packages for a tenant/company with pagination"""
|
||||
query = db.query(models.Package).filter(
|
||||
models.Package.tenant_id == tenant_id,
|
||||
models.Package.company_id == company_id,
|
||||
)
|
||||
|
||||
# Apply filters if provided
|
||||
if filters:
|
||||
if filters.get("key"):
|
||||
query = query.filter(
|
||||
models.Package.key.ilike(f"%{filters['key']}%")
|
||||
)
|
||||
if filters.get("description_es"):
|
||||
query = query.filter(
|
||||
models.Package.description_es.ilike(f"%{filters['description_es']}%")
|
||||
)
|
||||
|
||||
total = query.count()
|
||||
packages = query.offset(skip).limit(limit).all()
|
||||
|
||||
return packages, total
|
||||
|
||||
@staticmethod
|
||||
def create_gbulto(db: Session, gbulto_data: dto.GBultoCreateDTO):
|
||||
new_gbulto = models.Package(**gbulto_data.dict())
|
||||
db.add(new_gbulto)
|
||||
def get_by_id(
|
||||
db: Session, package_id: int, tenant_id: int, company_id: int
|
||||
) -> Optional[models.Package]:
|
||||
"""Get package by ID"""
|
||||
return (
|
||||
db.query(models.Package)
|
||||
.filter(
|
||||
models.Package.id == package_id,
|
||||
models.Package.tenant_id == tenant_id,
|
||||
models.Package.company_id == company_id,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def create(
|
||||
db: Session,
|
||||
package_data: dto.PackageCreateDTO,
|
||||
tenant_id: int,
|
||||
company_id: int,
|
||||
) -> models.Package:
|
||||
"""Create a new package"""
|
||||
new_package = models.Package(
|
||||
**package_data.model_dump(), tenant_id=tenant_id, company_id=company_id
|
||||
)
|
||||
db.add(new_package)
|
||||
db.commit()
|
||||
db.refresh(new_gbulto)
|
||||
return new_gbulto
|
||||
db.refresh(new_package)
|
||||
return new_package
|
||||
|
||||
@staticmethod
|
||||
def update_bulto(db: Session, code: str, bulto_data: dto.GBultoUpdateDTO):
|
||||
bulto = GBultoService.get_bulto_by_code(db, code)
|
||||
if bulto:
|
||||
for key, value in bulto_data.dict(exclude_unset=True).items():
|
||||
setattr(bulto, key, value)
|
||||
db.commit()
|
||||
db.refresh(bulto)
|
||||
return bulto
|
||||
def update(
|
||||
db: Session,
|
||||
package_id: int,
|
||||
tenant_id: int,
|
||||
company_id: int,
|
||||
package_data: dto.PackageUpdateDTO,
|
||||
) -> Optional[models.Package]:
|
||||
"""Update a package"""
|
||||
package = PackageService.get_by_id(db, package_id, tenant_id, company_id)
|
||||
if not package:
|
||||
return None
|
||||
|
||||
# Update fields (excluding key if it's meant to be immutable)
|
||||
update_data = package_data.model_dump(exclude_unset=True, exclude={"key"})
|
||||
for field, value in update_data.items():
|
||||
setattr(package, field, value)
|
||||
|
||||
db.commit()
|
||||
db.refresh(package)
|
||||
return package
|
||||
|
||||
@staticmethod
|
||||
def delete_bulto(db: Session, code: str):
|
||||
bulto = GBultoService.get_bulto_by_code(db, code)
|
||||
if bulto:
|
||||
db.delete(bulto)
|
||||
db.commit()
|
||||
return bulto
|
||||
def delete(
|
||||
db: Session, package_id: int, tenant_id: int, company_id: int
|
||||
) -> bool:
|
||||
"""Delete a package"""
|
||||
package = PackageService.get_by_id(db, package_id, tenant_id, company_id)
|
||||
if not package:
|
||||
return False
|
||||
|
||||
db.delete(package)
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user