diff --git a/Modules/AccountKit/Sources/Database/DatabaseOperations.swift b/Modules/AccountKit/Sources/Database/DatabaseOperations.swift index 8ec9fdd226..ae02418e55 100644 --- a/Modules/AccountKit/Sources/Database/DatabaseOperations.swift +++ b/Modules/AccountKit/Sources/Database/DatabaseOperations.swift @@ -143,14 +143,20 @@ public class DatabaseOperations { self.listenerRegistrations.removeAll() } - public func update(data: T, in collection: DatabaseCollection) -> AnyPublisher { + public func update(data: T, in collection: DatabaseCollection, ignoringFields: [String] = []) -> AnyPublisher { Future { promise in let docRef = self.database.collection(collection.rawValue).document(data.id!) do { - try docRef.setData(from: data, merge: true) { error in + var dataDict = try Firestore.Encoder().encode(data) + + for field in ignoringFields { + dataDict.removeValue(forKey: field) + } + + docRef.updateData(dataDict) { error in if let error { - log.error("\(error.localizedDescription)") + log.error("Update failed for document \(String(describing: data.id!)): \(error.localizedDescription)") promise(.failure(DatabaseError.customError(error.localizedDescription))) } else { log.info("Document \(String(describing: data.id!)) updated successfully in \(collection.rawValue). 🎉") diff --git a/Modules/AccountKit/Sources/Managers/Caregivers/CaregiverManager.swift b/Modules/AccountKit/Sources/Managers/Caregivers/CaregiverManager.swift index 58d9519836..1029dfb6cd 100644 --- a/Modules/AccountKit/Sources/Managers/Caregivers/CaregiverManager.swift +++ b/Modules/AccountKit/Sources/Managers/Caregivers/CaregiverManager.swift @@ -71,16 +71,17 @@ public class CaregiverManager { public func updateCaregiver(caregiver: inout Caregiver) { caregiver.lastEditedAt = nil - self.dbOps.update(data: caregiver, in: .caregivers) + let ignoredFields: [String] = ["root_owner_uid", "uuid", "created_at"] + self.dbOps.update(data: caregiver, in: .caregivers, ignoringFields: ignoredFields) .sink(receiveCompletion: { completion in if case let .failure(error) = completion { self.fetchError.send(error) } }, receiveValue: { [weak self] updatedCaregiver in guard let self else { return } + AnalyticsManager.logEventCaregiverEdit(caregiver: updatedCaregiver.id!) guard updatedCaregiver.id == self.currentCaregiver.value?.id else { return } self.setCurrentCaregiver(to: updatedCaregiver) - AnalyticsManager.logEventCaregiverEdit(caregiver: updatedCaregiver.id!) log.info("Caregiver successfully updated.") }) .store(in: &self.cancellables) diff --git a/Modules/AccountKit/Sources/Managers/Carereceivers/CarereceiverManager.swift b/Modules/AccountKit/Sources/Managers/Carereceivers/CarereceiverManager.swift index 667c7182fc..e9abe678e9 100644 --- a/Modules/AccountKit/Sources/Managers/Carereceivers/CarereceiverManager.swift +++ b/Modules/AccountKit/Sources/Managers/Carereceivers/CarereceiverManager.swift @@ -53,7 +53,8 @@ public class CarereceiverManager { public func updateCarereceiver(carereceiver: inout Carereceiver) { carereceiver.lastEditedAt = nil - self.dbOps.update(data: carereceiver, in: .carereceivers) + let ignoredFields: [String] = ["root_owner_uid", "uuid", "created_at"] + self.dbOps.update(data: carereceiver, in: .carereceivers, ignoringFields: ignoredFields) .sink(receiveCompletion: { completion in if case let .failure(error) = completion { self.fetchError.send(error) diff --git a/Modules/AccountKit/Sources/Managers/RootAccounts/RootAccountManager.swift b/Modules/AccountKit/Sources/Managers/RootAccounts/RootAccountManager.swift index 4d1a460e0e..6a7ba6a806 100644 --- a/Modules/AccountKit/Sources/Managers/RootAccounts/RootAccountManager.swift +++ b/Modules/AccountKit/Sources/Managers/RootAccounts/RootAccountManager.swift @@ -43,7 +43,8 @@ public class RootAccountManager { public func updateRootAccount(rootAccount: inout RootAccount) { rootAccount.lastEditedAt = nil - self.dbOps.update(data: rootAccount, in: .rootAccounts) + let ignoredFields: [String] = ["root_owner_uid", "uuid", "created_at"] + self.dbOps.update(data: rootAccount, in: .rootAccounts, ignoringFields: ignoredFields) .sink( receiveCompletion: { [weak self] completion in if case let .failure(error) = completion {