admin/modules/communcation.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 communication functions
 */
class adminCommuncationModule {
    /**
     * Get Single Template
     * @param {number} id Template ID which has to be fetched
     * @returns {object} sql response
     */
    static async etempsbyId(id) {
        const mysql = {}
        const escapeData = [id]
        const strQuery = await mysqclass.mysqli(mysql, 'get_single_comm_template')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * All Communication Templates
     * @param {object} req request data
     * @param {string} count count for the pagination
     * @returns {object} sql response
     */
    static async alltemps(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 (title 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_comm_templates_limit'
        } else {
            row = 'get_all_comm_templates'
        }
        const strQuery = await mysqclass.mysqli(mysql, row)
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        // console.log('dataReturn', strQuery)
        // console.log('dataReturn', dataReturn)
        return dataReturn
    }

    /**
     * All Email and SMS logs
     * @param {object} req request data
     * @param {string} count count for the pagination
     * @returns {object} sql response
     */
    static async alllogsemails(req, count) {
        const mysql = {}
        let limit = ''
        mysql.where = ''
        mysql.order = 'ORDER BY lg.id DESC'
        if (count === 0) {
            const pagen = (req.body.page - 1) * req.body.limit
            limit = `limit ${pagen},${req.body.limit}`
        } else {
            limit = ''
        }
        if (typeof req.body.searchterm !== 'undefined' && req.body.searchterm !== '') {
            mysql.where += ` and (lg.subject like "%${req.body.searchterm}%")`
        }
        if (typeof req.body.emailsearch !== 'undefined' && req.body.emailsearch !== '') {
            mysql.where += ` and (lg.email like "%${req.body.emailsearch}%")`
        }

        if (typeof req.body.phonesearch !== 'undefined' && req.body.phonesearch !== '') {
            mysql.where += ` and (lg.phone like "%${req.body.phonesearch}%")`
        }

        const sortype = req.query.stype === '' ? 'asc' : req.query.stype
        if (typeof req.query.sname !== 'undefined' && req.query.sname !== '') {
            if (req.query.sname === 'sortdate') {
                mysql.order = ` order by lg.date_sent ${sortype}`
            }
        }

        mysql.limit = limit
        let query
        if (count === 1) {
            if (req.body.action === 'smsLogs') {
                query = 'get_all_sms_logs'
            } else {
                query = 'get_all_email_logs'
            }
        } else if (req.body.action === 'smsLogs') {
            query = 'get_sms_logs_page'
        } else {
            query = 'get_email_logs_page'
        }
        const escapeData = []
        const strQuery = await mysqclass.mysqli(mysql, query)
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Complete a template action
     * @param {object} req request data
     * @returns {object} sql response
     */
    static async templateOperation(req) {
        const mysql = {}
        const postData = req.body
        req.body.type = 'global'
        req.body.status = 1
        req.body.buyer = 1
        const acceptedObjects = [
            'title',
            'subject',
            'template',
            'type',
            'method',
            'status',
            'buyer',
        ]
        let escapeData = []
        let row = ''
        if (req.body.id) {
            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
            row = 'update_on_templates'
            mysql.template_id = req.body.id
        } else {
            const defaultKeys = ['created_at']
            const defaultValues = [dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')]
            const valueInsert = commonSQL.InsertSQLFunction(
                postData,
                acceptedObjects,
                defaultKeys,
                defaultValues,
            )
            mysql.keys = valueInsert.keys
            mysql.values = valueInsert.values
            escapeData = valueInsert.escapeData
            row = 'insert_into_templates'
        }
        const strQuery = await mysqclass.mysqli(mysql, row)
        const data = await global.mysql.query(strQuery, escapeData)
        // console.log('strQuery', strQuery, escapeData)
        return data
    }

    /**
     * Get one static page content
     * @param {number} id static page id which details has to be fetched
     * @returns {object} sql response
     */
    static async getStaticPagebyId(id) {
        const mysql = {}
        const escapeData = [id, id]
        const strQuery = await mysqclass.mysqli(mysql, 'get_single_static_page')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * All Static page contents
     * @param {object} req request data
     * @param {string} count count for the pagination
     * @returns {object} sql response
     */
    static async allStaticPage(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_static_page_limit'
        } else {
            row = 'get_all_static_page'
        }
        const strQuery = await mysqclass.mysqli(mysql, row)
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }
}

module.exports.default = adminCommuncationModule