Skip to content
Draft
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
1 change: 1 addition & 0 deletions backend/database/conversations.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@ def get_closest_conversation_to_timestamps(uid: str, start_timestamp: int, end_t
db.collection('users')
.document(uid)
.collection(conversations_collection)
.where(filter=FieldFilter('status', '==', ConversationStatus.completed))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

While filtering by status is correct, the overall query is invalid and will cause a runtime error due to Firestore limitations. Specifically:

  1. The query uses range filters on two different fields (finished_at on line 962 and started_at on line 963), which is not allowed. A Firestore query can only have range filters on a single field.
  2. The query uses range filters, but the first order_by clause is on a different field (created_at on line 964). If a range filter is present, the first order_by must be on the same field as the range filter.

This query needs to be refactored. A possible solution is to use only one range filter in the query (e.g., on started_at) and then filter the remaining conditions (e.g., finished_at) in memory on the client side.

.where(filter=FieldFilter('finished_at', '>=', start_threshold))
.where(filter=FieldFilter('started_at', '<=', end_threshold))
.order_by('created_at', direction=firestore.Query.DESCENDING)
Expand Down