Mejora de las orders y actualizacion docs

This commit is contained in:
2026-03-04 13:51:24 +01:00
parent 5771972e2a
commit 39c0e87758
12 changed files with 269 additions and 209 deletions

View File

@@ -3,6 +3,7 @@ import { SimUsecases } from "./Sim.usecases.js"
import { activationValidator, iccidValidator } from "./httpValidators.js"
import { companyFromIccid } from "#domain/companies.js"
import { BodyValidator } from "sim-shared/aplication/BodyValidator.js"
import { tryCatch } from "packages/sim-shared/domain/Result.js"
export class SimController {
@@ -37,19 +38,21 @@ export class SimController {
const body = req.body
// 1. Validacion del body
try {
if (args.validator != undefined)
args.validator.validate(body)
} catch (e) {
if (args.onError != undefined) args.onError(body, e as string)
res.status(422).json({
errors: {
msg: e
}
})
if (args.validator != undefined) {
const validationResult = args.validator.validate(body)
if (validationResult.error != undefined) {
res.status(422).json({
errors: {
...validationResult.error
}
})
args.onError(body, validationResult.error.msg)
return 1;
}
}
// 2. Transformacion del body
// TODO: sustituir el try cach
let data: P = body;
try {
if (args.mapBody != undefined)
@@ -60,26 +63,33 @@ export class SimController {
msg: "Error parseando el body: " + e
}
})
args.onError(body, String(e))
return 1;
}
// 3. Aplicacion del UseCase
try {
const usecaseResult = await args.useCase(data)
// 4. Se devuelve al usuario el caso de exito
res.status(200).json(
usecaseResult
).send()
args.onSuccess(data)
} catch (err) {
// TODO: todos los use cases tienen que pasar a devolver un Result<>
const usecaseResult = await args.useCase(data) // no deberia hacer falta el trycatch
// 4. Casos de error del usecase
if (usecaseResult.error != undefined) {
// 4.1 Error del caso de uso
res.status(500).json({
errors: {
msg: "Error general:" + err
...usecaseResult.error
}
}).send()
return;
args.onError(body, usecaseResult.error.msg.message)
return 1;
}
// 5. Se devuelve al usuario el caso de exito
res.status(200).json(
usecaseResult.data
).send()
args.onSuccess(usecaseResult.data)
return 0;
}
}
@@ -92,155 +102,64 @@ export class SimController {
console.log("OK", data)
}
})
}
public preactivation() {
return async (req: Request, res: Response) => {
console.warn("[!] Se deberia de usar la peticion /sim/activate directamente")
try {
iccidValidator.validate(req.body)
} catch (e) {
res.status(422).json({
errors: {
msg: e
}
})
}
const { iccid } = req.body
const compañia = companyFromIccid(iccid)
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;
}
}
return this.controllerGenerator<{ iccid: string, offer: string }, { iccid: string, offer: string, compañia: string }>({
validator: activationValidator,
mapBody: (b) => {
const { iccid, offer } = b
const compañia = companyFromIccid(iccid)
return { iccid, compañia, offer }
},
useCase: (args) => this.simUseCases.preActivation(args),
onError: (d, e) => console.error("[x] Error preactivation: ", d, e),
onSuccess: console.log
})
}
public activation() {
return async (req: Request, res: Response) => {
try {
activationValidator.validate(req.body)
} catch (e) {
res.status(422).json({
errors: {
msg: e
}
})
console.error("[!] Error validando mensaje")
return;
}
const { iccid, offer } = req.body
const compañia = companyFromIccid(iccid)
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 })
res.status(200).json({
iccid: iccid,
operation: "activation"
}).send()
return;
} catch (err) {
console.error("Error activando la sim ", req.body)
res.status(500).json({
errors: {
msg: "Error general de activation"
}
}).send()
return;
}
}
return this.controllerGenerator<{ iccid: string, offer: string }, { iccid: string, offer: string, compañia: string }>({
validator: activationValidator,
mapBody: (b) => {
const { iccid, offer } = b
const compañia = companyFromIccid(iccid)
return { iccid, compañia, offer }
},
useCase: (args) => this.simUseCases.activation(args),
onError: (d, e) => console.error("[x] Error activacion: ", d, e),
onSuccess: console.log
})
}
public cancelation() {
return async (req: Request, res: Response) => {
try {
iccidValidator.validate(req.body)
} catch (e) {
res.status(422).json({
errors: {
msg: e
}
})
}
const { iccid } = req.body
const compañia = companyFromIccid(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"
}
})
}
}
return this.controllerGenerator<{ iccid: string }, { iccid: string, compañia: string }>({
validator: iccidValidator,
mapBody: (b) => {
const { iccid } = b
const compañia = companyFromIccid(iccid)
return { iccid, compañia }
},
useCase: (args) => this.simUseCases.cancelation(args),
// TODO: Meter en los mensajes el nombre de la operacion
onError: (d, e) => console.error("[x] Error cancelacion: ", d, e),
onSuccess: console.log
})
}
public pause() {
return async (req: Request, res: Response) => {
try {
iccidValidator.validate(req.body)
} catch (e) {
res.status(422).json({
errors: {
msg: e
}
})
}
return this.controllerGenerator<{ iccid: string }, { iccid: string, compañia: string }>({
validator: iccidValidator,
mapBody: (b) => {
const { iccid } = b
const compañia = companyFromIccid(iccid)
return { iccid, compañia }
},
useCase: (args) => this.simUseCases.pause(args),
onError: (d, e) => console.error("[x] Error pausa: ", d, e),
onSuccess: console.log
})
const { iccid } = req.body
const compañia = companyFromIccid(iccid)
try {
await this.simUseCases.pause({ iccid, compañia })
res.status(200).json({
iccid: iccid,
operation: "cancelation"
})
} catch (err) {
console.error("Error pausando la sim ", req.body)
res.status(500).json({
errors: {
msg: "Error pausando la sim"
}
})
}
}
}
public free() {