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 userDetails(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. * update Admin Profile details
  117. * @param {object} req request data
  118. * @param {string} data data is the req.body
  119. * @returns {object} sql response
  120. */
  121. static async updateProfile(req) {
  122. const mysql = {}
  123. const postData = req.body
  124. const acceptedObjects = [
  125. 'first_name',
  126. 'last_name',
  127. 'address1',
  128. 'password_hash',
  129. 'phone',
  130. 'city',
  131. 'state',
  132. 'zip',
  133. ]
  134. let escapeData = []
  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. mysql.user_id = req.user.id
  146. const strQuery = await mysqclass.mysqli(mysql, 'update_user_profile')
  147. const data = await global.mysql.query(strQuery, escapeData)
  148. return data
  149. }
  150. /**
  151. * Check whether admin exists or not
  152. * @param {string} userEmail email id which the admin exists or not
  153. * @returns {object} sql response
  154. */
  155. static async checkForgotUserExists(userEmail) {
  156. const mysql = {}
  157. const escapeData = [userEmail]
  158. const strQuery = await mysqclass.mysqli(mysql, 'check_forgot_password_user')
  159. const data = await global.mysql.query(strQuery, escapeData)
  160. return data
  161. }
  162. /**
  163. * Insert forgot password token
  164. * @param {object} user admin object which the forgot password is requested
  165. * @param {string} Token Token which is unique for each forgot password request
  166. * @returns {object} sql response
  167. */
  168. static async inserUserToken(user, Token) {
  169. const mysql = {}
  170. const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
  171. const escapeData = [user.id, user.email, Token, dateNow]
  172. const strQuery = await mysqclass.mysqli(mysql, 'insert_forgot_password_token')
  173. const data = await global.mysql.query(strQuery, escapeData)
  174. return data
  175. }
  176. /**
  177. * Get the forgot password token
  178. * @param {string} email admin object which the forgot password is requested
  179. * @param {string} token Token which is unique for each forgot password request
  180. * @returns {object} sql response
  181. */
  182. static async getForgotUserToken(email, token) {
  183. const mysql = {}
  184. const escapeData = [email, token]
  185. const strQuery = await mysqclass.mysqli(mysql, 'get_forgot_user_token')
  186. const data = await global.mysql.query(strQuery, escapeData)
  187. return data
  188. }
  189. }
  190. module.exports.default = adminModule