66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
import { EventBus } from "#shared/domain/EventBus.port";
|
|
import { ConsumeMessage } from "amqplib";
|
|
|
|
export class SimNosController {
|
|
private eventBus: EventBus;
|
|
private activationUseCases: any;
|
|
|
|
private routes = new Map<string, () => void>([
|
|
["activate", async () => { console.log("caso de uso activate") }],
|
|
["pause", async () => { console.log("caso de uso pause") }],
|
|
["cancel", async () => { console.log("caso de uso cancel") }],
|
|
])
|
|
|
|
constructor(
|
|
eventBus: EventBus
|
|
) {
|
|
this.eventBus = eventBus
|
|
|
|
// No se si hay un sistema mejor
|
|
// convertor en const () => {} para conservar el contexto??
|
|
this.recibeMsg = this.recibeMsg.bind(this)
|
|
}
|
|
|
|
public async recibeMsg(msg: ConsumeMessage | null) {
|
|
if (!this.validateActivationMsg(msg)) {
|
|
throw new Error("Error consumiendo el mensaje no es valido")
|
|
}
|
|
|
|
msg = msg!
|
|
|
|
const msgParsed = JSON.parse(String(msg.content))
|
|
const msgKey = msg.fields.routingKey.split(".")
|
|
const accion = msgKey[2]
|
|
|
|
if (accion == undefined) {
|
|
console.error("La routingKey es incorrecta: " + accion)
|
|
this.eventBus.nack(msg)
|
|
return;
|
|
}
|
|
|
|
if (this.routes.get(accion) == undefined) {
|
|
console.error("No hay una ruta definida para la accion")
|
|
this.eventBus.nack(msg)
|
|
return;
|
|
}
|
|
|
|
try {
|
|
this.routes.get(accion)!()
|
|
} catch (err) {
|
|
console.log("Error procesando el mensaje")
|
|
this.eventBus.nack(msg)
|
|
} finally {
|
|
this.eventBus.ack(msg)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* TODO:
|
|
* - Loguear motivos de la no validacion
|
|
*/
|
|
private validateActivationMsg(msg: ConsumeMessage | null) {
|
|
if (msg == undefined) return false;
|
|
return true;
|
|
}
|
|
}
|