All files / src/controllers/system-controller signUpUser.js

73.33% Statements 11/15
100% Branches 0/0
66.66% Functions 2/3
73.33% Lines 11/15

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            1x 1x 1x 1x 1x 1x   1x 1x 1x 1x                             1x          
import UsersModel from "../../models/Users.js";
import { hashSync } from "bcrypt";
import { generateJWT } from "../../utilities/jwtUtilities.js";
 
export default async function signUpUser(userData) {
 
    try {
        const { HASHING_SALT_ROUNDS } = process.env;
        const newUser = new UsersModel(userData);
        const hashedPassword = hashSync(newUser.userPassword, +HASHING_SALT_ROUNDS);
        newUser.userPassword = hashedPassword;
        const result = await newUser.save()
            .then(() => {
                const userId = newUser._id;
                const token = generateJWT({ userId, role: "user" });
                const { userName, avatar, userEmail, hisEmailVerified } = newUser;
                return {
                    userData: {
                        _id: userId,
                        userEmail,
                        userName,
                        avatar,
                        hisEmailVerified
                    },
                    token
                }
            })
            .catch((err) => {
                console.log(err)
                return;
            })
        return result;
    } catch (error) {
        console.log(error);
        return;
    }
};