Open-source personal finance manager. Self-hosted, privacy-first.
fix(transactions): require confirmation before unlinking a transfer pair (#1257)
Tássio Noronha510e66057468
diff --git a/frontend/src/components/transaction-dialog.test.tsx b/frontend/src/components/transaction-dialog.test.tsxindex 80ceee80..4512f8ac 100644--- a/frontend/src/components/transaction-dialog.test.tsx+++ b/frontend/src/components/transaction-dialog.test.tsx@@ -1,10 +1,10 @@-import { screen } from '@testing-library/react'+import { screen, within } from '@testing-library/react'import { afterEach, expect, it, vi } from 'vitest'import { TransactionDialog } from '@/components/transaction-dialog'-import { currencies, groups, payees } from '@/lib/api'+import { currencies, groups, payees, transactions } from '@/lib/api'import { renderWithProviders, t } from '@/test/utils'-import type { Account, RecurringTransaction } from '@/types'+import type { Account, RecurringTransaction, Transaction } from '@/types'vi.mock('@/contexts/auth-context', () => ({useAuth: () => ({ user: { preferences: { currency_display: 'USD' } } }),@@ -153,3 +153,103 @@ it('forgets a save variant the browser blocked, so a later plain Save just savesexpect(onSave).toHaveBeenCalledTimes(1)expect(onSave.mock.calls[0][4]).toBe('save')})++function transferLegFixture(pairId: string, over: Partial<Transaction> = {}): Transaction {+ return {+ id: 'tx-1160-leg',+ user_id: 'synthetic-user',+ account_id: 'acc-a',+ category_id: null,+ category: null,+ external_id: null,+ description: 'R1160 transfer repro',+ original_description: null,+ amount: 250,+ currency: 'USD',+ date: '2026-10-15',+ type: 'debit',+ source: 'transfer',+ status: 'posted',+ payee: null,+ payee_id: null,+ payee_name: null,+ notes: null,+ transfer_pair_id: pairId,+ amount_primary: null,+ fx_rate_used: null,+ fx_fallback: false,+ installment_number: null,+ total_installments: null,+ installment_total_amount: null,+ installment_purchase_date: null,+ installment_series_id: null,+ bill_id: null,+ effective_bill_date: null,+ splits: [],+ is_ignored: false,+ ...over,+ }+}++function renderTransferDialog(onUnlinkTransfer: (pairId: string) => void) {+ vi.spyOn(currencies, 'list').mockResolvedValue([])+ vi.spyOn(payees, 'list').mockResolvedValue([])+ vi.spyOn(groups, 'list').mockResolvedValue([])+ vi.spyOn(transactions, 'transferPair').mockResolvedValue(+ transferLegFixture('pair-1160', {+ id: 'tx-1160-counterpart',+ type: 'credit',+ account_id: 'acc-z',+ }),+ )+ return renderWithProviders(+ <TransactionDialog+ open+ onClose={vi.fn()}+ transaction={transferLegFixture('pair-1160')}+ categories={[]}+ categoryGroups={[]}+ accounts={accountFixtures}+ onUnlinkTransfer={onUnlinkTransfer}+ onSave={vi.fn()}+ loading={false}+ error={null}+ />,+ )+}++it('asks for confirmation before unlinking a transfer pair', async () => {+ const onUnlinkTransfer = vi.fn()+ const { user } = renderTransferDialog(onUnlinkTransfer)++ await user.click(await screen.findByRole('button', { name: t('transactions.unlinkTransfer') }))++ expect(await screen.findByText(t('transactions.unlinkTransferConfirm'))).toBeInTheDocument()+ expect(onUnlinkTransfer).not.toHaveBeenCalled()+})++it('keeps the transfer linked when the unlink confirmation is cancelled', async () => {+ const onUnlinkTransfer = vi.fn()+ const { user } = renderTransferDialog(onUnlinkTransfer)++ await user.click(await screen.findByRole('button', { name: t('transactions.unlinkTransfer') }))+ const confirmText = await screen.findByText(t('transactions.unlinkTransferConfirm'))+ const confirmDialog = confirmText.closest('[role="dialog"]') as HTMLElement+ await user.click(within(confirmDialog).getByRole('button', { name: t('common.cancel') }))++ expect(screen.queryByText(t('transactions.unlinkTransferConfirm'))).not.toBeInTheDocument()+ expect(onUnlinkTransfer).not.toHaveBeenCalled()+})++it('unlinks the pair only after the unlink confirmation is accepted', async () => {+ const onUnlinkTransfer = vi.fn()+ const { user } = renderTransferDialog(onUnlinkTransfer)++ await user.click(await screen.findByRole('button', { name: t('transactions.unlinkTransfer') }))+ const confirmText = await screen.findByText(t('transactions.unlinkTransferConfirm'))+ const confirmDialog = confirmText.closest('[role="dialog"]') as HTMLElement+ await user.click(within(confirmDialog).getByRole('button', { name: t('transactions.unlinkTransfer') }))++ expect(onUnlinkTransfer).toHaveBeenCalledTimes(1)+ expect(onUnlinkTransfer).toHaveBeenCalledWith('pair-1160')+})diff --git a/frontend/src/components/transaction-dialog.tsx b/frontend/src/components/transaction-dialog.tsxindex e5ab5ec2..2863bc5e 100644--- a/frontend/src/components/transaction-dialog.tsx+++ b/frontend/src/components/transaction-dialog.tsx@@ -541,6 +541,9 @@ function TransactionForm({const [unlinkingRecurring, setUnlinkingRecurring] = useState(false)const [addToRuleOpen, setAddToRuleOpen] = useState(false)const [extendRuleTarget, setExtendRuleTarget] = useState<Rule | null>(null)+ // Unlinking re-reports the other leg, so the pair link is removed only+ // after an explicit confirmation (issue #1160).+ const [unlinkConfirmOpen, setUnlinkConfirmOpen] = useState(false)const { data: rulesList, isLoading: rulesLoading } = useQuery({queryKey: ['rules'],@@ -921,20 +924,46 @@ function TransactionForm({<p className="text-xs text-blue-500 dark:text-blue-400">{t('transactions.transferTooltip')}</p></div>{onUnlinkTransfer && transaction?.transfer_pair_id && (- <button- type="button"- disabled={loading}- onClick={() => {- if (transaction?.transfer_pair_id) {- onUnlinkTransfer(transaction.transfer_pair_id)- }- }}- className="shrink-0 inline-flex items-center gap-1.5 rounded-md border border-blue-200 dark:border-blue-800 bg-white/60 dark:bg-blue-900/40 px-2.5 py-1.5 text-xs font-medium text-blue-700 dark:text-blue-200 hover:bg-white dark:hover:bg-blue-900/70 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"- title={t('transactions.unlinkTransferConfirm')}- >- <Unlink size={12} />- {t('transactions.unlinkTransfer')}- </button>+ <>+ <button+ type="button"+ disabled={loading}+ onClick={() => setUnlinkConfirmOpen(true)}+ className="shrink-0 inline-flex items-center gap-1.5 rounded-md border border-blue-200 dark:border-blue-800 bg-white/60 dark:bg-blue-900/40 px-2.5 py-1.5 text-xs font-medium text-blue-700 dark:text-blue-200 hover:bg-white dark:hover:bg-blue-900/70 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"+ >+ <Unlink size={12} />+ {t('transactions.unlinkTransfer')}+ </button>+ {/* Confirms the unlink: dropping the pair link re-reports the+ other leg, which changes reported totals (issue #1160). */}+ <Dialog open={unlinkConfirmOpen} onOpenChange={setUnlinkConfirmOpen}>+ <DialogContent className="sm:max-w-md">+ <DialogHeader>+ <DialogTitle>{t('transactions.unlinkTransfer')}</DialogTitle>+ <DialogDescription>+ {t('transactions.unlinkTransferConfirm')}+ </DialogDescription>+ </DialogHeader>+ <DialogFooter>+ <Button type="button" variant="outline" onClick={() => setUnlinkConfirmOpen(false)}>+ {t('common.cancel')}+ </Button>+ <Button+ type="button"+ variant="destructive"+ onClick={() => {+ setUnlinkConfirmOpen(false)+ if (transaction?.transfer_pair_id) {+ onUnlinkTransfer(transaction.transfer_pair_id)+ }+ }}+ >+ {t('transactions.unlinkTransfer')}+ </Button>+ </DialogFooter>+ </DialogContent>+ </Dialog>+ </>)}</div></div>