front/controllers/cart/index.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. /* eslint-disable prefer-destructuring */
  15. /* eslint-disable no-param-reassign */
  16. const cartModule = require('../../modules/cart').default
  17. const commonFunction = require('../../../common/function').default
  18. const userModule = require('../../modules/user').default
  19. const returnCtrl = require('../return')
  20. const schemaModule = require('./schema').default
  21. const { jsonResponse } = require('../logger')
  22. const shortDescribeSCH = async (items) => {
  23. function changingdata(item) {
  24. item.avatarorg =
  25. item.file_name === '' || item.file_name === null
  26. ? `${global.s3_static_image_url}images/pics-coming.jpg`
  27. : `${global.s3_static_image_url}uploads/product/${item.file_name}`
  28. return item
  29. }
  30. const promises = items.map(changingdata)
  31. items = await Promise.all(promises)
  32. return items
  33. }
  34. const cartItemsCalculations = async (items) => {
  35. const cartValues = {
  36. per_total: 0,
  37. total_amount: 0,
  38. total_items: 0,
  39. tax_percent: 0,
  40. buyer_premium: 0,
  41. total_premium: 0,
  42. total_tax: 0,
  43. total_shipping: 0,
  44. }
  45. function changingdata(item) {
  46. cartValues.per_total += parseFloat(item.buynowamount)
  47. cartValues.total_items += 1
  48. }
  49. const promises = items.map(changingdata)
  50. items = await Promise.all(promises)
  51. cartValues.tax_percent = 10
  52. cartValues.buyer_premium = 15
  53. cartValues.total_tax = (cartValues.per_total * (cartValues.tax_percent / 100)).toFixed(2)
  54. cartValues.total_premium = (cartValues.per_total * (cartValues.buyer_premium / 100)).toFixed(2)
  55. const totalTaxPremium = commonFunction.sumFloat(cartValues.total_premium, cartValues.total_tax)
  56. cartValues.total_amount = commonFunction.sumFloat(
  57. parseFloat(cartValues.per_total).toFixed(2),
  58. parseFloat(totalTaxPremium).toFixed(2),
  59. )
  60. return cartValues
  61. }
  62. const getorCreateCartID = async (req, userDetails, locationID) => {
  63. let cartId = 0
  64. const resultcart = await Promise.all([cartModule.checkUserCartExists(req, locationID)])
  65. if (resultcart[0].length > 0) {
  66. cartId = resultcart[0][0].id
  67. } else {
  68. const results = await Promise.all([
  69. cartModule.addNewCartEntry(req, userDetails, locationID),
  70. ])
  71. cartId = results[0].insertId
  72. }
  73. return cartId
  74. }
  75. const getSingleCartDetails = async (req, cartID) => {
  76. req.body.cart_id = cartID
  77. let [cartItems, cartValues, cartLocation] = await Promise.all([
  78. cartModule.allCartItems(req, req.body, 1),
  79. cartModule.getCartDetails(req.body.cart_id),
  80. cartModule.cartLocationDetails(req.body.cart_id),
  81. ])
  82. cartItems = await shortDescribeSCH(cartItems)
  83. const responseData = { cartItems, cartValues: cartValues[0], cartLocation: cartLocation[0] }
  84. return responseData
  85. }
  86. const checkAndAddtoCart = async (req) => {
  87. let success = false
  88. const resultuser = await Promise.all([
  89. cartModule.checkUserProductExists(req, req.body.id),
  90. userModule.userDetails(req.user.id),
  91. ])
  92. const userDetails = resultuser[1][0]
  93. if (resultuser[0].length > 0) {
  94. req.body.cart_id = await getorCreateCartID(req, userDetails, resultuser[0][0].location_id)
  95. const cartChange = req.body.cart === 1 ? req.body.cart_id : 0
  96. await Promise.all([cartModule.changeCartStatus(req, cartChange, req.body.id)])
  97. const [cartItems] = await Promise.all([cartModule.allCartItems(req, req.body, 1)])
  98. const cartValues = await cartItemsCalculations(cartItems)
  99. await Promise.all([cartModule.updateCartData(cartValues, req.body.cart_id)])
  100. success = true
  101. } else {
  102. success = false
  103. }
  104. const responseData = { success }
  105. return responseData
  106. }
  107. const getNonCartItems = async (req) => {
  108. let [nonCartItems] = await Promise.all([cartModule.allCartItems(req, req.body, 0)])
  109. nonCartItems = await shortDescribeSCH(nonCartItems)
  110. const responseData = nonCartItems
  111. return responseData
  112. }
  113. module.exports = {
  114. getSingleCartDetails,
  115. checkAndAddtoCart,
  116. /**
  117. * Get Single Cart Details
  118. *
  119. * @memberOf frontend.cart
  120. * @param {frontend.cart.getSingleCartDetails} modules
  121. */
  122. singleCart: async (req, res) => {
  123. try {
  124. await schemaModule.search().validateSync(req.body)
  125. const [responseData] = await Promise.all([getSingleCartDetails(req, req.body.cart_id)])
  126. jsonResponse(res, 'success', {
  127. responseType: 1,
  128. message: 'Details successfully retrieved!',
  129. responseData,
  130. })
  131. } catch (e) {
  132. console.error(e)
  133. jsonResponse(res, 'error', {
  134. responseType: 3,
  135. message: 'Internal Server error!',
  136. })
  137. }
  138. },
  139. /**
  140. * Check Appointment Details
  141. *
  142. * @memberOf frontend.cart
  143. * @param {cartModule.getAllAvailableAppointments} modules
  144. */
  145. checkAppointments: async (req, res) => {
  146. try {
  147. const [records] = await Promise.all([cartModule.getAllAvailableAppointments(req)])
  148. const responseData = {
  149. records,
  150. }
  151. jsonResponse(res, 'success', {
  152. responseType: 1,
  153. message: 'Details successfully retrieved!',
  154. responseData,
  155. })
  156. } catch (e) {
  157. console.error(e)
  158. jsonResponse(res, 'error', {
  159. responseType: 3,
  160. message: 'Internal Server error!',
  161. })
  162. }
  163. },
  164. /**
  165. * Search Cart Items
  166. *
  167. * @memberOf frontend.cart
  168. * @param {frontend.cart.getSingleCartDetails} modules
  169. * @param {cartModule.getAllCarts} modules
  170. */
  171. search: async (req, res) => {
  172. try {
  173. await schemaModule.search().validateSync(req.body)
  174. const allCarts = []
  175. const getallCartsFunction = async (cartID) => {
  176. const [responseData] = await Promise.all([getSingleCartDetails(req, cartID)])
  177. allCarts.push(responseData)
  178. }
  179. const [allCartsUsers, nonCartItems] = await Promise.all([
  180. cartModule.getAllCarts(req),
  181. getNonCartItems(req),
  182. ])
  183. await commonFunction.asyncForEach(allCartsUsers, async (carts) => {
  184. await getallCartsFunction(carts.id)
  185. })
  186. const responseData = {
  187. nonCartItems,
  188. cartItems: allCarts,
  189. }
  190. jsonResponse(res, 'success', {
  191. responseType: 1,
  192. message: 'Details successfully retrieved!',
  193. responseData,
  194. })
  195. } catch (e) {
  196. console.error(e)
  197. jsonResponse(res, 'error', {
  198. responseType: 3,
  199. message: 'Internal Server error!',
  200. })
  201. }
  202. },
  203. /**
  204. * Get Pending Cart Items
  205. *
  206. * @memberOf frontend.cart
  207. * @param {cartModule.getAllPendingCartItems} modules
  208. * @param {cartModule.getAllPendingCartID} modules
  209. */
  210. pendingCount: async (req, res) => {
  211. try {
  212. const [allPendingCarts] = await Promise.all([cartModule.getAllPendingCartID(req)])
  213. const [allPendingProducts] = await Promise.all([
  214. cartModule.getAllPendingCartItems(req, allPendingCarts[0].pendingCart),
  215. ])
  216. const responseData = {
  217. count: allPendingProducts[0].pendingCount,
  218. }
  219. jsonResponse(res, 'success', {
  220. responseType: 1,
  221. message: 'Details successfully retrieved!',
  222. responseData,
  223. })
  224. } catch (e) {
  225. console.error(e)
  226. jsonResponse(res, 'error', {
  227. responseType: 3,
  228. message: 'Internal Server error!',
  229. })
  230. }
  231. },
  232. /**
  233. * Change Status of the cart
  234. *
  235. * @memberOf frontend.cart
  236. * @param {frontend.cart.checkAndAddtoCart} modules
  237. */
  238. changeStatus: async (req, res) => {
  239. try {
  240. await schemaModule.changeStatus().validateSync(req.body)
  241. const [response] = await Promise.all([checkAndAddtoCart(req)])
  242. if (response.success) {
  243. jsonResponse(res, 'success', {
  244. responseType: 1,
  245. message:
  246. req.body.cart === 1
  247. ? 'Successfully added to cart!'
  248. : 'Successfully removed from cart!',
  249. })
  250. } else {
  251. jsonResponse(res, 'error', {
  252. responseType: 2,
  253. message: 'Cart action failed!',
  254. })
  255. }
  256. } catch (e) {
  257. console.error(e)
  258. jsonResponse(res, 'error', {
  259. responseType: 3,
  260. message: 'Internal Server error!',
  261. })
  262. }
  263. },
  264. /**
  265. * Cancel Item from one cart
  266. *
  267. * @memberOf frontend.cart
  268. * @param {frontend.cart.checkAndAddtoCart} modules
  269. * @param {frontend.return.relistProduct} modules
  270. */
  271. cancelItem: async (req, res) => {
  272. try {
  273. await schemaModule.changeStatus().validateSync(req.body)
  274. const [response] = await Promise.all([checkAndAddtoCart(req)])
  275. if (response.success) {
  276. await Promise.all([returnCtrl.relistProduct(req.body.id, 'relisted')])
  277. jsonResponse(res, 'success', {
  278. responseType: 1,
  279. message:
  280. req.body.cart === 1
  281. ? 'Successfully added to cart!'
  282. : 'Successfully removed from cart!',
  283. })
  284. } else {
  285. jsonResponse(res, 'error', {
  286. responseType: 2,
  287. message: 'Cart action failed!',
  288. })
  289. }
  290. } catch (e) {
  291. console.error(e)
  292. jsonResponse(res, 'error', {
  293. responseType: 3,
  294. message: 'Internal Server error!',
  295. })
  296. }
  297. },
  298. /**
  299. * Save Address for the cart
  300. *
  301. * @memberOf frontend.cart
  302. * @param {cartModule.updateCartData} modules
  303. */
  304. saveAddress: async (req, res) => {
  305. try {
  306. await schemaModule.saveAddress().validateSync(req.body)
  307. await Promise.all([cartModule.updateCartData(req.body, req.body.cart_id)])
  308. jsonResponse(res, 'success', {
  309. responseType: 1,
  310. message: 'Billing Address Updated!',
  311. })
  312. } catch (e) {
  313. console.error(e)
  314. jsonResponse(res, 'error', {
  315. responseType: 3,
  316. message: 'Internal Server error!',
  317. })
  318. }
  319. },
  320. }