2026-01-13 15:41:59 +01:00
|
|
|
import { Request, Response } from "express"
|
2026-02-09 13:24:04 +01:00
|
|
|
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"
|
2026-01-13 15:41:59 +01:00
|
|
|
|
2026-01-26 15:04:17 +01:00
|
|
|
|
2026-01-13 15:41:59 +01:00
|
|
|
export class SimController {
|
|
|
|
|
private simUseCases: SimUsecases
|
|
|
|
|
|
|
|
|
|
constructor(args: {
|
2026-02-24 11:27:47 +01:00
|
|
|
simUseCases: SimUsecases,
|
2026-01-13 15:41:59 +01:00
|
|
|
}) {
|
|
|
|
|
this.simUseCases = args.simUseCases
|
2026-01-16 13:40:29 +01:00
|
|
|
|
|
|
|
|
this.activation = this.activation.bind(this)
|
2026-01-13 15:41:59 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-10 17:26:04 +01:00
|
|
|
/**
|
|
|
|
|
* 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: {
|
2026-02-10 17:26:04 +01:00
|
|
|
validator?: BodyValidator<O>,
|
|
|
|
|
mapBody?: (body: O) => P,
|
2026-02-25 12:20:52 +01:00
|
|
|
useCase: (args: P) => Promise<any>,
|
2026-02-10 17:26:04 +01:00
|
|
|
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) => {
|
2026-02-10 17:26:04 +01:00
|
|
|
const body = req.body
|
|
|
|
|
|
|
|
|
|
// 1. Validacion del body
|
2026-02-10 15:57:03 +01:00
|
|
|
try {
|
2026-02-10 17:26:04 +01:00
|
|
|
if (args.validator != undefined)
|
|
|
|
|
args.validator.validate(body)
|
2026-02-10 15:57:03 +01:00
|
|
|
} catch (e) {
|
2026-02-10 17:26:04 +01:00
|
|
|
if (args.onError != undefined) args.onError(body, e as string)
|
2026-02-10 15:57:03 +01:00
|
|
|
res.status(422).json({
|
|
|
|
|
errors: {
|
|
|
|
|
msg: e
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-02-03 17:26:12 +01:00
|
|
|
|
2026-02-10 17:26:04 +01:00
|
|
|
// 2. Transformacion del body
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-02-03 17:26:12 +01:00
|
|
|
|
2026-02-10 17:26:04 +01:00
|
|
|
// 3. Aplicacion del UseCase
|
|
|
|
|
try {
|
|
|
|
|
const usecaseResult = await args.useCase(data)
|
2026-02-25 12:20:52 +01:00
|
|
|
// 4. Se devuelve al usuario el caso de exito
|
2026-02-10 17:26:04 +01:00
|
|
|
res.status(200).json(
|
|
|
|
|
usecaseResult
|
|
|
|
|
).send()
|
2026-02-25 12:20:52 +01:00
|
|
|
args.onSuccess(data)
|
2026-02-10 17:26:04 +01:00
|
|
|
} catch (err) {
|
2026-02-25 12:20:52 +01:00
|
|
|
// 4.1 Error del caso de uso
|
2026-02-03 17:26:12 +01:00
|
|
|
res.status(500).json({
|
|
|
|
|
errors: {
|
2026-02-10 17:26:04 +01:00
|
|
|
msg: "Error general:" + err
|
2026-02-03 17:26:12 +01:00
|
|
|
}
|
2026-02-10 17:26:04 +01:00
|
|
|
}).send()
|
2026-02-03 17:26:12 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2026-02-25 12:20:52 +01:00
|
|
|
|
2026-02-10 17:26:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-03 14:32:46 +01:00
|
|
|
|
2026-02-25 12:20:52 +01:00
|
|
|
public test() {
|
|
|
|
|
return this.controllerGenerator<{ iccid: string, offer: string }, { iccid: string }>({
|
2026-02-24 11:27:47 +01:00
|
|
|
validator: iccidValidator,
|
2026-02-25 12:20:52 +01:00
|
|
|
useCase: (args) => this.simUseCases.test(args),
|
2026-02-24 11:27:47 +01:00
|
|
|
onError: (data, error) => console.error(error),
|
|
|
|
|
onSuccess: (data) => {
|
|
|
|
|
console.log("OK", data)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 17:26:04 +01:00
|
|
|
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)
|
2026-02-03 14:32:46 +01:00
|
|
|
|
2026-02-03 17:26:12 +01:00
|
|
|
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;
|
|
|
|
|
}
|
2026-02-03 14:32:46 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-03 17:26:12 +01:00
|
|
|
public activation() {
|
|
|
|
|
return async (req: Request, res: Response) => {
|
2026-02-10 15:57:03 +01:00
|
|
|
try {
|
|
|
|
|
activationValidator.validate(req.body)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
res.status(422).json({
|
|
|
|
|
errors: {
|
|
|
|
|
msg: e
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-01-14 10:19:49 +01:00
|
|
|
|
2026-02-03 17:26:12 +01:00
|
|
|
const { iccid, offer } = req.body
|
2026-01-14 10:19:49 +01:00
|
|
|
|
2026-02-10 15:57:03 +01:00
|
|
|
const compañia = companyFromIccid(iccid)
|
2026-02-03 14:32:46 +01:00
|
|
|
|
2026-02-03 17:26:12 +01:00
|
|
|
if (compañia == undefined) {
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
errors: {
|
|
|
|
|
msg: "El iccid no pertenece a una compañia conocida"
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-01-13 15:41:59 +01:00
|
|
|
|
2026-01-28 10:25:49 +01:00
|
|
|
|
2026-02-03 17:26:12 +01:00
|
|
|
try {
|
|
|
|
|
await this.simUseCases.activation({ iccid, compañia, offer })
|
2026-01-30 10:42:48 +01:00
|
|
|
|
2026-02-03 17:26:12 +01:00
|
|
|
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;
|
|
|
|
|
}
|
2026-01-13 15:41:59 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-03 17:26:12 +01:00
|
|
|
public cancelation() {
|
|
|
|
|
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-01-14 10:19:49 +01:00
|
|
|
|
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-01-13 15:41:59 +01:00
|
|
|
|
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 cancelando la sim ", req.body)
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
errors: {
|
|
|
|
|
msg: "Error general de cancelacion"
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-01-14 10:19:49 +01:00
|
|
|
}
|
2026-01-13 15:41:59 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-03 15:40:30 +01:00
|
|
|
public pause() {
|
|
|
|
|
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 15:40:30 +01:00
|
|
|
|
|
|
|
|
const { iccid } = req.body
|
2026-02-10 15:57:03 +01:00
|
|
|
const compañia = companyFromIccid(iccid)
|
2026-02-03 15:40:30 +01:00
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-01-14 10:19:49 +01:00
|
|
|
}
|
2026-01-13 15:41:59 +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-01-14 10:19:49 +01:00
|
|
|
|
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-01-14 10:19:49 +01:00
|
|
|
|
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-01-14 10:19:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-01-14 10:19:49 +01:00
|
|
|
|
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-01-14 10:19:49 +01:00
|
|
|
|
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"
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-01-14 10:19:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-13 15:41:59 +01:00
|
|
|
}
|