Files
plantillas-proyectos/backend/api/v1/modules/a76/pedmientos/routes/pedimento_payments.py
acazares 07dfe1edb1 Add service layers and models for Pedimento CRUD operations
- Implemented service classes for PedimentoConfigParameters, PedimentoConfigSurcharges, PedimentoConfigUpdateRectification, PedimentoConfigUpdates, PedimentoCustomsOffices, PedimentoDates, PedimentoDecrementables, PedimentoIncrementables, PedimentoIndexes, PedimentoPayments, PedimentoRectificationDestination, PedimentoRectificationOrigin, PedimentoTransportMeans, and PedimentoValidation.
- Each service class includes methods for CRUD operations: create, read, update, and delete.
- Added a main router for the API v1, integrating various modules including authentication, tenants, licenses, and pedimentos.
- Created models for PedimentoValidation with appropriate constraints and relationships.
2025-11-06 17:16:29 -06:00

112 lines
3.6 KiB
Python

"""
Routes for PedimentoPayments CRUD operations
"""
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from typing import List
from core.database import get_core_db
from core.security import get_current_user, get_tenant_from_token
from ..services.pedimento_payments import PedimentoPaymentsService
from ..dtos.pedimento_payments import (
PedimentoPaymentsCreate,
PedimentoPaymentsUpdate,
PedimentoPaymentsResponse
)
router = APIRouter(prefix="/{pedimento_id}/payments")
@router.get("/", response_model=List[PedimentoPaymentsResponse])
async def list_payments(
pedimento_id: int,
db: Session = Depends(get_core_db),
current_user: dict = Depends(get_current_user)
):
"""Get all payments for a pedimento"""
tenant_id = get_tenant_from_token(current_user)
if not tenant_id:
raise HTTPException(status_code=400, detail="Tenant ID not found in token")
payments = PedimentoPaymentsService.get_by_pedimento_id(db, pedimento_id, tenant_id)
return payments
@router.get("/{payment_id}", response_model=PedimentoPaymentsResponse)
async def get_payment(
pedimento_id: int,
payment_id: int,
db: Session = Depends(get_core_db),
current_user: dict = Depends(get_current_user)
):
"""Get a specific payment by ID"""
tenant_id = get_tenant_from_token(current_user)
if not tenant_id:
raise HTTPException(status_code=400, detail="Tenant ID not found in token")
payment = PedimentoPaymentsService.get_by_id(db, payment_id, pedimento_id, tenant_id)
if not payment:
raise HTTPException(status_code=404, detail="Payment not found")
return payment
@router.post("/", response_model=PedimentoPaymentsResponse, status_code=201)
async def create_payment(
pedimento_id: int,
data: PedimentoPaymentsCreate,
db: Session = Depends(get_core_db),
current_user: dict = Depends(get_current_user)
):
"""Create a new payment"""
tenant_id = get_tenant_from_token(current_user)
if not tenant_id:
raise HTTPException(status_code=400, detail="Tenant ID not found in token")
# Ensure pedimento_id matches
if data.pedimento_id != pedimento_id:
raise HTTPException(status_code=400, detail="Pedimento ID mismatch")
payment = PedimentoPaymentsService.create(db, data, tenant_id)
return payment
@router.put("/{payment_id}", response_model=PedimentoPaymentsResponse)
async def update_payment(
pedimento_id: int,
payment_id: int,
data: PedimentoPaymentsUpdate,
db: Session = Depends(get_core_db),
current_user: dict = Depends(get_current_user)
):
"""Update a payment"""
tenant_id = get_tenant_from_token(current_user)
if not tenant_id:
raise HTTPException(status_code=400, detail="Tenant ID not found in token")
payment = PedimentoPaymentsService.update(db, payment_id, pedimento_id, tenant_id, data)
if not payment:
raise HTTPException(status_code=404, detail="Payment not found")
return payment
@router.delete("/{payment_id}", status_code=204)
async def delete_payment(
pedimento_id: int,
payment_id: int,
db: Session = Depends(get_core_db),
current_user: dict = Depends(get_current_user)
):
"""Delete a payment"""
tenant_id = get_tenant_from_token(current_user)
if not tenant_id:
raise HTTPException(status_code=400, detail="Tenant ID not found in token")
success = PedimentoPaymentsService.delete(db, payment_id, pedimento_id, tenant_id)
if not success:
raise HTTPException(status_code=404, detail="Payment not found")
return None