Todas las operaciones posibles de SIM

This commit is contained in:
2026-02-02 16:59:12 +01:00
parent c1370d6609
commit 93a9a1a8e1
7 changed files with 344 additions and 57 deletions

View File

@@ -4,6 +4,7 @@ import { SimUseCases } from "./Sim.usecases.js";
import { SimEvents } from "#shared/domain/SimEvents.js";
import { constants } from "node:buffer";
import { constrainedMemory } from "node:process";
import { Result } from "#shared/domain/Result.js";
/**
* La clase usa generadores de funciones para mantener el contexto
@@ -42,66 +43,108 @@ export class SimController {
}
}
public activateSim() {
return async (msg: ConsumeMessage) => {
if (!this.validateActivationMsg(msg)) {
throw new Error("Error consumiendo el mensaje no es valido")
}
const msgData = this.decodeMsg(msg) as SimEvents.activation
if (msgData == undefined || msgData.payload == undefined) throw new Error("Mensaje invalido")
console.log("Mensaje procesado", msgData?.toString())
// TODO: Añadir un validador del mensaje
const iccid = msgData.payload.iccid
const headers = msg.properties.headers
console.log("HEADERS: ", headers)
try {
// Caso de uso de activaciones
const result = await this.useCases.activate({
dueDate: this.genDueDate(2 * 60).toISOString(),
identifier: {
identifierType: "ICCID",
identifiers: [iccid]
}
})()
console.log("Resultado de la peticion", result)
if (result.error == undefined) {
console.log("Ack", msgData)
await this.eventBus.ack(msg)
} else {
console.log("Nack", msgData)
await this.eventBus.nack(msg)
}
} catch (e) {
this.eventBus.nack(msg)
/**
* Metodo general de lanzar un caso de uso con un mensaje, si el caso de uso es exitoso
* se ACK el mesaje, si hay algún error se NACK.
* TODO:
* - Se podrian hacer genericos los parametros
*/
private async tryUseCase<T extends any>(msg: ConsumeMessage, usecase: () => Promise<Result<string, T>>) {
try {
const result = await usecase()
if (result.error == undefined) {
await this.eventBus.ack(msg)
return result
} else {
console.error("Error general procesando el caso de uso", result.error)
}
} catch (e) {
console.error("Error general procesando el caso de uso")
this.eventBus.nack(msg)
}
}
public pauseSim() {
public activateSim() {
return async (msg: ConsumeMessage) => {
if (!this.validateActivationMsg(msg)) {
throw new Error("Error consumiendo el mensaje no es valido")
let msgData;
try {
msgData = this.validateMsg(msg) as SimEvents.activation
} catch (e) {
throw new Error("Error consumiendo el mensaje no es valido" + String(e))
}
const msgData = this.decodeMsg(msg)
if (msgData == undefined) Promise.reject("Mensaje invalido")
if (msgData == undefined || msgData.payload == undefined) throw new Error("Mensaje invalido")
const iccid = msgData.payload.iccid
this.tryUseCase(msg, this.useCases.activate({
dueDate: this.genDueDate(2 * 60).toISOString(),
identifier: {
identifierType: "ICCID",
identifiers: [iccid]
}
}))
}
}
public suspendSim() {
return async (msg: ConsumeMessage) => {
let msgData;
try {
msgData = this.validateMsg(msg) as SimEvents.pause
} catch (e) {
throw new Error("Error consumiendo el mensaje no es valido" + String(e))
}
if (msgData == undefined) {
return Promise.reject("Mensaje invalido")
}
const iccid = msgData.payload.iccid
this.tryUseCase(msg, this.useCases.suspend({
dueDate: this.genDueDate(2 * 60).toISOString(),
identifier: {
identifierType: "ICCID",
identifiers: [iccid]
}
}))
}
}
public terminate() {
return async (msg: ConsumeMessage) => {
let msgData;
try {
msgData = this.validateMsg(msg) as SimEvents.pause
} catch (e) {
throw new Error("Error consumiendo el mensaje no es valido" + String(e))
}
if (msgData == undefined) {
return Promise.reject("Mensaje invalido")
}
const iccid = msgData.payload.iccid
console.log("Mensaje procesado", String(msgData))
this.tryUseCase(msg, this.useCases.terminate({
dueDate: this.genDueDate(2 * 60).toISOString(),
identifier: {
identifierType: "ICCID",
identifiers: [iccid]
}
}))
}
}
/**
* TODO:
* - Loguear motivos de la no validacion
* - Añadir validadores inyectables
*/
private validateActivationMsg(msg: ConsumeMessage | null) {
private validateMsg(msg: ConsumeMessage | null) {
if (msg == undefined) return false;
return true;
const msgData = this.decodeMsg(msg) as SimEvents.general
if (msgData == undefined || msgData.payload == undefined) throw new Error("Mensaje invalido")
return msgData;
}
private genDueDate(secondsDue: number) {