@@ -115,46 +115,20 @@ class CountryService {
115115 }
116116
117117 _log.finer ('Fetching distinct event countries via aggregation.' );
118- try {
119- final pipeline = [
120- {
121- r'$match' : {
122- 'status' : ContentStatus .active.name,
123- 'eventCountry.id' : {r'$exists' : true },
124- },
125- },
126- {
127- r'$group' : {
128- '_id' : r'$eventCountry.id' ,
129- 'country' : {r'$first' : r'$eventCountry' },
130- },
131- },
132- {
133- r'$replaceRoot' : {'newRoot' : r'$country' },
134- },
135- ];
136-
137- final distinctCountriesJson = await _headlineRepository.aggregate (
138- pipeline: pipeline,
139- );
140-
141- final distinctCountries = distinctCountriesJson
142- .map (Country .fromJson)
143- .toList ();
144-
145- _cachedEventCountries = _CacheEntry (
146- distinctCountries,
147- DateTime .now ().add (_cacheDuration),
148- );
149- _log.info (
150- 'Successfully fetched and cached ${distinctCountries .length } '
151- 'event countries.' ,
152- );
153- return distinctCountries;
154- } catch (e, s) {
155- _log.severe ('Failed to fetch event countries via aggregation.' , e, s);
156- throw OperationFailedException ('Failed to retrieve event countries: $e ' );
157- }
118+ final distinctCountries = await _getDistinctCountriesFromAggregation (
119+ repository: _headlineRepository,
120+ fieldName: 'eventCountry' ,
121+ );
122+
123+ _cachedEventCountries = _CacheEntry (
124+ distinctCountries,
125+ DateTime .now ().add (_cacheDuration),
126+ );
127+ _log.info (
128+ 'Successfully fetched and cached ${distinctCountries .length } '
129+ 'event countries.' ,
130+ );
131+ return distinctCountries;
158132 }
159133
160134 /// Fetches a distinct list of countries that are referenced as
@@ -170,50 +144,75 @@ class CountryService {
170144 }
171145
172146 _log.finer ('Fetching distinct headquarter countries via aggregation.' );
147+ final distinctCountries = await _getDistinctCountriesFromAggregation (
148+ repository: _sourceRepository,
149+ fieldName: 'headquarters' ,
150+ );
151+
152+ _cachedHeadquarterCountries = _CacheEntry (
153+ distinctCountries,
154+ DateTime .now ().add (_cacheDuration),
155+ );
156+ _log.info (
157+ 'Successfully fetched and cached ${distinctCountries .length } '
158+ 'headquarter countries.' ,
159+ );
160+ return distinctCountries;
161+ }
162+
163+ /// Helper method to fetch a distinct list of countries from a given
164+ /// repository and field name using MongoDB aggregation.
165+ ///
166+ /// - [repository] : The [DataRepository] to perform the aggregation on.
167+ /// - [fieldName] : The name of the field within the documents that contains
168+ /// the country object (e.g., 'eventCountry', 'headquarters').
169+ ///
170+ /// Throws [OperationFailedException] for internal errors during data fetch.
171+ Future <List <Country >> _getDistinctCountriesFromAggregation ({
172+ required DataRepository <dynamic > repository,
173+ required String fieldName,
174+ }) async {
175+ _log.finer ('Fetching distinct countries for field "$fieldName " via aggregation.' );
173176 try {
174177 final pipeline = [
175178 {
176179 r'$match' : {
177180 'status' : ContentStatus .active.name,
178- 'headquarters .id' : {r'$exists' : true },
181+ '$ fieldName .id' : {r'$exists' : true },
179182 },
180183 },
181184 {
182185 r'$group' : {
183- '_id' : r'$headquarters .id' ,
184- 'country' : {r'$first' : r'$headquarters ' },
186+ '_id' : ' \$ $ fieldName .id' ,
187+ 'country' : {r'$first' : ' \$ $ fieldName ' },
185188 },
186189 },
187190 {
188191 r'$replaceRoot' : {'newRoot' : r'$country' },
189192 },
190193 ];
191194
192- final distinctCountriesJson = await _sourceRepository .aggregate (
195+ final distinctCountriesJson = await repository .aggregate (
193196 pipeline: pipeline,
194197 );
195198
196199 final distinctCountries = distinctCountriesJson
197200 .map (Country .fromJson)
198201 .toList ();
199202
200- _cachedHeadquarterCountries = _CacheEntry (
201- distinctCountries,
202- DateTime .now ().add (_cacheDuration),
203- );
204203 _log.info (
205- 'Successfully fetched and cached ${distinctCountries .length } '
206- 'headquarter countries .' ,
204+ 'Successfully fetched ${distinctCountries .length } distinct countries '
205+ 'for field "$ fieldName " .' ,
207206 );
208207 return distinctCountries;
209208 } catch (e, s) {
210209 _log.severe (
211- 'Failed to fetch headquarter countries via aggregation .' ,
210+ 'Failed to fetch distinct countries for field "$ fieldName " .' ,
212211 e,
213212 s,
214213 );
215214 throw OperationFailedException (
216- 'Failed to retrieve headquarter countries: $e ' ,
215+ 'Failed to retrieve distinct countries for field "$ fieldName " : $e ' ,
217216 );
218217 }
219218 }
0 commit comments