181 lines
5.0 KiB
TypeScript
181 lines
5.0 KiB
TypeScript
import { ConsumeMessage } from "amqplib";
|
|
import { Request, Response } from "express"
|
|
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 { iccidValidator } from "./httpValidators";
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Select especificamente por REST para evitar pasar por las colas.
|
|
* La respuesta es instantanea no se tiene que registrar como operación.
|
|
*/
|
|
public selectREST() {
|
|
return async (req: Request, res: Response) => {
|
|
const { query } = req
|
|
const body = { iccid: query.iccid as string }
|
|
console.log("Evento select", body)
|
|
const validateBody = iccidValidator.validate(body);
|
|
|
|
if (validateBody.error != undefined) {
|
|
res.status(402).json(validateBody)
|
|
return;
|
|
}
|
|
|
|
const iccid: string | string[] = body.iccid
|
|
|
|
console.log("ICCID", iccid)
|
|
|
|
if (Array.isArray(iccid)) {
|
|
const usecaseRes = this.uscases.selectMany({ iccid })
|
|
} else {
|
|
const usecaseRes = await this.uscases.selectOne({ iccid })
|
|
console.log(usecaseRes)
|
|
if (usecaseRes.error != undefined) {
|
|
res.status(500).json(usecaseRes)
|
|
} else {
|
|
res.send(usecaseRes.data)
|
|
}
|
|
return;
|
|
}
|
|
|
|
res.status(200).json(validateBody)
|
|
}
|
|
}
|
|
|
|
|
|
public selectPageREST() {
|
|
return async (req: Request, res: Response) => {
|
|
const { query } = req
|
|
const body = {
|
|
iccid: query.iccid as string
|
|
}
|
|
console.log("Evento page", body)
|
|
/*
|
|
const validateBody = iccidValidator.validate(body);
|
|
|
|
if (validateBody.error != undefined) {
|
|
res.status(402).json(validateBody)
|
|
return;
|
|
}
|
|
*/
|
|
const iccid: string | string[] = body.iccid
|
|
|
|
console.log("ICCID", iccid)
|
|
|
|
const usecaseRes = await this.uscases.selectPage({ iccid })
|
|
|
|
if (usecaseRes.error != undefined) {
|
|
res.status(500).json(usecaseRes)
|
|
} else {
|
|
res.status(200).send(usecaseRes.data)
|
|
}
|
|
|
|
res.status(200).json({ ok: "true" })
|
|
}
|
|
}
|
|
}
|