All files / core/libraries userlibs.js

57.53% Statements 42/73
50% Branches 17/34
51.43% Functions 18/35
57.53% Lines 42/73

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 2182x 2x 2x   2x 2x 2x 2x 2x 2x   2x           21x       8x 8x 4x   4x                         4x     4x                   3x 3x       3x                         3x 1x   2x                   13x 13x 1x   12x           12x 2x   10x                                                                             1x 1x     1x             1x                   1x 1x     1x   1x   1x   2x     1x                                                                                             2x    
const config = require('config');
const moment = require('moment');
const db = require('../../../library/sequelize-cli/models/index');
 
const userDetails = db.user_details;
const userActivation = db.user_activations;
const socialAccount = db.social_accounts;
const applicationInfo = db.application_informations;
const teamSocialAccountJoinTable = db.join_table_teams_social_accounts;
const AuthorizeServices = require('../../../library/utility/authorizeServices');
 
const logger = require('../../utils/logger');
 
 
class UserLibs {
 
    constructor() {
        this.authorizeServices = new AuthorizeServices(config.get('authorize'));
    }
 
    getUserDetails(userId) {
        return new Promise((resolve, reject) => {
            if (!userId) {
                reject({ error: true, message: "Invalid userId" });
            } else {
                return userDetails.findOne({
                    where: {
                        user_id: Number(userId)
                    },
                    attributes: ['user_id', 'email', 'phone_no', 'first_name', 'last_name', 'profile_picture', 'is_account_locked', 'is_admin_user'],
                    include: [{
                        model: userActivation,
                        as: "Activations",
                        where: { id: db.Sequelize.col('user_activation_id') },
                        attributes: ['id', 'last_login', 'user_plan', 'payment_type', 'account_expire_date', 'signup_type', 'activation_status', 'activate_2step_verification', 'email_validate_token', 'forgot_password_validate_token', 'forgot_password_token_expire']
                    }]
                })
                    .then((userDetails) => {
                        Iif (!userDetails)
                            reject({ error: true, message: "User not found!" });
                        else
                            resolve(userDetails);
                    })
                    .catch((error) => {
                        reject(error);
                    });
            }
        });
    }
 
    getUserDetailsByEmail(email) {
        return new Promise((resolve, reject) => {
            Iif (!email) {
                reject({ error: true, message: "Invalid email id." });
            } else {
 
                return userDetails.findOne({
                    where: {
                        email: email
                    },
                    attributes: ['user_id', 'email', 'phone_no', 'first_name', 'last_name', 'profile_picture', 'is_account_locked', 'is_admin_user', 'user_activation_id'],
                    include: [{
                        model: userActivation,
                        as: "Activations",
                        where: { id: db.Sequelize.col('user_activation_id') },
                        attributes: ['id', 'last_login', 'user_plan', 'payment_type', 'account_expire_date', 'signup_type', 'activation_status', 'activate_2step_verification', 'email_validate_token']
                    }]
                })
                    .then((userDetails) => {
                        if (!userDetails)
                            reject({ error: true, message: "Sorry, No such email found!" });
                        else
                            resolve(userDetails);
                    })
                    .catch((error) => {
                        reject(error);
                    });
            }
        });
    }
 
    getPlanDetails(planId) {
        return new Promise((resolve, reject) => {
            if (planId == null && planId == undefined) {
                reject({ error: true, message: 'Invalid data' });
            } else {
                return applicationInfo.findOne({
                    where: {
                        plan_id: planId
                    }
                })
                    .then((result) => {
                        if (!result)
                            reject({ error: true, message: 'Not found' });
                        else {
                            resolve(result);
                        }
                    })
                    .catch((error) => {
                        reject(error);
                    });
            }
        });
    }
 
    getUserAccessToken(userId) {
        return new Promise((resolve, reject) => {
            if (!userId && userId != 0) {
                reject({ message: 'Invalid UserId', error: true });
            } else {
                var userInfo = {};
                return this.updateUserLoginTime(userId)
                    .then(() => {
                        return this.getUserDetails(userId);
                    })
                    .then((userDetails) => {
                        userInfo = userDetails.toJSON();
                        return this.getPlanDetails(userInfo.Activations.user_plan);
                    })
                    .then((planDetails) => {
                        userInfo.userPlanDetails = planDetails.toJSON();
                        return this.authorizeServices.createToken(userInfo);
                    })
                    .then((accessToken) => {
                        resolve({ user: userInfo, accessToken: accessToken });
                    })
                    .catch((error) => {
                        reject(error);
                    });
            }
        });
    }
 
    getUserSocialAccounts(userId) {
        return new Promise((resolve, reject) => {
            Iif (!userId && userId != 0) {
                reject({ message: 'Invalid UserId', error: true });
            } else {
                return socialAccount.findAll({
                    where: {
                        account_admin_id: userId
                    },
                    attributes: ['account_id']
                })
                    .then((accounts) => {
                        resolve(accounts);
                    })
                    .catch((error) => {
                        reject(error);
                    });
            }
        });
    }
 
    lockUserSocialAccounts(userId) {
        return new Promise((resolve, reject) => {
            Iif (!userId && userId != 0) {
                reject({ message: 'Invalid UserId', error: true });
            } else {
                return this.getUserSocialAccounts(userId)
                    .then((accounts) => {
                        Iif (accounts.length <= 0)
                            return;
                        return teamSocialAccountJoinTable.update({
                            is_account_locked: 1
                        }, { where: { account_id: accounts.map(t => t.account_id) } });
                    })
                    .then(() => {
                        resolve({ success: true });
                    })
                    .catch((error) => {
                        reject(error);
                    });
            }
        });
 
 
    }
 
    updateUserLoginTime(userId) {
        return new Promise((resolve, reject) => {
            if (!userId && userId != 0) {
                reject({ message: 'Invalid UserId', error: true });
            } else {
                return userDetails.findOne({
                    where: {
                        user_id: userId
                    },
                    include: [{
                        model: userActivation,
                        as: "Activations",
                        where: { id: db.Sequelize.col('user_activation_id') },
                        attributes: ['id', 'last_login']
                    }]
                })
                    .then((user) => {
                        if (!user) {
                            reject({ message: 'Not Found', error: true });
                        } else {
                            return user.Activations.update({
                                last_login: moment()
                            });
                        }
                    })
                    .then(() => {
                        resolve({ status: 'success' });
                    })
                    .catch((error) => {
                        reject(error);
                    });
            }
        });
    }
}
 
module.exports = UserLibs;