111 lines
3.1 KiB
TypeScript
111 lines
3.1 KiB
TypeScript
import { ConsumeMessage } from "amqplib";
|
|
import { SimNosUsecases } from "./SimNOS.usecases";
|
|
import { EventBus } from "sim-shared/domain/EventBus.port.js";
|
|
import { Result } from "sim-shared/domain/Result.js";
|
|
import { SimEvents } from "sim-shared/domain/SimEvents.js";
|
|
import { error } from "node:console";
|
|
|
|
export class SimNosController {
|
|
|
|
constructor(
|
|
private uscases: SimNosUsecases,
|
|
private eventBus: EventBus,
|
|
) {
|
|
}
|
|
|
|
private validateMsg(msg: ConsumeMessage | null) {
|
|
if (msg == undefined) return false;
|
|
const msgData = this.decodeMsg(msg) as SimEvents.general
|
|
if (msgData == undefined || msgData.payload == undefined) throw new Error("Mensaje invalido")
|
|
return msgData;
|
|
}
|
|
|
|
private decodeMsg(msg: ConsumeMessage): object | undefined {
|
|
if (msg.content == undefined) {
|
|
console.warn('[Sim.controller] Mensaje vacío');
|
|
return undefined;
|
|
}
|
|
|
|
try {
|
|
// Convertir el Buffer a String (UTF-8)
|
|
const contentJson = JSON.parse(Buffer.from(msg.content).toString('utf8'))
|
|
return contentJson;
|
|
|
|
} catch (error) {
|
|
console.error('Error al decodificar JSON:', error);
|
|
console.error(Buffer.from(msg.content).toString(("utf8")))
|
|
// Aquí podrías decidir devolver el string crudo o null
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Metodo duplicado se puede generalizar la a una clase sharedController con las funciones basicas
|
|
*/
|
|
private async tryUseCase<T extends any>
|
|
(msg: ConsumeMessage, usecase: () => Promise<Result<string, T>>): 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)
|
|
this.eventBus.nack(msg)
|
|
return result
|
|
}
|
|
} catch (e) {
|
|
console.error("Error general procesando el caso de uso")
|
|
this.eventBus.nack(msg)
|
|
return {
|
|
error: String(e)
|
|
}
|
|
}
|
|
}
|
|
|
|
public activate() {
|
|
return async (msg: ConsumeMessage) => {
|
|
console.log("[i] Evento activate ", msg)
|
|
const data = this.validateMsg(msg) as SimEvents.activation
|
|
const iccid = data.payload.iccid
|
|
const res = await this.tryUseCase(msg, this.uscases.activate({
|
|
iccid: iccid
|
|
}))
|
|
|
|
return res;
|
|
}
|
|
}
|
|
|
|
public suspend() {
|
|
return async (msg: ConsumeMessage) => {
|
|
console.log("Evento suspend ", msg)
|
|
const data = this.validateMsg(msg) as SimEvents.suspend
|
|
const iccid = data.payload.iccid
|
|
const res = await this.tryUseCase(msg, this.uscases.suspend({
|
|
iccid: iccid
|
|
}))
|
|
|
|
return res;
|
|
}
|
|
}
|
|
|
|
public terminate() {
|
|
return async (msg: ConsumeMessage) => {
|
|
console.log("Evento termiante no soportado ", msg)
|
|
}
|
|
}
|
|
|
|
public reActivate() {
|
|
return async (msg: ConsumeMessage) => {
|
|
console.log("Evento reActivate ", msg)
|
|
const data = this.validateMsg(msg) as SimEvents.reActivation
|
|
const iccid = data.payload.iccid
|
|
const res = await this.tryUseCase(msg, this.uscases.reactivate({
|
|
iccid: iccid
|
|
}))
|
|
|
|
return res;
|
|
}
|
|
}
|
|
}
|