UNPKG

monzolib

Version:

Fully Featured JS/Node Monzo Library

131 lines (130 loc) 4.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const date_fns_1 = require("date-fns"); const lodash_1 = require("lodash"); const Amount_1 = require("./Amount"); var GroupingStrategy; (function (GroupingStrategy) { GroupingStrategy["Day"] = "day"; GroupingStrategy["Category"] = "category"; GroupingStrategy["Merchant"] = "merchant"; GroupingStrategy["None"] = "none"; })(GroupingStrategy = exports.GroupingStrategy || (exports.GroupingStrategy = {})); exports.groupTransactions = (txs, method = "none") => { const groupKey = { ["day"]: (tx) => { return date_fns_1.startOfDay(tx.created).toISOString(); }, ["category"]: (tx) => { return tx.category.formatted; }, ["merchant"]: (tx) => { if (typeof tx.merchant === 'string') { return tx.merchant; } else { return tx.merchant ? tx.merchant.groupId : tx.counterparty.user_id ? 'monzo-contacts' : 'top-ups'; } }, ["none"]: () => { return 'unsorted'; }, }; return lodash_1.map(lodash_1.groupBy(txs, groupKey[method]), (txs, id) => ({ id, method, txs, })); }; exports.getGroupTitle = (group) => { const titleFns = { ["day"]: (group) => { const created = date_fns_1.parseISO(group.id); const now = Date.now(); if (date_fns_1.isSameDay(now, created)) { return 'Today'; } else if (date_fns_1.isSameDay(created, date_fns_1.subDays(now, 1))) { return 'Yesterday'; } else if (date_fns_1.isSameYear(created, now)) { return date_fns_1.format(created, 'EEEE, do MMMM'); } else { return date_fns_1.format(created, 'EEEE, do MMMM yyyy'); } }, ["category"]: (group) => { return group.txs[0].category.formatted; }, ["merchant"]: (group) => { const tx = group.txs[0]; if (typeof tx.merchant === 'string') { return tx.merchant; } else { return tx.merchant ? tx.merchant.name : tx.counterparty.user_id ? 'Monzo Contacts' : 'Top Ups'; } }, ["none"]: () => { return 'Unsorted'; }, }; return titleFns[group.method](group); }; exports.sumGroup = (txs) => { const filtered = txs .filter((tx) => !tx.is.metaAction || !tx.declined) .filter((tx) => tx.amount.negative); const sum = lodash_1.sumBy(filtered, (tx) => tx.amount.raw); return new Amount_1.Amount({ domestic: { amount: sum, currency: txs[0].amount.currency, }, }); }; function extractBalanceAndSpent(bal) { const domesticBalance = { amount: bal.balance, currency: bal.currency, }; const domesticSpend = { amount: bal.spend_today, currency: bal.currency, }; if (bal.local_currency) { const localBalance = { amount: bal.balance * bal.local_exchange_rate, currency: bal.local_currency, }; const localSpend = { amount: bal.local_spend.length > 0 ? bal.local_spend[0].spend_today * bal.local_exchange_rate : 0, currency: bal.local_currency, }; return { balance: new Amount_1.Amount({ domestic: domesticBalance, local: localBalance, }), spent: new Amount_1.Amount({ domestic: domesticSpend, local: localSpend }), }; } else { return { balance: new Amount_1.Amount({ domestic: domesticBalance }), spent: new Amount_1.Amount({ domestic: domesticSpend }), }; } } exports.extractBalanceAndSpent = extractBalanceAndSpent;