feature/T2026-05-016 implementar cargas de tareas en background e implementar y corregir auditoria para datastages
This commit is contained in:
@@ -1,6 +1,14 @@
|
||||
from celery import shared_task, group
|
||||
from api.customs.models import ProcesamientoPedimento, Pedimento, Cove, EDocument
|
||||
from core.utils import xml_controller
|
||||
from api.customs.tasks.microservice import (
|
||||
procesar_cove_individual,
|
||||
procesar_acuse_individual,
|
||||
procesar_acuse_cove_individual,
|
||||
procesar_edoc_individual,
|
||||
procesar_partida_individual,
|
||||
procesar_remesa_individual,
|
||||
)
|
||||
|
||||
@shared_task
|
||||
def crear_procesamiento_remesa(pedimento_id):
|
||||
@@ -11,7 +19,7 @@ def crear_procesamiento_remesa(pedimento_id):
|
||||
if pedimento.remesas:
|
||||
existe = ProcesamientoPedimento.objects.filter(
|
||||
pedimento=pedimento,
|
||||
servicio_id=5, # ID del servicio de remesas
|
||||
servicio_id=5,
|
||||
organizacion=pedimento.organizacion,
|
||||
estado_id__in=[1, 2, 3, 4]
|
||||
).exists()
|
||||
@@ -19,10 +27,11 @@ def crear_procesamiento_remesa(pedimento_id):
|
||||
logger.info(f"[TAREA] ProcesamientoPedimento remesa creado para pedimento {pedimento_id}")
|
||||
ProcesamientoPedimento.objects.create(
|
||||
pedimento=pedimento,
|
||||
estado_id=1, # Estado "pendiente"
|
||||
estado_id=1,
|
||||
servicio_id=5,
|
||||
organizacion=pedimento.organizacion
|
||||
)
|
||||
procesar_remesa_individual.apply_async(args=[str(pedimento.id), str(pedimento.organizacion.id)])
|
||||
|
||||
@shared_task
|
||||
def crear_procesamiento_partida(pedimento_id):
|
||||
@@ -32,7 +41,7 @@ def crear_procesamiento_partida(pedimento_id):
|
||||
logger.info(f"[TAREA] crear_procesamiento_partida para pedimento {pedimento_id}")
|
||||
existe = ProcesamientoPedimento.objects.filter(
|
||||
pedimento=pedimento,
|
||||
servicio_id=4, # ID del servicio de partidas
|
||||
servicio_id=4,
|
||||
organizacion=pedimento.organizacion,
|
||||
estado_id__in=[1, 2, 3, 4]
|
||||
).exists()
|
||||
@@ -40,10 +49,11 @@ def crear_procesamiento_partida(pedimento_id):
|
||||
logger.info(f"[TAREA] ProcesamientoPedimento partida creado para pedimento {pedimento_id}")
|
||||
ProcesamientoPedimento.objects.create(
|
||||
pedimento=pedimento,
|
||||
estado_id=1, # Estado "pendiente"
|
||||
estado_id=1,
|
||||
servicio_id=4,
|
||||
organizacion=pedimento.organizacion
|
||||
)
|
||||
procesar_partida_individual.apply_async(args=[str(pedimento.id), str(pedimento.organizacion.id)])
|
||||
|
||||
@shared_task
|
||||
def crear_procesamiento_cove(pedimento_id):
|
||||
@@ -54,7 +64,7 @@ def crear_procesamiento_cove(pedimento_id):
|
||||
if pedimento.coves.exists():
|
||||
existe = ProcesamientoPedimento.objects.filter(
|
||||
pedimento=pedimento,
|
||||
servicio_id=8, # ID del servicio de Coves
|
||||
servicio_id=8,
|
||||
organizacion=pedimento.organizacion,
|
||||
estado_id__in=[1, 2, 3, 4]
|
||||
).exists()
|
||||
@@ -62,10 +72,11 @@ def crear_procesamiento_cove(pedimento_id):
|
||||
logger.info(f"[TAREA] ProcesamientoPedimento cove creado para pedimento {pedimento_id}")
|
||||
ProcesamientoPedimento.objects.create(
|
||||
pedimento=pedimento,
|
||||
estado_id=1, # Estado "pendiente"
|
||||
estado_id=1,
|
||||
servicio_id=8,
|
||||
organizacion=pedimento.organizacion
|
||||
)
|
||||
procesar_cove_individual.apply_async(args=[str(pedimento.id), str(pedimento.organizacion.id)])
|
||||
|
||||
@shared_task
|
||||
def crear_procesamiento_acuse(pedimento_id):
|
||||
@@ -73,10 +84,10 @@ def crear_procesamiento_acuse(pedimento_id):
|
||||
logger = logging.getLogger('api.customs.async_operations')
|
||||
pedimento = Pedimento.objects.get(id=pedimento_id)
|
||||
logger.info(f"[TAREA] crear_procesamiento_acuse para pedimento {pedimento_id}")
|
||||
if pedimento.coves.exists():
|
||||
if pedimento.documentos.exists():
|
||||
existe = ProcesamientoPedimento.objects.filter(
|
||||
pedimento=pedimento,
|
||||
servicio_id=6, # ID del servicio de Acuse Cove
|
||||
servicio_id=6,
|
||||
organizacion=pedimento.organizacion,
|
||||
estado_id__in=[1, 2, 3, 4]
|
||||
).exists()
|
||||
@@ -84,10 +95,11 @@ def crear_procesamiento_acuse(pedimento_id):
|
||||
logger.info(f"[TAREA] ProcesamientoPedimento acuse creado para pedimento {pedimento_id}")
|
||||
ProcesamientoPedimento.objects.create(
|
||||
pedimento=pedimento,
|
||||
estado_id=1, # Estado "pendiente"
|
||||
estado_id=1,
|
||||
servicio_id=6,
|
||||
organizacion=pedimento.organizacion
|
||||
)
|
||||
procesar_acuse_individual.apply_async(args=[str(pedimento.id), str(pedimento.organizacion.id)])
|
||||
|
||||
@shared_task
|
||||
def crear_procesamiento_acuse_cove(pedimento_id):
|
||||
@@ -98,7 +110,7 @@ def crear_procesamiento_acuse_cove(pedimento_id):
|
||||
if pedimento.coves.exists():
|
||||
existe = ProcesamientoPedimento.objects.filter(
|
||||
pedimento=pedimento,
|
||||
servicio_id=9, # ID del servicio de Acuse Cove
|
||||
servicio_id=9,
|
||||
organizacion=pedimento.organizacion,
|
||||
estado_id__in=[1, 2, 3, 4]
|
||||
).exists()
|
||||
@@ -106,10 +118,11 @@ def crear_procesamiento_acuse_cove(pedimento_id):
|
||||
logger.info(f"[TAREA] ProcesamientoPedimento acuse_cove creado para pedimento {pedimento_id}")
|
||||
ProcesamientoPedimento.objects.create(
|
||||
pedimento=pedimento,
|
||||
estado_id=1, # Estado "pendiente"
|
||||
estado_id=1,
|
||||
servicio_id=9,
|
||||
organizacion=pedimento.organizacion
|
||||
)
|
||||
procesar_acuse_cove_individual.apply_async(args=[str(pedimento.id), str(pedimento.organizacion.id)])
|
||||
|
||||
@shared_task
|
||||
def crear_procesamiento_edocument(pedimento_id):
|
||||
@@ -120,7 +133,7 @@ def crear_procesamiento_edocument(pedimento_id):
|
||||
if pedimento.documentos.exists():
|
||||
existe = ProcesamientoPedimento.objects.filter(
|
||||
pedimento=pedimento,
|
||||
servicio_id=7, # ID del servicio de EDocument
|
||||
servicio_id=7,
|
||||
organizacion=pedimento.organizacion,
|
||||
estado_id__in=[1, 2, 3, 4]
|
||||
).exists()
|
||||
@@ -128,10 +141,11 @@ def crear_procesamiento_edocument(pedimento_id):
|
||||
logger.info(f"[TAREA] ProcesamientoPedimento edocument creado para pedimento {pedimento_id}")
|
||||
ProcesamientoPedimento.objects.create(
|
||||
pedimento=pedimento,
|
||||
estado_id=1, # Estado "pendiente"
|
||||
estado_id=1,
|
||||
servicio_id=7,
|
||||
organizacion=pedimento.organizacion
|
||||
)
|
||||
procesar_edoc_individual.apply_async(args=[str(pedimento.id), str(pedimento.organizacion.id)])
|
||||
|
||||
@shared_task
|
||||
def crear_procesamiento_pedimento_completo(organizacion_id):
|
||||
|
||||
@@ -3,7 +3,12 @@ from django.urls import reverse
|
||||
from rest_framework.test import APITestCase, APIClient
|
||||
from rest_framework import status
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from unittest.mock import patch
|
||||
from io import BytesIO
|
||||
import zipfile
|
||||
from api.organization.models import Organizacion
|
||||
from api.licence.models import Licencia
|
||||
from .models import Pedimento, TipoOperacion, ProcesamientoPedimento, EDocument
|
||||
|
||||
User = get_user_model()
|
||||
@@ -75,3 +80,147 @@ class CustomsViewsTests(APITestCase):
|
||||
self.client.force_authenticate(user=self.admin)
|
||||
response = self.client.get(url)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Tests de integración para bulk-create (ViewSetPedimento.bulk_create)
|
||||
# Verifica que al re-cargar un pedimento existente sus documentos se actualicen
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class BulkCreateDocumentReplaceTests(APITestCase):
|
||||
"""Verifica que bulk-create actualiza los documentos de pedimentos existentes
|
||||
en vez de ignorarlos, y que no quedan archivos residuales en el storage."""
|
||||
|
||||
PEDIMENTO_APP = "24-01-3420-1234567"
|
||||
|
||||
def setUp(self):
|
||||
self.licencia = Licencia.objects.create(nombre="Lic100GB", almacenamiento=100)
|
||||
self.org = Organizacion.objects.create(
|
||||
nombre="OrgBulkCreate",
|
||||
licencia=self.licencia,
|
||||
is_active=True,
|
||||
is_verified=True,
|
||||
)
|
||||
self.user = User.objects.create_user(
|
||||
username="bulkcreateuser", password="pass", organizacion=self.org
|
||||
)
|
||||
self.pedimento = Pedimento.objects.create(
|
||||
organizacion=self.org,
|
||||
pedimento="1234567",
|
||||
pedimento_app=self.PEDIMENTO_APP,
|
||||
)
|
||||
from api.record.models import DocumentType, Fuente
|
||||
self.doc_type = DocumentType.objects.get_or_create(nombre="Pedimento")[0]
|
||||
# bulk_create usa fuente_id=4 hardcodeado; debe existir en la DB de test
|
||||
Fuente.objects.get_or_create(id=4, defaults={"nombre": "Bulk Create"})
|
||||
self.url = reverse("Pedimento-bulk-create")
|
||||
self.client.force_authenticate(user=self.user)
|
||||
|
||||
def _make_zip(self, files_dict):
|
||||
"""Crea un ZIP en memoria. files_dict = {nombre_archivo: contenido_bytes}"""
|
||||
buf = BytesIO()
|
||||
with zipfile.ZipFile(buf, "w") as zf:
|
||||
for name, content in files_dict.items():
|
||||
zf.writestr(name, content)
|
||||
buf.seek(0)
|
||||
return SimpleUploadedFile(
|
||||
f"{self.PEDIMENTO_APP}.zip", buf.read(), content_type="application/zip"
|
||||
)
|
||||
|
||||
def _post_zip(self, files_dict):
|
||||
return self.client.post(
|
||||
self.url,
|
||||
{"contribuyente": "XAXX010101000", "archivos": [self._make_zip(files_dict)]},
|
||||
format="multipart",
|
||||
)
|
||||
|
||||
@patch("api.customs.views.storage_service")
|
||||
def test_existing_pedimento_not_duplicated(self, mock_st):
|
||||
"""Re-subir un pedimento existente NO debe crear un segundo Pedimento."""
|
||||
mock_st.save_document_from_path.return_value = "org_1/documents/ped/informe_a1b2c3d4.pdf"
|
||||
|
||||
self._post_zip({"informe.pdf": b"contenido"})
|
||||
|
||||
self.assertEqual(
|
||||
Pedimento.objects.filter(
|
||||
organizacion=self.org, pedimento_app=self.PEDIMENTO_APP
|
||||
).count(),
|
||||
1,
|
||||
)
|
||||
|
||||
@patch("api.customs.views.storage_service")
|
||||
def test_existing_pedimento_document_replaced_not_duplicated(self, mock_st):
|
||||
"""Documento existente con el mismo nombre base se reemplaza, no se duplica."""
|
||||
from api.record.models import Document
|
||||
|
||||
old_path = f"org_1/documents/{self.PEDIMENTO_APP}/informe_a1b2c3d4.pdf"
|
||||
old_doc = Document.objects.create(
|
||||
organizacion=self.org,
|
||||
pedimento=self.pedimento,
|
||||
document_type=self.doc_type,
|
||||
archivo=old_path,
|
||||
size=500,
|
||||
extension="pdf",
|
||||
)
|
||||
new_path = f"org_1/documents/{self.PEDIMENTO_APP}/informe_b5c6d7e8.pdf"
|
||||
mock_st.save_document_from_path.return_value = new_path
|
||||
mock_st.delete_file.return_value = True
|
||||
|
||||
self._post_zip({"informe.pdf": b"contenido actualizado"})
|
||||
|
||||
docs = Document.objects.filter(pedimento=self.pedimento)
|
||||
# Sin duplicados
|
||||
self.assertEqual(docs.count(), 1)
|
||||
# Mismo registro
|
||||
self.assertEqual(docs.first().id, old_doc.id)
|
||||
# Archivo actualizado
|
||||
old_doc.refresh_from_db()
|
||||
self.assertEqual(old_doc.archivo.name, new_path)
|
||||
|
||||
@patch("api.customs.views.storage_service")
|
||||
def test_existing_pedimento_stale_file_deleted_from_storage(self, mock_st):
|
||||
"""Al reemplazar un documento, el archivo viejo debe eliminarse del storage."""
|
||||
from api.record.models import Document
|
||||
|
||||
old_path = f"org_1/documents/{self.PEDIMENTO_APP}/informe_a1b2c3d4.pdf"
|
||||
Document.objects.create(
|
||||
organizacion=self.org,
|
||||
pedimento=self.pedimento,
|
||||
document_type=self.doc_type,
|
||||
archivo=old_path,
|
||||
size=500,
|
||||
extension="pdf",
|
||||
)
|
||||
mock_st.save_document_from_path.return_value = f"org_1/documents/{self.PEDIMENTO_APP}/informe_b5c6d7e8.pdf"
|
||||
mock_st.delete_file.return_value = True
|
||||
|
||||
self._post_zip({"informe.pdf": b"contenido"})
|
||||
|
||||
# delete_file debe haberse llamado con la ruta del archivo viejo
|
||||
mock_st.delete_file.assert_called()
|
||||
called_arg = str(mock_st.delete_file.call_args[0][0])
|
||||
self.assertIn("informe_a1b2c3d4", called_arg)
|
||||
|
||||
@patch("api.customs.views.storage_service")
|
||||
def test_existing_pedimento_new_file_added(self, mock_st):
|
||||
"""Archivo nuevo en el ZIP se añade al pedimento existente."""
|
||||
from api.record.models import Document
|
||||
|
||||
mock_st.save_document_from_path.return_value = "org_1/documents/ped/nuevo_b5c6d7e8.pdf"
|
||||
|
||||
self._post_zip({"nuevo_documento.pdf": b"contenido nuevo"})
|
||||
|
||||
self.assertGreaterEqual(
|
||||
Document.objects.filter(pedimento=self.pedimento).count(), 1
|
||||
)
|
||||
|
||||
@patch("api.customs.views.storage_service")
|
||||
def test_already_existing_count_in_response(self, mock_st):
|
||||
"""La respuesta debe indicar que el pedimento ya existía (already_existing_count >= 1)."""
|
||||
mock_st.save_document_from_path.return_value = "org_1/documents/ped/f_a1b2c3d4.pdf"
|
||||
|
||||
response = self._post_zip({"archivo.pdf": b"contenido"})
|
||||
|
||||
self.assertIn(response.status_code, [status.HTTP_200_OK, status.HTTP_207_MULTI_STATUS, status.HTTP_201_CREATED])
|
||||
data = response.json()
|
||||
self.assertGreaterEqual(data.get("already_existing_count", 0), 1)
|
||||
|
||||
@@ -39,6 +39,7 @@ from .views_auditor import (
|
||||
auditar_acuse_cove_endpoint,
|
||||
auditar_edocuments_endpoint,
|
||||
auditar_acuse_endpoint,
|
||||
auditar_remesas_endpoint,
|
||||
auditar_cove_pedimento_endpoint,
|
||||
auditar_acuse_cove_pedimento_endpoint,
|
||||
auditar_edocument_pedimento_endpoint,
|
||||
@@ -72,6 +73,7 @@ urlpatterns = [
|
||||
path('auditor/auditar-acuse-cove/', auditar_acuse_cove_endpoint, name='auditar-acuse-cove'),
|
||||
path('auditor/auditar-edocuments/', auditar_edocuments_endpoint, name='auditar-edocuments'),
|
||||
path('auditor/auditar-acuse/', auditar_acuse_endpoint, name='auditar-acuse'),
|
||||
path('auditor/auditar-remesas/', auditar_remesas_endpoint, name='auditar-remesas'),
|
||||
path('auditor/auditar-cove/pedimento/', auditar_cove_pedimento_endpoint, name='auditar-cove-pedimento'),
|
||||
path('auditor/auditar-acuse-cove/pedimento/', auditar_acuse_cove_pedimento_endpoint, name='auditar-acuse-cove-pedimento'),
|
||||
path('auditor/auditar-edocument/pedimento/', auditar_edocument_pedimento_endpoint, name='auditar-edocument-pedimento'),
|
||||
|
||||
Reference in New Issue
Block a user