Files
sf-sim/packages/sim-entrada-eventos/aplication/Sim.controller.ts

242 lines
6.9 KiB
TypeScript
Raw Normal View History

import { Request, Response } from "express"
import { SimUsecases } from "./Sim.usecases.js"
2026-02-10 15:57:03 +01:00
import { activationValidator, iccidValidator } from "./httpValidators.js"
import { companyFromIccid } from "#domain/companies.js"
2026-02-10 17:28:32 +01:00
import { BodyValidator } from "sim-shared/aplication/BodyValidator.js"
import { tryCatch } from "packages/sim-shared/domain/Result.js"
2026-01-26 15:04:17 +01:00
export class SimController {
private simUseCases: SimUsecases
constructor(args: {
simUseCases: SimUsecases,
}) {
this.simUseCases = args.simUseCases
this.activation = this.activation.bind(this)
}
/**
* TODO:
* En proceso, tiene varios problemas
*
* Abstrae el proceso de
* Peticion -> validacion del body -> map del body -> useCase -> OK/ERR
*
* <O> Representa el dato original
* <P> Representa el dato después del mapeo
*/
2026-02-10 17:28:32 +01:00
public controllerGenerator<O extends Object, P extends Object>(args: {
validator?: BodyValidator<O>,
mapBody?: (body: O) => P,
useCase: (args: P) => Promise<any>,
onError: (args: O | P, error: string) => void,
onSuccess: (args: P) => void,
}) {
2026-02-03 17:26:12 +01:00
return async (req: Request, res: Response) => {
const body = req.body
// 1. Validacion del body
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;
}
2026-02-10 15:57:03 +01:00
}
2026-02-03 17:26:12 +01:00
// 2. Transformacion del body
// TODO: sustituir el try cach
let data: P = body;
try {
if (args.mapBody != undefined)
data = args.mapBody(body)
} catch (e) {
res.status(422).json({
errors: {
msg: "Error parseando el body: " + e
}
})
args.onError(body, String(e))
return 1;
}
2026-02-03 17:26:12 +01:00
// 3. Aplicacion del UseCase
// 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
2026-02-03 17:26:12 +01:00
res.status(500).json({
errors: {
...usecaseResult.error
2026-02-03 17:26:12 +01:00
}
}).send()
2026-03-05 10:33:38 +01:00
args.onError(body, usecaseResult.error ?? "Error indefinido")
return 1;
2026-02-03 17:26:12 +01:00
}
// 5. Se devuelve al usuario el caso de exito
res.status(200).json(
usecaseResult.data
).send()
args.onSuccess(usecaseResult.data)
return 0;
}
}
public test() {
return this.controllerGenerator<{ iccid: string, offer: string }, { iccid: string }>({
validator: iccidValidator,
useCase: (args) => this.simUseCases.test(args),
onError: (data, error) => console.error(error),
onSuccess: (data) => {
console.log("OK", data)
}
})
}
public preactivation() {
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
})
}
2026-02-03 17:26:12 +01:00
public activation() {
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
})
}
2026-04-15 10:17:36 +02:00
public reActivation() {
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.reActivation(args),
onError: (d, e) => console.error("[x] Error activacion: ", d, e),
onSuccess: console.log
})
}
2026-02-03 17:26:12 +01:00
public cancelation() {
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
})
}
2026-02-03 15:40:30 +01:00
public pause() {
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
})
2026-02-03 15:40:30 +01:00
}
2026-02-03 17:26:12 +01:00
public free() {
return async (req: Request, res: Response) => {
2026-02-10 15:57:03 +01:00
try {
iccidValidator.validate(req.body)
} catch (e) {
res.status(422).json({
errors: {
msg: e
}
})
}
2026-02-03 17:26:12 +01:00
const { iccid } = req.body
2026-02-10 15:57:03 +01:00
const compañia = companyFromIccid(iccid)
2026-02-03 17:26:12 +01:00
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"
}
})
}
}
}
2026-02-03 17:26:12 +01:00
public save() {
return async (req: Request, res: Response) => {
2026-02-10 17:28:32 +01:00
try {
iccidValidator.validate(req.body)
} catch (e) {
res.status(422).json({
errors: {
msg: e
}
})
}
2026-02-03 17:26:12 +01:00
const { iccid } = req.body
2026-02-10 15:57:03 +01:00
const compañia = companyFromIccid(iccid)
2026-02-03 17:26:12 +01:00
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"
}
})
}
}
}
}