All files / dist api.js

61.84% Statements 47/76
48.57% Branches 17/35
63.33% Functions 19/30
61.84% Lines 47/76
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256      1x 1x 1x 1x 1x     1x 1x                 2x     1x 2360x                   1x 40x 2360x             11x         11x 11x   11x         11x                                                             4x     4x                                     1x 1x             1x         1x   1x                               1x 1x                           1x         1x         1x     1x   1x 1x         1x                                                                                             1x 1x   1x   1x 1x 1x 1x 1x           1x         1x 1x 1x           1x        
'use strict';
 
// Dependencies
var moment = require('moment-timezone');
var web = require('./web');
var parser = require('./parser');
var Url = require('url');
var debug = require('debug')('node-cba-netbank');
 
// Constant
moment.tz.setDefault('Australia/Sydney');
var LINK = {
  BASE: 'https://www.my.commbank.com.au',
  LOGIN: '/netbank/Logon/Logon.aspx',
  TRANSACTION: '/netbank/TransactionHistory/History.aspx?RID=:RID&SID=:SID',
  EXPORT: '/netbank/TransactionHistory/Exports.aspx?RID=:RID&SID=:SID&ExportType=OFX'
};
 
//  Utilities
function toDateString(timestamp) {
  return moment(timestamp).format('DD/MM/YYYY');
}
 
var isSameTransaction = function isSameTransaction(left, right) {
  return (
    //  case 1. Have same receipt number, of course, it's not empty
    left.receiptnumber && left.receiptnumber === right.receiptnumber ||
    //  case 2. Same time, same description and same amount
    left.timestamp === right.timestamp && left.description === right.description && left.amount === right.amount
  );
};
 
//  concat 2 transactionList without any duplications.
function concat(a, b) {
  return a.concat(b.filter(function (vb) {
    return !a.find(function (va) {
      return isSameTransaction(va, vb);
    });
  }));
}
 
//  Return `${LINK.BASE}+${path}`
function getUrl(path) {
  return Url.resolve(LINK.BASE, path);
}
 
//  Change the BASE link if it's been redirected.
function refreshBase(resp) {
  var oldLink = Url.parse(LINK.BASE);
  var newLink = Url.parse(resp.url);
 
  Iif (oldLink.host !== newLink.host) {
    debug('refreshBase(' + oldLink.host + ' => ' + newLink.host);
    oldLink.host = newLink.host;
    LINK.BASE = Url.format(oldLink);
  }
  return resp;
}
 
//  If the search panel is lazy loading, not only the transactions data is not in
//  the page, the search page widget is not ready as well. So, search request will
//  fail. To workaround such problem, we send an update panel partial callback
//  first,let server side prepared the search panel and download transaction.
//  Then, do the real search using the information from the parital callback result.
function lazyLoading(response, account) {
  debug('lazyLoading(account: ' + account.name + ' [' + account.number + '] => ' + account.available + '))');
  return web.post({
    url: getUrl(account.link),
    form: Object.assign({}, response.form, {
      //  Send partial request
      ctl00$ctl00: 'ctl00$BodyPlaceHolder$UpdatePanelForAjax|ctl00$BodyPlaceHolder$UpdatePanelForAjaxh',
      __EVENTTARGET: 'ctl00$BodyPlaceHolder$UpdatePanelForAjax',
      __EVENTARGUMENT: 'doPostBackApiCall|LoadRecentTransactions|false'
    }),
    partial: true
  }).then(parser.parseViewState).then(function (resp) {
    return parser.parseForm(resp).catch(function () {
      return resp;
    });
  }).then(function (resp) {
    return Object.assign({}, resp, { form: Object.assign({}, response.form, resp.form) });
  });
}
 
//  API
function login(credentials) {
  //  retrieve the login page
  return web.get(getUrl(LINK.LOGIN)
  //  Parse the page to get login form
  ).then(refreshBase).then(parser.parseForm).then(function (resp) {
    return (
      //  fill the login form and submit
      web.post({
        url: getUrl(LINK.LOGIN),
        form: Object.assign({}, resp.form, {
          txtMyClientNumber$field: credentials.username,
          txtMyPassword$field: credentials.password,
          chkRemember$field: 'on',
          //  and make JS detector happy
          JS: 'E'
        })
      })
    );
  }).then(refreshBase
  //  parse the home page to retrieve the accounts list
  ).then(parser.parseHomePage);
}
 
function getMoreTransactions(response, account) {
  debug('getMoreTransactions(account: ' + account.name + ' [' + account.number + '] => ' + account.available + ')');
  var form = Object.assign({}, response.form, {
    // fill the form
    ctl00$ctl00: 'ctl00$BodyPlaceHolder$UpdatePanelForAjax|ctl00$BodyPlaceHolder$UpdatePanelForAjax',
    __EVENTTARGET: 'ctl00$BodyPlaceHolder$UpdatePanelForAjax',
    __EVENTARGUMENT: 'doPostBackApiCall|LoadTransactions|{"ClearCache":"false","IsSorted":false,"IsAdvancedSearch":true,"IsMonthSearch":false}'
  });
  // send the request
  return web.post({
    url: getUrl(account.link),
    form: form,
    partial: true
  }).then(parser.parseTransactions).then(refreshBase).then(function (resp) {
    Eif (!resp.more || resp.limit) {
      //  There is no more transactions or reached the limit.
      return resp;
    }
    return getMoreTransactions(Object.assign({}, resp, { form: form }), Object.assign({}, account, { link: Url.parse(resp.url).path })
    //  concat more transactions.
    ).then(function (r) {
      return Object.assign({}, r, { form: form, transactions: concat(resp.transactions, r.transactions) });
    }
    //  Ignore the error as we have got some transactions already
    ).catch(function (error) {
      debug(error);
      return resp;
    });
  });
}
 
