admin/controllers/setting/index.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  # ||
|| # ---------------------------------------------------------------------- # ||
|| ########################################################################## ||
\* ============================================================================ */

/* eslint-disable prefer-destructuring */
/* eslint-disable no-param-reassign */
const jwt = require('jsonwebtoken')
const _ = require('lodash')
const configure = require('../../../loaders/configure')

const adminSettingModule = require('../../modules/setting').default
const schemaModule = require('./schema').default
const commonFunction = require('../../../common/function').default

const { jsonResponse, errorResponse } = require('../logger')

module.exports = {
    /**
     * Get All configuration variable
     *
     * @memberOf adminside.setting
     * @param {adminSettingModule.allConfiguration} modules
     */
    configurationVariables: async (req, res) => {
        let records = []
        let totalRecords = []
        try {
            ;[records, totalRecords] = await Promise.all([
                adminSettingModule.allConfiguration(req, 0),
                adminSettingModule.allConfiguration(req, 1),
            ])
        } catch (e) {
            console.error(e)
            jsonResponse(res, 'error', {
                responseType: 3,
                message: 'Internal Server error!',
            })
        } finally {
            let responseData = { records, totalRecords }
            responseData = await commonFunction.getdynamicinnercontents(req, responseData)
            jsonResponse(res, 'success', {
                responseType: 1,
                message: 'Details successfully retrieved!',
                responseData,
            })
        }
    },

    /**
     * Get Single configuration variable
     *
     * @memberOf adminside.setting
     * @param {adminSettingModule.getConfigurationbyId} modules
     */
    getSingleConfiguration: async (req, res) => {
        let item = {}
        try {
            const id = typeof req.body.id === 'undefined' ? '' : req.body.id
            const [records] = await Promise.all([adminSettingModule.getConfigurationbyId(id)])
            item = records[0]
        } catch (e) {
            console.error(e)
            jsonResponse(res, 'error', {
                responseType: 3,
                message: 'Internal Server error!',
            })
        } finally {
            const responseData = { item }
            jsonResponse(res, 'success', {
                responseType: 1,
                message: 'Details successfully retrieved!',
                responseData,
            })
        }
    },

    /**
     * Configuration Action
     *
     * @memberOf adminside.setting
     * @param {adminSettingModule.configurationOperation} modules
     */
    configurationAction: async (req, res) => {
        try {
            req.body.id =
                typeof req.body.id === 'undefined' || req.body.id === 0 || req.body.id === ''
                    ? null
                    : req.body.id
            await Promise.all([adminSettingModule.configurationOperation(req)])
            await configure.default(global) // updaate global configuration variables
        } catch (e) {
            console.error(e)
            jsonResponse(res, 'error', {
                responseType: 3,
                message: 'Internal Server error!',
            })
        } finally {
            jsonResponse(res, 'success', {
                responseType: 1,
                message: req.body.id ? 'Successfully updated!' : 'Successfully added!',
            })
        }
    },
    /**
     * Get All Bid Increments
     *
     * @memberOf adminside.setting
     * @param {adminSettingModule.getAllBidIncrements} modules
     */
    getAllBidIncrements: async (req, res) => {
        let records = []
        let totalRecords = []
        try {
            ;[records, totalRecords] = await Promise.all([
                adminSettingModule.getAllBidIncrements(req, 0),
                adminSettingModule.getAllBidIncrements(req, 1),
            ])
        } catch (e) {
            console.error(e)
            jsonResponse(res, 'error', {
                responseType: 3,
                message: 'Internal Server error!',
            })
        } finally {
            let responseData = { records, totalRecords }
            responseData = await commonFunction.getdynamicinnercontents(req, responseData)
            jsonResponse(res, 'success', {
                responseType: 1,
                message: 'Details successfully retrieved!',
                responseData,
            })
        }
    },
    /**
     * Get Specific Bid Increment based on ID
     *
     * @memberOf adminside.setting
     * @param {adminSettingModule.getBidIncrementById} modules
     */
    getSingleBidIncrement: async (req, res) => {
        let item = {}
        try {
            const id = typeof req.body.id === 'undefined' ? '' : req.body.id
            const [records] = await Promise.all([adminSettingModule.getBidIncrementById(id)])
            item = records[0]
        } catch (e) {
            console.error(e)
            jsonResponse(res, 'error', {
                responseType: 3,
                message: 'Internal Server error!',
            })
        } finally {
            const responseData = { item }
            jsonResponse(res, 'success', {
                responseType: 1,
                message: 'Details successfully retrieved!',
                responseData,
            })
        }
    },
    /**
     * Bid Increment Action => Change Bid Increment Values
     *
     * @memberOf adminside.setting
     * @param {adminSettingModule.bidIncrementOperation} modules
     */
    bidIncrementAction: async (req, res) => {
        try {
            req.body.id =
                typeof req.body.id === 'undefined' || req.body.id === 0 || req.body.id === ''
                    ? null
                    : req.body.id
            await Promise.all([adminSettingModule.bidIncrementOperation(req)])
            await configure.default(global) // updaate global configuration variables
        } catch (e) {
            console.error(e)
            jsonResponse(res, 'error', {
                responseType: 3,
                message: 'Internal Server error!',
            })
        } finally {
            jsonResponse(res, 'success', {
                responseType: 1,
                message: req.body.id ? 'Successfully updated!' : 'Successfully added!',
            })
        }
    },

    /**
     * Delete Bid Increment Value
     *
     * @memberOf adminside.setting
     * @param {adminSettingModule.deleteBidIncrement} modules
     */
    deleteBidIncrement: async (req, res) => {
        try {
            req.body.id =
                typeof req.body.id === 'undefined' || req.body.id === 0 || req.body.id === ''
                    ? null
                    : req.body.id
            if (!req.body.id) {
                jsonResponse(res, 'error', {
                    responseType: 3,
                    message: 'Internal Server error!',
                })
                return
            }
            await Promise.all([adminSettingModule.deleteBidIncrement(req.body.id)])
            await configure.default(global) // updaate global configuration variables
        } catch (e) {
            console.error(e)
            jsonResponse(res, 'error', {
                responseType: 3,
                message: 'Internal Server error!',
            })
        } finally {
            jsonResponse(res, 'success', {
                responseType: 1,
                message: 'Successfully deleted',
            })
        }
    },

    /**
     * Get All buyer bremium
     *
     * @memberOf adminside.setting
     * @param {adminSettingModule.getAllBuyerPremium} modules
     */
    getAllBuyerPremium: async (req, res) => {
        let records = []
        let totalRecords = []
        try {
            ;[records, totalRecords] = await Promise.all([
                adminSettingModule.getAllBuyerPremium(req, 0),
                adminSettingModule.getAllBuyerPremium(req, 1),
            ])
        } catch (e) {
            console.error(e)
            jsonResponse(res, 'error', {
                responseType: 3,
                message: 'Internal Server error!',
            })
        } finally {
            let responseData = { records, totalRecords }
            responseData = await commonFunction.getdynamicinnercontents(req, responseData)
            jsonResponse(res, 'success', {
                responseType: 1,
                message: 'Details successfully retrieved!',
                responseData,
            })
        }
    },
    /**
     * Get Specific Buyer Premium based on ID
     *
     * @memberOf adminside.setting
     * @param {adminSettingModule.getBuyerPremiumById} modules
     */
    getBuyerPremiumById: async (req, res) => {
        let item = {}
        try {
            const id = typeof req.body.id === 'undefined' ? '' : req.body.id
            const [records] = await Promise.all([adminSettingModule.getBuyerPremiumById(id)])
            item = records[0]
        } catch (e) {
            console.error(e)
            jsonResponse(res, 'error', {
                responseType: 3,
                message: 'Internal Server error!',
            })
        } finally {
            const responseData = { item }
            jsonResponse(res, 'success', {
                responseType: 1,
                message: 'Details successfully retrieved!',
                responseData,
            })
        }
    },
    /**
     * Buyer Premium Action => Change Buyer Premium Values
     *
     * @memberOf adminside.setting
     * @param {adminSettingModule.buyerPremiumOperation} modules
     */
    buyerPremiumOperation: async (req, res) => {
        try {
            req.body.id =
                typeof req.body.id === 'undefined' || req.body.id === 0 || req.body.id === ''
                    ? null
                    : req.body.id
            await Promise.all([adminSettingModule.buyerPremiumOperation(req)])
            await configure.default(global) // updaate global configuration variables
        } catch (e) {
            console.error(e)
            jsonResponse(res, 'error', {
                responseType: 3,
                message: 'Internal Server error!',
            })
        } finally {
            jsonResponse(res, 'success', {
                responseType: 1,
                message: req.body.id ? 'Successfully updated!' : 'Successfully added!',
            })
        }
    },

    /**
     * Delete Buyer Premium Value
     *
     * @memberOf adminside.setting
     * @param {adminSettingModule.deleteBuyerPremium} modules
     */
    deleteBuyerPremium: async (req, res) => {
        try {
            req.body.id =
                typeof req.body.id === 'undefined' || req.body.id === 0 || req.body.id === ''
                    ? null
                    : req.body.id
            if (!req.body.id) {
                jsonResponse(res, 'error', {
                    responseType: 3,
                    message: 'Internal Server error!',
                })
                return
            }
            await Promise.all([adminSettingModule.deleteBuyerPremium(req.body.id)])
            await configure.default(global) // updaate global configuration variables
        } catch (e) {
            console.error(e)
            jsonResponse(res, 'error', {
                responseType: 3,
                message: 'Internal Server error!',
            })
        } finally {
            jsonResponse(res, 'success', {
                responseType: 1,
                message: 'Successfully deleted',
            })
        }
    },

    /**
     * search table Action
     *
     * @memberOf adminside.setting
     * @param {commonFunction.getValidID} modules
     * @param {commonFunction.tableFunction} modules
     */
    searchTableAction: async (req, res) => {
        try {
            req.body.id = commonFunction.getValidID(req.body.id)
            const baseTableUsed = global.configColumns[req.body.table]
            const [results] = await Promise.all([
                commonFunction.tableFunction(req, baseTableUsed, { enabled: 0 }, false, ['id']),
            ])
            await configure.default(global)
        } catch (e) {
            console.error(e)
            jsonResponse(res, 'error', {
                responseType: 3,
                message: 'Internal Server error!',
            })
        } finally {
            jsonResponse(res, 'success', {
                responseType: 1,
                message: req.body.id ? 'Successfully updated!' : 'Successfully added!',
            })
        }
    },
    /**
     * Data Table insert
     *
     * @memberOf adminside.setting
     * @param {adminSettingModule.insertDataTable} modules
     * @param {commonFunction.tableFunction} modules
     */
    dataTableInsert: async (req, res) => {
        try {
            if (req.body.tableName) {
                if (global.configColumns[req.body.tableName]) {
                    if (_.size(req.body.columns) > 0) {
                        await Promise.all([adminSettingModule.insertDataTable(req, req.body)])
                        jsonResponse(res, 'success', {
                            responseType: 1,
                            message: 'Details successfully Inserted!',
                        })
                    } else {
                        errorResponse('Must specify column name to insert ', res)
                    }
                } else {
                    errorResponse('Invalid request, TableName not pre-defined', res)
                }
            } else {
                errorResponse('Must specify the tableName', res)
            }
        } catch (e) {
            errorResponse(e, res)
        }
    },
    /**
     * Data Table Update
     *
     * @memberOf adminside.setting
     * @param {adminSettingModule.updateDataTable} modules
     */
    dataTableUpdate: async (req, res) => {
        try {
            if (req.body.tableName) {
                if (global.configColumns[req.body.tableName]) {
                    if (_.size(req.body.columns) > 0) {
                        if (_.size(req.body.where) > 0) {
                            await Promise.all([adminSettingModule.updateDataTable(req, req.body)])
                            jsonResponse(res, 'success', {
                                responseType: 1,
                                message: 'Details successfully Updated!',
                            })
                        } else {
                            errorResponse('Must specify Where condition to update', res)
                        }
                    } else {
                        errorResponse('Must specify column name to insert ', res)
                    }
                } else {
                    errorResponse('Invalid request, TableName not pre-defined', res)
                }
            } else {
                errorResponse('Must specify the tableName', res)
            }
        } catch (e) {
            errorResponse(e, res)
        }
    },
}