Base de los controladores y casos de uso

This commit is contained in:
2026-01-14 10:19:49 +01:00
parent f8164ac38c
commit 42cef3f9ba
7 changed files with 159 additions and 137 deletions

View File

@@ -28,15 +28,36 @@ export class SimUsecases {
return this.eventBus.publish([activationEvent])
}
cancelation(args: { iccid: string }) {
throw new Error("Function not implemented.");
async cancelation(args: { iccid: string }) {
const cancelationEvent = <SimEvents.cancelation>{
key: "sim.cancelation",
payload: {
iccid: args.iccid
}
}
return this.eventBus.publish([cancelationEvent])
}
pause(args: { iccid: string }) {
throw new Error("Function not implemented.");
async pause(args: { iccid: string }) {
const cancelationEvent = <SimEvents.pause>{
key: "sim.pause",
payload: {
iccid: args.iccid
}
}
return this.eventBus.publish([cancelationEvent])
}
free(args: { iccid: string }) {
throw new Error("Function not implemented.");
async free(args: { iccid: string }) {
const cancelationEvent = <SimEvents.free>{
key: "sim.free",
payload: {
iccid: args.iccid
}
}
return this.eventBus.publish([cancelationEvent])
}
}

View File

@@ -11,27 +11,147 @@ export class SimController {
}
async activation(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
if (iccid == undefined) {
// TODO: excepcion con nombre se va a repetir
res.status(400).json({
msg: "iccid invalido"
try {
await this.simUseCases.activation({ iccid })
} catch (err) {
console.error("Error activando la sim ", req.body)
res.status(500).json({
errors: {
msg: "Error general de activation"
}
})
}
const resp = await this.simUseCases.activation({ iccid })
res.status(200).json({
iccid: iccid,
operation: "activation"
})
}
cancelation(req: Request, res: Response) {
async cancelation(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
try {
await this.simUseCases.cancelation({ iccid })
} catch (err) {
console.error("Error cancelando la sim ", req.body)
res.status(500).json({
errors: {
msg: "Error general de cancelacion"
}
})
}
res.status(200).json({
iccid: iccid,
operation: "cancelation"
})
}
pause(req: Request, res: Response) {
async pause(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
try {
await this.simUseCases.cancelation({ iccid })
} catch (err) {
console.error("Error pausando la sim ", req.body)
res.status(500).json({
errors: {
msg: "Error pausando la sim"
}
})
}
res.status(200).json({
iccid: iccid,
operation: "cancelation"
})
}
free(req: Request, res: Response) {
async free(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
try {
await this.simUseCases.cancelation({ iccid })
} catch (err) {
console.error("Error liberando la sim ", req.body)
res.status(500).json({
errors: {
msg: "Error general de liberacion"
}
})
}
res.status(200).json({
iccid: iccid,
operation: "liberacion"
})
}
async save(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
try {
await this.simUseCases.cancelation({ iccid })
} catch (err) {
console.error("Error activando la sim ", req.body)
res.status(500).json({
errors: {
msg: "Error general de activation"
}
})
}
res.status(200).json({
iccid: iccid,
operation: "cancelation"
})
}
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
}
res.json({
errors: errors
})
return valid;
}
}

View File

@@ -15,20 +15,15 @@ const simController = new SimController({
simRoutes.get("/status", () => { })
simRoutes.post("/save", (req, res) => {
})
simRoutes.post("/save", simController.save)
simRoutes.post("/activate", simController.activation)
simRoutes.post("/pause", (req, res) => {
})
simRoutes.post("/pause", simController.pause)
simRoutes.post("/cancel", (req, res) => {
})
simRoutes.post("/cancel", simController.cancelation)
// Proceso especifico de ALAI para liberar sims canceladas
simRoutes.post("/free", (req, res) => {
})
simRoutes.post("/free", simController.free)
export { simRoutes }