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-01-13 15:41:59 +01:00
|
|
|
|
2026-01-26 15:04:17 +01:00
|
|
|
// Partiendo del caracter 3 2 de pais + 2 de compañia
|
|
|
|
|
// Metiendolo a la BDD podria ser mas dinamico pero perderia
|
|
|
|
|
// tiempo de query
|
|
|
|
|
// Puede que esté bien crear un endpoint para administrarlo
|
|
|
|
|
const COMPAÑIASICCID = new Map<string, string>(
|
|
|
|
|
[
|
|
|
|
|
["3490", "alai"],
|
2026-01-27 14:48:44 +01:00
|
|
|
["3510", "nos"],
|
2026-01-29 12:57:51 +01:00
|
|
|
["3320", "objenious"]
|
2026-01-26 15:04:17 +01:00
|
|
|
])
|
|
|
|
|
|
2026-01-13 15:41:59 +01:00
|
|
|
export class SimController {
|
|
|
|
|
private simUseCases: SimUsecases
|
|
|
|
|
|
|
|
|
|
constructor(args: {
|
|
|
|
|
simUseCases: SimUsecases
|
|
|
|
|
}) {
|
|
|
|
|
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-03 17:26:12 +01:00
|
|
|
public preactivation() {
|
|
|
|
|
return async (req: Request, res: Response) => {
|
|
|
|
|
const valido = this.validateBody(req.body, res)
|
|
|
|
|
if (valido == false) return;
|
|
|
|
|
|
|
|
|
|
const { iccid } = req.body
|
|
|
|
|
const compañia = this.compañiaFromIccid(iccid)
|
|
|
|
|
|
|
|
|
|
if (compañia == undefined) {
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
errors: {
|
|
|
|
|
msg: "El iccid no pertenece a una compañia conocida"
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return;
|
|
|
|
|
}
|
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) => {
|
|
|
|
|
const valido = this.validateBody(req.body, res)
|
|
|
|
|
|
|
|
|
|
if (valido == false) return; // Si no es valido ya se ha enviado el error
|
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-03 17:26:12 +01:00
|
|
|
const compañia = this.compañiaFromIccid(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) => {
|
|
|
|
|
const valido = this.validateBody(req.body, res)
|
2026-01-14 10:19:49 +01:00
|
|
|
|
2026-02-03 17:26:12 +01:00
|
|
|
if (valido == false) return; // Si no es valido ya se ha enviado el error
|
2026-01-14 10:19:49 +01:00
|
|
|
|
2026-02-03 17:26:12 +01:00
|
|
|
const { iccid } = req.body
|
|
|
|
|
const compañia = this.compañiaFromIccid(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) => {
|
|
|
|
|
const valido = this.validateBody(req.body, res)
|
|
|
|
|
|
|
|
|
|
if (valido == false) return; // Si no es valido ya se ha enviado el error
|
|
|
|
|
|
|
|
|
|
const { iccid } = req.body
|
|
|
|
|
const compañia = this.compañiaFromIccid(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"
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
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) => {
|
|
|
|
|
const valido = this.validateBody(req.body, res)
|
2026-01-14 10:19:49 +01:00
|
|
|
|
2026-02-03 17:26:12 +01:00
|
|
|
if (valido == false) return; // Si no es valido ya se ha enviado el error
|
2026-01-14 10:19:49 +01:00
|
|
|
|
2026-02-03 17:26:12 +01:00
|
|
|
const { iccid } = req.body
|
|
|
|
|
const compañia = this.compañiaFromIccid(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() {
|
2026-01-14 10:19:49 +01:00
|
|
|
|
2026-02-03 17:26:12 +01:00
|
|
|
return async (req: Request, res: Response) => {
|
|
|
|
|
const valido = this.validateBody(req.body, res)
|
2026-01-14 10:19:49 +01:00
|
|
|
|
2026-02-03 17:26:12 +01:00
|
|
|
if (valido == false) return; // Si no es valido ya se ha enviado el error
|
2026-01-14 10:19:49 +01:00
|
|
|
|
2026-02-03 17:26:12 +01:00
|
|
|
const { iccid } = req.body
|
|
|
|
|
const compañia = this.compañiaFromIccid(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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 13:40:29 +01:00
|
|
|
if (valid == false) {
|
|
|
|
|
res.json({
|
|
|
|
|
errors: errors
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-01-13 15:41:59 +01:00
|
|
|
|
2026-01-14 10:19:49 +01:00
|
|
|
return valid;
|
2026-01-13 15:41:59 +01:00
|
|
|
}
|
2026-01-26 15:04:17 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A partir del iccid completo devuelve la compañia a la que pertenece
|
|
|
|
|
* @throws Error si no hay una compañia definida en COMPAÑIASICCID con el codigo
|
|
|
|
|
*/
|
|
|
|
|
private compañiaFromIccid(iccid: string) {
|
|
|
|
|
const caracteresCommpañia = iccid.slice(2, 6)
|
|
|
|
|
const compañia = COMPAÑIASICCID.get(caracteresCommpañia)
|
|
|
|
|
|
|
|
|
|
if (compañia == undefined) throw new Error("El la compañia es desconocida: " + caracteresCommpañia)
|
|
|
|
|
return compañia
|
|
|
|
|
}
|
2026-01-13 15:41:59 +01:00
|
|
|
}
|