diff --git a/Data/Data/Repository/ExpenseRepository.swift b/Data/Data/Repository/ExpenseRepository.swift index 85bc26885..7bad325b9 100644 --- a/Data/Data/Repository/ExpenseRepository.swift +++ b/Data/Data/Repository/ExpenseRepository.swift @@ -33,13 +33,12 @@ public class ExpenseRepository: ObservableObject { return newExpense } - public func deleteExpense(group: Groups, expense: Expense) async throws { - guard let userId = preference.user?.id else { return } - + public func deleteExpense(group: Groups, expense: Expense) async throws -> Expense { var updatedExpense = expense updatedExpense.isActive = false // Make expense inactive - updatedExpense.updatedBy = userId - try await updateExpense(group: group, expense: updatedExpense, oldExpense: expense, type: .expenseDeleted) + updatedExpense.updatedBy = preference.user?.id ?? "" + updatedExpense.updatedAt = Timestamp() + return try await updateExpense(group: group, expense: updatedExpense, oldExpense: expense, type: .expenseDeleted) } public func updateExpenseWithImage(imageData: Data?, newImageUrl: String?, group: Groups, expense: (new: Expense, old: Expense), type: ActivityType) async throws -> Expense { @@ -50,17 +49,16 @@ public class ExpenseRepository: ObservableObject { let uploadedImageUrl = try await uploadImage(imageData: imageData, expense: updatedExpense) updatedExpense.imageUrl = uploadedImageUrl } else if let currentUrl = updatedExpense.imageUrl, newImageUrl == nil { - // If there's a current image URL and we want to remove it, delete the image and set imageUrl to nil + // If there's a current image URL and we want to remove it, delete the image and set imageUrl empty try await storageManager.deleteImage(imageUrl: currentUrl) - updatedExpense.imageUrl = nil + updatedExpense.imageUrl = "" } else if let newImageUrl { // If a new image URL is explicitly passed, update it updatedExpense.imageUrl = newImageUrl } guard hasExpenseChanged(updatedExpense, oldExpense: expense.old) else { return updatedExpense } - try await updateExpense(group: group, expense: updatedExpense, oldExpense: expense.old, type: type) - return updatedExpense + return try await updateExpense(group: group, expense: updatedExpense, oldExpense: expense.old, type: type) } private func uploadImage(imageData: Data, expense: Expense) async throws -> String { @@ -76,10 +74,10 @@ public class ExpenseRepository: ObservableObject { oldExpense.splitData != expense.splitData || oldExpense.isActive != expense.isActive } - public func updateExpense(group: Groups, expense: Expense, oldExpense: Expense, type: ActivityType) async throws { - guard let groupId = group.id else { return } - try await store.updateExpense(groupId: groupId, expense: expense) + public func updateExpense(group: Groups, expense: Expense, oldExpense: Expense, type: ActivityType) async throws -> Expense { + try await store.updateExpense(groupId: group.id ?? "", expense: expense) try await addActivityLogForExpense(group: group, expense: expense, oldExpense: oldExpense, type: type) + return expense } private func addActivityLogForExpense(group: Groups, expense: Expense, oldExpense: Expense, type: ActivityType) async throws { diff --git a/Data/Data/Repository/TransactionRepository.swift b/Data/Data/Repository/TransactionRepository.swift index af417f5c1..1ad619b29 100644 --- a/Data/Data/Repository/TransactionRepository.swift +++ b/Data/Data/Repository/TransactionRepository.swift @@ -38,9 +38,9 @@ public class TransactionRepository: ObservableObject { var updatedTransaction = transaction updatedTransaction.isActive = false // Make transaction inactive updatedTransaction.updatedBy = preference.user?.id ?? "" - try await updateTransaction(group: group, transaction: updatedTransaction, oldTransaction: transaction, - members: (payer, receiver), type: .transactionDeleted) - return updatedTransaction + updatedTransaction.updatedAt = Timestamp() + return try await updateTransaction(group: group, transaction: updatedTransaction, oldTransaction: transaction, + members: (payer, receiver), type: .transactionDeleted) } public func updateTransactionWithImage(imageData: Data?, newImageUrl: String?, group: Groups, transaction: (new: Transactions, old: Transactions), members: (payer: AppUser, receiver: AppUser)) async throws -> Transactions { @@ -51,17 +51,16 @@ public class TransactionRepository: ObservableObject { let uploadedImageUrl = try await uploadImage(imageData: imageData, transaction: updatedTransaction) updatedTransaction.imageUrl = uploadedImageUrl } else if let currentUrl = updatedTransaction.imageUrl, newImageUrl == nil { - // If there's a current image URL and we want to remove it, delete the image and set imageUrl to nil + // If there's a current image URL and we want to remove it, delete the image and set imageUrl empty try await storageManager.deleteImage(imageUrl: currentUrl) - updatedTransaction.imageUrl = nil + updatedTransaction.imageUrl = "" } else if let newImageUrl { // If a new image URL is explicitly passed, update it updatedTransaction.imageUrl = newImageUrl } guard hasTransactionChanged(updatedTransaction, oldTransaction: transaction.old) else { return updatedTransaction } - try await updateTransaction(group: group, transaction: updatedTransaction, oldTransaction: transaction.old, members: members, type: .transactionUpdated) - return updatedTransaction + return try await updateTransaction(group: group, transaction: updatedTransaction, oldTransaction: transaction.old, members: members, type: .transactionUpdated) } private func uploadImage(imageData: Data, transaction: Transactions) async throws -> String { @@ -78,11 +77,11 @@ public class TransactionRepository: ObservableObject { } public func updateTransaction(group: Groups, transaction: Transactions, oldTransaction: Transactions, - members: (payer: AppUser, receiver: AppUser), type: ActivityType) async throws { - guard let groupId = group.id else { return } - try await store.updateTransaction(groupId: groupId, transaction: transaction) + members: (payer: AppUser, receiver: AppUser), type: ActivityType) async throws -> Transactions { + try await store.updateTransaction(groupId: group.id ?? "", transaction: transaction) try await addActivityLogForTransaction(group: group, transaction: transaction, oldTransaction: oldTransaction, type: type, members: members) + return transaction } private func addActivityLogForTransaction(group: Groups, transaction: Transactions, oldTransaction: Transactions, diff --git a/Splito/UI/Home/Expense/Expense Detail/ExpenseDetailsViewModel.swift b/Splito/UI/Home/Expense/Expense Detail/ExpenseDetailsViewModel.swift index 4ea0093f9..3dab8213e 100644 --- a/Splito/UI/Home/Expense/Expense Detail/ExpenseDetailsViewModel.swift +++ b/Splito/UI/Home/Expense/Expense Detail/ExpenseDetailsViewModel.swift @@ -8,6 +8,7 @@ import Data import SwiftUI import BaseStyle +import FirebaseFirestore class ExpenseDetailsViewModel: BaseViewModel, ObservableObject { @@ -146,8 +147,7 @@ class ExpenseDetailsViewModel: BaseViewModel, ObservableObject { return } - guard var expense, let userId = preference.user?.id, - validateUserPermission(operationText: "restored", action: "restored"), + guard var expense, let userId = preference.user?.id, validateUserPermission(operationText: "restored", action: "restored"), validateGroupMembers(action: "restored") else { return } Task { [weak self] in @@ -156,8 +156,9 @@ class ExpenseDetailsViewModel: BaseViewModel, ObservableObject { self.viewState = .loading expense.isActive = true expense.updatedBy = userId + expense.updatedAt = Timestamp() - try await self.expenseRepository.updateExpense(group: group, expense: expense, + self.expense = try await self.expenseRepository.updateExpense(group: group, expense: expense, oldExpense: expense, type: .expenseRestored) await self.updateGroupMemberBalance(updateType: .Add) @@ -188,8 +189,8 @@ class ExpenseDetailsViewModel: BaseViewModel, ObservableObject { Task { do { viewState = .loading - try await expenseRepository.deleteExpense(group: group, expense: expense) - NotificationCenter.default.post(name: .deleteExpense, object: expense) + self.expense = try await expenseRepository.deleteExpense(group: group, expense: expense) + NotificationCenter.default.post(name: .deleteExpense, object: self.expense) await self.updateGroupMemberBalance(updateType: .Delete) viewState = .initial diff --git a/Splito/UI/Home/Expense/Note/AddNoteViewModel.swift b/Splito/UI/Home/Expense/Note/AddNoteViewModel.swift index d9527841e..6fddfdca0 100644 --- a/Splito/UI/Home/Expense/Note/AddNoteViewModel.swift +++ b/Splito/UI/Home/Expense/Note/AddNoteViewModel.swift @@ -60,7 +60,7 @@ class AddNoteViewModel: BaseViewModel, ObservableObject { var updatedExpense = expense updatedExpense.note = note - try await expenseRepository.updateExpense(group: group, expense: updatedExpense, oldExpense: expense, type: .expenseUpdated) + updatedExpense = try await expenseRepository.updateExpense(group: group, expense: updatedExpense, oldExpense: expense, type: .expenseUpdated) NotificationCenter.default.post(name: .updateExpense, object: updatedExpense) showLoader = false @@ -83,8 +83,9 @@ class AddNoteViewModel: BaseViewModel, ObservableObject { var updatedPayment = payment updatedPayment.note = note - try await transactionRepository.updateTransaction(group: group, transaction: updatedPayment, oldTransaction: payment, - members: members, type: .transactionUpdated) + updatedPayment = try await transactionRepository.updateTransaction(group: group, transaction: updatedPayment, + oldTransaction: payment, members: members, + type: .transactionUpdated) NotificationCenter.default.post(name: .updateTransaction, object: updatedPayment) showLoader = false diff --git a/Splito/UI/Home/Groups/Group/Group Options/Transactions/Transaction Detail/GroupTransactionDetailViewModel.swift b/Splito/UI/Home/Groups/Group/Group Options/Transactions/Transaction Detail/GroupTransactionDetailViewModel.swift index 7a7242649..319e984ed 100644 --- a/Splito/UI/Home/Groups/Group/Group Options/Transactions/Transaction Detail/GroupTransactionDetailViewModel.swift +++ b/Splito/UI/Home/Groups/Group/Group Options/Transactions/Transaction Detail/GroupTransactionDetailViewModel.swift @@ -6,7 +6,7 @@ // import Data -import Foundation +import FirebaseFirestore class GroupTransactionDetailViewModel: BaseViewModel, ObservableObject { @@ -154,14 +154,15 @@ class GroupTransactionDetailViewModel: BaseViewModel, ObservableObject { } Task { [weak self] in - guard let self, let payer = getMemberDataBy(id: transaction.payerId), + guard let self, let userId = preference.user?.id, let payer = getMemberDataBy(id: transaction.payerId), let receiver = getMemberDataBy(id: transaction.receiverId) else { return } do { self.viewState = .loading transaction.isActive = true - transaction.updatedBy = preference.user?.id ?? "" + transaction.updatedBy = userId + transaction.updatedAt = Timestamp() - try await self.transactionRepository.updateTransaction(group: group, transaction: transaction, oldTransaction: transaction, members: (payer, receiver), type: .transactionRestored) + self.transaction = try await self.transactionRepository.updateTransaction(group: group, transaction: transaction, oldTransaction: transaction, members: (payer, receiver), type: .transactionRestored) await self.updateGroupMemberBalance(updateType: .Add) self.viewState = .initial diff --git a/Splito/UI/Home/Groups/Group/GroupHomeViewModelExtension.swift b/Splito/UI/Home/Groups/Group/GroupHomeViewModelExtension.swift index 562304573..bbb80435d 100644 --- a/Splito/UI/Home/Groups/Group/GroupHomeViewModelExtension.swift +++ b/Splito/UI/Home/Groups/Group/GroupHomeViewModelExtension.swift @@ -129,15 +129,11 @@ extension GroupHomeViewModel { } private func deleteExpense(expense: Expense) { - guard let group, let expenseId = expense.id, let userId = preference.user?.id, - validateGroupMembers(expense: expense) else { return } + guard let group, let expenseId = expense.id, validateGroupMembers(expense: expense) else { return } Task { do { - var deletedExpense = expense - deletedExpense.updatedBy = userId - - try await expenseRepository.deleteExpense(group: group, expense: deletedExpense) + let deletedExpense = try await expenseRepository.deleteExpense(group: group, expense: expense) await updateGroupMemberBalance(expense: deletedExpense, updateType: .Delete) LogD("GroupHomeViewModel: \(#function) Expense deleted successfully.") } catch {