From 35a6f88211912a56720da5c1754577f7f26be6d1 Mon Sep 17 00:00:00 2001 From: AlexeerCT Date: Fri, 27 Feb 2026 11:07:16 -0600 Subject: [PATCH] Refactor US Tariff Fractions service to use async for get_all method and add USTariffFractionMapper for response mapping --- .../fractions/us_tariff_fractions/routes.py | 3 +- .../fractions/us_tariff_fractions/service.py | 37 ++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/backend/api/v1/modules/a76/general_catalogs/fractions/us_tariff_fractions/routes.py b/backend/api/v1/modules/a76/general_catalogs/fractions/us_tariff_fractions/routes.py index f6521624..158bf765 100644 --- a/backend/api/v1/modules/a76/general_catalogs/fractions/us_tariff_fractions/routes.py +++ b/backend/api/v1/modules/a76/general_catalogs/fractions/us_tariff_fractions/routes.py @@ -54,8 +54,7 @@ async def list_us_tariff_fractions( if search: filters["search"] = search - # Updated to sync call - items, total = USTariffFractionService.get_all( + items, total = await USTariffFractionService.get_all( db, tenant_id, company_id, skip, page_size, filters ) diff --git a/backend/api/v1/modules/a76/general_catalogs/fractions/us_tariff_fractions/service.py b/backend/api/v1/modules/a76/general_catalogs/fractions/us_tariff_fractions/service.py index 1409b2f9..124f6460 100644 --- a/backend/api/v1/modules/a76/general_catalogs/fractions/us_tariff_fractions/service.py +++ b/backend/api/v1/modules/a76/general_catalogs/fractions/us_tariff_fractions/service.py @@ -1,4 +1,5 @@ from typing import List, Optional, Tuple, Dict, Any +from datetime import datetime from sqlalchemy.orm import Session from sqlalchemy.exc import IntegrityError from fastapi import HTTPException @@ -7,15 +8,49 @@ from decimal import Decimal from .models import USTariffFraction from .dto import USTariffFractionCreateDTO, USTariffFractionUpdateDTO +from api.v1.modules.sitar.fracciones_usa.service import FraccionesUSAService logger = logging.getLogger(__name__) +class USTariffFractionMapper: + """Maps Sitar FraccionesUSAResponse objects to USTariffFraction domain objects""" + + @staticmethod + def to_domain( + item: Any, tenant_id: int, company_id: int + ) -> USTariffFraction: + """Convert a FraccionesUSAResponse to a USTariffFraction instance (not persisted)""" + code = item.FRACCION_CON_PUNTO or item.FRACCION_MOSTRAR or item.FRACCION_SIN_PUNTO or "" + ad_valorem: Optional[float] = None + if item.TARIFA1: + try: + ad_valorem = float(str(item.TARIFA1).replace("%", "").strip()) + except (ValueError, TypeError): + ad_valorem = None + + fraction = USTariffFraction() + fraction.id = item.CONSECUTIVO + fraction.tenant_id = tenant_id + fraction.company_id = company_id + fraction.code = code + fraction.prefix = item.FRACCION_SIN_PUNTO + fraction.type_code = str(item.NIVEL) if item.NIVEL is not None else None + fraction.ad_valorem = ad_valorem + fraction.fixed_cost = None + fraction.unit_of_measure = item.UNIDADCANTIDAD + fraction.description = item.DESCRIPCION + now = datetime.now() + fraction.created_at = now + fraction.updated_at = now + return fraction + + class USTariffFractionService: """Service para gestionar fracciones arancelarias americanas""" @staticmethod - def get_all( + async def get_all( db: Session, tenant_id: int, company_id: int,