import { BodyValidator } from "sim-shared/aplication/BodyValidator.js" import { OrderUsecases } from "./Order.usecases.js" import { Request, Response } from "express" import { idValidator, uuidValidator } from "./httpValidators.js" import { PaginationArgs } from "sim-shared/domain/PaginationArgs.js" export class OrderController { private orderUseCases: OrderUsecases constructor(args: { orderUseCases: OrderUsecases }) { this.orderUseCases = args.orderUseCases } public getById() { return this.controllerGenerator<{ id: number }, { id: number }>({ validator: idValidator, useCase: this.orderUseCases.getById(), onError: (data, error) => { console.error(error) }, onSuccess: (data) => console.log(data) }) } public getPending() { return this.controllerGenerator({ validator: undefined, useCase: this.orderUseCases.getPending(), onError: (data, error) => { console.error(error) }, onSuccess: (data) => console.log(data) }) } public getByQueueId() { return this.controllerGenerator<{ correlation_id: string }, { correlation_id: string }>({ validator: uuidValidator, useCase: this.orderUseCases.getByQueueId(), onError: (data, error) => { console.error(error) }, onSuccess: (data) => console.log(data) }) } /** * TODO: * - En proceso de validacion, tiene varios problemas * - Está copiado, planteado inyectarlo * - Map para la respuesta? * * Abstrae el proceso de * Peticion -> validacion del body -> map del body -> useCase -> OK/ERR * * Representa el dato original *

Representa el dato después del mapeo */ public controllerGenerator(args: { validator?: BodyValidator, mapBody?: (body: O) => P, useCase: (args: P) => Promise, onError: (args: O | P, error: string) => void, onSuccess: (args: P) => void, }) { return async (req: Request, res: Response) => { //scketchy const body = { ...req.body, ...req.params } // 1. Validacion del body try { if (args.validator != undefined) args.validator.validate(body) } catch (e) { if (args.onError != undefined) args.onError(body, e as string) res.status(422).json({ errors: { msg: e } }) } // 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 } }) } // 3. Aplicacion del UseCase try { const usecaseResult = await args.useCase(data) // 4.1 Se devuelve el caso de exito pero no encontrado if (usecaseResult.data == undefined && usecaseResult.error == undefined) { res.status(404).json(usecaseResult).send() args.onSuccess(data) return; } // 4.2 Caso de error controlado desde el caso de uso if (usecaseResult.error != undefined) { res.status(500).json(usecaseResult).send() return; } // 4.2 Se devuelve al usuario el caso de exito de encontrado res.status(200).json( usecaseResult ).send() args.onSuccess(data) } catch (err) { // 4.3 Error del caso de uso res.status(500).json({ errors: { msg: "Error general:" + err } }).send() return; } } } }