import { ClassConstructor } from 'class-transformer';
import { RequestHandler } from 'express';
import { HumandLocals } from '../interfaces/locals';
import { HumandResponse } from '../types/response';
import { ValidationClass } from '../validationClasses/validationClass';
import { genericValidateParams } from './genericValidateParams';

export const validateBodyParams = <T extends HumandLocals = HumandLocals>(
    modelClass: ClassConstructor<ValidationClass>,
): RequestHandler<any, HumandResponse<T>, any, any, T> =>
    genericValidateParams<ValidationClass, T>(
        (req) => req.body as ValidationClass,
        (res, output) => {
            res.locals.bodyParams = output;
        },
        modelClass,
    );