function getTransactionsByDate(response, account, from, to) {
  debug('getTransactionsByDate(account: ' + account.name + ' [' + account.number + '] => ' + account.available + ', from: ' + from + ', to: ' + to + ')');
  var form = Object.assign({}, response.form, {
    // fill the form
    ctl00$ctl00: 'ctl00$BodyPlaceHolder$updatePanelSearch|ctl00$BodyPlaceHolder$lbSearch',
    __EVENTTARGET: 'ctl00$BodyPlaceHolder$lbSearch',
    __EVENTARGUMENT: '',
    ctl00$BodyPlaceHolder$searchTypeField: '1',
    ctl00$BodyPlaceHolder$radioSwitchDateRange$field$: 'ChooseDates',
    ctl00$BodyPlaceHolder$dateRangeField: 'ChooseDates',
    ctl00$BodyPlaceHolder$fromCalTxtBox$field: from,
    ctl00$BodyPlaceHolder$toCalTxtBox$field: to,
    //  Add this for partial update
    ctl00$BodyPlaceHolder$radioSwitchSearchType$field$: 'AllTransactions'
  });
 
  return web.post({
    url: getUrl(account.link),
    form: form,
    partial: true
  }).then(parser.parseTransactions).then(refreshBase).then(function (resp) {
    Iif (!resp.more || resp.limit) {
      //  there is no more transactions or reached the limit.
      return resp;
    }
    //  There are more transactions available.
    return getMoreTransactions(Object.assign({}, resp, { form: form }), Object.assign({}, account, { link: Url.parse(resp.url).path })
    //  concat more transactions.
    ).then(function (r) {
      return Object.assign({}, r, { form: form, transactions: concat(resp.transactions, r.transactions) });
    }).then(function (r) {
      debug('getTransactionsByDate(): getMoreTransactions(): More => ' + r.more + ', Limit => ' + r.limit);
      Iif (r.more && r.limit) {
        //  if there are more transactions, however it reaches limit
        //  we need to send another search request to overcome the limit.
        throw Object.assign(new Error('Reach transations limit'), { response: r });
      }
      return r;
    }).catch(function (error) {
      //  an error happend during load more, it means that it may have more,
      //  however, some restriction made it stopped, so we call it again,
      //  but this time, we use the eariliest date from the transactions
      //  we retrieved so far as the toDate, so it might workaround this
      //  problem.
 
      debug(error);
 
      //  if there is a `response` object attached to `error` object, that means
      //  it's just reach the limit, and contains transations. Otherwise, it don't
      //  have the transactions, a real error, then use previous `resp` instead.
      var r = error.response || resp;
      //  find the earliest date as new 'to' date.
      var timestamp = r.transactions[0].timestamp;
      r.transactions.forEach(function (t) {
        if (timestamp > t.timestamp) {
          timestamp = t.timestamp;
        }
      });
      var newTo = toDateString(timestamp);
 
      // Call self again
      debug('Call getTransactionsByDate() again with new \'to\' date (' + to + ' => ' + newTo + ')');
      return getTransactionsByDate(Object.assign({}, r, { form: form }), Object.assign({}, account, { link: Url.parse(r.url).path }), from, newTo
      //  concat more transactions
      ).then(function (rr) {
        return Object.assign({}, rr, { form: form, transactions: concat(r.transactions, rr.transactions) });
      }).catch(function (err) {
        //  cannot call it again, but we got some transactions at least,
        //  so, just call it a success.
        debug(err);
        debug('getTransactionsByDate(): failed to call self again to load more');
        return Object.assign({}, r, { form: form, transactions: r.transactions });
      });
    });
  });
}
 
//  Get transaction history data for given time period.
//
//  * `from`: Default download begin at 6 years ago. FYI, I tried 8 years
//    without getting any error message, however, keep in mind that bank
//    usually only stored 2 years transactions history data.
//  * `to`: Default value is today.
function getTransactions(account) {
  var from = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : toDateString(moment().subtract(6, 'years').valueOf());
  var to = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : toDateString(moment().valueOf());
 
  debug('getTransactions(account: ' + account.name + ' [' + account.number + '] => ' + account.available + ')');
  //  retrieve post form and key for the given account
  return web.get(getUrl(account.link)).then(parser.parseTransactionPage).then(refreshBase).then(function (resp) {
    var acc = Object.assign({}, account, { link: Url.parse(resp.url).path });
    Eif (resp.accounts !== null) {
      acc.key = resp.accounts.find(function (a) {
        return a.number === account.number;
      }).key;
    }
 
    //  if the transaction section is lazy loading, we need do a panel update
    //  first, before the real search.
    Iif (!resp.form.ctl00$BodyPlaceHolder$radioSwitchSearchType$field$) {
      return lazyLoading(resp, acc).then(function (r) {
        return getTransactionsByDate(r, acc, from, to);
      });
    }
    return getTransactionsByDate(resp, acc, from, to).then(function (r) {
      debug('getTransactions(): Total received ' + r.transactions.length + ' transactions.');
      return r;
    });
  });
}
 
//  Exports
module.exports = {
  toDateString: toDateString,
  login: login,
  getTransactions: getTransactions
};