Registro del estado/resultado de las operaciones de NOS

This commit is contained in:
2026-04-21 15:51:16 +02:00
parent ee8f84bc57
commit f0f3827fd0
2 changed files with 92 additions and 25 deletions

View File

@@ -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;

View File

@@ -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<T, R>(
func: (_: T) => Promise<Result<string, R>>,
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 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 activate(args: {
iccid: string,
correlation_id?: string
}) {
return this.usecaseTemplate(this.nosRepository.activateSim, args.iccid, args.correlation_id)
}
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 {}
}
*/
}