getTasksCompletedInLastDays function
Returns the number of tasks completed by the given userId
in the last days
.
Queries the tasksCollectionFieldName
collection in Firestore for tasks
that belong to the user, are marked as completed, and have a completion date
within the last days
from now.
Implementation
Future<int> getTasksCompletedInLastDays(
FirebaseFirestore firestore,
String userId,
int days,
) async {
final now = DateTime.now();
final startOfPeriod = now.subtract(Duration(days: days));
final snapshot =
await firestore
.collection(tasksCollectionFieldName)
.where(ownerUserIdFieldName, isEqualTo: userId)
.where(isCompletedFieldName, isEqualTo: true)
.where(
taskCompletionDateTimeFieldName,
isGreaterThanOrEqualTo: startOfPeriod,
)
.get();
return snapshot.docs.length;
}