Skip to content

Commit

Permalink
Removed unused fetch expense func & changed query filter
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Jan 1, 2025
1 parent 9bf2312 commit 9ffdaba
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 53 deletions.
52 changes: 1 addition & 51 deletions Data/Data/Store/ExpenseStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,61 +60,11 @@ public class ExpenseStore: ObservableObject {
return (expenses, snapshot.documents.last)
}

func fetchExpensesForUser2(userId: String, limit: Int, lastDocument: DocumentSnapshot?) async throws -> (expenses: [Expense], lastDocument: DocumentSnapshot?) {
var userExpenses: [Expense] = []

// Step 1: Fetch groups where the user is a member
let groupSnapshot = try await database.collection("groups")
.whereField("is_active", isEqualTo: true)
.whereField("members", arrayContains: userId)
.getDocuments()

let groupDocuments = groupSnapshot.documents
var lastFetchedDocument: DocumentSnapshot?

// Step 2: Fetch expenses from each group with pagination
for groupDoc in groupDocuments {
let groupId = groupDoc.documentID

var query = database.collection("groups")
.document(groupId)
.collection("expenses")
.whereField("is_active", isEqualTo: true)
.order(by: "date", descending: true)
.limit(to: limit)

// Apply pagination
if let lastDocument = lastDocument {
query = query.start(afterDocument: lastDocument)
}

let expenseSnapshot = try await query.getDocuments()

let fetchedExpenses = expenseSnapshot.documents.compactMap { doc -> Expense? in
do {
return try doc.data(as: Expense.self)
} catch {
LogE("ExpenseStore: \(#function) Error decoding expense: \(error.localizedDescription)")
return nil
}
}

userExpenses.append(contentsOf: fetchedExpenses)

// Track the last document for pagination
if let lastDoc = expenseSnapshot.documents.last {
lastFetchedDocument = lastDoc
}
}

return (userExpenses, lastFetchedDocument)
}

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)
.whereField("split_to", arrayContains: userId)
.order(by: "date", descending: true)
.limit(to: limit)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ class SearchExpensesViewModel: BaseViewModel, ObservableObject {
await combineMemberWithExpense(expenses: result.expenses.uniqued())
hasMoreExpenses = !(result.expenses.count < self.EXPENSES_LIMIT)
LogD("SearchExpensesViewModel: \(#function) Expenses fetched successfully.")

print("xxx \(expenses.count)")
} catch {
LogE("SearchExpensesViewModel: \(#function) Failed to fetch expenses: \(error).")
handleServiceError()
Expand Down
9 changes: 9 additions & 0 deletions firestore.rules
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {

match /{document=**} {
allow read, write: if false;
}

// Allow access to all comments subcollections
match /{path=**}/expenses/{id} {
allow read, write;
}

function isAuthorized() {
return request.auth != null ;
}
Expand Down

0 comments on commit 9ffdaba

Please sign in to comment.