/* ============================================================================ *\
|| ########################################################################## ||
|| # 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 adminInvoiceModule = require('../../modules/invoice').default
const buynowModule = require('../../../front/modules/buynow').default
const schemaModule = require('./schema').default
const commonFunction = require('../../../common/function').default
const { jsonResponse } = require('../logger')
const { autopayment } = require('../../../cron/controller')
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 Invoices
*
* @memberOf adminside.invoice
* @param {adminInvoiceModule.fetchInvoicesAll} modules
*/
invoices: async (req, res) => {
req.body.action = typeof req.body.action === 'undefined' ? 'open' : req.body.action
let records = []
let totalRecords = []
try {
;[records, totalRecords] = await Promise.all([
adminInvoiceModule.fetchInvoicesAll(req, 0),
adminInvoiceModule.fetchInvoicesAll(req, 1),
])
totalRecords = [{ totallength: totalRecords.length }]
records = await shortDescribeSCH(records)
} catch (e) {
console.error(e)
jsonResponse(res, 'error', {
responseType: 3,
message: 'Internal Server error!',
})
} finally {
let responseData = { records, totalRecords }
responseData = await commonFunction.getdynamicinnercontents(req, responseData)
jsonResponse(res, 'success', {
responseType: 1,
message: 'Details successfully retrieved!',
responseData,
})
}
},
/**
* Change Invoice status
*
* @memberOf adminside.invoice
* @param {adminInvoiceModule.invoiceStatusUpdate} modules
* @param {adminInvoiceModule.invoiceBuynowStatusUpdate} modules
*/
changeStatus: async (req, res) => {
try {
if (req.body.action === 'autopaid') {
if (req.body.project_id) {
await commonFunction.asyncForEach(req.body.project_id, async (element) => {
const [data] = await Promise.all([
adminInvoiceModule.buynowActiveStatusUpdate(req.body.action, element),
])
await autopayment(...data)
})
}
} else if (req.body.action && req.body.project_id.length > 0) {
await Promise.all([
adminInvoiceModule.invoiceStatusUpdate(req.body.action, req.body.project_id),
/* adminInvoiceModule.invoiceBuynowStatusUpdate(
req.body.action,
req.body.project_id,
), */
])
if (req.body.action === 'paid') {
if (
typeof global.configFeatures.invoice_address !== 'undefined' &&
global.configFeatures.invoice_address.enabled
) {
const [productData] = await Promise.all([
adminInvoiceModule.invoiceBuynowUserId(req.body.project_id),
])
await Promise.all([
buynowModule.updateAddress(
req.body.project_id,
productData[0].user_id,
'bill',
),
])
}
}
if (req.body.action === 'unpaid') {
if (
typeof global.configFeatures.invoice_generated_mail !== 'undefined' &&
global.configFeatures.invoice_generated_mail.enabled
) {
await commonFunction.asyncForEach(req.body.project_id, async (element) => {
const invoiceDetails = {
body: {},
}
invoiceDetails.body.filters = invoiceDetails.body.filters
? invoiceDetails.body.filters
: {}
invoiceDetails.body.filters.invoice = {
value: element,
field: 'b.id',
type: 'in',
}
const [details] = await Promise.all([
adminInvoiceModule.fetchInvoicesAllNew(invoiceDetails, 0),
])
if (details.length) {
const invoiceRetrieved = details[0]
await Promise.all([
commonFunction.eventTriggerFuction(
{
...invoiceRetrieved,
},
invoiceRetrieved.users_email,
invoiceRetrieved.users_phone,
'invoice_generated',
0,
1,
invoiceRetrieved.user_id,
),
])
}
})
}
}
}
const responseData = {}
} 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,
})
}
},
/**
* Change Invoice Partial status
*
* @memberOf adminside.invoice
* @param {adminInvoiceModule.buynowPartialStatusUpdate} modules
*/
updateVerifedStatus: async (req, res) => {
try {
if (req.body.id.length > 0) {
await Promise.all([
adminInvoiceModule.buynowPartialStatusUpdate(req.body.action, req.body.id),
])
if (typeof global.configFeatures.move_to_unpaid !== 'undefined') {
if (parseInt(global.configFeatures.move_to_unpaid.enabled, 10) === 1) {
if (req.body.action === 'unpaid') {
await commonFunction.asyncForEach(req.body.id, async (element) => {
req.body.refer_id = element
req.body.refer_field = global.configFeatures.move_to_unpaid.cat_name
req.body.type = 'move_to_unpaid'
await Promise.all([commonFunction.notifyinsert(req)])
})
}
}
}
}
} 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,
})
}
},
allinvoices: async (req, res) => {
req.body.action =
typeof req.body.filters.action.value === 'undefined'
? 'open'
: req.body.filters.action.value
delete req.body.filters.action
let records = []
let totalRecords = []
try {
;[records, totalRecords] = await Promise.all([
adminInvoiceModule.fetchInvoicesAllNew(req, 0),
adminInvoiceModule.fetchInvoicesAllNew(req, 1),
])
totalRecords = [{ totallength: totalRecords[0].totallength }]
records = await shortDescribeSCH(records)
} catch (e) {
console.error(e)
jsonResponse(res, 'error', {
responseType: 3,
message: 'Internal Server error!',
})
} finally {
let responseData = { records, totalRecords }
responseData = await commonFunction.getdynamicinnercontents(req, responseData)
jsonResponse(res, 'success', {
responseType: 1,
message: 'Details successfully retrieved!',
responseData,
})
}
},
}