import { NextFunction, Request, RequestHandler } from 'express';
import { BadRequestError } from '../../business/errors/badRequestError';
import { ErrorCodes } from '../../business/errors/errorCodes';
import { HumandResponse } from '../types/response';

export const checkExcelMimeTypes = (req: Request, _res: HumandResponse, next: NextFunction): RequestHandler | void => {
    const { file } = req;
    if (!file || file.size === 0) return next(new BadRequestError('The provided file cannot be empty', ErrorCodes.INVALID_DATA));
    if (!new RegExp('application\\/(vnd.ms-excel|vnd.openxmlformats-officedocument.spreadsheetml.sheet)').test(file.mimetype)) {
        return next(new BadRequestError('The provided file mimetype is not accepted', ErrorCodes.INVALID_DATA));
    }
    return next();
};
