All files / src/controllers/users-controllers changePassword.js

63.63% Statements 7/11
50% Branches 2/4
100% Functions 1/1
63.63% Lines 7/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23            2x 2x 2x   2x 2x 2x 2x                  
import UsersModel from "../../models/Users.js";
import { compareSync } from "bcrypt";
import isUserAllowedToChangeHisPassword from "./isUserAllowedToChangeHisPassword.js";
import SystemController from "../system-controller/SystemController.js";
 
export default async function changePassword(userId, { currentPassword, newPassword }) {
    try {
        const projection = { userPassword: 1, lastPasswordChange: 1 };
        const { userPassword, lastPasswordChange } = await UsersModel.findById(userId, projection);
 
        if (isUserAllowedToChangeHisPassword(lastPasswordChange)) {
            const result = compareSync(currentPassword, userPassword);
            if (result) {
                return await SystemController.changeUserPassword({ userId }, newPassword)
            }
            else Ereturn false;
        }
        else Ereturn null
    } catch (error) {
        console.log(error);
        return;
    }
}