front/controllers/auction/index.js

/* ============================================================================ *\
|| ########################################################################## ||
|| # 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 jwt = require('jsonwebtoken')
const auctionModule = require('../../modules/auction').default

const schemaModule = require('./schema').default
const { jsonResponse } = require('../logger')

/**
 * Get Dynamic contects for the pagination for auction
 *
 * @memberOf frontend.auction
 * @param {object} req req data with images which has to be uploaded
 * @param {number} responseData response Data which has to be retrieved
 */
const getdynamicinnercontents = async (req, responseData) => {
    const returnResponse = responseData
    returnResponse.totalRecords = returnResponse.totalRecords[0]
        ? returnResponse.totalRecords[0].totallength
        : 0
    let disp = (parseInt(req.body.page, 10) - 1) * req.body.limit + 1
    const afifen = (parseInt(req.body.page, 10) - 1) * req.body.limit + parseInt(req.body.limit, 10)
    const orgifen = afifen > returnResponse.totalRecords ? returnResponse.totalRecords : afifen
    disp = returnResponse.totalRecords === 0 ? 0 : disp
    returnResponse.setDisp = `${disp}-${orgifen}`
    return returnResponse
}

/**
 * Add image link to the data
 *
 * @memberOf frontend.auction
 * @param {array} items Items array in which image url is added to the name
 */
const shortDescribeSCHOnlyAvatar = 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/auction/${item.file_name}`
        return item
    }
    const promises = items.map(changingdata)
    items = await Promise.all(promises)
    return items
}

module.exports = {
    /**
     * Search Auction Items
     *
     * @memberOf frontend.auction
     * @param {auctionModule.searchAuctions} modules
     * @param {frontend.auction.shortDescribeSCHOnlyAvatar} modules
     * @param {frontend.auction.getdynamicinnercontents} modules
     */
    search: async (req, res) => {
        try {
            await schemaModule.search().validateSync(req.body)
            let [records, totalRecords] = await Promise.all([
                auctionModule.searchAuctions(req, req.body, 1),
                auctionModule.searchAuctions(req, req.body, 0),
            ])
            totalRecords = [{ totallength: totalRecords.length }]
            let responseData = { records, totalRecords }
            records = await shortDescribeSCHOnlyAvatar(records)
            responseData = await getdynamicinnercontents(req, responseData)
            jsonResponse(res, 'success', {
                responseType: 1,
                message: 'Details successfully retrieved!',
                responseData,
            })
        } catch (e) {
            console.error(e)
            jsonResponse(res, 'error', {
                responseType: 3,
                message: 'Internal Server error!',
            })
        }
    },
}