/* ============================================================================ *\
|| ########################################################################## ||
|| # 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 user functions
*/
class adminUserModule {
/**
* Fetch All Buyers
* @param {object} req request data
* @param {string} count count for the pagination
* @returns {object} sql response
*/
static async fetchBuyersAll(req, count) {
const mysql = {}
mysql.where = ''
const { action } = req.body
const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
if (
action === 'active' ||
action === 'moderate' ||
action === 'unverified' ||
action === 'deactivate'
) {
mysql.where += ` and status = "${action}"`
}
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}`
}
const acceptedKeysInside = [
'email',
'first_name',
'last_name',
'username',
'user_type',
'status',
]
const dynamicValuesFiltered = _.omit(req.body, (value, key, object) => {
if (
_.isNull(value) ||
_.isUndefined(value) ||
value === '' ||
!_.contains(acceptedKeysInside, key)
) {
return true
}
return false
})
Object.keys(dynamicValuesFiltered).forEach((value, key) => {
mysql.where += ` and ${value} like '%${dynamicValuesFiltered[value]}%'`
})
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_buyers_limit'
} else {
row = 'get_all_buyers'
}
const strQuery = await mysqclass.mysqli(mysql, row)
const dataReturn = await global.mysql.query(strQuery, escapeData)
console.log('strQuery', strQuery)
return dataReturn
}
/**
* Fetch Single User
* @param {number} pid User ID which details has to be fetched
* @returns {object} sql response
*/
static async fetchUsersbyID(pid) {
const mysql = {}
const escapeData = [pid]
const strQuery = await mysqclass.mysqli(mysql, 'get_single_user_buyer')
const dataReturn = await global.mysql.query(strQuery, escapeData)
return dataReturn
}
/**
* Check Email Exists
* @param {string} nameID Email which has to be checked for unique
* @returns {object} sql response
*/
static async checkEmailExisting(nameID) {
const mysql = {}
const escapeData = [nameID]
const strQuery = await mysqclass.mysqli(mysql, 'check_email_exists')
const data = await global.mysql.query(strQuery, escapeData)
return data
}
/**
* User Operation to update or insert
* @param {object} req req object
* @returns {object} sql response
*/
static async userOperation(req) {
const mysql = {}
const postData = req.body
const acceptedObjects = [
'first_name',
'last_name',
'address1',
'status',
'email',
'password_hash',
'password_salt',
'phone',
'city',
'state',
'zip',
]
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_users'
mysql.user_id = req.body.id
} else {
req.body.password_salt = '12345'
req.body.password_hash = md5(md5(req.body.password) + req.body.password_salt)
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
row = 'insert_into_users'
}
const strQuery = await mysqclass.mysqli(mysql, row)
const data = await global.mysql.query(strQuery, escapeData)
console.log('strQuery', strQuery, escapeData)
return data
}
/**
* Manual insert notification
* @param {number} uid user ID which notification has to be added
* @returns {object} sql response
*/
static async manualInsertUsernotify(uid) {
const mysql = {}
const escapeData = [uid]
const strQuery = await mysqclass.mysqli(mysql, 'insert_into_notification')
const data = await global.mysql.query(strQuery, escapeData)
return data
}
/**
* Update user status
* @param {string} status status which has to be updated
* @param {number} uid user ID where status to be updated
* @returns {object} sql response
*/
static async userStatusUpdate(status, pid) {
const mysql = {}
const escapeData = [status, pid]
const strQuery = await mysqclass.mysqli(mysql, 'user_change_market_status')
const dataReturn = await global.mysql.query(strQuery, escapeData)
console.log('dataReturn', strQuery, escapeData)
return dataReturn
}
/**
* Create a user insert query
* @param {object} req request data
* @param {object} data request.body data
* @param {string} baseTableUsed baseTable which the query has to be generated
* @returns {object} sql response
*/
static async createuserlist(req, data, baseTableUsed) {
const mysqli = {}
let escapeData = []
const postData = data
const acceptedObjects = baseTableUsed.array_columns
const defaultKeys = ['created_at', 'updated_at']
const defaultValues = [
dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss'),
dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss'),
]
const valueInsert = commonSQL.InsertSQLFunction(
postData,
acceptedObjects,
defaultKeys,
defaultValues,
)
mysqli.keys = valueInsert.keys
escapeData = valueInsert.escapeData
mysqli.values = valueInsert.values
mysqli.tables = baseTableUsed.ext_name
const strQuery = await mysqclass.mysqli(mysqli, 'insert_tables')
const dataPromise = await global.mysql.query(strQuery, escapeData)
return dataPromise
}
/**
* Update query for the users
* @param {object} req request data
* @param {object} data request.body data
* @param {string} baseTableUsed baseTable which the query has to be generated
* @returns {object} sql response
*/
static async updatealltypeofuser(req, data, proid, typeoffield, baseTableUsed) {
const mysqli = {}
let escapeData = []
const postData = data
const postID = proid
const fieldType = typeoffield
const acceptedObjects = baseTableUsed.array_columns
const defaultKeys = ['updated_at']
const defaultValues = [dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')]
const valueInsert = commonSQL.updateSQLFunction(
postData,
acceptedObjects,
defaultKeys,
defaultValues,
)
mysqli.keys = valueInsert.keys
escapeData = valueInsert.escapeData
mysqli.values = ` ${fieldType}=${postID}`
mysqli.tables = baseTableUsed.ext_name
const strQuery = await mysqclass.mysqli(mysqli, 'update_tables')
const dataPromise = await global.mysql.query(strQuery, escapeData)
return dataPromise
}
/**
* Select query to fetch all users
* @param {object} data request.body data
* @param {string} baseTableUsed baseTable which the query has to be generated
* @returns {object} sql response
*/
static async commonselectparenttable(data, proid, baseTableUsed, baseid) {
const mysqli = {}
const proId = proid.split(',')
const escapeData = [proId]
mysqli.keys = data
mysqli.values = `${baseid} IN (?)`
mysqli.tables = baseTableUsed.ext_name
mysqli.group_by = ''
mysqli.order_by = ''
mysqli.limit_by = ''
const strQuery = await mysqclass.mysqli(mysqli, 'select_tables')
const dataReturn = await global.mysql.query(strQuery, escapeData)
return dataReturn
}
}
module.exports.default = adminUserModule