Open source personal finance application
refactor(wallets): extract pure wallet filter logic and add tests
Ilya Komichev9a24123b5da2
diff --git a/app/components/wallets/filters.test.ts b/app/components/wallets/filters.test.tsnew file mode 100644index 00000000..7dfe2609--- /dev/null+++ b/app/components/wallets/filters.test.ts@@ -0,0 +1,141 @@+import { describe, expect, it } from 'vitest'++import type { WalletItemComputed } from '~/components/wallets/types'++import { filterWalletsByCurrency, filterWalletsByViewType } from '~/components/wallets/filters'++function wallet(overrides: Partial<WalletItemComputed> & Pick<WalletItemComputed, 'amount' | 'currency' | 'type'>): WalletItemComputed {+ return {+ color: 'blue',+ desc: '',+ isArchived: false,+ isExcludeInTotal: false,+ isWithdrawal: false,+ name: 'Test',+ order: 0,+ updatedAt: 0,+ ...overrides,+ } as WalletItemComputed+}++const wallets = {+ archived: wallet({ amount: 0, currency: 'USD', isArchived: true, type: 'cash' }),+ creditUSD: wallet({ amount: -100, creditLimit: 5000, currency: 'USD', type: 'credit' }),+ cryptoBTC: wallet({ amount: 1, currency: 'BTC', type: 'crypto' }),+ excludeUSD: wallet({ amount: 500, currency: 'USD', isExcludeInTotal: true, type: 'cashless' }),+ mainRUB: wallet({ amount: 5000, currency: 'RUB', type: 'cashless' }),+ mainUSD: wallet({ amount: 1000, currency: 'USD', type: 'cashless' }),+ withdrawalUSD: wallet({ amount: 3000, currency: 'USD', isWithdrawal: true, type: 'cashless' }),+} as Record<string, WalletItemComputed>++const allIds = Object.keys(wallets)++describe('filterWalletsByCurrency', () => {+ it('returns all wallets when currencyFilter is "all"', () => {+ const result = filterWalletsByCurrency(wallets, 'type', 'all')+ expect(result).toEqual(allIds)+ })++ it('returns all wallets when groupedBy is "currency"', () => {+ const result = filterWalletsByCurrency(wallets, 'currency', 'USD')+ expect(result).toEqual(allIds)+ })++ it('filters by specific currency', () => {+ const result = filterWalletsByCurrency(wallets, 'type', 'USD')+ expect(result).toEqual(['archived', 'creditUSD', 'excludeUSD', 'mainUSD', 'withdrawalUSD'])+ })++ it('filters by RUB', () => {+ const result = filterWalletsByCurrency(wallets, 'type', 'RUB')+ expect(result).toEqual(['mainRUB'])+ })++ it('returns empty for currency with no wallets', () => {+ const result = filterWalletsByCurrency(wallets, 'type', 'EUR')+ expect(result).toEqual([])+ })++ it('returns all when groupedBy is "none"', () => {+ const result = filterWalletsByCurrency(wallets, 'none', 'all')+ expect(result).toEqual(allIds)+ })+})++describe('filterWalletsByViewType', () => {+ it('total: excludes archived', () => {+ const result = filterWalletsByViewType(allIds, wallets, 'total')+ expect(result).not.toContain('archived')+ expect(result).toContain('mainUSD')+ expect(result).toContain('creditUSD')+ expect(result).toContain('excludeUSD')+ })++ it('isWithdrawal: returns withdrawal non-archived wallets', () => {+ const result = filterWalletsByViewType(allIds, wallets, 'isWithdrawal')+ expect(result).toEqual(['withdrawalUSD'])+ })++ it('isWithdrawal: excludes archived withdrawal wallets', () => {+ const walletsWithArchivedWithdrawal = {+ ...wallets,+ archivedW: wallet({ amount: 0, currency: 'USD', isArchived: true, isWithdrawal: true, type: 'cashless' }),+ }+ const ids = Object.keys(walletsWithArchivedWithdrawal)+ const result = filterWalletsByViewType(ids, walletsWithArchivedWithdrawal, 'isWithdrawal')+ expect(result).not.toContain('archivedW')+ })++ it('isExcludeInTotal: returns excluded wallets', () => {+ const result = filterWalletsByViewType(allIds, wallets, 'isExcludeInTotal')+ expect(result).toEqual(['excludeUSD'])+ })++ it('isArchived: returns only archived wallets', () => {+ const result = filterWalletsByViewType(allIds, wallets, 'isArchived')+ expect(result).toEqual(['archived'])+ })++ it('isAvailable: returns credit and withdrawal, excludes archived', () => {+ const result = filterWalletsByViewType(allIds, wallets, 'isAvailable')+ expect(result).toContain('creditUSD')+ expect(result).toContain('withdrawalUSD')+ expect(result).not.toContain('mainUSD')+ expect(result).not.toContain('archived')+ })++ it('isAvailable: excludes archived credit wallets', () => {+ const walletsWithArchivedCredit = {+ ...wallets,+ archivedCredit: wallet({ amount: -50, creditLimit: 1000, currency: 'USD', isArchived: true, type: 'credit' }),+ }+ const ids = Object.keys(walletsWithArchivedCredit)+ const result = filterWalletsByViewType(ids, walletsWithArchivedCredit, 'isAvailable')+ expect(result).not.toContain('archivedCredit')+ })++ it('cash: returns only cash wallets', () => {+ const result = filterWalletsByViewType(allIds, wallets, 'cash')+ expect(result).toEqual(['archived'])+ })++ it('cashless: returns cashless wallets', () => {+ const result = filterWalletsByViewType(allIds, wallets, 'cashless')+ expect(result).toEqual(['excludeUSD', 'mainRUB', 'mainUSD', 'withdrawalUSD'])+ })++ it('credit: returns only credit wallets', () => {+ const result = filterWalletsByViewType(allIds, wallets, 'credit')+ expect(result).toEqual(['creditUSD'])+ })++ it('crypto: returns only crypto wallets', () => {+ const result = filterWalletsByViewType(allIds, wallets, 'crypto')+ expect(result).toEqual(['cryptoBTC'])+ })++ it('skips missing wallet IDs', () => {+ const result = filterWalletsByViewType(['nonexistent', 'mainUSD'], wallets, 'total')+ expect(result).toEqual(['mainUSD'])+ })+})diff --git a/app/components/wallets/filters.ts b/app/components/wallets/filters.tsnew file mode 100644index 00000000..fb665785--- /dev/null+++ b/app/components/wallets/filters.ts@@ -0,0 +1,33 @@+import type { WalletId, WalletItemComputed, WalletsCurrencyFiltered, WalletsGroupedBy, WalletViewTypes } from '~/components/wallets/types'++export function filterWalletsByCurrency(+ wallets: Record<WalletId, WalletItemComputed>,+ groupedBy: WalletsGroupedBy,+ currencyFilter: WalletsCurrencyFiltered,+): WalletId[] {+ const ids = Object.keys(wallets)+ if (groupedBy === 'currency' || currencyFilter === 'all')+ return ids+ return ids.filter(id => wallets[id]?.currency === currencyFilter)+}++export function filterWalletsByViewType(+ walletIds: WalletId[],+ wallets: Record<WalletId, WalletItemComputed>,+ viewType: WalletViewTypes | 'total',+): WalletId[] {+ return walletIds.filter((id) => {+ const wallet = wallets[id]+ if (!wallet)+ return false++ switch (viewType) {+ case 'isWithdrawal': return wallet.isWithdrawal && !wallet.isArchived+ case 'isExcludeInTotal': return wallet.isExcludeInTotal+ case 'isArchived': return wallet.isArchived+ case 'isAvailable': return (wallet.type === 'credit' || wallet.isWithdrawal) && !wallet.isArchived+ case 'total': return !wallet.isArchived+ default: return wallet.type === viewType+ }+ })+}diff --git a/app/components/wallets/page/List.vue b/app/components/wallets/page/List.vueindex 63caf872..1f3ee226 100644--- a/app/components/wallets/page/List.vue+++ b/app/components/wallets/page/List.vue@@ -12,7 +12,7 @@ import { useWalletsStore } from '~/components/wallets/useWalletsStore'import { showSuccessToast } from '~/composables/useStoreSync'import { useWalletsCounts } from '~/components/wallets/useWalletsCounts'-import { useWalletsPageFilter } from './useWalletsPageFilter'+import { useWalletsFilter } from '~/components/wallets/useWalletsFilter'import { useWalletsPageGrouping } from './useWalletsPageGrouping'const { t } = useI18n()@@ -83,7 +83,7 @@ const {selectedWalletsIdsWithCurrency,setWalletViewType,walletViewType,-} = useWalletsPageFilter(groupedBy)+} = useWalletsFilter(groupedBy)const {counts,diff --git a/app/components/wallets/useWalletsFilter.ts b/app/components/wallets/useWalletsFilter.tsnew file mode 100644index 00000000..77316831--- /dev/null+++ b/app/components/wallets/useWalletsFilter.ts@@ -0,0 +1,45 @@+import type { Ref } from 'vue'++import { useStorage } from '@vueuse/core'++import type { CurrencyCode } from '~/components/currencies/types'+import type { WalletId, WalletsCurrencyFiltered, WalletsGroupedBy, WalletType, WalletViewTypes } from '~/components/wallets/types'++import { filterWalletsByCurrency, filterWalletsByViewType } from '~/components/wallets/filters'+import { useWalletsStore } from '~/components/wallets/useWalletsStore'++export function useWalletsFilter(groupedBy: Ref<WalletsGroupedBy>) {+ const walletsStore = useWalletsStore()++ const currencyFiltered = useStorage<WalletsCurrencyFiltered>('finapp-wallets-active-currency', 'all')+ const walletViewType = useStorage<WalletViewTypes | 'total'>('finapp-wallets-active-type', 'total')++ function setWalletViewType(v: WalletType | 'total') {+ walletViewType.value = v+ }++ const selectedWalletsIdsWithCurrency = computed<WalletId[]>(() =>+ filterWalletsByCurrency(walletsStore.itemsComputed, groupedBy.value, currencyFiltered.value),+ )++ const selectedWalletsIds = computed<WalletId[]>(() =>+ filterWalletsByViewType(selectedWalletsIdsWithCurrency.value, walletsStore.itemsComputed, walletViewType.value),+ )++ function onSelectFilterCurrency(code: CurrencyCode, toggle?: () => void) {+ if (currencyFiltered.value === code && toggle)+ toggle()++ walletViewType.value = 'total'+ currencyFiltered.value = code+ }++ return {+ currencyFiltered,+ onSelectFilterCurrency,+ selectedWalletsIds,+ selectedWalletsIdsWithCurrency,+ setWalletViewType,+ walletViewType,+ }+}