Skip to content

Commit b2cc147

Browse files
committed
feat(country): add name filter to distinct countries aggregation
- Add optional nameFilter parameter to _getDistinctCountriesFromAggregation method - Implement regex filter for country name in aggregation pipeline - Update logging to include nameFilter information - Adjust error messages to reflect new parameter
1 parent 002d383 commit b2cc147

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

lib/src/services/country_service.dart

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,21 +202,38 @@ class CountryService {
202202
/// - [repository]: The [DataRepository] to perform the aggregation on.
203203
/// - [fieldName]: The name of the field within the documents that contains
204204
/// the country object (e.g., 'eventCountry', 'headquarters').
205+
/// - [nameFilter]: An optional map containing a regex filter for the country name.
205206
///
206207
/// Throws [OperationFailedException] for internal errors during data fetch.
207208
Future<List<Country>> _getDistinctCountriesFromAggregation<T extends FeedItem>({
208209
required DataRepository<T> repository,
209210
required String fieldName,
211+
Map<String, dynamic>? nameFilter,
210212
}) async {
211-
_log.finer('Fetching distinct countries for field "$fieldName" via aggregation.');
213+
_log.finer(
214+
'Fetching distinct countries for field "$fieldName" via aggregation '
215+
'with nameFilter: $nameFilter.',
216+
);
212217
try {
213-
final pipeline = [
218+
final pipeline = <Map<String, dynamic>>[
214219
{
215220
r'$match': {
216221
'status': ContentStatus.active.name,
217222
'$fieldName.id': {r'$exists': true},
218223
},
219224
},
225+
];
226+
227+
// Add name filter if provided
228+
if (nameFilter != null && nameFilter.isNotEmpty) {
229+
pipeline.add({
230+
r'$match': {
231+
'$fieldName.name': nameFilter,
232+
},
233+
});
234+
}
235+
236+
pipeline.addAll([
220237
{
221238
r'$group': {
222239
'_id': '\$$fieldName.id',
@@ -226,7 +243,7 @@ class CountryService {
226243
{
227244
r'$replaceRoot': {'newRoot': r'$country'},
228245
},
229-
];
246+
]);
230247

231248
final distinctCountriesJson = await repository.aggregate(
232249
pipeline: pipeline,
@@ -238,12 +255,13 @@ class CountryService {
238255

239256
_log.info(
240257
'Successfully fetched ${distinctCountries.length} distinct countries '
241-
'for field "$fieldName".',
258+
'for field "$fieldName" with nameFilter: $nameFilter.',
242259
);
243260
return distinctCountries;
244261
} catch (e, s) {
245262
_log.severe(
246-
'Failed to fetch distinct countries for field "$fieldName".',
263+
'Failed to fetch distinct countries for field "$fieldName" '
264+
'with nameFilter: $nameFilter.',
247265
e,
248266
s,
249267
);

0 commit comments

Comments
 (0)