Skip to content

Commit

Permalink
fix: Lost tags when cloning or editing
Browse files Browse the repository at this point in the history
  • Loading branch information
enrique-lozano committed Dec 21, 2024
1 parent 441ae6e commit 23cf884
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 90 deletions.
2 changes: 1 addition & 1 deletion lib/app/categories/form/category_form_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class CategoryFormFunctions {
includeParentCategoriesInSearch: true))
.first) {
futures.add(
TransactionService.instance.insertOrUpdateTransaction(
TransactionService.instance.updateTransaction(
tr.copyWith(categoryID: drift.Value(selCategory.id))),
);
}
Expand Down
117 changes: 70 additions & 47 deletions lib/app/transactions/form/transaction_form.page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,57 +258,80 @@ class _TransactionFormPageState extends State<TransactionFormPage>

final newTrID = widget.transactionToEdit?.id ?? generateUUID();

TransactionService.instance
.insertOrUpdateTransaction(
TransactionInDB(
id: newTrID,
date: date,
type: transactionType,
accountID: fromAccount!.id,
value: transactionType == TransactionType.E
? transactionValue * -1
: transactionValue,
isHidden: false,
status: date.compareTo(DateTime.now()) > 0
? TransactionStatus.pending
: status,
notes: notesController.text.isEmpty ? null : notesController.text,
title: titleController.text.isEmpty ? null : titleController.text,
intervalEach: recurrentRule.intervalEach,
intervalPeriod: recurrentRule.intervalPeriod,
endDate: recurrentRule.ruleRecurrentLimit?.endDate,
remainingTransactions:
recurrentRule.ruleRecurrentLimit?.remainingIterations,
valueInDestiny:
transactionType.isTransfer ? valueInDestinyToNumber : null,
categoryID:
transactionType.isIncomeOrExpense ? selectedCategory?.id : null,
receivingAccountID:
transactionType.isTransfer ? transferAccount?.id : null,
),
)
.then((value) {
final transactionToPost = TransactionInDB(
id: newTrID,
date: date,
type: transactionType,
accountID: fromAccount!.id,
value: transactionType == TransactionType.E
? transactionValue * -1
: transactionValue,
isHidden: false,
status: date.compareTo(DateTime.now()) > 0
? TransactionStatus.pending
: status,
notes: notesController.text.isEmpty ? null : notesController.text,
title: titleController.text.isEmpty ? null : titleController.text,
intervalEach: recurrentRule.intervalEach,
intervalPeriod: recurrentRule.intervalPeriod,
endDate: recurrentRule.ruleRecurrentLimit?.endDate,
remainingTransactions:
recurrentRule.ruleRecurrentLimit?.remainingIterations,
valueInDestiny:
transactionType.isTransfer ? valueInDestinyToNumber : null,
categoryID:
transactionType.isIncomeOrExpense ? selectedCategory?.id : null,
receivingAccountID:
transactionType.isTransfer ? transferAccount?.id : null,
);

Future<int> postCall =
TransactionService.instance.updateTransaction(transactionToPost);

if (!isEditMode) {
postCall =
TransactionService.instance.insertTransaction(transactionToPost);
}

postCall.then((value) async {
final db = AppDB.instance;

Future.wait(
[
for (final tag in tags)
if (widget.transactionToEdit != null &&
widget.transactionToEdit!.tags
.any((element) => element.id == tag.id))
(db.delete(db.transactionTags)
..where((tbl) => tbl.tagID.isValue(tag.id)))
.go()
else
db
.into(db.transactionTags)
.insert(TransactionTag(transactionID: newTrID, tagID: tag.id))
],
).then((value) {
final existingTags = widget.transactionToEdit?.tags ?? [];

// Tags to remove: present in the current transaction but not in the new tags list
final tagsToRemove = existingTags
.where((existingTag) =>
!tags.any((newTag) => newTag.id == existingTag.id))
.toList();

// Tags to add: present in the new tags list but not in the current transaction
final tagsToAdd = tags
.where((newTag) =>
!existingTags.any((existingTag) => existingTag.id == newTag.id))
.toList();

try {
// Remove tags
for (final tag in tagsToRemove) {
await (db.delete(db.transactionTags)
..where((tbl) =>
tbl.tagID.isValue(tag.id) &
tbl.transactionID.isValue(newTrID)))
.go();
}

// Add new tags
for (final tag in tagsToAdd) {
await db.into(db.transactionTags).insert(
TransactionTag(transactionID: newTrID, tagID: tag.id),
);
}

onSuccess();
}).catchError((error) {
} catch (error) {
Navigator.pop(context);
scMessenger.showSnackBar(SnackBar(content: Text(error.toString())));
});
}
}).catchError((error) {
scMessenger.showSnackBar(SnackBar(content: Text(error.toString())));
});
Expand Down
6 changes: 3 additions & 3 deletions lib/app/transactions/transaction_details.page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class _TransactionDetailsPageState extends State<TransactionDetailsPage> {
const nullValue = drift.Value(null);

TransactionService.instance
.insertOrUpdateTransaction(transaction.copyWith(
.updateTransaction(transaction.copyWith(
date: datetime,
status: nullValue,
id: newId,
Expand Down Expand Up @@ -133,7 +133,7 @@ class _TransactionDetailsPageState extends State<TransactionDetailsPage> {
.ruleRecurrentLimit!.remainingIterations;

TransactionService.instance
.insertOrUpdateTransaction(
.updateTransaction(
transaction.copyWith(
date: transaction.followingDateToNext,
remainingTransactions: remainingIterations != null
Expand Down Expand Up @@ -222,7 +222,7 @@ class _TransactionDetailsPageState extends State<TransactionDetailsPage> {
transaction.recurrentInfo.ruleRecurrentLimit!.remainingIterations;

TransactionService.instance
.insertOrUpdateTransaction(transaction.copyWith(
.updateTransaction(transaction.copyWith(
date: transaction.followingDateToNext,
remainingTransactions: remainingIterations != null
? drift.Value(remainingIterations - 1)
Expand Down
9 changes: 4 additions & 5 deletions lib/app/transactions/widgets/bulk_edit_transaction_modal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class BulkEditTransactionModal extends StatelessWidget {
context,
futures: transactionsToEdit.map(
(e) => TransactionService.instance
.insertOrUpdateTransaction(e.copyWith(date: date)),
.updateTransaction(e.copyWith(date: date)),
),
);
},
Expand All @@ -71,9 +71,8 @@ class BulkEditTransactionModal extends StatelessWidget {
performUpdates(
context,
futures: transactionsToEdit.map(
(e) => TransactionService.instance
.insertOrUpdateTransaction(
e.copyWith(categoryID: Value(modalRes.id))),
(e) => TransactionService.instance.updateTransaction(
e.copyWith(categoryID: Value(modalRes.id))),
),
);
},
Expand All @@ -95,7 +94,7 @@ class BulkEditTransactionModal extends StatelessWidget {
context,
futures: transactionsToEdit.map(
(e) => TransactionService.instance
.insertOrUpdateTransaction(e.copyWith(
.updateTransaction(e.copyWith(
status: Value(modalRes.result),
)),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,14 @@ class TransactionService {
return toReturn;
}

Future<int> insertOrUpdateTransaction(TransactionInDB transaction) async {
final toReturn = await db
.into(db.transactions)
.insert(transaction, mode: InsertMode.insertOrReplace);
Future<int> updateTransaction(TransactionInDB transaction) async {
final toReturn = await db.update(db.transactions).replace(transaction);

// To update the getAccountsData() function results
// TODO: Check why we need this. The function already listen to changes in the transactions table
db.markTablesUpdated([db.accounts]);

return toReturn;
return toReturn ? 1 : 0;
}

Future<int> deleteTransaction(String transactionId) {
Expand Down
9 changes: 5 additions & 4 deletions lib/core/models/transaction/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,11 @@ class MoneyTransaction extends TransactionInDB {
baseValue = baseValue / intervalEachDivider;

if (recurrentInfo.intervalPeriod != null) {
return baseValue * Periodicity.getConversionFactor(
recurrentInfo.intervalPeriod!,
periodicity,
);
return baseValue *
Periodicity.getConversionFactor(
recurrentInfo.intervalPeriod!,
periodicity,
);
}

throw Exception('We could not calculate this value');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,35 +114,34 @@ class TransactionViewActionService {
dialogTitle: t.transaction.duplicate,
contentParagraphs: [Text(t.transaction.duplicate_warning_message)],
confirmationText: t.general.continue_text,
).then((isConfirmed) {
).then((isConfirmed) async {
if (isConfirmed != true) return;

transactionService
.insertTransaction(
TransactionInDB(
id: generateUUID(),
accountID: transaction.accountID,
date: transaction.date,
value: transaction.value,
type: transaction.type,
isHidden: transaction.isHidden,
categoryID: transaction.categoryID,
notes: transaction.notes,
title: transaction.title,
receivingAccountID: transaction.receivingAccountID,
status: transaction.status,
valueInDestiny: transaction.valueInDestiny,
),
)
.then((value) {
final newTrId = generateUUID();

try {
await _duplicateTransaction(transaction, newTrId);

scaffold.showSnackBar(
SnackBar(
content: Text(t.transaction.duplicate_success),
),
SnackBar(content: Text(t.transaction.duplicate_success)),
);
}).catchError((err) {
scaffold.showSnackBar(SnackBar(content: Text('$err')));
});
} catch (error) {
scaffold.showSnackBar(SnackBar(content: Text('$error')));
}
});
}

Future<void> _duplicateTransaction(
MoneyTransaction transaction, String newTrId) async {
final db = AppDB.instance;

await transactionService
.insertTransaction(transaction.copyWith(id: newTrId));

for (final tag in transaction.tags) {
await db.into(db.transactionTags).insert(
TransactionTag(transactionID: newTrId, tagID: tag.id),
);
}
}
}

0 comments on commit 23cf884

Please sign in to comment.