admin/modules/refund.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  # ||
|| # ---------------------------------------------------------------------- # ||
|| ########################################################################## ||
\* ============================================================================ */

const dateFormat = require('dateformat')
const md5 = require('md5')
const _ = require('underscore')
const moment = require('moment')
const mysqclass = require('./mysqli').default
/**
 * @class class to handle communication functions
 */
class adminRefundModule {
    /**
     * Mark Refund Cart as Paid
     * @param {number} cartID cart ID which has to be marked as paid
     * @param {number} paid paid status 0 or 1
     * @returns {object} sql response
     */
    static async markReturnCartPaid(cartID, paid) {
        const mysql = {}
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
        const escapeData = [paid, dateNow, cartID]
        const strQuery = await mysqclass.mysqli(mysql, 'refund_confirm_payment_success_cart')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Mark Buy Now Refund confirmation
     * @param {number} cartID cart ID which has to be marked as paid
     * @param {number} paid paid status 0 or 1
     * @returns {object} sql response
     */
    static async markBuyNowRefundConfirm(cartID, paid) {
        const mysql = {}
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
        const escapeData = [paid, dateNow, cartID]
        const strQuery = await mysqclass.mysqli(mysql, 'refund_update_payment_success_buynow')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Update Buy Now Appointment
     * @param {number} cartID cart ID which has to be marked as paid
     * @param {number} aptnID appointment ID which has to be updated
     * @returns {object} sql response
     */
    static async updateBuynowAppointment(cartID, aptnID) {
        const mysql = {}
        const escapeData = [aptnID, cartID]
        const strQuery = await mysqclass.mysqli(mysql, 'update_appointment_success_buynow')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    static async addAppointment(apntTime, userID, locationID, aptnDate) {
        const mysql = {}
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
        const aptnDateTime = dateFormat(
            new Date(`${moment(aptnDate, 'MMM Do YYYY').format('YYYY-MM-DD')} ${apntTime}:00`),
            'yyyy-mm-dd HH:MM:ss',
        )
        const escapeData = [aptnDateTime, userID, locationID, dateNow, 1]
        const strQuery = await mysqclass.mysqli(mysql, 'insert_appointments_table')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    static async insertPayLogRequest(cartID, userID, payType, gateWay) {
        const mysql = {}
        const escapeData = [userID, cartID, gateWay, payType]
        const strQuery = await mysqclass.mysqli(mysql, 'insert_payment_request_log')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    static async updatePayLogRequest(data, id, type) {
        let postData = data
        const mysqli = {}
        const acceptedObjects = [
            'requested_at',
            'response_at',
            'request',
            'response',
            'transactionID',
            'refTransactionID',
        ]
        postData = _.omit(postData, (value, key) => {
            if (
                _.isNull(value) ||
                _.isUndefined(value) ||
                value === '' ||
                !_.contains(acceptedObjects, key)
            ) {
                return true
            }
            return false
        })
        let escapeData = []

        postData = _.omit(postData, (value, key, object) => {
            if (!_.contains(acceptedObjects, key)) {
                return true
            }
            return false
        })
        let defaultKeys = []
        if (type === 1) {
            defaultKeys = ['requested_at']
        } else {
            defaultKeys = ['response_at']
        }
        const defaultValues = [dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')]
        const keyswithescape = defaultKeys.concat(_.keys(postData))
        escapeData = defaultValues.concat(_.values(postData))
        mysqli.keys = _.map(keyswithescape, (val) => {
            return `${val} = ?`
        })
        mysqli.log_id = id
        const strQuery = await mysqclass.mysqli(mysqli, 'update_payment_log')
        console.log('strQuery', strQuery)
        console.log('escapeData', escapeData)
        const dataPromise = await global.mysql.query(strQuery, escapeData)
        return dataPromise
    }

    /**
     * Insert Pay records
     * @param {object} cardDetails object of the Cart Details
     * @param {object} cartValue object of the Cart Values
     * @param {object} data req.body object
     * @returns {object} sql response
     */
    static async authorizeInsertPayRecord(cardDetails, cartValue, data) {
        const mysql = {}
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
        const escapeData = [
            cartValue.id,
            cartValue.user_id,
            data.transId,
            data.refTransID,
            dateNow,
            'authorize',
            data.accountNumber,
            cardDetails.card_amount,
            data.accountType,
        ]
        const strQuery = await mysqclass.mysqli(mysql, 'insert_refund_record')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    static async getMinimumInvoiceID(cardID) {
        const mysql = {}
        const escapeData = [cardID]
        const strQuery = await mysqclass.mysqli(mysql, 'get_minimum_invoiceid')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    static async getCartDetails(cartID) {
        const mysql = {}
        const escapeData = [cartID]
        const strQuery = await mysqclass.mysqli(mysql, 'return_get_cart_all_data')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    static async allCartItems(req, data, count) {
        let mysql = ''
        let row = ''
        let where = ''
        const order = 'p.date_closed asc'
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
        where +=
            count === 1
                ? `and b.returncart_id = ${req.body.returncart_id}`
                : ' and b.returncart_id = 0'

        row = 'return_cart_all_item'
        let uidc = 0
        if (req.user) {
            uidc = typeof req.user.id === 'undefined' ? 0 : req.user.id
        }
        const escapeData = [req.body.invoice_id]
        const ordergor = order === '' ? '' : `order by ${order}`
        mysql = {
            where,
            order: ordergor,
            dateNow,
            userid: uidc,
        }

        const strQuery = await mysqclass.mysqli(mysql, row)
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    static async cartLocationDetails(cartID) {
        const mysql = {}
        const escapeData = [cartID]
        const strQuery = await mysqclass.mysqli(mysql, 'return_get_cart_location_data')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Get Pay records
     * @param {number} cartID cart ID which has to be marked as paid
     * @returns {object} sql response
     */
    static async getPayRecords(cartID) {
        const mysql = {}
        const escapeData = [cartID]
        const strQuery = await mysqclass.mysqli(mysql, 'return_get_cart_pay_records')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }
}

module.exports.default = adminRefundModule