listtrx.js 1.77 KB
const coreEndpoint = '/histories/trx';

const config = require('komodo-sdk/config');

const commandError = require('./error');
const coreapi = require('../coreapi');

function help(keyword, commandGroup) {
    return `Untuk melihat list transaksi${ commandGroup === 'listtrxall' ? ' termasuk downline': ''}, ketik perintah dengan format: ${ keyword.toUpperCase() }.<PIN> atau ${ keyword.toUpperCase() }.<TANGGAL>.<PIN>. Format tanggal YYYY-MM-DD, contoh 2018-12-31 untuk tanggal 31 Desember 2018.`;
}

module.exports = (tokens, params, cb) => {
    if (params.commandGroup === 'listtrxall') {
        const blacklisted = ((config.blacklist_help_for_origins || []).indexOf(params.origin || '') >= 0)
            || ((config.blacklist_help_for_origin_transports || []).indexOf(params.origin_transport || '') >= 0);

        if (blacklisted) {
            const body = `Perintah "${tokens[0]}" tdk tersedia pd jalur SMS dan sejenis.`;
            const responseParams = {
                body
            }
    
            cb(null, null, responseParams);
            return;
        }
    }
    
    

    if (!tokens || tokens.length < 2) {
        const responseParams = {
            body: `${ commandError.ERR_INVALID_FORMAT }. ${ help(tokens[0], params.commandGroup) }`
        }

        cb(null, null, responseParams);
        return;
    }

    const idxPin = tokens.length < 3 ? 1 : 2;

    const coreParams = {
        origin: params.origin,
        asker_terminal_name: params.from,
        asker_terminal_password: tokens[idxPin],
        date: tokens.length < 3 ? null : tokens[1],
        include_downlines: params.commandGroup === 'listtrxall',
        dont_include_failed: true,
        limit: 100,
        offset: 0,
        order: 'DESC',
    };

    coreapi(coreEndpoint, coreParams, 'GET', cb);
};