Files
sf-sim/packages/sim-consumidor-nos/aplication/SimNOS.usecases.ts

147 lines
4.0 KiB
TypeScript
Raw Normal View History

2026-04-16 17:46:32 +02:00
/**
* 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
*
2026-04-16 17:46:32 +02:00
* TODO:
* - Control de errores más preciso
*
*/
import { NosHttpClient } from "infrastructure/NosHttpClient";
2026-04-17 15:49:53 +02:00
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";
2026-04-16 17:46:32 +02:00
export class SimNosUsecases {
constructor(
2026-04-17 14:06:41 +02:00
private httpClient: NosHttpClient,
private nosRepository: NosRepository,
private orderRepository: OrderRepository
2026-04-16 17:46:32 +02:00
) {
}
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
2026-04-16 17:46:32 +02:00
}
const order = await this.orderRepository.updateOrder(updateData)
return order
2026-04-16 17:46:32 +02:00
}
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
2026-04-17 15:49:53 +02:00
}
const order = await this.orderRepository.errorOrder(updateData)
return order
2026-04-17 14:06:41 +02:00
}
public usecaseTemplate<T, R>(
func: (_: T) => Promise<Result<string, R>>,
args: T,
correlation_id?: string | undefined
) {
2026-04-17 15:49:53 +02:00
return async () => {
// Operacion pending -> running
if (correlation_id != undefined)
this.setRunning(correlation_id)
.then()
.catch(e => console.error("Error actualizando el order", e))
2026-04-17 15:49:53 +02:00
try {
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;
}
2026-04-17 15:49:53 +02:00
} catch (e) {
if (correlation_id != undefined)
2026-04-21 17:39:09 +02:00
this.setFailed(correlation_id, "Error general de operacion de SIM (NOS) ", String(e)).then()
2026-04-17 15:49:53 +02:00
return {
2026-04-21 17:39:09 +02:00
error: "Error general de operacion de SIM (NOS) " + String(e)
2026-04-17 15:49:53 +02:00
}
}
2026-04-17 15:49:53 +02:00
}
}
2026-04-17 14:06:41 +02:00
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)
}
2026-04-17 15:49:53 +02:00
public terminate(args: { iccid: string }) {
throw new Error("No hay termination para NOS")
2026-04-17 14:06:41 +02:00
}
/* Importante: Las operaciones de lectua no dejan registro en orders */
2026-04-21 10:11:21 +02:00
public async selectOne(args: {
iccid: string
}) {
const res = await this.nosRepository.getLineInfo(args.iccid)
return res
}
public async selectPage(args: {
2026-04-21 12:50:54 +02:00
offset?: number,
limit?: number,
filter?: string,
orderBy?: string
2026-04-21 10:11:21 +02:00
}) {
const res = await this.nosRepository.getLinePage(args)
return res
}
/**
2026-04-21 10:11:21 +02:00
public selectMany(args: {
iccid: string[]
}) {
return {}
}
*/
2026-04-16 17:46:32 +02:00
}