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 }

View File

@@ -1,29 +0,0 @@
import express from 'express';
import cors from 'cors';
import { config } from './config';
const PORT = config.port;
const server = express();
// Middleware
server.use(cors());
// Webhooks often require raw body for signature verification if not handled by express.json()
// using verify option in body-parser/express.json to get raw body if needed.
server.use(express.json({
verify: (req: any, res, buf) => {
req.rawBody = buf;
}
}));
server.use(express.urlencoded({ extended: true }));
// Health check
server.get('/health', (req, res) => {
res.status(200).send({ resp: 'OK' });
});
server.listen(PORT, () => {
console.log(`[Server] Server is running on port ${PORT} in ${config.env} mode`);
console.log(`[Server] Webhook endpoint: http://localhost:${PORT}/api/webhooks`);
});

View File

@@ -1,12 +0,0 @@
import dotenv from 'dotenv';
dotenv.config();
export const config = {
port: process.env.PORT || 3000,
env: process.env.NODE_ENV || 'development',
};
export const rabbitConfing = {
}

View File

@@ -1,70 +0,0 @@
/**
* Configuracion de los host iniciales y a que topics escuchan
*/
import { ScheduledEvent, SetupSubscription, SubscriptionData } from "#controllers/subscriptions.types.js"
import { webhooksConfig } from "."
export function generateSubscriptions(initialHosts?: SetupSubscription[]) {
const initialhs = initialHosts || INITIAL_HOSTS
const topicSubscriberMap = new Map<string, SubscriptionData>()
for (const setup of initialhs) {
const topic = setup.topic
if (topicSubscriberMap.get(topic) == undefined) {
topicSubscriberMap.set(topic, {
topic: topic,
subscriptors: []
})
}
topicSubscriberMap.get(topic)!.subscriptors.push({
...setup
})
}
return topicSubscriberMap
}
type InitialHost = {
topic: string,
host: string,
port: string,
endpoint: string,
method: "POST" | "PUT" | "DELETE",
secretkey: string,
}
export const INITIAL_HOSTS: InitialHost[] = [
{
topic: "order/create",
host: "localhost",
port: "3500",
endpoint: "/order/create",
method: "POST",
secretkey: webhooksConfig.apiSecret || "1234",
},
{
topic: "order/create",
host: "localhost",
port: "3000",
endpoint: '/webhooks/shopify/orders',
method: "POST",
secretkey: webhooksConfig.apiSecret || "1234",
},
{
topic: "order/create",
host: "sf-shopify-orders-api",
port: "3000",
endpoint: '/webhooks/shopify/orders',
method: "POST",
secretkey: webhooksConfig.apiSecret || "1234",
}
]
export const INITIAL_EVENTS: ScheduledEvent[] = [
{
topic: "order/create",
numberOfMessages: -1,
periodMs: 5000
}
]

View File

@@ -1,3 +0,0 @@
import rabbit from "rabbitmq-stream-js-client"
const client = await rabbit.connect()