A local-first personal finance app
✨ New Rule `delete transaction` action (#4603)
lelemmedad7ce0e345
diff --git a/packages/desktop-client/src/components/ManageRules.tsx b/packages/desktop-client/src/components/ManageRules.tsxindex e6f2e33a8d..d8fabf4199 100644--- a/packages/desktop-client/src/components/ManageRules.tsx+++ b/packages/desktop-client/src/components/ManageRules.tsx@@ -107,6 +107,8 @@ export function ruleToString(rule: RuleEntity, data: FilterData) {} else if (action.op === 'prepend-notes' || action.op === 'append-notes') {const noteValue = String(action.value || '');return [friendlyOp(action.op), '\u201c' + noteValue + '\u201d'];+ } else if (action.op === 'delete-transaction') {+ return [friendlyOp(action.op), '(delete)'];} else {return [];}diff --git a/packages/desktop-client/src/components/accounts/Account.tsx b/packages/desktop-client/src/components/accounts/Account.tsxindex 8712a39f8a..64fe710b1f 100644--- a/packages/desktop-client/src/components/accounts/Account.tsx+++ b/packages/desktop-client/src/components/accounts/Account.tsx@@ -1048,7 +1048,8 @@ class AccountInternal extends PureComponent<// sync the reconciliation transactionawait send('transactions-batch-update', {- added: ruledTransactions,+ added: ruledTransactions.filter(trans => !trans.tombstone),+ deleted: ruledTransactions.filter(trans => trans.tombstone),});await this.refetchTransactions();};diff --git a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsxindex 9d9c421cb8..8c2582b944 100644--- a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx+++ b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx@@ -14,7 +14,6 @@ import { Stack } from '@actual-app/components/stack';import { Text } from '@actual-app/components/text';import { theme } from '@actual-app/components/theme';import { View } from '@actual-app/components/view';-import deepEqual from 'deep-equal';import { send } from 'loot-core/platform/client/fetch';import { type ParseFileOptions } from 'loot-core/server/transactions/import/parse-file';@@ -180,6 +179,9 @@ export function ImportTransactionsModal({} | null>(null);const [filename, setFilename] = useState(originalFileName);const [transactions, setTransactions] = useState<ImportTransaction[]>([]);+ const [parsedTransactions, setParsedTransactions] = useState<+ ImportTransaction[]+ >([]);const [filetype, setFileType] = useState('unknown');const [fieldMappings, setFieldMappings] = useState<FieldMapping | null>(null);const [splitMode, setSplitMode] = useState(false);@@ -286,6 +288,7 @@ export function ImportTransactionsModal({ignored,selected,selected_merge,+ tombstone,...finalTransaction} = trans;previewTransactions.push({@@ -322,6 +325,8 @@ export function ImportTransactionsModal({// (reconciled transactions or no change detected)current_trx.ignored = entry?.ignored || false;+ current_trx.tombstone = entry?.tombstone || false;+current_trx.selected = !current_trx.ignored;current_trx.selected_merge = current_trx.existing;@@ -433,8 +438,12 @@ export function ImportTransactionsModal({// Reverse the transactions because it's very common for them to// be ordered ascending, but we show transactions descending by// date. This is purely cosmetic.+ const reversedTransactions =+ transactions.reverse() as ImportTransaction[];+ setParsedTransactions(reversedTransactions);+const transactionPreview = await getImportPreview(- transactions.reverse(),+ reversedTransactions,filetype,flipAmount,fieldMappings,@@ -456,7 +465,6 @@ export function ImportTransactionsModal({const amt = e;if (!amt || amt.match(/^\d{1,}(\.\d{0,4})?$/)) {setMultiplierAmount(amt);- runImportPreview();}}@@ -535,7 +543,6 @@ export function ImportTransactionsModal({[field]: name === '' ? null : name,};setFieldMappings(newFieldMappings);- runImportPreview();}function onCheckTransaction(trx_id: string) {@@ -716,8 +723,20 @@ export function ImportTransactionsModal({}const runImportPreview = useCallback(async () => {+ // preserve user's selection choices before re-running preview+ const selectionMap = new Map();+ transactions.forEach(trans => {+ if (!trans.isMatchedTransaction) {+ selectionMap.set(trans.trx_id, {+ selected: trans.selected,+ selected_merge: trans.selected_merge,+ });+ }+ });++ // always start from the original parsed transactions, not the previewed ones to ensure rules runconst transactionPreview = await getImportPreview(- transactions,+ parsedTransactions,filetype,flipAmount,fieldMappings,@@ -728,12 +747,25 @@ export function ImportTransactionsModal({multiplierAmount,);- if (!deepEqual(transactions, transactionPreview)) {- setTransactions(transactionPreview);- }+ // restore selections to the new preview results+ const transactionPreviewWithSelections = transactionPreview.map(trans => {+ if (!trans.isMatchedTransaction && selectionMap.has(trans.trx_id)) {+ const saved = selectionMap.get(trans.trx_id);+ return {+ ...trans,+ selected: saved.selected,+ selected_merge: saved.selected_merge,+ };+ }+ return trans;+ });++ setTransactions(transactionPreviewWithSelections);+ // intentionally exclude transactions from dependencies to avoid infinite rerenders+ // eslint-disable-next-line react-hooks/exhaustive-deps}, [getImportPreview,- transactions,+ parsedTransactions,filetype,flipAmount,fieldMappings,@@ -744,6 +776,29 @@ export function ImportTransactionsModal({multiplierAmount,]);+ useEffect(() => {+ if (parsedTransactions.length === 0 || loadingState === 'parsing') {+ return;+ }++ if (filetype === 'csv' || filetype === 'qif') {+ runImportPreview();+ }+ // intentionally exclude runImportPreview from dependencies to avoid infinite rerenders+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [+ filetype,+ flipAmount,+ fieldMappings,+ splitMode,+ parseDateFormat,+ inOutMode,+ outValue,+ multiplierAmount,+ loadingState,+ parsedTransactions.length,+ ]);+const headers: ComponentProps<typeof TableHeader>['headers'] = [{ name: t('Date'), width: 200 },{ name: t('Payee'), width: 'flex' },@@ -964,7 +1019,6 @@ export function ImportTransactionsModal({parseDateFormat={parseDateFormat || undefined}onChange={value => {setParseDateFormat(isDateFormat(value) ? value : null);- runImportPreview();}}/>)}@@ -1082,7 +1136,6 @@ export function ImportTransactionsModal({checked={flipAmount}onChange={() => {setFlipAmount(!flipAmount);- runImportPreview();}}><Trans>Flip amount</Trans>@@ -1093,7 +1146,6 @@ export function ImportTransactionsModal({onToggle={() => {setMultiplierEnabled(!multiplierEnabled);setMultiplierAmount('');- runImportPreview();}}onChangeAmount={onMultiplierChange}/>@@ -1104,7 +1156,6 @@ export function ImportTransactionsModal({checked={splitMode}onChange={() => {onSplitMode();- runImportPreview();}}><Trans>@@ -1116,7 +1167,6 @@ export function ImportTransactionsModal({outValue={outValue}onToggle={() => {setInOutMode(!inOutMode);- runImportPreview();}}onChangeText={setOutValue}/>@@ -1139,7 +1189,10 @@ export function ImportTransactionsModal({>{(() => {const count = transactions?.filter(- trans => !trans.isMatchedTransaction && trans.selected,+ trans =>+ !trans.isMatchedTransaction &&+ trans.selected &&+ !trans.tombstone,).length;return (diff --git a/packages/desktop-client/src/components/modals/ImportTransactionsModal/Transaction.tsx b/packages/desktop-client/src/components/modals/ImportTransactionsModal/Transaction.tsxindex f54528b360..96ae6964e2 100644--- a/packages/desktop-client/src/components/modals/ImportTransactionsModal/Transaction.tsx+++ b/packages/desktop-client/src/components/modals/ImportTransactionsModal/Transaction.tsx@@ -98,9 +98,11 @@ export function Transaction({<Rowstyle={{backgroundColor: theme.tableBackground,+ textDecoration: transaction.tombstone ? 'line-through' : 'none',color:(transaction.isMatchedTransaction && !transaction.selected_merge) ||- !transaction.selected+ !transaction.selected ||+ transaction.tombstone? theme.tableTextInactive: theme.tableText,}}@@ -110,22 +112,24 @@ export function Transaction({{!transaction.isMatchedTransaction && (<Tooltipcontent={- !transaction.existing && !transaction.ignored- ? t('New transaction. You can import it, or skip it.')- : transaction.ignored- ? t(- 'Already imported transaction. You can skip it, or import it again.',- )- : transaction.existing+ transaction.tombstone+ ? t('This transaction will be deleted by Rules')+ : !transaction.existing && !transaction.ignored+ ? t('New transaction. You can import it, or skip it.')+ : transaction.ignored? t(- 'Updated transaction. You can update it, import it again, or skip it.',+ 'Already imported transaction. You can skip it, or import it again.',)- : ''+ : transaction.existing+ ? t(+ 'Updated transaction. You can update it, import it again, or skip it.',+ )+ : ''}placement="right top"><Checkbox- checked={transaction.selected}+ checked={transaction.selected && !transaction.tombstone}onChange={() => onCheckTransaction(transaction.trx_id)}style={transaction.selected_merge@@ -140,36 +144,43 @@ export function Transaction({},},}- : {- '&': {- border:- '1px solid ' + theme.buttonNormalDisabledBorder,- backgroundColor: theme.buttonNormalDisabledBorder,- '::after': {- display: 'block',- background:- theme.buttonNormalDisabledBorder +- // minus sign adapted from packages/desktop-client/src/icons/v1/add.svg- // eslint-disable-next-line actual/typography- ' url(\'data:image/svg+xml; utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="white" className="path" d="M23,11.5 L23,11.5 L23,11.5 C23,12.3284271 22.3284271,13 21.5,13 L1.5,13 L1.5,13 C0.671572875,13 1.01453063e-16,12.3284271 0,11.5 L0,11.5 L0,11.5 C-1.01453063e-16,10.6715729 0.671572875,10 1.5,10 L21.5,10 L21.5,10 C22.3284271,10 23,10.6715729 23,11.5 Z" /></svg>\') 9px 9px',- width: 9,- height: 9,- // eslint-disable-next-line actual/typography- content: '" "',+ : transaction.tombstone+ ? {+ '&': {+ opacity: 0.3,+ backgroundColor: theme.buttonNormalDisabledBorder,},- },- ':checked': {- border: '1px solid ' + theme.checkboxBorderSelected,- backgroundColor: theme.checkboxBackgroundSelected,- '::after': {- background:- theme.checkboxBackgroundSelected +- // plus sign from packages/desktop-client/src/icons/v1/add.svg+ }+ : {+ '&': {+ border:+ '1px solid ' + theme.buttonNormalDisabledBorder,+ backgroundColor: theme.buttonNormalDisabledBorder,+ '::after': {+ display: 'block',+ background:+ theme.buttonNormalDisabledBorder ++ // minus sign adapted from packages/desktop-client/src/icons/v1/add.svg+ // eslint-disable-next-line actual/typography+ ' url(\'data:image/svg+xml; utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="white" className="path" d="M23,11.5 L23,11.5 L23,11.5 C23,12.3284271 22.3284271,13 21.5,13 L1.5,13 L1.5,13 C0.671572875,13 1.01453063e-16,12.3284271 0,11.5 L0,11.5 L0,11.5 C-1.01453063e-16,10.6715729 0.671572875,10 1.5,10 L21.5,10 L21.5,10 C22.3284271,10 23,10.6715729 23,11.5 Z" /></svg>\') 9px 9px',+ width: 9,+ height: 9,// eslint-disable-next-line actual/typography- ' url(\'data:image/svg+xml; utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="white" className="path" d="M23,11.5 L23,11.5 L23,11.5 C23,12.3284271 22.3284271,13 21.5,13 L1.5,13 L1.5,13 C0.671572875,13 1.01453063e-16,12.3284271 0,11.5 L0,11.5 L0,11.5 C-1.01453063e-16,10.6715729 0.671572875,10 1.5,10 L21.5,10 L21.5,10 C22.3284271,10 23,10.6715729 23,11.5 Z" /><path fill="white" className="path" d="M11.5,23 C10.6715729,23 10,22.3284271 10,21.5 L10,1.5 C10,0.671572875 10.6715729,1.52179594e-16 11.5,0 C12.3284271,-1.52179594e-16 13,0.671572875 13,1.5 L13,21.5 C13,22.3284271 12.3284271,23 11.5,23 Z" /></svg>\') 9px 9px',+ content: '" "',+ },},- },- }+ ':checked': {+ border: '1px solid ' + theme.checkboxBorderSelected,+ backgroundColor: theme.checkboxBackgroundSelected,+ '::after': {+ background:+ theme.checkboxBackgroundSelected ++ // plus sign from packages/desktop-client/src/icons/v1/add.svg+ // eslint-disable-next-line actual/typography+ ' url(\'data:image/svg+xml; utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="white" className="path" d="M23,11.5 L23,11.5 L23,11.5 C23,12.3284271 22.3284271,13 21.5,13 L1.5,13 L1.5,13 C0.671572875,13 1.01453063e-16,12.3284271 0,11.5 L0,11.5 L0,11.5 C-1.01453063e-16,10.6715729 0.671572875,10 1.5,10 L21.5,10 L21.5,10 C22.3284271,10 23,10.6715729 23,11.5 Z" /><path fill="white" className="path" d="M11.5,23 C10.6715729,23 10,22.3284271 10,21.5 L10,1.5 C10,0.671572875 10.6715729,1.52179594e-16 11.5,0 C12.3284271,-1.52179594e-16 13,0.671572875 13,1.5 L13,21.5 C13,22.3284271 12.3284271,23 11.5,23 Z" /></svg>\') 9px 9px',+ },+ },+ }}/></Tooltip>diff --git a/packages/desktop-client/src/components/modals/ImportTransactionsModal/utils.ts b/packages/desktop-client/src/components/modals/ImportTransactionsModal/utils.tsindex a801ebfd6c..23fcdd528c 100644--- a/packages/desktop-client/src/components/modals/ImportTransactionsModal/utils.ts+++ b/packages/desktop-client/src/components/modals/ImportTransactionsModal/utils.ts@@ -166,6 +166,7 @@ export function applyFieldMappings(result.ignored = transaction.ignored;result.selected = transaction.selected;result.selected_merge = transaction.selected_merge;+ result.tombstone = transaction.tombstone;return result as ImportTransaction;}@@ -263,8 +264,15 @@ export function parseAmountFields(}export function stripCsvImportTransaction(transaction: ImportTransaction) {- const { existing, ignored, selected, selected_merge, trx_id, ...trans } =- transaction;+ const {+ existing,+ ignored,+ selected,+ selected_merge,+ trx_id,+ tombstone,+ ...trans+ } = transaction;return trans;}diff --git a/packages/desktop-client/src/components/rules/ActionExpression.tsx b/packages/desktop-client/src/components/rules/ActionExpression.tsxindex a7e6298042..dad5218bf2 100644--- a/packages/desktop-client/src/components/rules/ActionExpression.tsx+++ b/packages/desktop-client/src/components/rules/ActionExpression.tsx@@ -17,6 +17,7 @@ import {type SetRuleActionEntity,type AppendNoteRuleActionEntity,type PrependNoteRuleActionEntity,+ type DeleteTransactionRuleActionEntity,} from 'loot-core/types/models';import { ScheduleValue } from './ScheduleValue';@@ -56,6 +57,8 @@ export function ActionExpression({ style, ...props }: ActionExpressionProps) {<PrependNoteActionExpression {...props} />) : props.op === 'append-notes' ? (<AppendNoteActionExpression {...props} />+ ) : props.op === 'delete-transaction' ? (+ <DeleteTransactionActionExpression {...props} />) : null}</View>);@@ -139,3 +142,9 @@ function AppendNoteActionExpression({ op, value }: AppendNoteRuleActionEntity) {</>);}++function DeleteTransactionActionExpression({+ op,+}: DeleteTransactionRuleActionEntity) {+ return <Text>{friendlyOp(op)}</Text>;+}diff --git a/packages/desktop-client/src/components/rules/RuleEditor.tsx b/packages/desktop-client/src/components/rules/RuleEditor.tsxindex 70c1600f22..17f073631b 100644--- a/packages/desktop-client/src/components/rules/RuleEditor.tsx+++ b/packages/desktop-client/src/components/rules/RuleEditor.tsx@@ -504,7 +504,7 @@ function ActionEditor({{op === 'set' ? (<><OpSelect- ops={['set', 'prepend-notes', 'append-notes']}+ ops={['set', 'prepend-notes', 'append-notes', 'delete-transaction']}value={op}onChange={onChange}/>@@ -608,7 +608,7 @@ function ActionEditor({) : op === 'prepend-notes' || op === 'append-notes' ? (<><OpSelect- ops={['set', 'prepend-notes', 'append-notes']}+ ops={['set', 'prepend-notes', 'append-notes', 'delete-transaction']}value={op}onChange={onChange}/>@@ -625,17 +625,27 @@ function ActionEditor({/></View></>+ ) : op === 'delete-transaction' ? (+ <OpSelect+ ops={['set', 'prepend-notes', 'append-notes', 'delete-transaction']}+ value={op}+ onChange={onChange}+ />) : null}- <Stack direction="row" style={{ flexShrink: 0 }}>- <EditorButtons- onAdd={onAdd}- onDelete={- (op === 'set' || op === 'prepend-notes' || op === 'append-notes') &&- onDelete- }- />- </Stack>+ {op !== 'delete-transaction' && (+ <Stack direction="row" style={{ flexShrink: 0 }}>+ <EditorButtons+ onAdd={onAdd}+ onDelete={+ (op === 'set' ||+ op === 'prepend-notes' ||+ op === 'append-notes') &&+ onDelete+ }+ />+ </Stack>+ )}</Editor>);}diff --git a/packages/loot-core/src/server/accounts/sync.ts b/packages/loot-core/src/server/accounts/sync.tsindex ea6ff45fdc..5dbddab9e4 100644--- a/packages/loot-core/src/server/accounts/sync.ts+++ b/packages/loot-core/src/server/accounts/sync.ts@@ -477,6 +477,7 @@ export type ReconcileTransactionsResult = {transaction: TransactionEntity;existing?: TransactionEntity;ignored?: boolean;+ tombstone?: boolean;}>;};@@ -566,6 +567,14 @@ export async function reconcileTransactions(updated.push({ id: child.id, cleared: updates.cleared });}}+ } else if (trans.tombstone) {+ if (isPreview) {+ updatedPreview.push({+ transaction: trans,+ existing: false,+ tombstone: true,+ });+ }} else {// Insert a new transactionconst { forceAddTransaction, ...newTrans } = trans;diff --git a/packages/loot-core/src/server/rules/action.ts b/packages/loot-core/src/server/rules/action.tsindex f6ef17d8c6..43e82136fd 100644--- a/packages/loot-core/src/server/rules/action.ts+++ b/packages/loot-core/src/server/rules/action.ts@@ -14,6 +14,7 @@ const ACTION_OPS = ['link-schedule','prepend-notes','append-notes',+ 'delete-transaction',] as const;type ActionOperator = (typeof ACTION_OPS)[number];@@ -132,6 +133,9 @@ export class Action {? object[this.field] + this.value: this.value;break;+ case 'delete-transaction':+ object['tombstone'] = 1;+ break;default:}}diff --git a/packages/loot-core/src/server/rules/app.ts b/packages/loot-core/src/server/rules/app.tsindex de9bffd162..7c2d2e2baa 100644--- a/packages/loot-core/src/server/rules/app.ts+++ b/packages/loot-core/src/server/rules/app.ts@@ -42,13 +42,15 @@ function validateRule(rule: Partial<RuleEntity>) {);const actionErrors = runValidation(rule.actions, action =>- action.op === 'set-split-amount'- ? new Action(action.op, null, action.value, action.options)- : action.op === 'link-schedule'- ? new Action(action.op, null, action.value, null)- : action.op === 'prepend-notes' || action.op === 'append-notes'+ action.op === 'delete-transaction'+ ? new Action(action.op, null, null, null)+ : action.op === 'set-split-amount'+ ? new Action(action.op, null, action.value, action.options)+ : action.op === 'link-schedule'? new Action(action.op, null, action.value, null)- : new Action(action.op, action.field, action.value, action.options),+ : action.op === 'prepend-notes' || action.op === 'append-notes'+ ? new Action(action.op, null, action.value, null)+ : new Action(action.op, action.field, action.value, action.options),);if (conditionErrors || actionErrors) {diff --git a/packages/loot-core/src/server/transactions/transaction-rules.ts b/packages/loot-core/src/server/transactions/transaction-rules.tsindex d0f4a6c34c..0505cc0a7b 100644--- a/packages/loot-core/src/server/transactions/transaction-rules.ts+++ b/packages/loot-core/src/server/transactions/transaction-rules.ts@@ -665,6 +665,8 @@ export async function applyActions(action.op === 'append-notes') {return new Action(action.op, null, action.value, null);+ } else if (action.op === 'delete-transaction') {+ return new Action(action.op, null, null, null);}return new Action(diff --git a/packages/loot-core/src/shared/rules.ts b/packages/loot-core/src/shared/rules.tsindex 6cd007ee36..b0b6b86470 100644--- a/packages/loot-core/src/shared/rules.ts+++ b/packages/loot-core/src/shared/rules.ts@@ -227,6 +227,8 @@ export function friendlyOp(op, type?) {return t('is on budget');case 'offBudget':return t('is off budget');+ case 'delete-transaction':+ return 'delete transaction';default:return '';}diff --git a/packages/loot-core/src/types/models/rule.ts b/packages/loot-core/src/types/models/rule.tsindex aa2aa6e5f3..ad2f8d2d14 100644--- a/packages/loot-core/src/types/models/rule.ts+++ b/packages/loot-core/src/types/models/rule.ts@@ -141,7 +141,8 @@ export type RuleActionEntity =| SetSplitAmountRuleActionEntity| LinkScheduleRuleActionEntity| PrependNoteRuleActionEntity- | AppendNoteRuleActionEntity;+ | AppendNoteRuleActionEntity+ | DeleteTransactionRuleActionEntity;export interface SetRuleActionEntity {field: string;@@ -177,3 +178,8 @@ export interface AppendNoteRuleActionEntity {op: 'append-notes';value: string;}++export interface DeleteTransactionRuleActionEntity {+ op: 'delete-transaction';+ value: string;+}diff --git a/upcoming-release-notes/4603.md b/upcoming-release-notes/4603.mdnew file mode 100644index 0000000000..6215e939af--- /dev/null+++ b/upcoming-release-notes/4603.md@@ -0,0 +1,6 @@+---+category: Features+authors: [lelemm, youngcw, matt-fidd]+---++Add `delete transaction` rule action