partner.js 2 KB
const moment = require('moment');

const logger = require('tektrans-logger');
const config = require('komodo-sdk/config');
const pull = require('komodo-sdk/gateway/pull');

function buy(task, xid) {
    logger.verbose('Got task from CORE', { xid, task });
    const [remoteProduct, amount] = task.remote_product.split(/ *, */);
    pull.report({
        trx_id: task.trx_id,
        rc: '68',
        message: {
            xid,
            msg: 'Waiting for result',
        },
    }, xid);

    setTimeout(
        () => {
            const destination = task.destination.toString();

            if ((destination % 10) === 0) {
                pull.report({
                    trx_id: task.trx_id,
                    rc: '68',
                    balance: config.supplier_ending_balance,
                    message: {
                        xid,
                        msg: `PENDING karena nomor tujuan ${task.destination} diakhiri 0`,
                    },
                }, xid);
            } else if (destination % 2) {
                pull.report({
                    trx_id: task.trx_id,
                    rc: '14',
                    balance: config.supplier_ending_balance,
                    message: {
                        xid,
                        msg: `GAGAL karena nomor tujuan ${task.destination} adalah nomor ganjil`,
                    },
                }, xid);
            } else {
                pull.report({
                    trx_id: task.trx_id,
                    rc: '00',
                    sn: moment().format('YYYYMMDDHHmmssSSS'),
                    amount,
                    remote_product: remoteProduct,
                    balance: config.supplier_ending_balance,
                    message: {
                        xid,
                        msg: `BERHASIL karena nomor tujuan ${task.destination} adalah nomor genap`,
                    },
                }, xid);
            }
        },
        (config.max_result_timeout_ms || 2000) * Math.random(),
    );
}

exports.buy = buy;