111 lines
3.0 KiB
TypeScript
111 lines
3.0 KiB
TypeScript
import { BodyValidator } from "sim-shared/aplication/BodyValidator.js"
|
|
import { OrderUsecases } from "./Order.usecases.js"
|
|
import { Request, Response } from "express"
|
|
import { PaginationArgs } from "#domain/common.js"
|
|
|
|
export class OrderController {
|
|
private orderUseCases: OrderUsecases
|
|
|
|
constructor(args: {
|
|
orderUseCases: OrderUsecases
|
|
}) {
|
|
this.orderUseCases = args.orderUseCases
|
|
}
|
|
|
|
public getById(args: { id: number }) {
|
|
return this.controllerGenerator<{ id: number }, { id: number }>({
|
|
validator: undefined,
|
|
useCase: this.orderUseCases.getById(args),
|
|
onError: (data, error) => { console.error(error) },
|
|
onSuccess: (data) => console.log(data)
|
|
})
|
|
}
|
|
|
|
public getPending(args: PaginationArgs) {
|
|
return this.controllerGenerator<{ id: number }, { id: number }>({
|
|
validator: undefined,
|
|
useCase: this.orderUseCases.getPending(args),
|
|
onError: (data, error) => { console.error(error) },
|
|
onSuccess: (data) => console.log(data)
|
|
})
|
|
}
|
|
|
|
public getByQueueId(args: { message_id: string }) {
|
|
return this.controllerGenerator({
|
|
validator: undefined,
|
|
useCase: this.orderUseCases.getByQueueId(args),
|
|
onError: (data, error) => { console.error(error) },
|
|
onSuccess: (data) => console.log(data)
|
|
})
|
|
}
|
|
|
|
|
|
/**
|
|
* TODO:
|
|
* - En proceso de validacion, tiene varios problemas
|
|
* - Está copiado, planteado inyectarlo
|
|
*
|
|
* Abstrae el proceso de
|
|
* Peticion -> validacion del body -> map del body -> useCase -> OK/ERR
|
|
*
|
|
* <O> Representa el dato original
|
|
* <P> Representa el dato después del mapeo
|
|
*/
|
|
public controllerGenerator<O extends Object, P extends Object>(args: {
|
|
validator?: BodyValidator<O>,
|
|
mapBody?: (body: O) => P,
|
|
useCase: (args: P) => Promise<any>,
|
|
onError: (args: O | P, error: string) => void,
|
|
onSuccess: (args: P) => void,
|
|
}) {
|
|
return async (req: Request, res: Response) => {
|
|
const body = req.body
|
|
|
|
// 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. Se devuelve al usuario el caso de exito
|
|
res.status(200).json(
|
|
usecaseResult
|
|
).send()
|
|
args.onSuccess(data)
|
|
} catch (err) {
|
|
// 4.1 Error del caso de uso
|
|
res.status(500).json({
|
|
errors: {
|
|
msg: "Error general:" + err
|
|
}
|
|
}).send()
|
|
return;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|