Open-source personal finance manager. Self-hosted, privacy-first.
feat(transactions): require an explicit account in new transaction forms (#1194)
patouinparis7956bb56c11b
diff --git a/frontend/src/components/transaction-dialog.test.tsx b/frontend/src/components/transaction-dialog.test.tsxindex 58da7f7c..80ceee80 100644--- a/frontend/src/components/transaction-dialog.test.tsx+++ b/frontend/src/components/transaction-dialog.test.tsx@@ -4,7 +4,7 @@ import { afterEach, expect, it, vi } from 'vitest'import { TransactionDialog } from '@/components/transaction-dialog'import { currencies, groups, payees } from '@/lib/api'import { renderWithProviders, t } from '@/test/utils'-import type { RecurringTransaction } from '@/types'+import type { Account, RecurringTransaction } from '@/types'vi.mock('@/contexts/auth-context', () => ({useAuth: () => ({ user: { preferences: { currency_display: 'USD' } } }),@@ -71,3 +71,85 @@ it.each(['America/Los_Angeles', 'Pacific/Kiritimati'])(}},)++const accountFixtures = [+ { id: 'acc-z', name: 'Zeta Bank', type: 'checking', currency: 'USD' },+ { id: 'acc-a', name: 'Alpha Card', type: 'checking', currency: 'USD' },+] as Account[]++function renderNewTransaction(defaultAccountId?: string) {+ vi.spyOn(currencies, 'list').mockResolvedValue([])+ vi.spyOn(payees, 'list').mockResolvedValue([])+ vi.spyOn(groups, 'list').mockResolvedValue([])+ renderWithProviders(+ <TransactionDialog+ open+ onClose={vi.fn()}+ transaction={null}+ categories={[]}+ categoryGroups={[]}+ accounts={accountFixtures}+ defaultAccountId={defaultAccountId}+ onSave={vi.fn()}+ loading={false}+ error={null}+ />,+ )+ return screen.getByRole('option', { name: 'Zeta Bank' }).closest('select') as HTMLSelectElement+}++it('starts a new transaction with no account selected, so one must be chosen', () => {+ const accountSelect = renderNewTransaction()++ expect(accountSelect).toHaveValue('')+ // The placeholder shows in the closed field but is hidden from the open list.+ const placeholder = accountSelect.selectedOptions[0]+ expect(placeholder).toHaveTextContent(t('transactions.createCounterpartSelectAccount'))+ expect(placeholder).toBeDisabled()+ expect(placeholder).toHaveAttribute('hidden')+ expect(accountSelect.validity.valueMissing).toBe(true)+})++it('keeps preselecting the account the form was opened from', () => {+ const accountSelect = renderNewTransaction('acc-z')++ expect(accountSelect).toHaveValue('acc-z')+ expect(screen.queryByText(t('transactions.createCounterpartSelectAccount'))).not.toBeInTheDocument()+})++it('forgets a save variant the browser blocked, so a later plain Save just saves', async () => {+ vi.spyOn(currencies, 'list').mockResolvedValue([])+ vi.spyOn(payees, 'list').mockResolvedValue([])+ vi.spyOn(groups, 'list').mockResolvedValue([])+ const onSave = vi.fn()+ const { user } = renderWithProviders(+ <TransactionDialog+ open+ onClose={vi.fn()}+ transaction={null}+ categories={[]}+ categoryGroups={[]}+ accounts={accountFixtures}+ onSave={onSave}+ loading={false}+ error={null}+ />,+ )++ // "Save and new" with no account: the browser blocks the submit.+ await user.click(screen.getByRole('button', { name: t('transactions.moreSaveOptions') }))+ await user.click(await screen.findByRole('menuitem', { name: t('transactions.saveAndNew') }))+ expect(onSave).not.toHaveBeenCalled()++ // Complete the form and use the plain Save button.+ const accountSelect = screen.getByRole('option', { name: 'Zeta Bank' }).closest('select') as HTMLSelectElement+ const form = accountSelect.closest('form') as HTMLFormElement+ const [description, amount] = Array.from(form.querySelectorAll<HTMLInputElement>('input[required]'))+ await user.type(description, 'Groceries')+ await user.type(amount, '12')+ await user.selectOptions(accountSelect, 'acc-z')+ await user.click(screen.getByRole('button', { name: t('common.save') }))++ expect(onSave).toHaveBeenCalledTimes(1)+ expect(onSave.mock.calls[0][4]).toBe('save')+})diff --git a/frontend/src/components/transaction-dialog.tsx b/frontend/src/components/transaction-dialog.tsxindex b972a55d..e5ab5ec2 100644--- a/frontend/src/components/transaction-dialog.tsx+++ b/frontend/src/components/transaction-dialog.tsx@@ -458,7 +458,7 @@ function TransactionForm({)const [categoryId, setCategoryId] = useState(seed?.category_id ?? '')const [payeeId, setPayeeId] = useState(seed?.payee_id ?? '')- const [accountId, setAccountId] = useState(seed?.account_id ?? defaultAccountId ?? sortedAccounts[0]?.id ?? '')+ const [accountId, setAccountId] = useState(seed?.account_id ?? defaultAccountId ?? '')const [notes, setNotes] = useState(seed?.notes ?? '')// Manual CC bucketing override (issue #92). Empty = auto. Visible only// when the selected account is a credit card.@@ -658,7 +658,14 @@ function TransactionForm({const triggerSubmit = (action: SaveAction) => {pendingActionRef.current = action- formRef.current?.requestSubmit()+ try {+ // The submit handler runs synchronously inside requestSubmit() and reads+ // the action first. If validation blocks the submit, the handler never+ // runs, so reset here or a later plain Save would reuse this action.+ formRef.current?.requestSubmit()+ } finally {+ pendingActionRef.current = 'save'+ }}const showSaveVariants = isCreating && !isSynced@@ -1180,6 +1187,7 @@ function TransactionForm({onChange={(e) => setAccountId(e.target.value)}required>+ {!accountId && <option value="" disabled hidden>{t('transactions.createCounterpartSelectAccount')}</option>}{sortedAccounts.map((acc) => (<option key={acc.id} value={acc.id}>{getAccountLabel(acc)}</option>))}diff --git a/frontend/src/locales/cs.json b/frontend/src/locales/cs.jsonindex 3c8ca1cc..76695782 100644--- a/frontend/src/locales/cs.json+++ b/frontend/src/locales/cs.json@@ -1529,7 +1529,6 @@"endDate": "Datum konce (volitelné)","category": "Kategorie","account": "Účet",- "noAccount": "Žádný","nextOccurrence": "Nejbližší","status": "Stav","actions": "Akce",diff --git a/frontend/src/locales/de.json b/frontend/src/locales/de.jsonindex d37e7845..9dc9a31f 100644--- a/frontend/src/locales/de.json+++ b/frontend/src/locales/de.json@@ -1438,7 +1438,6 @@"endDate": "Enddatum (optional)","category": "Kategorie","account": "Konto",- "noAccount": "Keins","nextOccurrence": "Nächste","status": "Status","actions": "Aktionen",diff --git a/frontend/src/locales/el.json b/frontend/src/locales/el.jsonindex 5137c70d..cf30b9c4 100644--- a/frontend/src/locales/el.json+++ b/frontend/src/locales/el.json@@ -1437,7 +1437,6 @@"endDate": "Ημερομηνία λήξης (προαιρετικό)","category": "Κατηγορία","account": "Λογαριασμός",- "noAccount": "Κανένας","nextOccurrence": "Επόμενη","status": "Κατάσταση","actions": "Ενέργειες",diff --git a/frontend/src/locales/en.json b/frontend/src/locales/en.jsonindex 64c066d4..55be8df1 100644--- a/frontend/src/locales/en.json+++ b/frontend/src/locales/en.json@@ -1437,7 +1437,6 @@"endDate": "End date (optional)","category": "Category","account": "Account",- "noAccount": "None","nextOccurrence": "Next","status": "Status","actions": "Actions",diff --git a/frontend/src/locales/es.json b/frontend/src/locales/es.jsonindex fb4f1669..c482e7fb 100644--- a/frontend/src/locales/es.json+++ b/frontend/src/locales/es.json@@ -1441,7 +1441,6 @@"endDate": "Fecha de fin (opcional)","category": "Categoría","account": "Cuenta",- "noAccount": "Ninguna","nextOccurrence": "Siguiente","status": "Estado","actions": "Acciones",diff --git a/frontend/src/locales/fr.json b/frontend/src/locales/fr.jsonindex ca972640..3cd35b21 100644--- a/frontend/src/locales/fr.json+++ b/frontend/src/locales/fr.json@@ -1441,7 +1441,6 @@"endDate": "Date de fin (optionnel)","category": "Catégorie","account": "Compte",- "noAccount": "Aucun","nextOccurrence": "Prochaine","status": "Statut","actions": "Actions",diff --git a/frontend/src/locales/hi.json b/frontend/src/locales/hi.jsonindex 69e7614c..8a81820e 100644--- a/frontend/src/locales/hi.json+++ b/frontend/src/locales/hi.json@@ -1437,7 +1437,6 @@"endDate": "अंत तिथि (वैकल्पिक)","category": "श्रेणी","account": "खाता",- "noAccount": "कोई नहीं","nextOccurrence": "अगला","status": "स्थिति","actions": "कार्रवाइयाँ",diff --git a/frontend/src/locales/hu.json b/frontend/src/locales/hu.jsonindex d21b338d..c77fca94 100644--- a/frontend/src/locales/hu.json+++ b/frontend/src/locales/hu.json@@ -1437,7 +1437,6 @@"endDate": "Záró dátum (nem kötelező)","category": "Kategória","account": "Számla",- "noAccount": "Nincs","nextOccurrence": "Következő","status": "Állapot","actions": "Műveletek",diff --git a/frontend/src/locales/it.json b/frontend/src/locales/it.jsonindex 0b5e0d61..766cf6e8 100644--- a/frontend/src/locales/it.json+++ b/frontend/src/locales/it.json@@ -1441,7 +1441,6 @@"endDate": "Data di fine (facoltativa)","category": "Categoria","account": "Conto",- "noAccount": "Nessuno","nextOccurrence": "Prossima","status": "Stato","actions": "Azioni",diff --git a/frontend/src/locales/ja.json b/frontend/src/locales/ja.jsonindex 0a1a359d..27b2b5cf 100644--- a/frontend/src/locales/ja.json+++ b/frontend/src/locales/ja.json@@ -1437,7 +1437,6 @@"endDate": "終了日(任意)","category": "カテゴリ","account": "口座",- "noAccount": "なし","nextOccurrence": "次回","status": "ステータス","actions": "操作",diff --git a/frontend/src/locales/nl.json b/frontend/src/locales/nl.jsonindex 4d9a466e..1b4f448e 100644--- a/frontend/src/locales/nl.json+++ b/frontend/src/locales/nl.json@@ -1439,7 +1439,6 @@"endDate": "Einddatum (optioneel)","category": "Categorie","account": "Rekening",- "noAccount": "Geen","nextOccurrence": "Volgende","status": "Status","actions": "Acties",diff --git a/frontend/src/locales/pl.json b/frontend/src/locales/pl.jsonindex 4119c8cd..163cda38 100644--- a/frontend/src/locales/pl.json+++ b/frontend/src/locales/pl.json@@ -1496,7 +1496,6 @@"endDate": "Data zakończenia (opcjonalnie)","category": "Kategoria","account": "Konto",- "noAccount": "Brak","nextOccurrence": "Następna","status": "Status","actions": "Akcje",diff --git a/frontend/src/locales/pt-BR.json b/frontend/src/locales/pt-BR.jsonindex f2742aed..73636288 100644--- a/frontend/src/locales/pt-BR.json+++ b/frontend/src/locales/pt-BR.json@@ -1441,7 +1441,6 @@"endDate": "Data de fim (opcional)","category": "Categoria","account": "Conta",- "noAccount": "Nenhuma","nextOccurrence": "Próxima","status": "Status","actions": "Ações",diff --git a/frontend/src/locales/pt-PT.json b/frontend/src/locales/pt-PT.jsonindex 94e3ee95..0c320e2f 100644--- a/frontend/src/locales/pt-PT.json+++ b/frontend/src/locales/pt-PT.json@@ -1441,7 +1441,6 @@"endDate": "Data de fim (opcional)","category": "Categoria","account": "Conta",- "noAccount": "Nenhuma","nextOccurrence": "Próxima","status": "Estado","actions": "Ações",diff --git a/frontend/src/locales/ru.json b/frontend/src/locales/ru.jsonindex b3ca250c..386359bf 100644--- a/frontend/src/locales/ru.json+++ b/frontend/src/locales/ru.json@@ -1454,7 +1454,6 @@"endDate": "Дата окончания (опционально)","category": "Категория","account": "Счёт",- "noAccount": "Нет","nextOccurrence": "Следующая","status": "Статус","actions": "Действия",diff --git a/frontend/src/locales/sk.json b/frontend/src/locales/sk.jsonindex 568a4e80..5cbbcb9f 100644--- a/frontend/src/locales/sk.json+++ b/frontend/src/locales/sk.json@@ -1513,7 +1513,6 @@"endDate": "Dátum konca (voliteľné)","category": "Kategória","account": "Účet",- "noAccount": "Žiadny","nextOccurrence": "Najbližšie","status": "Stav","actions": "Akcie",diff --git a/frontend/src/locales/uk.json b/frontend/src/locales/uk.jsonindex bace692f..40a8d10f 100644--- a/frontend/src/locales/uk.json+++ b/frontend/src/locales/uk.json@@ -1454,7 +1454,6 @@"endDate": "Дата закінчення (необов'язково)","category": "Категорія","account": "Рахунок",- "noAccount": "Немає","nextOccurrence": "Наступна","status": "Статус","actions": "Дії",diff --git a/frontend/src/pages/recurring.tsx b/frontend/src/pages/recurring.tsxindex fa081c8e..21a13bf2 100644--- a/frontend/src/pages/recurring.tsx+++ b/frontend/src/pages/recurring.tsx@@ -339,7 +339,7 @@ function RecurringForm({const [startDate, setStartDate] = useState(recurring?.start_date ?? localDateString())const [endDate, setEndDate] = useState(recurring?.end_date ?? '')const [categoryId, setCategoryId] = useState(recurring?.category_id ?? '')- const [accountId, setAccountId] = useState(recurring?.account_id ?? sortedAccounts[0]?.id ?? '')+ const [accountId, setAccountId] = useState(recurring?.account_id ?? '')const [isActive, setIsActive] = useState(recurring?.is_active ?? true)const [autoGenerate, setAutoGenerate] = useState(recurring?.auto_generate ?? true)@@ -454,7 +454,7 @@ function RecurringForm({onChange={(e) => setAccountId(e.target.value)}required>- {!accountId && <option value="" disabled>{t('recurring.noAccount')}</option>}+ {!accountId && <option value="" disabled hidden>{t('transactions.createCounterpartSelectAccount')}</option>}{sortedAccounts.map((acc) => (<option key={acc.id} value={acc.id}>{getAccountName(acc)}</option>))}