From f0f3827fd0f10cd5110db040a9cb2a816745acf3 Mon Sep 17 00:00:00 2001 From: Alvar San Martin Date: Tue, 21 Apr 2026 15:51:16 +0200 Subject: [PATCH] Registro del estado/resultado de las operaciones de NOS --- .../aplication/SimNOS.controller.ts | 4 +- .../aplication/SimNOS.usecases.ts | 113 ++++++++++++++---- 2 files changed, 92 insertions(+), 25 deletions(-) diff --git a/packages/sim-consumidor-nos/aplication/SimNOS.controller.ts b/packages/sim-consumidor-nos/aplication/SimNOS.controller.ts index 7ba0457..dafb2a2 100644 --- a/packages/sim-consumidor-nos/aplication/SimNOS.controller.ts +++ b/packages/sim-consumidor-nos/aplication/SimNOS.controller.ts @@ -69,8 +69,10 @@ export class SimNosController { console.log("[i] Evento activate ", msg) const data = this.validateMsg(msg) as SimEvents.activation const iccid = data.payload.iccid + const correlation_id = data.headers?.message_id const res = await this.tryUseCase(msg, this.uscases.activate({ - iccid: iccid + iccid: iccid, + correlation_id: correlation_id })) return res; diff --git a/packages/sim-consumidor-nos/aplication/SimNOS.usecases.ts b/packages/sim-consumidor-nos/aplication/SimNOS.usecases.ts index 37b07fc..8806a2f 100644 --- a/packages/sim-consumidor-nos/aplication/SimNOS.usecases.ts +++ b/packages/sim-consumidor-nos/aplication/SimNOS.usecases.ts @@ -2,60 +2,123 @@ * Documentación de referencia: * https://pelion-help.iot-x.com/nos/en-US/Content/API/APIReference/API%20Reference.htm?tocpath=_____7 * + * En nos el correlation_id ya va a ser obligatorio en todos los mensajes + * * TODO: * - Control de errores más preciso * */ import { NosHttpClient } from "infrastructure/NosHttpClient"; import { NosRepository } from "infrastructure/NosRepository"; +import { ErrorOrderDTO, FinishOrderDTO, UpdateOrderDTO } from "sim-shared/domain/Order.js"; +import { Result } from "sim-shared/domain/Result.js"; +import { OrderRepository } from "sim-shared/infrastructure/OrderRepository.js"; export class SimNosUsecases { constructor( private httpClient: NosHttpClient, - private nosRepository: NosRepository + private nosRepository: NosRepository, + private orderRepository: OrderRepository ) { } - public activate(args: { iccid: string }) { + private async setRunning(correlation_id: string) { + // En NOS el updateOrder se hace con el correlation_id que viene en la cabecera del + // mensaje consumido + const updateData: UpdateOrderDTO = { + new_status: "running", + correlation_id: correlation_id + } + const order = await this.orderRepository.updateOrder(updateData) + return order + } + + private async setFinished(correlation_id: string) { + // En NOS el updateOrder se hace con el correlation_id que viene en la cabecera del + // mensaje consumido + const updateData: FinishOrderDTO = { + correlation_id: correlation_id + } + const order = await this.orderRepository.finishOrder(updateData) + return order + } + + private async setFailed(correlation_id: string, reason: string, detail?: string) { + // En NOS el updateOrder se hace con el correlation_id que viene en la cabecera del + // mensaje consumido + const updateData: ErrorOrderDTO = { + status: "failed", + correlation_id: correlation_id, + reason: reason, + stackTrace: detail + } + const order = await this.orderRepository.errorOrder(updateData) + return order + } + + public usecaseTemplate( + func: (_: T) => Promise>, + args: T, + correlation_id?: string | undefined + ) { return async () => { + // Operacion pending -> running + if (correlation_id != undefined) + this.setRunning(correlation_id) + .then() + .catch(e => console.error("Error actualizando el order", e)) + try { - return await this.nosRepository.activateSim(args.iccid) + const res = await func(args) + + if (res.error != undefined) { + if (correlation_id != undefined) + this.setFailed(correlation_id, res.error).then() + return res; + } else { + if (correlation_id != undefined) + this.setFinished(correlation_id).then() + return res; + } + } catch (e) { + if (correlation_id != undefined) + this.setFailed(correlation_id, "Error general de operacion de SIM (NOS)", String(e)).then() return { - error: "Error general de activación de sim" + String(e) + error: "Error general de operacion de SIM (NOS)" + String(e) } } + } } - public suspend(args: { iccid: string }) { - return async () => { - try { - return await this.nosRepository.bar(args.iccid) - } catch (e) { - return { - error: "Error general de suspension de sim" + String(e) - } - } - } + public activate(args: { + iccid: string, + correlation_id?: string + }) { + return this.usecaseTemplate(this.nosRepository.activateSim, args.iccid, args.correlation_id) } - public reactivate(args: { iccid: string }) { - return async () => { - try { - return await this.nosRepository.unbar(args.iccid) - } catch (e) { - return { - error: "Error general de reactivación de sim" + String(e) - } - } - } + public suspend(args: { + iccid: string, + correlation_id?: string + }) { + return this.usecaseTemplate(this.nosRepository.bar, args.iccid, args.correlation_id) + } + + public reactivate(args: { + iccid: string, + correlation_id?: string + }) { + return this.usecaseTemplate(this.nosRepository.unbar, args.iccid, args.correlation_id) } public terminate(args: { iccid: string }) { throw new Error("No hay termination para NOS") } + /* Importante: Las operaciones de lectua no dejan registro en orders */ + public async selectOne(args: { iccid: string }) { @@ -73,9 +136,11 @@ export class SimNosUsecases { return res } + /** public selectMany(args: { iccid: string[] }) { return {} } +*/ }