admin/modules/auction.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 moment = require('moment')
const commonSQL = require('../../common/sql').default

const mysqclass = require('./mysqli').default
/**
 * @class class to handle auction functions
 */
class adminAuctionModule {
    /**
     * fetch all auctions
     * @param {object} req request data
     * @param {string} count count for the pagination
     * @returns {object} sql response
     */
    static async fetchAuctionsAll(req, count) {
        const mysql = {}
        mysql.where = ''
        const { action } = req.body
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
        if (action === 'all') {
            mysql.where += ` and a.market_status != "removed"`
            mysql.where += ''
        } else {
            mysql.where += ` and a.market_status = "${action}"`
        }

        mysql.order = 'order by a.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 (a.title like "%${changed}%" or a.id like "%${changed}%")`
        }
        const dateSortValue = req.body.fielddate ? req.body.fielddate : 'a.date_closed'
        if (req.body.fromdate !== '' && req.body.fromdate !== undefined) {
            const date1 = dateFormat(
                new Date(
                    moment(req.body.fromdate, global.configurations.variables.time_format).format(),
                ),
                'yyyy-mm-dd',
            )
            mysql.where += ` and ${dateSortValue} >= "${date1} 00:00:00"`
        }
        if (req.body.todate !== '' && req.body.todate !== undefined) {
            const date2 = dateFormat(
                new Date(
                    moment(req.body.todate, global.configurations.variables.time_format).format(),
                ),
                'yyyy-mm-dd',
            )
            mysql.where += ` and ${dateSortValue} <= "${date2} 23:59:59"`
        }

        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 = []
        if (count === 1) {
            row = 'get_all_auctions_limit'
        } else {
            row = 'get_all_auctions'
        }
        const strQuery = await mysqclass.mysqli(mysql, row)
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * fetch Active Auctions
     * @returns {object} sql response
     */
    static async fetchActiveAuctions() {
        const mysql = {}
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
        const escapeData = [dateNow, dateNow]
        const strQuery = await mysqclass.mysqli(mysql, 'get_all_auct_active')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Do a Auction Action
     * @param {object} req request data
     * @returns {object} sql response
     */
    static async auctionOperation(req) {
        const mysql = {}
        const postData = req.body
        console.log('req.body', req.body)
        const acceptedObjects = [
            'title',
            'description',
            'date_added',
            'date_closed',
            'avatar',
            'ending_enable',
            'ending_items',
            'ending_mins',
        ]
        if (req.body.date_added) {
            req.body.date_added = commonSQL.dateTimeFormatConvert(req.body.date_added)
        }
        if (req.body.date_closed) {
            req.body.date_closed = commonSQL.dateTimeFormatConvert(req.body.date_closed)
        }
        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_auction'
            mysql.auction_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
            escapeData = valueInsert.escapeData
            mysql.values = valueInsert.values
            row = 'insert_into_auction'
        }
        const strQuery = await mysqclass.mysqli(mysql, row)
        console.log('strQuery', strQuery, escapeData)
        const data = await global.mysql.query(strQuery, escapeData)

        return data
    }

    /**
     * fetch one Auction By ID
     * @param {object} pid auction ID to which the data is to be fetched
     * @returns {object} sql response
     */
    static async fetchAuctionbyID(pid) {
        const mysql = {}
        const escapeData = [pid]
        const strQuery = await mysqclass.mysqli(mysql, 'get_single_auctions')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * empty the projects which has the auction ID
     * @param {object} auctID auction ID to which the data has to be cleared
     * @returns {object} sql response
     */
    static async emptyOutAuctionID(auctID) {
        const mysql = {}
        const escapeData = [auctID]
        const strQuery = await mysqclass.mysqli(mysql, 'update_auct_id_null')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        console.log(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Update the Projects with the acution ID
     * @param {object} req request data
     * @returns {object} sql response
     */
    static async auctionProjectUpdateexist(req) {
        const mysql = {}
        let projectId = []
        if (req.body.project_id instanceof Array) {
            projectId = req.body.project_id
        } else {
            projectId = req.body.project_id.split(',')
        }
        const escapeData = [req.body.auction_id, projectId]
        const strQuery = await mysqclass.mysqli(mysql, 'auct_project_update')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        console.log(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Get All Sub lots for the auction
     * @param {number} id auction ID for which the lots is to be fetched
     * @returns {object} sql response
     */
    static async getAllSubLots(id) {
        const mysql = {}
        const escapeData = [id]
        const strQuery = await mysqclass.mysqli(mysql, 'getAllSubLots')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Update the lot information for the projects
     * @param {number} id auction ID for which the lots is to be updated
     * @param {number} date close date which has to be updated for the projects
     * @returns {object} sql response
     */
    static async updatelofdetails(id, date) {
        const mysql = {}
        const closeData = commonSQL.dateTimeConvert(date)
        const escapeData = [closeData, id]
        const strQuery = await mysqclass.mysqli(mysql, 'updatelotnew')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Update the close dates for the projects
     * @param {string} dateAdded date added field which has to be updated
     * @param {string} dateClosed close date which has to be updated for the projects
     * @param {number} id auction ID for which the lots is to be updated
     * @returns {object} sql response
     */
    static async updateAllCloseDates(dateAdded, dateClosed, id) {
        const mysql = {}
        const escapeData = [dateAdded, dateClosed, id]
        const strQuery = await mysqclass.mysqli(mysql, 'update_all_auct_closedate')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Get All associated auctions
     * @param {number} pid auction ID for which the lots is to be updated
     * @returns {object} sql response
     */
    static async getAllAssociatedAuctions(pid) {
        const mysql = {}
        const escapeData = [pid || 0]
        const strQuery = await mysqclass.mysqli(mysql, 'get_all_auction_projects')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Update Auction with the status
     * @param {number} status Status which has to be updated
     * @param {number} auctID auction ID for which the lots is to be updated
     * @returns {object} sql response
     */
    static async auctionStatusUpdate(status, auctID) {
        const mysql = {}
        const escapeData = [status, auctID]
        const strQuery = await mysqclass.mysqli(mysql, 'auction_change_status')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        console.log('dataReturn', strQuery, escapeData)
        return dataReturn
    }
}

module.exports.default = adminAuctionModule