|
| 1 | +import startOfMonth from 'date-fns/startOfMonth'; |
| 2 | +import subMonths from 'date-fns/subMonths'; |
| 3 | +import isAfter from 'date-fns/isAfter'; |
| 4 | +import { getToday } from '../lib'; |
| 5 | +import { |
| 6 | + getAccounts, |
| 7 | + getTransactions, |
| 8 | + filterAccounts, |
| 9 | + Transaction, |
| 10 | + Account, |
| 11 | +} from '../moneymoney'; |
| 12 | + |
| 13 | +import { BudgetState, IncomeCategory, VERSION } from './Types'; |
| 14 | + |
| 15 | +function getStartBalance( |
| 16 | + startDate: Date, |
| 17 | + transactions: Transaction[], |
| 18 | + accounts: Account[], |
| 19 | +): number { |
| 20 | + const transactionsSinceStart = transactions.filter(({ bookingDate }) => |
| 21 | + isAfter(bookingDate, startDate), |
| 22 | + ); |
| 23 | + const transactionBal = transactionsSinceStart.reduce( |
| 24 | + (m, { amount }) => m + amount, |
| 25 | + 0, |
| 26 | + ); |
| 27 | + const accountsBal = accounts.reduce((m, { balance }) => m + balance, 0); |
| 28 | + |
| 29 | + return accountsBal + transactionBal * -1; |
| 30 | +} |
| 31 | + |
| 32 | +function isLaterHalfOfMonth({ bookingDate }: Transaction) { |
| 33 | + return bookingDate.getDate() >= 15; |
| 34 | +} |
| 35 | + |
| 36 | +function getIncomeCategories(transactions: Transaction[]): IncomeCategory[] { |
| 37 | + const transactionsByCat = transactions.reduce((memo, transaction) => { |
| 38 | + const { categoryUuid, amount } = transaction; |
| 39 | + |
| 40 | + if (!memo[categoryUuid]) { |
| 41 | + memo[categoryUuid] = { transactions: [], balance: 0, hasNegative: false }; |
| 42 | + } |
| 43 | + |
| 44 | + memo[categoryUuid].transactions.push(transaction); |
| 45 | + memo[categoryUuid].balance += amount; |
| 46 | + if (amount < 0) { |
| 47 | + memo[categoryUuid].hasNegative = true; |
| 48 | + } |
| 49 | + |
| 50 | + return memo; |
| 51 | + }, {} as { [key: string]: { transactions: Transaction[]; balance: number; hasNegative: boolean } }); |
| 52 | + |
| 53 | + const positiveCats = Object.entries(transactionsByCat) |
| 54 | + .filter(([_, { hasNegative }]) => !hasNegative) |
| 55 | + .sort(([_, { balance: a }], [__, { balance: b }]) => b - a); |
| 56 | + |
| 57 | + return positiveCats.map(([id, { transactions }]) => ({ |
| 58 | + id, |
| 59 | + availableIn: transactions.some(isLaterHalfOfMonth) ? 1 : 0, |
| 60 | + })); |
| 61 | +} |
| 62 | + |
| 63 | +export default async function createInitialState(): Promise<BudgetState> { |
| 64 | + const [allAccounts, allTransactions] = await Promise.all([ |
| 65 | + getAccounts(), |
| 66 | + getTransactions(), |
| 67 | + ]); |
| 68 | + const currenciesWithUsage = allAccounts.reduce( |
| 69 | + (memo, { group, currency }) => { |
| 70 | + if (group) { |
| 71 | + return memo; |
| 72 | + } |
| 73 | + memo[currency] = (memo[currency] || 0) + 1; |
| 74 | + return memo; |
| 75 | + }, |
| 76 | + { USD: 1 } as { [key: string]: number }, |
| 77 | + ); |
| 78 | + const currenciesByUsage = Object.entries(currenciesWithUsage) |
| 79 | + .sort(([_, a], [__, b]) => b - a) |
| 80 | + .map(([c]) => c); |
| 81 | + const currency = currenciesByUsage[0]; |
| 82 | + const accounts = filterAccounts(currency, allAccounts).filter( |
| 83 | + ({ group, portfolio }) => !group && !portfolio, |
| 84 | + ); |
| 85 | + const accountUuids = accounts.map(({ uuid }) => uuid); |
| 86 | + const transactionsOfAccounts = allTransactions.filter(({ accountUuid }) => |
| 87 | + accountUuids.includes(accountUuid), |
| 88 | + ); |
| 89 | + const startDate = startOfMonth(subMonths(getToday(), 1)); |
| 90 | + |
| 91 | + return { |
| 92 | + name: '', |
| 93 | + version: VERSION, |
| 94 | + budgets: {}, |
| 95 | + settings: { |
| 96 | + currency, |
| 97 | + incomeCategories: getIncomeCategories(transactionsOfAccounts), |
| 98 | + accounts: accountUuids, |
| 99 | + fractionDigits: 2, |
| 100 | + startDate: startDate.getTime(), |
| 101 | + startBalance: getStartBalance( |
| 102 | + startDate, |
| 103 | + transactionsOfAccounts, |
| 104 | + accounts, |
| 105 | + ), |
| 106 | + }, |
| 107 | + }; |
| 108 | +} |
0 commit comments