2026-04-23 13:18:50 +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
|
|
|
|
|
*
|
|
|
|
|
* TODO:
|
|
|
|
|
* - Control de errores más preciso
|
|
|
|
|
*
|
|
|
|
|
*/
|
2026-04-30 17:41:49 +02:00
|
|
|
import { AlaiRepository } from "#infrastructure/AlaiRepository.js";
|
|
|
|
|
import { ConsumeMessage } from "amqplib";
|
2026-04-23 13:18:50 +02:00
|
|
|
import { ErrorOrderDTO, FinishOrderDTO, UpdateOrderDTO } from "sim-shared/domain/Order.js";
|
|
|
|
|
import { Result } from "sim-shared/domain/Result.js";
|
2026-04-30 17:41:49 +02:00
|
|
|
import { HttpClient } from "sim-shared/infrastructure/HTTPClient.js";
|
2026-04-23 13:18:50 +02:00
|
|
|
import { OrderRepository } from "sim-shared/infrastructure/OrderRepository.js";
|
|
|
|
|
|
|
|
|
|
export class SimAlaiUsecases {
|
|
|
|
|
constructor(
|
2026-04-30 17:41:49 +02:00
|
|
|
private httpClient: HttpClient,
|
|
|
|
|
private alaiRepository: AlaiRepository,
|
2026-04-23 13:18:50 +02:00
|
|
|
private orderRepository: OrderRepository
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async setRunning(correlation_id: string) {
|
|
|
|
|
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,
|
|
|
|
|
error: reason,
|
|
|
|
|
stackTrace: detail
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log("SET FAILED DATA:", updateData)
|
|
|
|
|
const order = await this.orderRepository.errorOrder(updateData)
|
|
|
|
|
console.log("SET FAILED RES:", order)
|
|
|
|
|
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 {
|
|
|
|
|
const res = await func(args)
|
|
|
|
|
|
|
|
|
|
if (res.error != undefined) {
|
|
|
|
|
console.log("Error peticion: ", res, correlation_id)
|
|
|
|
|
if (correlation_id != undefined)
|
|
|
|
|
this.setFailed(correlation_id, res.error)
|
|
|
|
|
.then(e => console.log("failed", e))
|
|
|
|
|
.catch(e => console.error(e))
|
|
|
|
|
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 operacion de SIM (NOS) " + String(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public activate(args: {
|
|
|
|
|
iccid: string,
|
2026-04-30 17:41:49 +02:00
|
|
|
correlation_id: string | undefined
|
2026-04-23 13:18:50 +02:00
|
|
|
}) {
|
2026-04-30 17:41:49 +02:00
|
|
|
return this.usecaseTemplate(async (args /*iccid*/) => {
|
|
|
|
|
const order = await this.alaiRepository.createOrder()
|
|
|
|
|
if (order.error != undefined) {
|
|
|
|
|
// Falla el crearse un order (problema de servidor, token, etc)
|
|
|
|
|
console.error(order.error)
|
|
|
|
|
return order
|
|
|
|
|
}
|
|
|
|
|
const reserved = await this.alaiRepository.createReserve(order.data.id, args)
|
|
|
|
|
return reserved
|
|
|
|
|
}, args.iccid, args.correlation_id)
|
2026-04-23 13:18:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public suspend(args: {
|
|
|
|
|
iccid: string,
|
2026-04-30 17:41:49 +02:00
|
|
|
correlation_id: string | undefined
|
2026-04-23 13:18:50 +02:00
|
|
|
}) {
|
2026-04-30 17:41:49 +02:00
|
|
|
return this.usecaseTemplate(async (args /*iccid*/) => {
|
|
|
|
|
const order = await this.alaiRepository.createOrder()
|
|
|
|
|
if (order.error != undefined) {
|
|
|
|
|
// Falla el crearse un order (problema de servidor, token, etc)
|
|
|
|
|
console.error(order.error)
|
|
|
|
|
return order
|
|
|
|
|
}
|
|
|
|
|
const reserved = await this.alaiRepository.createReserve(order.data.id, args)
|
|
|
|
|
return reserved
|
|
|
|
|
}, args.iccid, args.correlation_id)
|
2026-04-23 13:18:50 +02:00
|
|
|
}
|
|
|
|
|
}
|