80 lines
2.4 KiB
TypeScript
80 lines
2.4 KiB
TypeScript
/**
|
|
* Dirige cada mensaje dependiendo de el tipo de acción que contenga
|
|
* Podría hacerse con varias colas, pero así se controla mejor que
|
|
* las operaciones se hagan de 1 en 1.
|
|
*/
|
|
|
|
import { ConsumeMessage } from "amqplib";
|
|
import { EventBus } from "sim-shared/domain/EventBus.port.js";
|
|
import { Result } from "sim-shared/domain/Result.js";
|
|
import { SimAlaiController } from "./SimAlai.controller.js";
|
|
|
|
type FuncType = ((m: ConsumeMessage) => Promise<Result<string, any>>)
|
|
|
|
export class SimAlaiRouter {
|
|
private readonly routes: Map<string, FuncType>;
|
|
|
|
// WIP
|
|
constructor(
|
|
private readonly simController: SimAlaiController,
|
|
private readonly eventBus: EventBus
|
|
) {
|
|
this.routes = new Map<string, FuncType>([
|
|
["activate", this.simController.activate()],
|
|
["pause", this.simController.suspend()],
|
|
["reactivate", this.simController.reActivate()],
|
|
["cancel", this.simController.terminate()],
|
|
["preActivate", this.simController.preactivate()]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Enruta el mensaje a la acción correspondiente basándose en la routing key
|
|
* TODO: No estoy seguro que deba meter el nack aqui
|
|
* - De moemento el ack-nack se gestiona en los controller, por si acaso hay casos
|
|
* limite en
|
|
*/
|
|
public route = async (msg: ConsumeMessage | null): Promise<void> => {
|
|
if (!msg) {
|
|
console.error("[Router] Mensaje vacío");
|
|
return;
|
|
}
|
|
|
|
const action = this.extractAction(msg);
|
|
|
|
if (!action) {
|
|
console.error("[Router] La routing key no tiene una acción definida", msg.fields.routingKey);
|
|
this.eventBus.nack(msg)
|
|
return;
|
|
}
|
|
|
|
const handler = this.routes.get(action);
|
|
|
|
if (!handler) {
|
|
console.error(`[Router] La acción '${action}' no tiene un controlador asociado`);
|
|
this.eventBus.nack(msg)
|
|
return;
|
|
}
|
|
|
|
try {
|
|
console.log("[Router] Ejecutando operación:", action);
|
|
|
|
// El controlador devuelve una función (thunk) que debe ser ejecutada
|
|
const executeParams = handler(msg);
|
|
|
|
if (typeof executeParams === "function") {
|
|
const res = await executeParams;
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error(`[Router] Error al ejecutar la operación '${action}':`, error);
|
|
this.eventBus.nack(msg)
|
|
}
|
|
};
|
|
|
|
private extractAction(msg: ConsumeMessage): string | undefined {
|
|
// Se asume que la acción está en la tercera posición: domain.compañia.accion
|
|
return msg.fields.routingKey.split(".")[2];
|
|
}
|
|
}
|