From 2c9bf9dd9369a350d498bd6872c899fd2a56c81c Mon Sep 17 00:00:00 2001 From: Alvar San Martin Date: Tue, 10 Feb 2026 17:28:32 +0100 Subject: [PATCH] Mejora del commit anterior --- .../aplication/Sim.controller.ts | 43 +++++-------------- 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/packages/sim-entrada-eventos/aplication/Sim.controller.ts b/packages/sim-entrada-eventos/aplication/Sim.controller.ts index 4e183bc..5030db6 100644 --- a/packages/sim-entrada-eventos/aplication/Sim.controller.ts +++ b/packages/sim-entrada-eventos/aplication/Sim.controller.ts @@ -2,8 +2,7 @@ import { Request, Response } from "express" import { SimUsecases } from "./Sim.usecases.js" import { activationValidator, iccidValidator } from "./httpValidators.js" import { companyFromIccid } from "#domain/companies.js" -import { BodyValidator } from "#shared/aplication/BodyValidator.js" -import assert from "node:assert" +import { BodyValidator } from "sim-shared/aplication/BodyValidator.js" export class SimController { @@ -27,7 +26,7 @@ export class SimController { * Representa el dato original *

Representa el dato después del mapeo */ - public controllerGenerator(args: { + public controllerGenerator(args: { validator?: BodyValidator, mapBody?: (body: O) => P, useCase: (args: P) => Promise, @@ -257,11 +256,16 @@ export class SimController { } public save() { - 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 + try { + iccidValidator.validate(req.body) + } catch (e) { + res.status(422).json({ + errors: { + msg: e + } + }) + } const { iccid } = req.body const compañia = companyFromIccid(iccid) @@ -282,29 +286,4 @@ export class SimController { } } } - - private validateBody(body: any, res: Response) { - const { iccid } = body - let errors = {} - let valid = true - - if (iccid == undefined) { - res.status(400) - - errors = { - ...errors, - iccid: "El iccid es undefined" - } - valid = false - } - - if (valid == false) { - res.json({ - errors: errors - }) - } - - return valid; - } - }