admin/modules/setting.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 commonSQL = require('../../common/sql').default
const mysqclass = require('./mysqli').default
/**
 * @class class to handle settings functions
 */
class adminSettingModule {
    /**
     * All Configurations
     * @param {object} req request data
     * @param {string} count count for the pagination
     * @returns {object} sql response
     */
    static async allConfiguration(req, count) {
        const mysql = {}
        req.body.action = typeof req.body.action === 'undefined' ? 'email' : req.body.action
        mysql.where = ''
        const { action } = req.body
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
        mysql.order = 'order by id desc'
        const order = req.body.order === '' ? 'asc' : req.body.order
        const orderby = req.body.orderby === '' && !req.body.orderby ? '' : req.body.orderby
        if (orderby !== '') {
            mysql.order = ` order by ${orderby} ${order}`
        }
        if (req.body.searchterm !== '' && req.body.searchterm !== undefined) {
            let changed = req.body.searchterm.replace(/\\/g, '\\\\\\\\')
            changed = changed.replace(/"/g, '\\"')
            mysql.where += `and (name like "%${changed}%" or id like "%${changed}%")`
        }

        let row = ''
        const pagen = (req.body.page - 1) * req.body.limit
        const limitf = ` limit ${pagen},${req.body.limit}`
        mysql.limit = limitf
        mysql.dateNow = dateNow
        const escapeData = [action]
        if (count === 1) {
            row = 'get_all_setting_configuration_limit'
        } else {
            row = 'get_all_setting_configuration'
        }
        const strQuery = await mysqclass.mysqli(mysql, row)
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * All Single Configuration
     * @param {number} id ID which the configuration has to be fetched
     * @returns {object} sql response
     */
    static async getConfigurationbyId(id) {
        const mysql = {}
        const escapeData = [id]
        const strQuery = await mysqclass.mysqli(mysql, 'get_single_setting_configuration')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * All Single Configuration
     * @param {object} req req object
     * @returns {object} sql response
     */

    static async configurationOperation(req) {
        const mysql = {}
        const postData = req.body
        const acceptedObjects = ['value', 'variable', 'question', 'enabled', 'type']
        let escapeData = []
        let row = ''
        if (req.body.id) {
            const defaultKeys = []
            const defaultValues = []
            const valueInsert = commonSQL.updateSQLFunction(
                postData,
                acceptedObjects,
                defaultKeys,
                defaultValues,
            )
            mysql.keys = valueInsert.keys
            escapeData = valueInsert.escapeData
            row = 'update_on_setting_configuration'
            mysql.static_page_id = req.body.id
        } else {
            const defaultKeys = []
            const defaultValues = []
            const valueInsert = commonSQL.InsertSQLFunction(
                postData,
                acceptedObjects,
                defaultKeys,
                defaultValues,
            )
            mysql.keys = valueInsert.keys
            mysql.values = valueInsert.values
            escapeData = valueInsert.escapeData
            row = 'insert_into_setting_configuration'
        }
        const strQuery = await mysqclass.mysqli(mysql, row)
        const data = await global.mysql.query(strQuery, escapeData)
        return data
    }

    /**
     * Get All Bid Increment Values
     * @param {object} req request data
     * @param {string} count count for the pagination
     * @returns {object} sql response
     */
    static async getAllBidIncrements(req, count) {
        const mysql = {}
        mysql.where = ''
        mysql.order = 'order by id asc'
        const order = req.body.order === '' ? 'asc' : req.body.order
        const orderby = req.body.orderby === '' && !req.body.orderby ? '' : req.body.orderby
        if (orderby !== '') {
            mysql.order = ` order by ${orderby} ${order}`
        }
        let row = ''
        const pagen = (req.body.page - 1) * req.body.limit
        const limitf = ` limit ${pagen},${req.body.limit}`
        mysql.limit = limitf
        const escapeData = []
        if (count === 1) {
            row = 'get_all_bid_increments_limit'
        } else {
            row = 'get_all_bid_increments'
        }
        const strQuery = await mysqclass.mysqli(mysql, row)
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Get Single Bid Increment
     * @param {number} id ID in bid_increment table
     * @returns {object} sql response
     */
    static async getBidIncrementById(id) {
        const mysql = {}
        const escapeData = [id]
        const strQuery = await mysqclass.mysqli(mysql, 'get_single_bid_increment')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Bid Increment Change Operation
     * @param {object} req req object
     * @returns {object} sql response
     */
    static async bidIncrementOperation(req) {
        const mysql = {}
        const postData = req.body
        const acceptedObjects = ['increment', 'from_amount', 'to_amount']
        let escapeData = []
        let row = ''
        if (req.body.id) {
            const defaultKeys = []
            const defaultValues = []
            const valueInsert = commonSQL.updateSQLFunction(
                postData,
                acceptedObjects,
                defaultKeys,
                defaultValues,
            )
            mysql.keys = valueInsert.keys
            escapeData = valueInsert.escapeData
            row = 'update_bid_increment'
            mysql.id = req.body.id
        } else {
            const defaultKeys = []
            const defaultValues = []
            const valueInsert = commonSQL.InsertSQLFunction(
                postData,
                acceptedObjects,
                defaultKeys,
                defaultValues,
            )
            mysql.keys = valueInsert.keys
            mysql.values = valueInsert.values
            escapeData = valueInsert.escapeData
            row = 'insert_bid_increment'
        }
        const strQuery = await mysqclass.mysqli(mysql, row)
        const data = await global.mysql.query(strQuery, escapeData)
        return data
    }

    /**
     * Delete Bid Increment
     * @param {number} id ID in bid_increment table
     * @returns {object} sql response
     */
    static async deleteBidIncrement(id) {
        const mysql = {}
        const escapeData = [id]
        const strQuery = await mysqclass.mysqli(mysql, 'delete_bid_increment')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Get All buyer premium Values
     * @param {object} req request data
     * @param {string} count count for the pagination
     * @returns {object} sql response
     */
    static async getAllBuyerPremium(req, count) {
        const mysql = {}
        mysql.where = ''
        mysql.order = 'order by id asc'
        const order = req.body.order === '' ? 'asc' : req.body.order
        const orderby = req.body.orderby === '' && !req.body.orderby ? '' : req.body.orderby
        if (orderby !== '') {
            mysql.order = ` order by ${orderby} ${order}`
        }
        let row = ''
        const pagen = (req.body.page - 1) * req.body.limit
        const limitf = ` limit ${pagen},${req.body.limit}`
        mysql.limit = limitf
        const escapeData = []
        if (count === 1) {
            row = 'get_all_buyer_premiums_limit'
        } else {
            row = 'get_all_buyer_premiums'
        }
        const strQuery = await mysqclass.mysqli(mysql, row)
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Get Single Buyer Premium
     * @param {number} id ID in config_buyer_premium table
     * @returns {object} sql response
     */
    static async getBuyerPremiumById(id) {
        const mysql = {}
        const escapeData = [id]
        const strQuery = await mysqclass.mysqli(mysql, 'get_single_buyer_premium')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Buyer Premium Change Operation
     * @param {object} req req object
     * @returns {object} sql response
     */
    static async buyerPremiumOperation(req) {
        const mysql = {}
        const postData = req.body
        const acceptedObjects = ['increment', 'from_amount', 'to_amount']
        let escapeData = []
        let row = ''
        if (req.body.id) {
            const defaultKeys = []
            const defaultValues = []
            const valueInsert = commonSQL.updateSQLFunction(
                postData,
                acceptedObjects,
                defaultKeys,
                defaultValues,
            )
            mysql.keys = valueInsert.keys
            escapeData = valueInsert.escapeData
            row = 'update_buyer_premium'
            mysql.id = req.body.id
        } else {
            const defaultKeys = []
            const defaultValues = []
            const valueInsert = commonSQL.InsertSQLFunction(
                postData,
                acceptedObjects,
                defaultKeys,
                defaultValues,
            )
            mysql.keys = valueInsert.keys
            mysql.values = valueInsert.values
            escapeData = valueInsert.escapeData
            row = 'insert_buyer_premium'
        }
        const strQuery = await mysqclass.mysqli(mysql, row)
        const data = await global.mysql.query(strQuery, escapeData)
        return data
    }

    /**
     * Delete Buyer Premium
     * @param {number} id ID in buyer_premium table
     * @returns {object} sql response
     */
    static async deleteBuyerPremium(id) {
        const mysql = {}
        const escapeData = [id]
        const strQuery = await mysqclass.mysqli(mysql, 'delete_buyer_premiumt')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }
    /**
     * Delete Buyer Premium
     * @param {object} req request data
     * @param {Object} data data to de inseted
     * @returns {object} sql response
     */
    static async insertDataTable(req, data) {
        const mysqli = {}
        let escapeData = []
        const tableUsed = req.body.tableName
        const postData = data.columns
        const acceptedObjects = global.configColumns[tableUsed].array_columns
        const defaultKeys = []
        const defaultValues = []
        /* if(global.configColumns[tableUsed].default_values.length > 0) {
        defaultValues = global.configColumns[tableUsed].default_values
        defaultKeys = global.configColumns[tableUsed].default_columns
      } */
        const valueInsert = commonSQL.InsertSQLFunction(
            postData,
            acceptedObjects,
            defaultKeys,
            defaultValues,
        )
        mysqli.keys = valueInsert.keys
        escapeData = valueInsert.escapeData
        mysqli.values = valueInsert.values
        mysqli.tables = tableUsed
        const strQuery = await mysqclass.mysqli(mysqli, 'insert_tables')
        const dataPromise = await global.mysql.query(strQuery, escapeData)
        return dataPromise
    }
    /**
     * update data table
     * @param {object} req
     * @param {number} id data to be updated
     * @returns {object} sql response
     */
    static async updateDataTable(req, data) {
        const mysqli = {}
        let escapeData = []
        const tableUsed = req.body.tableName
        const postData = data.columns
        const acceptedObjects = global.configColumns[tableUsed].array_columns
        const defaultKeys = []
        const defaultValues = []
        const valueInsert = commonSQL.updateSQLFunction(
            postData,
            acceptedObjects,
            defaultKeys,
            defaultValues,
        )
        mysqli.keys = valueInsert.keys
        escapeData = valueInsert.escapeData
        let whereCond = _.map(req.body.where, (key, val) => {
            return ` \`${val}\` = '${key}' `
        })
        whereCond = whereCond.join(' AND ')

        // mysqli.values = ` id=${req.body.where.id}`
        mysqli.values = whereCond
        mysqli.tables = tableUsed
        console.log(valueInsert)
        const strQuery = await mysqclass.mysqli(mysqli, 'update_tables')
        console.log(strQuery)
        const dataPromise = await global.mysql.query(strQuery, escapeData)
        return dataPromise
    }
}

module.exports.default = adminSettingModule