All files / src/routes/root_routes forgetPassword_sendVerificationCode_post.js

69.56% Statements 16/23
50% Branches 4/8
50% Functions 1/2
75% Lines 15/20

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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38              1x 1x 1x 1x   1x 1x   1x 1x 1x 1x 1x   1x     1x   1x 1x                    
import SystemController from "../../controllers/system-controller/SystemController.js";
import UsersModel from "../../models/Users.js";
import ErrorGenerator from "../../utilities/ErrorGenerator.js";
import genRandomNumber from "../../utilities/genRandomNumber.js";
 
 
export default async function forgetPassword_sendVerificationCode_post(req, res, next) {
    const failResponse = (message) => { next(new ErrorGenerator(message, 400)) };
    try {
        const { userEmail } = req.body;
        Iif (!userEmail) return failResponse("Failed to access the email from the server !");
 
        const projection = { signingMethod: 1, userEmail: 1, userName: 1 };
        const user = await UsersModel.findOne({ userEmail }, projection);
 
        if (user) {
            const { signingMethod, userEmail, userName } = user;
            if (signingMethod == "Email & Password") {
                const verificationCode = genRandomNumber(6);
                const subject = "AM Store Changing Password Request";
                const body =
                    `Hi ${userName}, Here is your verification code '${verificationCode}' ` +
                    "to change your password that you forgot it, copy it and go back to 'forget-password' page"
                    + " on AM Store and paste the code there to continue changing your password";
                const mailContent = { userEmail, userName, subject, body, verificationCode };
 
                const response = await SystemController.sendVerificationCodeToEmail(mailContent);
                if (response) res.status(200).json(true);
                else EfailResponse("Sending cheking code to your email failed for unknown reason.");
            }
            else EfailResponse("This email didn't signed up using (Email & Password) method.");
        }
        else EfailResponse("This email didn't signed in our store before.");
    } catch (error) {
        console.log(error)
        failResponse("Error happened while sending cheking code to your email.");
    }
}