Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ (AccountKit): Update only fields non-restricted fields #1655

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading