256 lines
5.6 KiB
TypeScript
256 lines
5.6 KiB
TypeScript
import { Request, Response } from "express"
|
|
import { SimUsecases } from "./Sim.usecases.js"
|
|
import { activationValidator, iccidValidator } from "./httpValidators.js"
|
|
import { companyFromIccid } from "#domain/companies.js"
|
|
|
|
|
|
export class SimController {
|
|
private simUseCases: SimUsecases
|
|
|
|
constructor(args: {
|
|
simUseCases: SimUsecases
|
|
}) {
|
|
this.simUseCases = args.simUseCases
|
|
|
|
this.activation = this.activation.bind(this)
|
|
}
|
|
|
|
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)
|
|
|
|
if (compañia == undefined) {
|
|
res.status(500).json({
|
|
errors: {
|
|
msg: "El iccid no pertenece a una compañia conocida"
|
|
}
|
|
})
|
|
return;
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
public activation() {
|
|
return async (req: Request, res: Response) => {
|
|
try {
|
|
activationValidator.validate(req.body)
|
|
} catch (e) {
|
|
res.status(422).json({
|
|
errors: {
|
|
msg: e
|
|
}
|
|
})
|
|
}
|
|
|
|
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()
|
|
} catch (err) {
|
|
console.error("Error activando la sim ", req.body)
|
|
res.status(500).json({
|
|
errors: {
|
|
msg: "Error general de activation"
|
|
}
|
|
}).send()
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
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"
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
public pause() {
|
|
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.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() {
|
|
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: "liberacion"
|
|
})
|
|
} catch (err) {
|
|
console.error("Error liberando la sim ", req.body)
|
|
res.status(500).json({
|
|
errors: {
|
|
msg: "Error general de liberacion"
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
|
|
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 activando la sim ", req.body)
|
|
res.status(500).json({
|
|
errors: {
|
|
msg: "Error general de activation"
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
}
|