admin/modules/user.js

  1. /* ============================================================================ *\
  2. || ########################################################################## ||
  3. || # Auction Software Marketplace Release: 0.6 Build 0.7 # ||
  4. || # ---------------------------------------------------------------------- # ||
  5. || # License # 35YAHCNR9344X6O666C123AB # ||
  6. || # ---------------------------------------------------------------------- # ||
  7. || # Copyright ©2014–2021 Develop Scripts LLC. All Rights Reserved # ||
  8. || # This file may not be redistributed in whole or significant part. # ||
  9. || # ------------- AUCTION SOFTWARE IS NOT FREE SOFTWARE ------------------ # ||
  10. || # http://www.auctionsoftwaremarketplace.com|support@auctionsoftware.com # ||
  11. || # ---------------------------------------------------------------------- # ||
  12. || ########################################################################## ||
  13. \* ============================================================================ */
  14. const dateFormat = require('dateformat')
  15. const md5 = require('md5')
  16. const _ = require('underscore')
  17. const moment = require('moment')
  18. const commonSQL = require('../../common/sql').default
  19. const mysqclass = require('./mysqli').default
  20. /**
  21. * @class class to handle user functions
  22. */
  23. class adminUserModule {
  24. /**
  25. * Fetch All Buyers
  26. * @param {object} req request data
  27. * @param {string} count count for the pagination
  28. * @returns {object} sql response
  29. */
  30. static async fetchBuyersAll(req, count) {
  31. const mysql = {}
  32. mysql.where = ''
  33. const { action } = req.body
  34. const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
  35. if (
  36. action === 'active' ||
  37. action === 'moderate' ||
  38. action === 'unverified' ||
  39. action === 'deactivate'
  40. ) {
  41. mysql.where += ` and status = "${action}"`
  42. }
  43. mysql.order = 'order by id desc'
  44. const order = req.body.order === '' ? 'asc' : req.body.order
  45. const orderby = req.body.orderby === '' && !req.body.orderby ? '' : req.body.orderby
  46. if (orderby !== '') {
  47. mysql.order = ` order by ${orderby} ${order}`
  48. }
  49. const acceptedKeysInside = [
  50. 'email',
  51. 'first_name',
  52. 'last_name',
  53. 'username',
  54. 'user_type',
  55. 'status',
  56. ]
  57. const dynamicValuesFiltered = _.omit(req.body, (value, key, object) => {
  58. if (
  59. _.isNull(value) ||
  60. _.isUndefined(value) ||
  61. value === '' ||
  62. !_.contains(acceptedKeysInside, key)
  63. ) {
  64. return true
  65. }
  66. return false
  67. })
  68. Object.keys(dynamicValuesFiltered).forEach((value, key) => {
  69. mysql.where += ` and ${value} like '%${dynamicValuesFiltered[value]}%'`
  70. })
  71. let row = ''
  72. const pagen = (req.body.page - 1) * req.body.limit
  73. const limitf = ` limit ${pagen},${req.body.limit}`
  74. mysql.limit = limitf
  75. mysql.dateNow = dateNow
  76. const escapeData = []
  77. if (count === 1) {
  78. row = 'get_all_buyers_limit'
  79. } else {
  80. row = 'get_all_buyers'
  81. }
  82. const strQuery = await mysqclass.mysqli(mysql, row)
  83. const dataReturn = await global.mysql.query(strQuery, escapeData)
  84. console.log('strQuery', strQuery)
  85. return dataReturn
  86. }
  87. /**
  88. * Fetch Single User
  89. * @param {number} pid User ID which details has to be fetched
  90. * @returns {object} sql response
  91. */
  92. static async fetchUsersbyID(pid) {
  93. const mysql = {}
  94. const escapeData = [pid]
  95. const strQuery = await mysqclass.mysqli(mysql, 'get_single_user_buyer')
  96. const dataReturn = await global.mysql.query(strQuery, escapeData)
  97. return dataReturn
  98. }
  99. /**
  100. * Check Email Exists
  101. * @param {string} nameID Email which has to be checked for unique
  102. * @returns {object} sql response
  103. */
  104. static async checkEmailExisting(nameID) {
  105. const mysql = {}
  106. const escapeData = [nameID]
  107. const strQuery = await mysqclass.mysqli(mysql, 'check_email_exists')
  108. const data = await global.mysql.query(strQuery, escapeData)
  109. return data
  110. }
  111. /**
  112. * User Operation to update or insert
  113. * @param {object} req req object
  114. * @returns {object} sql response
  115. */
  116. static async userOperation(req) {
  117. const mysql = {}
  118. const postData = req.body
  119. const acceptedObjects = [
  120. 'first_name',
  121. 'last_name',
  122. 'address1',
  123. 'status',
  124. 'email',
  125. 'password_hash',
  126. 'password_salt',
  127. 'phone',
  128. 'city',
  129. 'state',
  130. 'zip',
  131. ]
  132. let escapeData = []
  133. let row = ''
  134. if (req.body.id) {
  135. const defaultKeys = ['updated_at']
  136. const defaultValues = [dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')]
  137. const valueInsert = commonSQL.updateSQLFunction(
  138. postData,
  139. acceptedObjects,
  140. defaultKeys,
  141. defaultValues,
  142. )
  143. mysql.keys = valueInsert.keys
  144. escapeData = valueInsert.escapeData
  145. row = 'update_on_users'
  146. mysql.user_id = req.body.id
  147. } else {
  148. req.body.password_salt = '12345'
  149. req.body.password_hash = md5(md5(req.body.password) + req.body.password_salt)
  150. const defaultKeys = ['created_at']
  151. const defaultValues = [dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')]
  152. const valueInsert = commonSQL.InsertSQLFunction(
  153. postData,
  154. acceptedObjects,
  155. defaultKeys,
  156. defaultValues,
  157. )
  158. mysql.keys = valueInsert.keys
  159. escapeData = valueInsert.escapeData
  160. row = 'insert_into_users'
  161. }
  162. const strQuery = await mysqclass.mysqli(mysql, row)
  163. const data = await global.mysql.query(strQuery, escapeData)
  164. console.log('strQuery', strQuery, escapeData)
  165. return data
  166. }
  167. /**
  168. * Manual insert notification
  169. * @param {number} uid user ID which notification has to be added
  170. * @returns {object} sql response
  171. */
  172. static async manualInsertUsernotify(uid) {
  173. const mysql = {}
  174. const escapeData = [uid]
  175. const strQuery = await mysqclass.mysqli(mysql, 'insert_into_notification')
  176. const data = await global.mysql.query(strQuery, escapeData)
  177. return data
  178. }
  179. /**
  180. * Update user status
  181. * @param {string} status status which has to be updated
  182. * @param {number} uid user ID where status to be updated
  183. * @returns {object} sql response
  184. */
  185. static async userStatusUpdate(status, pid) {
  186. const mysql = {}
  187. const escapeData = [status, pid]
  188. const strQuery = await mysqclass.mysqli(mysql, 'user_change_market_status')
  189. const dataReturn = await global.mysql.query(strQuery, escapeData)
  190. console.log('dataReturn', strQuery, escapeData)
  191. return dataReturn
  192. }
  193. /**
  194. * Create a user insert query
  195. * @param {object} req request data
  196. * @param {object} data request.body data
  197. * @param {string} baseTableUsed baseTable which the query has to be generated
  198. * @returns {object} sql response
  199. */
  200. static async createuserlist(req, data, baseTableUsed) {
  201. const mysqli = {}
  202. let escapeData = []
  203. const postData = data
  204. const acceptedObjects = baseTableUsed.array_columns
  205. const defaultKeys = ['created_at', 'updated_at']
  206. const defaultValues = [
  207. dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss'),
  208. dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss'),
  209. ]
  210. const valueInsert = commonSQL.InsertSQLFunction(
  211. postData,
  212. acceptedObjects,
  213. defaultKeys,
  214. defaultValues,
  215. )
  216. mysqli.keys = valueInsert.keys
  217. escapeData = valueInsert.escapeData
  218. mysqli.values = valueInsert.values
  219. mysqli.tables = baseTableUsed.ext_name
  220. const strQuery = await mysqclass.mysqli(mysqli, 'insert_tables')
  221. const dataPromise = await global.mysql.query(strQuery, escapeData)
  222. return dataPromise
  223. }
  224. /**
  225. * Update query for the users
  226. * @param {object} req request data
  227. * @param {object} data request.body data
  228. * @param {string} baseTableUsed baseTable which the query has to be generated
  229. * @returns {object} sql response
  230. */
  231. static async updatealltypeofuser(req, data, proid, typeoffield, baseTableUsed) {
  232. const mysqli = {}
  233. let escapeData = []
  234. const postData = data
  235. const postID = proid
  236. const fieldType = typeoffield
  237. const acceptedObjects = baseTableUsed.array_columns
  238. const defaultKeys = ['updated_at']
  239. const defaultValues = [dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')]
  240. const valueInsert = commonSQL.updateSQLFunction(
  241. postData,
  242. acceptedObjects,
  243. defaultKeys,
  244. defaultValues,
  245. )
  246. mysqli.keys = valueInsert.keys
  247. escapeData = valueInsert.escapeData
  248. mysqli.values = ` ${fieldType}=${postID}`
  249. mysqli.tables = baseTableUsed.ext_name
  250. const strQuery = await mysqclass.mysqli(mysqli, 'update_tables')
  251. const dataPromise = await global.mysql.query(strQuery, escapeData)
  252. return dataPromise
  253. }
  254. /**
  255. * Select query to fetch all users
  256. * @param {object} data request.body data
  257. * @param {string} baseTableUsed baseTable which the query has to be generated
  258. * @returns {object} sql response
  259. */
  260. static async commonselectparenttable(data, proid, baseTableUsed, baseid) {
  261. const mysqli = {}
  262. const proId = proid.split(',')
  263. const escapeData = [proId]
  264. mysqli.keys = data
  265. mysqli.values = `${baseid} IN (?)`
  266. mysqli.tables = baseTableUsed.ext_name
  267. mysqli.group_by = ''
  268. mysqli.order_by = ''
  269. mysqli.limit_by = ''
  270. const strQuery = await mysqclass.mysqli(mysqli, 'select_tables')
  271. const dataReturn = await global.mysql.query(strQuery, escapeData)
  272. return dataReturn
  273. }
  274. }
  275. module.exports.default = adminUserModule