From 46ac54f7abd2a553163413b0576956d1745296eb Mon Sep 17 00:00:00 2001 From: Alvar San Martin Date: Wed, 11 Feb 2026 12:19:16 +0100 Subject: [PATCH] Seguimiento de ordenes desde la ingesta --- deployment/database/01-ordenes-sim.sql | 40 +++++++++++++++++++ .../{01-objenious.sql => 02-objenious.sql} | 8 ++-- .../aplication/Sim.controller.ts | 7 +++- .../aplication/httpValidators.ts | 2 - .../tasks/check_objenious_request.ts | 1 + .../infrastructure/SimOrderRepoository.ts | 0 6 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 deployment/database/01-ordenes-sim.sql rename deployment/database/{01-objenious.sql => 02-objenious.sql} (82%) create mode 100644 packages/sim-shared/infrastructure/SimOrderRepoository.ts diff --git a/deployment/database/01-ordenes-sim.sql b/deployment/database/01-ordenes-sim.sql new file mode 100644 index 0000000..e5791a4 --- /dev/null +++ b/deployment/database/01-ordenes-sim.sql @@ -0,0 +1,40 @@ +-- Tablas para el seguimiento de las operaciones de SIM sin importar +-- la cmpañia. + +CREATE TYPE order_types AS ENUM ('activate','preactivate','cancel','pause','reactivate','unknown'); +CREATE TYPE order_status AS ENUM ( + 'pending', -- Mensaje creado/enviado a RabbitMQ + 'running', -- Consumidor ha cogido el mensaje (opcional) + 'finished', -- Procesado correctamente + 'failed', -- Falló, pero podría reintentarse (Pasar a delay?) + 'dlx' -- Falló definitivamente y está en Dead Letter Exchange +); + +CREATE TABLE IF NOT EXISTS order_tracking ( + order_id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, + correlation_id VARCHAR(255) NOT NULL, -- ID compartido con RabbitMQ (message_id) + exchange VARCHAR(100), -- Exchange al que se envia (de momento solo hay 1 principal sin contar delay y dlx) + routing_key VARCHAR(100), -- Routing key del mensaje + + payload JSONB, -- Duda si es optimo guardar la copia, es útil en caso de fallo + + -- Campos de reintentos? + + status order_status NOT NULL DEFAULT 'PENDING', + retry_count INT DEFAULT 0, + error_message TEXT, -- Razón del fallo + -- error_stacktrace TEXT, -- Recomendación, no se hasta que punto es necesario + + start_date TIMESTAMP NOT NULL DEFAULT now(), + update_date TIMESTAMP NOT NULL DEFAULT now(), + finished_date TIMESTAMP +) + +CREATE TABALE IF NOT EXISTS order_history( + history_id SERIAL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, + order_id UUID REFERENCES queue_operations(operation_id) ON DELETE CASCADE, + previous_status order_status NOT NULL, -- Siempre hay un estado anterior, para casos excepcioneale "unknown" + new_status order_status NOT NULL, + change_reason TEXT, + change_date TIMESTAMP NOT NULL DEFAULT NOW(), +); diff --git a/deployment/database/01-objenious.sql b/deployment/database/02-objenious.sql similarity index 82% rename from deployment/database/01-objenious.sql rename to deployment/database/02-objenious.sql index fde74f0..fcf84b5 100644 --- a/deployment/database/01-objenious.sql +++ b/deployment/database/02-objenious.sql @@ -4,11 +4,13 @@ CREATE TYPE status_enum AS ENUM ('noRequestID','noMassID','running','finished',' -- Tabla para gestionar las peticiones de cambio de objenious. -- Para una o mas lineas se pueden lanzar operacione que no sabemos -- con certeza cuando van a terminar. +-- Estas tablas está fuertemente ligadas al sistema que usa la plataforma +-- de objenioius y no debe unsarse para otra compañia. CREATE TABLE if not exists objenious_operation ( id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - retry_count INT DEFAULT 0, - max_retry INT DEFAULT 5, - max_date_retry TIMESTAMP DEFAULT NULL, + retry_count INT DEFAULT 0, -- No implementado en codigo + max_retry INT DEFAULT 5, -- No implementado en codigo + max_date_retry TIMESTAMP DEFAULT NULL, -- No implementado en codigo iccids TEXT, request_id TEXT, mass_action_id TEXT, diff --git a/packages/sim-consumidor-objenious/aplication/Sim.controller.ts b/packages/sim-consumidor-objenious/aplication/Sim.controller.ts index 3797703..7712730 100644 --- a/packages/sim-consumidor-objenious/aplication/Sim.controller.ts +++ b/packages/sim-consumidor-objenious/aplication/Sim.controller.ts @@ -84,7 +84,7 @@ export class SimController { throw new Error("Error activando la sim, no se ha especificado la oferta") } - this.tryUseCase(msg, this.useCases.activate({ + const resp = this.tryUseCase(msg, this.useCases.activate({ dueDate: this.genDueDate(DUE_DATE_SECONDS).toISOString(), customerAccountCode: env.OBJ_CUSTOMER_CODE, identifier: { @@ -96,6 +96,11 @@ export class SimController { services: [] } })) + + // TODO: + // - Crear un registro de operación + // - Si ha salido bien id de operación -> webhook? + // - Si ha salido mal notificar solo cuando se manda a dlx ?? } } diff --git a/packages/sim-entrada-eventos/aplication/httpValidators.ts b/packages/sim-entrada-eventos/aplication/httpValidators.ts index 10cae2a..a9fc907 100644 --- a/packages/sim-entrada-eventos/aplication/httpValidators.ts +++ b/packages/sim-entrada-eventos/aplication/httpValidators.ts @@ -24,14 +24,12 @@ const iccidWithValidCompany = >{ validationFunc: (a: { iccid: string }) => companyFromIccid(a.iccid) != undefined, } - const offerExists = >{ field: "offer", errorMsg: "La oferta introducida no es valida", validationFunc: (a: { offer: string }) => offers.has(a.offer), } - export const activationValidator = new BodyValidator<{ iccid: string, offer: string }>( [ iccidRequired, diff --git a/packages/sim-objenious-cron/tasks/check_objenious_request.ts b/packages/sim-objenious-cron/tasks/check_objenious_request.ts index f47897e..befcbce 100644 --- a/packages/sim-objenious-cron/tasks/check_objenious_request.ts +++ b/packages/sim-objenious-cron/tasks/check_objenious_request.ts @@ -198,3 +198,4 @@ export class CheckObjeniousRequests { return operationsList } } + diff --git a/packages/sim-shared/infrastructure/SimOrderRepoository.ts b/packages/sim-shared/infrastructure/SimOrderRepoository.ts new file mode 100644 index 0000000..e69de29