Skip to content

Commit

Permalink
Added splito in query
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Jan 1, 2025
1 parent 88aeafb commit 10a30b7
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Data/Data/Repository/ExpenseRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
3 changes: 2 additions & 1 deletion Data/Data/Store/ExpenseStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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

Expand Down
5 changes: 2 additions & 3 deletions firestore.rules
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 10a30b7

Please sign in to comment.