/* ============================================================================ *\
|| ########################################################################## ||
|| # 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 # ||
|| # ---------------------------------------------------------------------- # ||
|| ########################################################################## ||
\* ============================================================================ */
/* eslint-disable prefer-destructuring */
/* eslint-disable no-param-reassign */
const cartModule = require('../../modules/cart').default
const commonFunction = require('../../../common/function').default
const userModule = require('../../modules/user').default
const returnCtrl = require('../return')
const schemaModule = require('./schema').default
const { jsonResponse } = require('../logger')
const shortDescribeSCH = async (items) => {
function changingdata(item) {
item.avatarorg =
item.file_name === '' || item.file_name === null
? `${global.s3_static_image_url}images/pics-coming.jpg`
: `${global.s3_static_image_url}uploads/product/${item.file_name}`
return item
}
const promises = items.map(changingdata)
items = await Promise.all(promises)
return items
}
const cartItemsCalculations = async (items) => {
const cartValues = {
per_total: 0,
total_amount: 0,
total_items: 0,
tax_percent: 0,
buyer_premium: 0,
total_premium: 0,
total_tax: 0,
total_shipping: 0,
}
function changingdata(item) {
cartValues.per_total += parseFloat(item.buynowamount)
cartValues.total_items += 1
}
const promises = items.map(changingdata)
items = await Promise.all(promises)
cartValues.tax_percent = 10
cartValues.buyer_premium = 15
cartValues.total_tax = (cartValues.per_total * (cartValues.tax_percent / 100)).toFixed(2)
cartValues.total_premium = (cartValues.per_total * (cartValues.buyer_premium / 100)).toFixed(2)
const totalTaxPremium = commonFunction.sumFloat(cartValues.total_premium, cartValues.total_tax)
cartValues.total_amount = commonFunction.sumFloat(
parseFloat(cartValues.per_total).toFixed(2),
parseFloat(totalTaxPremium).toFixed(2),
)
return cartValues
}
const getorCreateCartID = async (req, userDetails, locationID) => {
let cartId = 0
const resultcart = await Promise.all([cartModule.checkUserCartExists(req, locationID)])
if (resultcart[0].length > 0) {
cartId = resultcart[0][0].id
} else {
const results = await Promise.all([
cartModule.addNewCartEntry(req, userDetails, locationID),
])
cartId = results[0].insertId
}
return cartId
}
const getSingleCartDetails = async (req, cartID) => {
req.body.cart_id = cartID
let [cartItems, cartValues, cartLocation] = await Promise.all([
cartModule.allCartItems(req, req.body, 1),
cartModule.getCartDetails(req.body.cart_id),
cartModule.cartLocationDetails(req.body.cart_id),
])
cartItems = await shortDescribeSCH(cartItems)
const responseData = { cartItems, cartValues: cartValues[0], cartLocation: cartLocation[0] }
return responseData
}
const checkAndAddtoCart = async (req) => {
let success = false
const resultuser = await Promise.all([
cartModule.checkUserProductExists(req, req.body.id),
userModule.userDetails(req.user.id),
])
const userDetails = resultuser[1][0]
if (resultuser[0].length > 0) {
req.body.cart_id = await getorCreateCartID(req, userDetails, resultuser[0][0].location_id)
const cartChange = req.body.cart === 1 ? req.body.cart_id : 0
await Promise.all([cartModule.changeCartStatus(req, cartChange, req.body.id)])
const [cartItems] = await Promise.all([cartModule.allCartItems(req, req.body, 1)])
const cartValues = await cartItemsCalculations(cartItems)
await Promise.all([cartModule.updateCartData(cartValues, req.body.cart_id)])
success = true
} else {
success = false
}
const responseData = { success }
return responseData
}
const getNonCartItems = async (req) => {
let [nonCartItems] = await Promise.all([cartModule.allCartItems(req, req.body, 0)])
nonCartItems = await shortDescribeSCH(nonCartItems)
const responseData = nonCartItems
return responseData
}
module.exports = {
getSingleCartDetails,
checkAndAddtoCart,
/**
* Get Single Cart Details
*
* @memberOf frontend.cart
* @param {frontend.cart.getSingleCartDetails} modules
*/
singleCart: async (req, res) => {
try {
await schemaModule.search().validateSync(req.body)
const [responseData] = await Promise.all([getSingleCartDetails(req, req.body.cart_id)])
jsonResponse(res, 'success', {
responseType: 1,
message: 'Details successfully retrieved!',
responseData,
})
} catch (e) {
console.error(e)
jsonResponse(res, 'error', {
responseType: 3,
message: 'Internal Server error!',
})
}
},
/**
* Check Appointment Details
*
* @memberOf frontend.cart
* @param {cartModule.getAllAvailableAppointments} modules
*/
checkAppointments: async (req, res) => {
try {
const [records] = await Promise.all([cartModule.getAllAvailableAppointments(req)])
const responseData = {
records,
}
jsonResponse(res, 'success', {
responseType: 1,
message: 'Details successfully retrieved!',
responseData,
})
} catch (e) {
console.error(e)
jsonResponse(res, 'error', {
responseType: 3,
message: 'Internal Server error!',
})
}
},
/**
* Search Cart Items
*
* @memberOf frontend.cart
* @param {frontend.cart.getSingleCartDetails} modules
* @param {cartModule.getAllCarts} modules
*/
search: async (req, res) => {
try {
await schemaModule.search().validateSync(req.body)
const allCarts = []
const getallCartsFunction = async (cartID) => {
const [responseData] = await Promise.all([getSingleCartDetails(req, cartID)])
allCarts.push(responseData)
}
const [allCartsUsers, nonCartItems] = await Promise.all([
cartModule.getAllCarts(req),
getNonCartItems(req),
])
await commonFunction.asyncForEach(allCartsUsers, async (carts) => {
await getallCartsFunction(carts.id)
})
const responseData = {
nonCartItems,
cartItems: allCarts,
}
jsonResponse(res, 'success', {
responseType: 1,
message: 'Details successfully retrieved!',
responseData,
})
} catch (e) {
console.error(e)
jsonResponse(res, 'error', {
responseType: 3,
message: 'Internal Server error!',
})
}
},
/**
* Get Pending Cart Items
*
* @memberOf frontend.cart
* @param {cartModule.getAllPendingCartItems} modules
* @param {cartModule.getAllPendingCartID} modules
*/
pendingCount: async (req, res) => {
try {
const [allPendingCarts] = await Promise.all([cartModule.getAllPendingCartID(req)])
const [allPendingProducts] = await Promise.all([
cartModule.getAllPendingCartItems(req, allPendingCarts[0].pendingCart),
])
const responseData = {
count: allPendingProducts[0].pendingCount,
}
jsonResponse(res, 'success', {
responseType: 1,
message: 'Details successfully retrieved!',
responseData,
})
} catch (e) {
console.error(e)
jsonResponse(res, 'error', {
responseType: 3,
message: 'Internal Server error!',
})
}
},
/**
* Change Status of the cart
*
* @memberOf frontend.cart
* @param {frontend.cart.checkAndAddtoCart} modules
*/
changeStatus: async (req, res) => {
try {
await schemaModule.changeStatus().validateSync(req.body)
const [response] = await Promise.all([checkAndAddtoCart(req)])
if (response.success) {
jsonResponse(res, 'success', {
responseType: 1,
message:
req.body.cart === 1
? 'Successfully added to cart!'
: 'Successfully removed from cart!',
})
} else {
jsonResponse(res, 'error', {
responseType: 2,
message: 'Cart action failed!',
})
}
} catch (e) {
console.error(e)
jsonResponse(res, 'error', {
responseType: 3,
message: 'Internal Server error!',
})
}
},
/**
* Cancel Item from one cart
*
* @memberOf frontend.cart
* @param {frontend.cart.checkAndAddtoCart} modules
* @param {frontend.return.relistProduct} modules
*/
cancelItem: async (req, res) => {
try {
await schemaModule.changeStatus().validateSync(req.body)
const [response] = await Promise.all([checkAndAddtoCart(req)])
if (response.success) {
await Promise.all([returnCtrl.relistProduct(req.body.id, 'relisted')])
jsonResponse(res, 'success', {
responseType: 1,
message:
req.body.cart === 1
? 'Successfully added to cart!'
: 'Successfully removed from cart!',
})
} else {
jsonResponse(res, 'error', {
responseType: 2,
message: 'Cart action failed!',
})
}
} catch (e) {
console.error(e)
jsonResponse(res, 'error', {
responseType: 3,
message: 'Internal Server error!',
})
}
},
/**
* Save Address for the cart
*
* @memberOf frontend.cart
* @param {cartModule.updateCartData} modules
*/
saveAddress: async (req, res) => {
try {
await schemaModule.saveAddress().validateSync(req.body)
await Promise.all([cartModule.updateCartData(req.body, req.body.cart_id)])
jsonResponse(res, 'success', {
responseType: 1,
message: 'Billing Address Updated!',
})
} catch (e) {
console.error(e)
jsonResponse(res, 'error', {
responseType: 3,
message: 'Internal Server error!',
})
}
},
}