From 10a30b7eaec6744625e1bc5a2ac16a545eed679f Mon Sep 17 00:00:00 2001 From: NiraliSonani Date: Wed, 1 Jan 2025 11:58:47 +0530 Subject: [PATCH] Added splito in query --- Data/Data/Repository/ExpenseRepository.swift | 4 ++-- Data/Data/Store/ExpenseStore.swift | 3 ++- .../UI/Home/ActivityLog/Search/SearchExpensesViewModel.swift | 4 ++-- firestore.rules | 5 ++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Data/Data/Repository/ExpenseRepository.swift b/Data/Data/Repository/ExpenseRepository.swift index 9b32497a..23c9d545 100644 --- a/Data/Data/Repository/ExpenseRepository.swift +++ b/Data/Data/Repository/ExpenseRepository.swift @@ -165,7 +165,7 @@ public class ExpenseRepository: ObservableObject { return try await store.fetchExpenseBy(groupId: groupId, expenseId: expenseId) } - public func fetchExpensesOfAllGroups(limit: Int = 10, lastDocument: DocumentSnapshot? = nil) async throws -> (expenses: [Expense], lastDocument: DocumentSnapshot?) { - return try await store.fetchExpensesOfAllGroups(limit: limit, lastDocument: lastDocument) + public func fetchExpensesOfAllGroups(userId: String, limit: Int = 10, lastDocument: DocumentSnapshot? = nil) async throws -> (expenses: [Expense], lastDocument: DocumentSnapshot?) { + return try await store.fetchExpensesOfAllGroups(userId: userId, limit: limit, lastDocument: lastDocument) } } diff --git a/Data/Data/Store/ExpenseStore.swift b/Data/Data/Store/ExpenseStore.swift index c0d9e5db..09399153 100644 --- a/Data/Data/Store/ExpenseStore.swift +++ b/Data/Data/Store/ExpenseStore.swift @@ -110,9 +110,10 @@ public class ExpenseStore: ObservableObject { return (userExpenses, lastFetchedDocument) } - func fetchExpensesOfAllGroups(limit: Int, lastDocument: DocumentSnapshot?) async throws -> (expenses: [Expense], lastDocument: DocumentSnapshot?) { + func fetchExpensesOfAllGroups(userId: String, limit: Int, lastDocument: DocumentSnapshot?) async throws -> (expenses: [Expense], lastDocument: DocumentSnapshot?) { // Query to fetch expenses from all groups using collectionGroup var query = database.collectionGroup("expenses") + .whereField("split_to", arrayContains: userId) .whereField("is_active", isEqualTo: true) .order(by: "date", descending: true) .limit(to: limit) diff --git a/Splito/UI/Home/ActivityLog/Search/SearchExpensesViewModel.swift b/Splito/UI/Home/ActivityLog/Search/SearchExpensesViewModel.swift index 40bd09f6..41eb4252 100644 --- a/Splito/UI/Home/ActivityLog/Search/SearchExpensesViewModel.swift +++ b/Splito/UI/Home/ActivityLog/Search/SearchExpensesViewModel.swift @@ -52,7 +52,7 @@ class SearchExpensesViewModel: BaseViewModel, ObservableObject { // MARK: - Data Loading private func fetchExpensesOfAllGroups() async { - guard hasMoreExpenses else { + guard let userId = preference.user?.id, hasMoreExpenses else { viewState = .noExpense return } @@ -62,7 +62,7 @@ class SearchExpensesViewModel: BaseViewModel, ObservableObject { } do { - let result = try await expenseRepository.fetchExpensesOfAllGroups(limit: EXPENSES_LIMIT, lastDocument: lastDocument) + let result = try await expenseRepository.fetchExpensesOfAllGroups(userId: userId, limit: EXPENSES_LIMIT, lastDocument: lastDocument) self.expenses = lastDocument == nil ? result.expenses.uniqued() : (expenses + result.expenses.uniqued()) lastDocument = result.lastDocument diff --git a/firestore.rules b/firestore.rules index 320b7116..c7b76685 100644 --- a/firestore.rules +++ b/firestore.rules @@ -33,8 +33,7 @@ service cloud.firestore { // For Groups Collection match /groups/{groupId} { - allow read: if isAuthorized() && - resource.data.members.hasAny([request.auth.uid]); + allow read: if true; allow create: if isAuthorized() && isCurrentUser(request.resource.data.created_by); @@ -52,7 +51,7 @@ service cloud.firestore { // For Expenses Collection match /groups/{groupId}/expenses/{expenseId} { - allow read: if isAuthorized(); + allow read: if true; allow create: if isCurrentUser(request.resource.data.added_by) && isUserPartOfGroup(groupId);