admin/modules/admin.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 mysqclass = require('./mysqli').default
  18. const commonSQL = require('../../common/sql').default
  19. const commonProduct = require('../../common/products').default
  20. /**
  21. * @class class to handle admin functions
  22. */
  23. class adminModule {
  24. /**
  25. * @param {string} nameID email ID to check in the database.
  26. * @returns {object} sql response
  27. */
  28. static async checkEmailExisting(nameID) {
  29. const mysql = {}
  30. const escapeData = [nameID]
  31. const strQuery = await mysqclass.mysqli(mysql, 'okta_im_2')
  32. const data = await global.mysql.query(strQuery, escapeData)
  33. return data
  34. }
  35. /**
  36. * Login Process function. Checking the password and salt
  37. * @param {object} req request data
  38. * @param {string} data data is the req.body
  39. * @returns {object} sql response
  40. */
  41. static async process(req, callback) {
  42. const userData = req.body.email.toLowerCase()
  43. const mysql = {
  44. username: userData,
  45. }
  46. const strQuery = await mysqclass.mysqli(mysql, 'im_2')
  47. global.mysql.query(strQuery, (error, results, fields) => {
  48. if (error) {
  49. callback('')
  50. }
  51. if (results.length > 0) {
  52. const result = results[0]
  53. result.password = md5(md5(req.body.password) + result.password_salt)
  54. if (result.password === result.password_hash) {
  55. callback(results)
  56. } else {
  57. callback('')
  58. }
  59. } else {
  60. callback('')
  61. }
  62. })
  63. }
  64. /**
  65. * Update Last Login date for the admin
  66. * @param {object} req request data
  67. * @param {string} data data is the req.body
  68. * @returns {object} sql response
  69. */
  70. static async updateLastLogin(uid) {
  71. const mysql = {}
  72. const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
  73. const escapeData = [dateNow, uid]
  74. const strQuery = await mysqclass.mysqli(mysql, 'im_3')
  75. const data = await global.mysql.query(strQuery, escapeData)
  76. return data
  77. }
  78. /**
  79. * Add a entry in login table to log admin login
  80. * @param {object} req request data
  81. * @param {string} data data is the req.body
  82. * @returns {object} sql response
  83. */
  84. static async userViews(req, uid) {
  85. const mysql = {}
  86. const userIp = typeof req.headers.ipaddress === 'undefined' ? '' : req.headers.ipaddress
  87. const userHeader = req.headers['user-agent']
  88. const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss.l')
  89. const escapeData = [uid, dateNow, userHeader, userIp]
  90. const strQuery = await mysqclass.mysqli(mysql, 'im_4')
  91. const data = await global.mysql.query(strQuery, escapeData)
  92. return data
  93. }
  94. /**
  95. * get adminDetails to verify the token
  96. * @param {string} id id for the admin which is to be fetched
  97. * @returns {object} sql response
  98. */
  99. static async adminDetails(id) {
  100. const mysql = {}
  101. const baseTableUsed = global.configColumns.employees
  102. const customTableUsed = global.configColumns.custom_employees
  103. const generatedData = commonProduct.generateJoinWithColum(baseTableUsed, customTableUsed, [
  104. 'id',
  105. ])
  106. mysql.baseTableName = baseTableUsed.ext_name
  107. mysql.baseTableShort = baseTableUsed.short_name
  108. mysql.customTableJoin = generatedData.customTableJoin
  109. mysql.columns = generatedData.rowstoFetch
  110. const escapeData = [id]
  111. const strQuery = await mysqclass.mysqli(mysql, 'im_token')
  112. const data = await global.mysql.query(strQuery, escapeData)
  113. return data
  114. }
  115. /**
  116. * get userDetails
  117. * @param {string} id id for the user which is to be fetched
  118. * @returns {object} sql response
  119. */
  120. static async userDetails(id) {
  121. const mysql = {}
  122. const baseTableUsed = global.configColumns.users
  123. const customTableUsed = global.configColumns.custom_users
  124. const generatedData = commonProduct.generateJoinWithColum(
  125. baseTableUsed,
  126. customTableUsed,
  127. ['id'],
  128. 'left join',
  129. )
  130. generatedData.rowstoFetch.push('un.email as email_notification')
  131. generatedData.rowstoFetch.push('un.sms as sms_notification')
  132. generatedData.customTableJoin += ' left join user_notification as un on un.user_id = u.id'
  133. mysql.baseTableName = baseTableUsed.ext_name
  134. mysql.baseTableShort = baseTableUsed.short_name
  135. mysql.customTableJoin = generatedData.customTableJoin
  136. mysql.columns = generatedData.rowstoFetch
  137. const escapeData = [id]
  138. const strQuery = await mysqclass.mysqli(mysql, 'im_token')
  139. const data = await global.mysql.query(strQuery, escapeData)
  140. return data
  141. }
  142. /**
  143. * update Admin Profile details
  144. * @param {object} req request data
  145. * @param {string} data data is the req.body
  146. * @returns {object} sql response
  147. */
  148. static async updateProfile(req) {
  149. const tableUsed = 'employees'
  150. const mysql = {}
  151. let escapeData = []
  152. const postData = req.body
  153. const acceptedObjects = global.configColumns[tableUsed].array_columns
  154. const defaultKeys = ['updated_at']
  155. const defaultValues = [dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')]
  156. const valueInsert = commonSQL.updateSQLFunction(
  157. postData,
  158. acceptedObjects,
  159. defaultKeys,
  160. defaultValues,
  161. )
  162. mysql.keys = valueInsert.keys
  163. mysql.mainTable = global.configColumns[tableUsed].ext_name
  164. escapeData = valueInsert.escapeData
  165. mysql.user_id = req.user.admin_id
  166. const strQuery = await mysqclass.mysqli(mysql, 'update_user_profile')
  167. const data = await global.mysql.query(strQuery, escapeData)
  168. return data
  169. }
  170. /**
  171. * Update Custom User Profile details
  172. * @param {object} req request data
  173. * @param {string} data data is the req.body
  174. * @returns {object} sql response
  175. */
  176. static async updateCustomUsersProfile(req) {
  177. const tableUsed = 'custom_employees'
  178. const mysql = {}
  179. let escapeData = []
  180. const postData = req.body
  181. const acceptedObjects = global.configColumns[tableUsed].array_columns
  182. const defaultKeys = []
  183. const defaultValues = []
  184. const valueInsert = commonSQL.updateSQLFunction(
  185. postData,
  186. acceptedObjects,
  187. defaultKeys,
  188. defaultValues,
  189. )
  190. mysql.keys = valueInsert.keys
  191. escapeData = valueInsert.escapeData
  192. mysql.user_id = req.user.admin_id
  193. mysql.mainTable = global.configColumns[tableUsed].ext_name
  194. const strQuery = await mysqclass.mysqli(mysql, 'update_custom_user_profile')
  195. let dataPromise = ''
  196. if (mysql.keys.length) {
  197. dataPromise = await global.mysql.query(strQuery, escapeData)
  198. } else {
  199. dataPromise = Promise.resolve()
  200. }
  201. return dataPromise
  202. }
  203. /**
  204. * Check whether admin exists or not
  205. * @param {string} userEmail email id which the admin exists or not
  206. * @returns {object} sql response
  207. */
  208. static async checkForgotUserExists(userEmail) {
  209. const mysql = {}
  210. const escapeData = [userEmail]
  211. const strQuery = await mysqclass.mysqli(mysql, 'check_forgot_password_user')
  212. const data = await global.mysql.query(strQuery, escapeData)
  213. return data
  214. }
  215. /**
  216. * Insert forgot password token
  217. * @param {object} user admin object which the forgot password is requested
  218. * @param {string} Token Token which is unique for each forgot password request
  219. * @returns {object} sql response
  220. */
  221. static async inserUserToken(user, Token) {
  222. const mysql = {}
  223. const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
  224. const escapeData = [user.id, user.email, Token, dateNow]
  225. const strQuery = await mysqclass.mysqli(mysql, 'insert_forgot_password_token')
  226. const data = await global.mysql.query(strQuery, escapeData)
  227. return data
  228. }
  229. /**
  230. * Get the forgot password token
  231. * @param {string} email admin object which the forgot password is requested
  232. * @param {string} token Token which is unique for each forgot password request
  233. * @returns {object} sql response
  234. */
  235. static async getForgotUserToken(email, token) {
  236. const mysql = {}
  237. const escapeData = [email, token]
  238. const strQuery = await mysqclass.mysqli(mysql, 'get_forgot_user_token')
  239. const data = await global.mysql.query(strQuery, escapeData)
  240. return data
  241. }
  242. /**
  243. * Get listing count for each status
  244. * @returns {object} sql response
  245. */
  246. static async getMainDashboardData() {
  247. const mysql = {}
  248. const escapeData = []
  249. const strQuery = await mysqclass.mysqli(mysql, 'get_main_dashboard_data')
  250. const data = await global.mysql.query(strQuery, escapeData)
  251. return data
  252. }
  253. /**
  254. * Get listing count for each status
  255. * @returns {object} sql response
  256. */
  257. static async getMainDashboardDataMore(query) {
  258. const mysql = { query }
  259. const escapeData = []
  260. const strQuery = await mysqclass.mysqli(mysql, 'get_main_dashboard_data_more')
  261. const data = await global.mysql.query(strQuery, escapeData)
  262. return data
  263. }
  264. }
  265. module.exports.default = adminModule