admin/modules/admin.js

/* ============================================================================ *\
|| ########################################################################## ||
|| # Auction Software Marketplace          Release: 0.6   Build 0.7         # ||
|| # ---------------------------------------------------------------------- # ||
|| # License # 35YAHCNR9344X6O666C123AB                                     # ||
|| # ---------------------------------------------------------------------- # ||
|| # Copyright ©2014–2021 Develop Scripts LLC. All Rights Reserved          # ||
|| # This file may not be redistributed in whole or significant part.       # ||
|| # ------------- AUCTION SOFTWARE IS NOT FREE SOFTWARE ------------------ # ||
|| # http://www.auctionsoftwaremarketplace.com|support@auctionsoftware.com  # ||
|| # ---------------------------------------------------------------------- # ||
|| ########################################################################## ||
\* ============================================================================ */

const dateFormat = require('dateformat')
const md5 = require('md5')
const _ = require('underscore')

const mysqclass = require('./mysqli').default
const commonSQL = require('../../common/sql').default
const commonProduct = require('../../common/products').default
/**
 * @class class to handle admin functions
 */
class adminModule {
    /**
     * @param {string} nameID email ID to check in the database.
     * @returns {object} sql response
     */
    static async checkEmailExisting(nameID) {
        const mysql = {}
        const escapeData = [nameID]
        const strQuery = await mysqclass.mysqli(mysql, 'okta_im_2')
        const data = await global.mysql.query(strQuery, escapeData)
        return data
    }

    /**
     * Login Process function. Checking the password and salt
     * @param {object} req request data
     * @param {string} data data is the req.body
     * @returns {object} sql response
     */
    static async process(req, callback) {
        const userData = req.body.email.toLowerCase()
        const mysql = {
            username: userData,
        }
        const strQuery = await mysqclass.mysqli(mysql, 'im_2')
        global.mysql.query(strQuery, (error, results, fields) => {
            if (error) {
                callback('')
            }
            if (results.length > 0) {
                const result = results[0]
                result.password = md5(md5(req.body.password) + result.password_salt)
                if (result.password === result.password_hash) {
                    callback(results)
                } else {
                    callback('')
                }
            } else {
                callback('')
            }
        })
    }

    /**
     * Update Last Login date for the admin
     * @param {object} req request data
     * @param {string} data data is the req.body
     * @returns {object} sql response
     */
    static async updateLastLogin(uid) {
        const mysql = {}
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
        const escapeData = [dateNow, uid]
        const strQuery = await mysqclass.mysqli(mysql, 'im_3')
        const data = await global.mysql.query(strQuery, escapeData)
        return data
    }

    /**
     * Add a entry in login table to log admin login
     * @param {object} req request data
     * @param {string} data data is the req.body
     * @returns {object} sql response
     */
    static async userViews(req, uid) {
        const mysql = {}
        const userIp = typeof req.headers.ipaddress === 'undefined' ? '' : req.headers.ipaddress
        const userHeader = req.headers['user-agent']
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss.l')
        const escapeData = [uid, dateNow, userHeader, userIp]
        const strQuery = await mysqclass.mysqli(mysql, 'im_4')
        const data = await global.mysql.query(strQuery, escapeData)
        return data
    }

    /**
     * get adminDetails to verify the token
     * @param {string} id id for the admin which is to be fetched
     * @returns {object} sql response
     */
    static async userDetails(id) {
        const mysql = {}
        const baseTableUsed = global.configColumns.employees
        const customTableUsed = global.configColumns.custom_employees
        const generatedData = commonProduct.generateJoinWithColum(baseTableUsed, customTableUsed, [
            'id',
        ])
        mysql.baseTableName = baseTableUsed.ext_name
        mysql.baseTableShort = baseTableUsed.short_name
        mysql.customTableJoin = generatedData.customTableJoin
        mysql.columns = generatedData.rowstoFetch
        const escapeData = [id]
        const strQuery = await mysqclass.mysqli(mysql, 'im_token')
        const data = await global.mysql.query(strQuery, escapeData)
        return data
    }

    /**
     * update Admin Profile details
     * @param {object} req request data
     * @param {string} data data is the req.body
     * @returns {object} sql response
     */
    static async updateProfile(req) {
        const mysql = {}
        const postData = req.body
        const acceptedObjects = [
            'first_name',
            'last_name',
            'address1',
            'password_hash',
            'phone',
            'city',
            'state',
            'zip',
        ]
        let escapeData = []
        const defaultKeys = ['updated_at']
        const defaultValues = [dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')]
        const valueInsert = commonSQL.updateSQLFunction(
            postData,
            acceptedObjects,
            defaultKeys,
            defaultValues,
        )
        mysql.keys = valueInsert.keys
        escapeData = valueInsert.escapeData
        mysql.user_id = req.user.id
        const strQuery = await mysqclass.mysqli(mysql, 'update_user_profile')
        const data = await global.mysql.query(strQuery, escapeData)
        return data
    }

    /**
     * Check whether admin exists or not
     * @param {string} userEmail email id which the admin exists or not
     * @returns {object} sql response
     */
    static async checkForgotUserExists(userEmail) {
        const mysql = {}
        const escapeData = [userEmail]
        const strQuery = await mysqclass.mysqli(mysql, 'check_forgot_password_user')
        const data = await global.mysql.query(strQuery, escapeData)
        return data
    }

    /**
     * Insert forgot password token
     * @param {object} user admin object which the forgot password is requested
     * @param {string} Token Token which is unique for each forgot password request
     * @returns {object} sql response
     */
    static async inserUserToken(user, Token) {
        const mysql = {}
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
        const escapeData = [user.id, user.email, Token, dateNow]
        const strQuery = await mysqclass.mysqli(mysql, 'insert_forgot_password_token')
        const data = await global.mysql.query(strQuery, escapeData)
        return data
    }

    /**
     * Get the forgot password token
     * @param {string} email admin object which the forgot password is requested
     * @param {string} token Token which is unique for each forgot password request
     * @returns {object} sql response
     */
    static async getForgotUserToken(email, token) {
        const mysql = {}
        const escapeData = [email, token]
        const strQuery = await mysqclass.mysqli(mysql, 'get_forgot_user_token')
        const data = await global.mysql.query(strQuery, escapeData)
        return data
    }
}

module.exports.default = adminModule