Skip to content

Commit c78dfe9

Browse files
SelaseKayjude.kwashierussellwheatley
authored
fix(firestore, ios): account for filters in getAggregateFromServer with collection groups (#8259)
Co-authored-by: jude.kwashie <[email protected]> Co-authored-by: Russell Wheatley <[email protected]>
1 parent 72b0c47 commit c78dfe9

File tree

3 files changed

+244
-189
lines changed

3 files changed

+244
-189
lines changed

packages/firestore/e2e/Aggregate/AggregateQuery.e2e.js

+44
Original file line numberDiff line numberDiff line change
@@ -563,5 +563,49 @@ describe('getAggregateFromServer()', function () {
563563
data.averageBaz.should.eql(-0.19999999999999998);
564564
});
565565
});
566+
567+
describe('collectionGroup()', function () {
568+
it('test count, sum, average with collectionGroup', async function () {
569+
const {
570+
getAggregateFromServer,
571+
doc,
572+
setDoc,
573+
collection,
574+
getFirestore,
575+
count,
576+
average,
577+
collectionGroup,
578+
sum,
579+
FieldPath,
580+
} = firestoreModular;
581+
const firestore = getFirestore();
582+
583+
const colRef = collection(firestore, 'collectionGroup');
584+
585+
await Promise.all([
586+
setDoc(doc(colRef, 'one'), { docId: '123', status: 'paid', amount: 100 }),
587+
setDoc(doc(colRef, 'two'), { docId: '123', status: 'paid', amount: 200 }),
588+
setDoc(doc(colRef, 'three'), { docId: '123', status: 'unpaid', amount: 400 }),
589+
]);
590+
591+
const query = collectionGroup(firestore, 'collectionGroup')
592+
.where('docId', '==', '123')
593+
.where('status', '==', 'paid');
594+
595+
const aggregateSpec = {
596+
countCollection: count(),
597+
averageAmount: average(new FieldPath('amount')),
598+
sumAmount: sum(new FieldPath('amount')),
599+
};
600+
601+
const result = await getAggregateFromServer(query, aggregateSpec);
602+
603+
const data = result.data();
604+
605+
data.countCollection.should.eql(2);
606+
data.averageAmount.should.eql(150);
607+
data.sumAmount.should.eql(300);
608+
});
609+
});
566610
});
567611
});

packages/firestore/ios/RNFBFirestore/RNFBFirestoreCollectionModule.m

+12-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,18 @@ - (void)invalidate {
229229
: (RCTPromiseRejectBlock)reject) {
230230
FIRFirestore *firestore = [RNFBFirestoreCommon getFirestoreForApp:firebaseApp
231231
databaseId:databaseId];
232-
FIRQuery *query = [RNFBFirestoreCommon getQueryForFirestore:firestore path:path type:type];
232+
233+
FIRQuery *firestoreBaseQuery = [RNFBFirestoreCommon getQueryForFirestore:firestore
234+
path:path
235+
type:type];
236+
RNFBFirestoreQuery *firestoreQuery =
237+
[[RNFBFirestoreQuery alloc] initWithModifiers:firestore
238+
query:firestoreBaseQuery
239+
filters:filters
240+
orders:orders
241+
options:options];
242+
243+
FIRQuery *query = [firestoreQuery instance];
233244

234245
NSMutableArray<FIRAggregateField *> *aggregateFields =
235246
[[NSMutableArray<FIRAggregateField *> alloc] init];

0 commit comments

Comments
 (0)