diff --git a/docs/sim-api/Cancel.bru b/docs/sim-api/Cancel.bru index e1ce60a..3d6108f 100644 --- a/docs/sim-api/Cancel.bru +++ b/docs/sim-api/Cancel.bru @@ -6,10 +6,14 @@ meta { post { url: {{baseurl}}/sim/cancel - body: none + body: formUrlEncoded auth: inherit } +body:form-urlencoded { + iccid: 8933201124059176320 +} + settings { encodeUrl: true timeout: 0 diff --git a/docs/sim-api/Preactivate.bru b/docs/sim-api/Preactivate.bru index 3adb322..b164f68 100644 --- a/docs/sim-api/Preactivate.bru +++ b/docs/sim-api/Preactivate.bru @@ -6,10 +6,18 @@ meta { post { url: {{baseurl}}/sim/preactivate - body: none + body: formUrlEncoded auth: inherit } +params:query { + ~iccid: 1234 +} + +body:form-urlencoded { + iccid: 1234 +} + settings { encodeUrl: true timeout: 0 diff --git a/packages/shared/infrastructure/RabbitMQEventBus.ts b/packages/shared/infrastructure/RabbitMQEventBus.ts index df996db..8714cba 100644 --- a/packages/shared/infrastructure/RabbitMQEventBus.ts +++ b/packages/shared/infrastructure/RabbitMQEventBus.ts @@ -52,7 +52,7 @@ export class RabbitMQEventBus implements EventBus { const routingKey = msg.fields.routingKey if (numberRetry < this.maxRetry) { - console.log("Dalaying") + console.log("Delaying") await this.channel.publish("sim.ex.objenious.delayed", routingKey, msg.content, { headers: { ...headers, diff --git a/packages/sim-consumidor-objenious/aplication/Sim.controller.ts b/packages/sim-consumidor-objenious/aplication/Sim.controller.ts index 77f6b68..157ffc2 100644 --- a/packages/sim-consumidor-objenious/aplication/Sim.controller.ts +++ b/packages/sim-consumidor-objenious/aplication/Sim.controller.ts @@ -57,6 +57,7 @@ export class SimController { return result } else { console.error("Error general procesando el caso de uso", result.error) + this.eventBus.nack(msg) } } catch (e) { console.error("Error general procesando el caso de uso") diff --git a/packages/sim-consumidor-objenious/aplication/Sim.router.ts b/packages/sim-consumidor-objenious/aplication/Sim.router.ts index 66c38fb..c766017 100644 --- a/packages/sim-consumidor-objenious/aplication/Sim.router.ts +++ b/packages/sim-consumidor-objenious/aplication/Sim.router.ts @@ -6,11 +6,15 @@ import { ConsumeMessage } from "amqplib"; import { SimController } from "./Sim.controller.js"; +import { EventBus } from "#shared/domain/EventBus.port.js"; export class SimRouter { private readonly routes: Map Promise>; - constructor(private readonly simController: SimController) { + constructor( + private readonly simController: SimController, + private readonly eventBus: EventBus + ) { this.routes = new Map([ ["activate", this.simController.activate()], ["pause", this.simController.suspend()], @@ -22,6 +26,7 @@ export class SimRouter { /** * Enruta el mensaje a la acción correspondiente basándose en la routing key + * TODO: No estoy seguro que deba meter el nack aqui */ public route = async (msg: ConsumeMessage | null): Promise => { if (!msg) { @@ -33,6 +38,7 @@ export class SimRouter { if (!action) { console.error("[Router] La routing key no tiene una acción definida", msg.fields.routingKey); + this.eventBus.nack(msg) return; } @@ -40,6 +46,7 @@ export class SimRouter { if (!handler) { console.error(`[Router] La acción '${action}' no tiene un controlador asociado`); + this.eventBus.nack(msg) return; } @@ -55,6 +62,7 @@ export class SimRouter { } catch (error) { console.error(`[Router] Error al ejecutar la operación '${action}':`, error); + this.eventBus.nack(msg) } }; diff --git a/packages/sim-consumidor-objenious/aplication/Sim.usecases.ts b/packages/sim-consumidor-objenious/aplication/Sim.usecases.ts index 234a056..9d63217 100644 --- a/packages/sim-consumidor-objenious/aplication/Sim.usecases.ts +++ b/packages/sim-consumidor-objenious/aplication/Sim.usecases.ts @@ -49,7 +49,7 @@ export class SimUseCases { } catch (error) { console.error("[Sim.usecase] Error activando ", (error as AxiosError).response?.status) return { - error: "Error general de la petiacion", + error: "Error general de la peticion", data: undefined } } diff --git a/packages/sim-consumidor-objenious/index.ts b/packages/sim-consumidor-objenious/index.ts index bbf0f9c..dc2d585 100644 --- a/packages/sim-consumidor-objenious/index.ts +++ b/packages/sim-consumidor-objenious/index.ts @@ -14,7 +14,7 @@ async function startWorker() { httpClient: httpClient }) ) - const simRouter = new SimRouter(simActivationController) + const simRouter = new SimRouter(simActivationController, rmqClient) // de momento solo una cola por simplificar rmqClient.consume("sim.objenious", simRouter.route) diff --git a/packages/sim-entrada-eventos/aplication/Sim.controller.ts b/packages/sim-entrada-eventos/aplication/Sim.controller.ts index 47a27b1..3742cf2 100644 --- a/packages/sim-entrada-eventos/aplication/Sim.controller.ts +++ b/packages/sim-entrada-eventos/aplication/Sim.controller.ts @@ -24,101 +24,106 @@ export class SimController { this.activation = this.activation.bind(this) } - async preactivation(req: Request, res: Response) { - const valido = this.validateBody(req.body, res) - if (valido == false) return; + public preactivation() { + return async (req: Request, res: Response) => { + const valido = this.validateBody(req.body, res) + if (valido == false) return; - const { iccid } = req.body - const compañia = this.compañiaFromIccid(iccid) + const { iccid } = req.body + const compañia = this.compañiaFromIccid(iccid) - if (compañia == undefined) { - res.status(500).json({ - errors: { - msg: "El iccid no pertenece a una compañia conocida" - } - }) - return; - } + if (compañia == undefined) { + res.status(500).json({ + errors: { + msg: "El iccid no pertenece a una compañia conocida" + } + }) + return; + } - try { - await this.simUseCases.preActivation({ iccid, compañia }) + try { + await this.simUseCases.preActivation({ iccid, compañia }) - res.status(200).json({ - iccid: iccid, - operation: "activation" - }).send() - } catch (err) { - console.error("Error activando la sim ", req.body) - res.status(500).json({ - errors: { - msg: "Error general de activation" - } - }).send() - return; + res.status(200).json({ + iccid: iccid, + operation: "activation" + }).send() + } catch (err) { + console.error("Error activando la sim ", req.body) + res.status(500).json({ + errors: { + msg: "Error general de activation" + } + }).send() + return; + } } } - async activation(req: Request, res: Response) { - const valido = this.validateBody(req.body, res) + public activation() { + return async (req: Request, res: Response) => { + const valido = this.validateBody(req.body, res) - if (valido == false) return; // Si no es valido ya se ha enviado el error + if (valido == false) return; // Si no es valido ya se ha enviado el error - const { iccid, offer } = req.body + const { iccid, offer } = req.body - const compañia = this.compañiaFromIccid(iccid) + const compañia = this.compañiaFromIccid(iccid) - if (compañia == undefined) { - res.status(500).json({ - errors: { - msg: "El iccid no pertenece a una compañia conocida" - } - }) - return; - } + if (compañia == undefined) { + res.status(500).json({ + errors: { + msg: "El iccid no pertenece a una compañia conocida" + } + }) + return; + } - try { - await this.simUseCases.activation({ iccid, compañia, offer }) + try { + await this.simUseCases.activation({ iccid, compañia, offer }) - res.status(200).json({ - iccid: iccid, - operation: "activation" - }).send() - } catch (err) { - console.error("Error activando la sim ", req.body) - res.status(500).json({ - errors: { - msg: "Error general de activation" - } - }).send() - return; + res.status(200).json({ + iccid: iccid, + operation: "activation" + }).send() + } catch (err) { + console.error("Error activando la sim ", req.body) + res.status(500).json({ + errors: { + msg: "Error general de activation" + } + }).send() + return; + } } } - async cancelation(req: Request, res: Response) { - const valido = this.validateBody(req.body, res) + public cancelation() { + return async (req: Request, res: Response) => { + const valido = this.validateBody(req.body, res) - if (valido == false) return; // Si no es valido ya se ha enviado el error + if (valido == false) return; // Si no es valido ya se ha enviado el error - const { iccid } = req.body - const compañia = this.compañiaFromIccid(iccid) + const { iccid } = req.body + const compañia = this.compañiaFromIccid(iccid) - try { - await this.simUseCases.cancelation({ iccid, compañia }) - res.status(200).json({ - iccid: iccid, - operation: "cancelation" - }) - } catch (err) { - console.error("Error cancelando la sim ", req.body) - res.status(500).json({ - errors: { - msg: "Error general de cancelacion" - } - }) + try { + await this.simUseCases.cancelation({ iccid, compañia }) + res.status(200).json({ + iccid: iccid, + operation: "cancelation" + }) + } catch (err) { + console.error("Error cancelando la sim ", req.body) + res.status(500).json({ + errors: { + msg: "Error general de cancelacion" + } + }) + } } - } public pause() { @@ -147,56 +152,57 @@ export class SimController { } } - async free(req: Request, res: Response) { + public free() { + return async (req: Request, res: Response) => { + const valido = this.validateBody(req.body, res) - const valido = this.validateBody(req.body, res) + if (valido == false) return; // Si no es valido ya se ha enviado el error - if (valido == false) return; // Si no es valido ya se ha enviado el error + const { iccid } = req.body + const compañia = this.compañiaFromIccid(iccid) - const { iccid } = req.body - const compañia = this.compañiaFromIccid(iccid) - - try { - await this.simUseCases.cancelation({ iccid, compañia }) - res.status(200).json({ - iccid: iccid, - operation: "liberacion" - }) - } catch (err) { - console.error("Error liberando la sim ", req.body) - res.status(500).json({ - errors: { - msg: "Error general de liberacion" - } - }) + try { + await this.simUseCases.cancelation({ iccid, compañia }) + res.status(200).json({ + iccid: iccid, + operation: "liberacion" + }) + } catch (err) { + console.error("Error liberando la sim ", req.body) + res.status(500).json({ + errors: { + msg: "Error general de liberacion" + } + }) + } } - } - async save(req: Request, res: Response) { + public save() { - const valido = this.validateBody(req.body, res) + return async (req: Request, res: Response) => { + const valido = this.validateBody(req.body, res) - if (valido == false) return; // Si no es valido ya se ha enviado el error + if (valido == false) return; // Si no es valido ya se ha enviado el error - const { iccid } = req.body - const compañia = this.compañiaFromIccid(iccid) + const { iccid } = req.body + const compañia = this.compañiaFromIccid(iccid) - try { - await this.simUseCases.cancelation({ iccid, compañia }) - res.status(200).json({ - iccid: iccid, - operation: "cancelation" - }) - } catch (err) { - console.error("Error activando la sim ", req.body) - res.status(500).json({ - errors: { - msg: "Error general de activation" - } - }) + try { + await this.simUseCases.cancelation({ iccid, compañia }) + res.status(200).json({ + iccid: iccid, + operation: "cancelation" + }) + } catch (err) { + console.error("Error activando la sim ", req.body) + res.status(500).json({ + errors: { + msg: "Error general de activation" + } + }) + } } - } private validateBody(body: any, res: Response) { diff --git a/packages/sim-entrada-eventos/aplication/Sim.usecases.ts b/packages/sim-entrada-eventos/aplication/Sim.usecases.ts index 460ffc6..591f295 100644 --- a/packages/sim-entrada-eventos/aplication/Sim.usecases.ts +++ b/packages/sim-entrada-eventos/aplication/Sim.usecases.ts @@ -64,8 +64,8 @@ export class SimUsecases { */ async cancelation(args: { iccid: string, compañia: string }) { - const activationEvent = { - key: `sim.${args.compañia}.cancelation`, + const activationEvent = { + key: `sim.${args.compañia}.cancel`, payload: { iccid: args.iccid } diff --git a/packages/sim-entrada-eventos/infrastructure/simRoutes.http.ts b/packages/sim-entrada-eventos/infrastructure/simRoutes.http.ts index d705fb9..e7e16db 100644 --- a/packages/sim-entrada-eventos/infrastructure/simRoutes.http.ts +++ b/packages/sim-entrada-eventos/infrastructure/simRoutes.http.ts @@ -15,17 +15,17 @@ const simController = new SimController({ simRoutes.get("/status", () => { }) -simRoutes.post("/save", simController.save) +simRoutes.post("/save", simController.save()) -simRoutes.post("/activate", simController.activation) +simRoutes.post("/activate", simController.activation()) -simRoutes.post("/preActivate", simController.preactivation) +simRoutes.post("/preActivate", simController.preactivation()) simRoutes.post("/pause", simController.pause()) -simRoutes.post("/cancel", simController.cancelation) +simRoutes.post("/cancel", simController.cancelation()) // Proceso especifico de ALAI para liberar sims canceladas -simRoutes.post("/free", simController.free) +simRoutes.post("/free", simController.free()) export { simRoutes }