admin/controllers/product/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 adminProductModule = require('../../modules/product').default
const schemaModule = require('./schema').default
const commonFunction = require('../../../common/function').default
const returnCtrl = require('../../../front/controllers/return')

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
}

module.exports = {
    /**
     * Get all project listing
     *
     * @memberOf adminside.product
     * @param {adminProductModule.fetchProductsAll} modules
     * @param {adminProductModule.fetchActiveAuctions} modules
     */
    listings: async (req, res) => {
        req.body.action = typeof req.body.action === 'undefined' ? 'open' : req.body.action
        let records = []
        let totalRecords = []
        let activelots = []
        try {
            ;[records, totalRecords, activelots] = await Promise.all([
                adminProductModule.fetchProductsAll(req, 0),
                adminProductModule.fetchProductsAll(req, 1),
                adminProductModule.fetchActiveAuctions(),
            ])
            records = await shortDescribeSCH(records)
        } catch (e) {
            console.error(e)
            jsonResponse(res, 'error', {
                responseType: 3,
                message: 'Internal Server error!',
            })
        } finally {
            let responseData = { records, totalRecords, activelots }
            responseData = await commonFunction.getdynamicinnercontents(req, responseData)
            jsonResponse(res, 'success', {
                responseType: 1,
                message: 'Details successfully retrieved!',
                responseData,
            })
        }
    },
    /**
     * Change Product Status
     *
     * @memberOf adminside.product
     * @param {frontend.return.relistProduct} modules
     * @param {adminProductModule.marketStatusUpdate} modules
     */
    changeStatus: async (req, res) => {
        try {
            if (req.body.action === 'relist') {
                const processAllItems = async (items) => {
                    await Promise.all([returnCtrl.relistProduct(items, 'relisted')])
                    return true
                }
                const resultloop = []
                req.body.project_id.forEach(async (element) => {
                    const looptoformat = await processAllItems(element)
                    resultloop.push(looptoformat)
                })
                await Promise.all(resultloop)
                jsonResponse(res, 'success', 'Relisted successfully')
            } else if (req.body.action && req.body.project_id.length > 0) {
                await adminProductModule.marketStatusUpdate(req.body.action, req.body.project_id)
            }
        } catch (e) {
            console.error(e)
            jsonResponse(res, 'error', {
                responseType: 3,
                message: 'Internal Server error!',
            })
        } finally {
            const responseData = {}
            jsonResponse(res, 'success', {
                responseType: 1,
                message: 'Status successfully changed',
                responseData,
            })
        }
    },
}