Skip to content

Commit

Permalink
🔀️ Merge branch 'mathieu/refactor/Update-only-edited-fields-to-Firest…
Browse files Browse the repository at this point in the history
…ore' into develop
  • Loading branch information
ladislas committed Dec 18, 2024
2 parents 09f1f18 + 2522d7c commit b7ba40c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
12 changes: 9 additions & 3 deletions Modules/AccountKit/Sources/Database/DatabaseOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,20 @@ public class DatabaseOperations {
self.listenerRegistrations.removeAll()
}

public func update<T: DatabaseDocument>(data: T, in collection: DatabaseCollection) -> AnyPublisher<T, Error> {
public func update<T: DatabaseDocument>(data: T, in collection: DatabaseCollection, ignoringFields: [String] = []) -> AnyPublisher<T, Error> {
Future<T, Error> { 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). 🎉")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b7ba40c

Please sign in to comment.