Skip to content

Commit 03dbac4

Browse files
authored
Merge pull request #56 from flutter-news-app-full-source-code/enhance-seeding-service
refactor(database): improve remote config seeding and ad platform setup
2 parents 99f8c21 + 1de4f45 commit 03dbac4

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

lib/src/services/database_seeding_service.dart

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class DatabaseSeedingService {
2727

2828
await _ensureIndexes();
2929
await _seedOverrideAdminUser();
30+
await _seedRemoteConfig();
3031
await _seedCollection<Country>(
3132
collectionName: 'countries',
3233
fixtureData: countriesFixturesData,
@@ -39,12 +40,6 @@ class DatabaseSeedingService {
3940
getId: (item) => item.id,
4041
toJson: (item) => item.toJson(),
4142
);
42-
await _seedCollection<RemoteConfig>(
43-
collectionName: 'remote_configs',
44-
fixtureData: remoteConfigsFixturesData,
45-
getId: (item) => item.id,
46-
toJson: (item) => item.toJson(),
47-
);
4843
await _seedCollection<LocalAd>(
4944
collectionName: 'local_ads',
5045
fixtureData: localAdsFixturesData,
@@ -86,7 +81,9 @@ class DatabaseSeedingService {
8681
// Filter by the specific, deterministic _id.
8782
'filter': {'_id': objectId},
8883
// Set the fields of the document.
89-
'update': {r'$set': document},
84+
// Use $setOnInsert to set fields ONLY if the document is newly inserted.
85+
// This ensures existing documents are not overwritten by fixture data.
86+
'update': {r'$setOnInsert': document},
9087
'upsert': true,
9188
},
9289
});
@@ -103,6 +100,53 @@ class DatabaseSeedingService {
103100
}
104101
}
105102

103+
/// Ensures the initial RemoteConfig document exists and has
104+
/// a default for the primary ad platform, if not already set.
105+
///
106+
/// This method only creates the document if it does not exist.
107+
/// It does NOT overwrite existing configurations.
108+
Future<void> _seedRemoteConfig() async {
109+
_log.info('Seeding RemoteConfig...');
110+
try {
111+
final remoteConfigCollection = _db.collection('remote_configs');
112+
final defaultRemoteConfigId = remoteConfigsFixturesData.first.id;
113+
final objectId = ObjectId.fromHexString(defaultRemoteConfigId);
114+
115+
final existingConfig = await remoteConfigCollection.findOne(
116+
where.id(objectId),
117+
);
118+
119+
if (existingConfig == null) {
120+
_log.info('No existing RemoteConfig found. Creating initial config.');
121+
// Take the default from fixtures
122+
final initialConfig = remoteConfigsFixturesData.first;
123+
124+
// Ensure primaryAdPlatform is not 'demo' for initial setup
125+
// sic its not intended for any use outside teh mobile client code.
126+
final productionReadyAdConfig = initialConfig.adConfig.copyWith(
127+
primaryAdPlatform: AdPlatformType.local,
128+
);
129+
130+
final productionReadyConfig = initialConfig.copyWith(
131+
adConfig: productionReadyAdConfig,
132+
createdAt: DateTime.now(),
133+
updatedAt: DateTime.now(),
134+
);
135+
136+
await remoteConfigCollection.insertOne({
137+
'_id': objectId,
138+
...productionReadyConfig.toJson()..remove('id'),
139+
});
140+
_log.info('Initial RemoteConfig created successfully.');
141+
} else {
142+
_log.info('RemoteConfig already exists. Skipping creation.');
143+
}
144+
} on Exception catch (e, s) {
145+
_log.severe('Failed to seed RemoteConfig.', e, s);
146+
rethrow;
147+
}
148+
}
149+
106150
/// Ensures that the necessary indexes exist on the collections.
107151
///
108152
/// This method is idempotent; it will only create indexes if they do not

0 commit comments

Comments
 (0)