import {Users} from "../../models/index";
import userNotFound from '../../utils/customErrors/userNotFound';
import uncorrectPassword from '../../utils/customErrors/uncorrectPassword';

module.exports = async (req, res, next) => {
    const {oldPassword, id, password, confirmPassword} = req.body;
    if(oldPassword) {
        const user = await Users.findOne({
            where: {
                id: id
            }
        });
        if(!user) {
            next(new userNotFound());
        }else {
            if(password && confirmPassword && password == confirmPassword ) {
                req.body.rightPassword = user.password;
                next();
            }else {
                next(new uncorrectPassword());
            }
        }
    }else {
        next();
    }
};