Mejora del commit anterior
This commit is contained in:
@@ -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 {
|
||||
* <O> Representa el dato original
|
||||
* <P> Representa el dato después del mapeo
|
||||
*/
|
||||
public controllerGenerator<O, P>(args: {
|
||||
public controllerGenerator<O extends Object, P extends Object>(args: {
|
||||
validator?: BodyValidator<O>,
|
||||
mapBody?: (body: O) => P,
|
||||
useCase: (args: P) => Promise<void>,
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user