127.0.0.1:8000 budget / master server / external_apis / gmail / email_parsers / dcu_transfer.js
master

Tree @master (Download .tar.gz)

dcu_transfer.js @masterraw · history · blame

const {
  get_date_header_parser,
  parsers_html_wrapper
} = require('./helpers');

module.exports = function (account_name) {
  return {
    q: `no-reply@notification-alerts.dcu.org subject:("Large Withdrawal Alert for ${account_name}" OR "Large Deposit Alert for ${account_name}")`,
    headers: get_date_header_parser(),
    html: parsers_html_wrapper(function (root) {
      const transaction = {};
      const deposit_text = 'The following deposits have posted:'
      const withdrawal_text = 'The following withdrawals have posted:'

      const ps = root.querySelectorAll('p');
      for (const p of ps) {
        for (const search_text of [withdrawal_text, deposit_text]) {
          if (p.innerText.includes(search_text)) {
            const [portion_with_value] = p.innerText.split(search_text).slice(1);
            for (const text of portion_with_value.split(' ')) {
              if (text.startsWith('$')) {
                return {
                  // Don't multiply by -1 here, DCU already
                  // reports a negative value for withdrawals.
                  value: Number(text.replace('$', '').replace(',', '')),
                }
              }
            }
          }
        }
      }

      console.error('DCU Transfer parser failed to find all data');
      return transaction;
    })
  }
};