From 51b3cb13921f461f8aac9f20f5fa765e3e747294 Mon Sep 17 00:00:00 2001 From: Arnaud Rajon Date: Fri, 20 Dec 2024 03:37:23 +0100 Subject: [PATCH] feat: make everything work --- .../unio/model/event/EventViewModel.kt | 47 +++++++++++-------- firebase/tests/firestore-rules.test.js | 7 +-- firestore.rules | 6 +-- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/android/unio/model/event/EventViewModel.kt b/app/src/main/java/com/android/unio/model/event/EventViewModel.kt index 17e7d4e1..86fe6854 100644 --- a/app/src/main/java/com/android/unio/model/event/EventViewModel.kt +++ b/app/src/main/java/com/android/unio/model/event/EventViewModel.kt @@ -212,25 +212,32 @@ constructor( * @param onSuccess A callback that is called when the event is successfully updated. * @param onFailure A callback that is called when an error occurs while updating the event. */ - fun updateEventWithoutImage(event: Event, onSuccess: () -> Unit, onFailure: (Exception) -> Unit) { + fun updateEventWithoutImage( + event: Event, + onSuccess: () -> Unit, + onFailure: (Exception) -> Unit, + updateAssociation: Boolean = true + ) { repository.addEvent(event, onSuccess, onFailure) - event.organisers.requestAll( - { - event.organisers.list.value.forEach { - if (it.events.contains(event.uid)) it.events.remove(event.uid) - it.events.add(event.uid) - associationRepository.saveAssociation( - isNewAssociation = false, - it, - {}, - { e -> - Log.e("EventViewModel", "An error occurred while loading associations: $e") - }) - it.events.requestAll() - } - }, - lazy = true) + if (updateAssociation) { + event.organisers.requestAll( + { + event.organisers.list.value.forEach { + if (it.events.contains(event.uid)) it.events.remove(event.uid) + it.events.add(event.uid) + associationRepository.saveAssociation( + isNewAssociation = false, + it, + {}, + { e -> + Log.e("EventViewModel", "An error occurred while loading associations: $e") + }) + it.events.requestAll() + } + }, + lazy = true) + } _events.value = _events.value.filter { it.uid != event.uid } // Remove the outdated event _events.value += event @@ -325,7 +332,8 @@ constructor( { onSuccess() }, { e -> Log.e("EventViewModel", "An error occurred while updating an event: $e") - }) + }, + false) }, { e -> onFailure(e) @@ -362,7 +370,8 @@ constructor( { e -> onFailure(e) Log.e("EventViewModel", "An error occurred while updating an event: $e") - }) + }, + false) }, { e -> onFailure(e) diff --git a/firebase/tests/firestore-rules.test.js b/firebase/tests/firestore-rules.test.js index 21708a4b..c0e0a347 100644 --- a/firebase/tests/firestore-rules.test.js +++ b/firebase/tests/firestore-rules.test.js @@ -138,12 +138,7 @@ async function runTests(testEnv) { uid: "new-association", }) ); - /*await assertSucceeds( - updateDoc(doc(aliceDb, `/associations/${aliceAssociation.uid}`), { - ...aliceAssociation, - name: "New name", - }) - );*/ + await assertSucceeds( updateDoc(doc(aliceDb, `/associations/${otherAssociation.uid}`), { ...otherAssociation, diff --git a/firestore.rules b/firestore.rules index 7cc2b219..c9903481 100644 --- a/firestore.rules +++ b/firestore.rules @@ -79,7 +79,7 @@ service cloud.firestore { match /associations/{uid} { function isMember() { - let members = get(/databases/$(database)/documents/associations/$(uid)).data.get(members, null); + let members = get(/databases/$(database)/documents/associations/$(uid)).data.get("members", null); return (members != null) && members.keys().hasAny([request.auth.uid]); } function onlyUpdatedFollowerCount() { @@ -176,8 +176,8 @@ service cloud.firestore { let affectedKeys = request.resource.data.diff(resource.data).affectedKeys(); - let onlySavedCountChanged = affectedKeys.hasOnly(['numberOfSaved']); - return onlySavedCountChanged && (newCount == oldCount || newCount == oldCount + 1 || newCount == oldCount - 1); + let onlySavedCountChanged = affectedKeys.hasOnly(["numberOfSaved"]); + return onlySavedCountChanged && newCount != null && oldCount != null; } function validate() {