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 commonProduct = require('../../common/products').default
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 = {}
        const { action } = req.body
        const baseTableUsed = global.configColumns.auctionlot
        const customTableUsed = global.configColumns.custom_auctionlot

        let row = ''
        let where = ''
        let having = ''
        let limitf = ''
        let order = ''
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
        if (typeof req.body.limit === 'undefined') {
            req.body.limit = global.configurations.variables.search_limit || 25
        }
        if (typeof req.body.page === 'undefined') {
            req.body.page = 1
        }

        order = `order by ac.id desc`
        if (req.body.orderby || req.body.order) {
            const sortReturn = commonProduct.sortOrderData(req, where)
            if (sortReturn.order) {
                order = sortReturn.order
            }
            if (sortReturn.where) {
                where += sortReturn.where
            }
        }

        if (req.body.similars) {
            where += commonProduct.similarData(req)
        }
        if (req.body.filters) {
            where += commonProduct.filterData(req)
        }
        if (req.body.having) {
            having += commonProduct.havingData(req)
        }

        const generatedData = commonProduct.generateJoinWithColum(baseTableUsed, customTableUsed, [
            'id',
        ])

        mysql.customTableJoin = generatedData.customTableJoin
        mysql.mainTableJoin = generatedData.mainTableJoin

        mysql.columns = generatedData.rowstoFetch

        const additionalTable2 = global.configColumns.employees
        const customAdditionalTable2 = { enabled: 0 }

        const additionalGeneratedData2 = commonProduct.additionalTableJoin(
            additionalTable2,
            customAdditionalTable2,
            baseTableUsed,
            'left join',
            'id',
            'user_id',
        )

        mysql.customTableJoin = `${mysql.customTableJoin}  ${additionalGeneratedData2.customTableJoin}`
        mysql.columns = mysql.columns.concat(additionalGeneratedData2.rowstoFetch)

        row = count === 1 ? 'get_all_auctions' : 'get_all_auctions_limit'

        const limitrg = req.body.limit
        let uidc = 0
        if (req.user) {
            uidc = typeof req.user.id === 'undefined' ? 0 : req.user.id
        }

        const escapeData = []

        if (count === 1) {
            limitf = ` `
        } else {
            let pagen = (req.body.page - 1) * limitrg
            if (parseInt(pagen, 10) < 0) {
                pagen = 0
            }
            limitf = ` limit ${pagen},${limitrg}`
        }

        mysql.order = order
        mysql.having = having

        mysql.where = where
        mysql.limitf = limitf
        mysql.dateNow = dateNow

        const strQuery = await mysqclass.mysqli(mysql, row)
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        console.log('strQuery111', strQuery)
        return dataReturn
    }

    /**
     * fetch Active Auctions
     * @returns {object} sql response
     */
    static async fetchActiveAuctions(req) {
        const mysql = {}
        if (req.body.auctionlot > 0) {
            mysql.where = ` and id = "${req.body.auctionlot}" `
        } else {
            mysql.where = ''
        }
        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
        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)

        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
    }
    /**
     * fetch one Bid By ID
     * @param {object} pid bid ID to which the data is to be fetched
     * @returns {object} sql response
     */
    static async fetchBidByID(pid) {
        const mysql = {}
        const escapeData = [pid]
        const strQuery = await mysqclass.mysqli(mysql, 'get_single_project_bid')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }
    /**
     * fetch project by auction id
     * @param {object} pid auction ID to which the data is to be fetched
     * @returns {object} sql response
     */
    static async getAuctionProjectID(pid) {
        const mysql = {}
        const escapeData = [pid]
        const strQuery = await mysqclass.mysqli(mysql, 'get_project_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
    }

    static async updatelofActivedetails(id, status) {
        const mysql = {}
        const escapeData = [status, id]
        const strQuery = await mysqclass.mysqli(mysql, 'updatelotstatus')
        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 = []
        mysql.where = ''
        if (pid) {
            escapeData.push(pid)
            mysql.where += 'or p.auctionid=?'
        }
        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