All files / src/routes/root_routes emailVerification_get.js

85.71% Statements 12/14
62.5% Branches 5/8
100% Functions 1/1
85.71% Lines 12/14

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                  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 asyncRouteHandler from "../../utilities/asyncRouteHandler.js";
import emailVerificationHtmlTemplate from "../../utilities/emailVerificationHtmlTemplate.js";
import genRandomNumber from '../../utilities/genRandomNumber.js';
 
 
export default asyncRouteHandler(
    async function emailVerification_get(req, res) {
        const user = await UsersModel.findById(req.userId);
        if (user) {
            const { userEmail, userName, hisEmailVerified } = user;
 
            Iif (hisEmailVerified) {
                res.status(200).json({ ok: false, message: "Your email is already verifyed!" });
            } else {
                const verificationCode = genRandomNumber(6);
                const subject = "AM Store Email Verification";
                const body =
                    `Wellcome to AM Store ${userName}, Here is your verification code '${verificationCode}', `
                    + "copy it and go back to verification page on AM Store and paste the code there"
                const htmlTemplate = emailVerificationHtmlTemplate({ userName }, verificationCode);
                const mailContent = { userEmail, subject, body, htmlTemplate, verificationCode }
 
                const response = await SystemController.sendVerificationCodeToEmail(mailContent);
                response && res.status(200).json({ ok: true });
                !response && res.status(400).json();
            }
        } else Eres.status(400).json();
    }
)