Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 51 additions & 7 deletions lib/src/services/database_seeding_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class DatabaseSeedingService {

await _ensureIndexes();
await _seedOverrideAdminUser();
await _seedRemoteConfig();
await _seedCollection<Country>(
collectionName: 'countries',
fixtureData: countriesFixturesData,
Expand All @@ -39,12 +40,6 @@ class DatabaseSeedingService {
getId: (item) => item.id,
toJson: (item) => item.toJson(),
);
await _seedCollection<RemoteConfig>(
collectionName: 'remote_configs',
fixtureData: remoteConfigsFixturesData,
getId: (item) => item.id,
toJson: (item) => item.toJson(),
);
await _seedCollection<LocalAd>(
collectionName: 'local_ads',
fixtureData: localAdsFixturesData,
Expand Down Expand Up @@ -86,7 +81,9 @@ class DatabaseSeedingService {
// Filter by the specific, deterministic _id.
'filter': {'_id': objectId},
// Set the fields of the document.
'update': {r'$set': document},
// Use $setOnInsert to set fields ONLY if the document is newly inserted.
// This ensures existing documents are not overwritten by fixture data.
'update': {r'$setOnInsert': document},
'upsert': true,
},
});
Expand All @@ -103,6 +100,53 @@ class DatabaseSeedingService {
}
}

/// Ensures the initial RemoteConfig document exists and has
/// a default for the primary ad platform, if not already set.
///
/// This method only creates the document if it does not exist.
/// It does NOT overwrite existing configurations.
Future<void> _seedRemoteConfig() async {
_log.info('Seeding RemoteConfig...');
try {
final remoteConfigCollection = _db.collection('remote_configs');
final defaultRemoteConfigId = remoteConfigsFixturesData.first.id;
final objectId = ObjectId.fromHexString(defaultRemoteConfigId);

final existingConfig = await remoteConfigCollection.findOne(
where.id(objectId),
);

if (existingConfig == null) {
_log.info('No existing RemoteConfig found. Creating initial config.');
// Take the default from fixtures
final initialConfig = remoteConfigsFixturesData.first;

// Ensure primaryAdPlatform is not 'demo' for initial setup
// sic its not intended for any use outside teh mobile client code.
final productionReadyAdConfig = initialConfig.adConfig.copyWith(
primaryAdPlatform: AdPlatformType.local,
);

final productionReadyConfig = initialConfig.copyWith(
adConfig: productionReadyAdConfig,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);

await remoteConfigCollection.insertOne({
'_id': objectId,
...productionReadyConfig.toJson()..remove('id'),
});
_log.info('Initial RemoteConfig created successfully.');
} else {
_log.info('RemoteConfig already exists. Skipping creation.');
}
} on Exception catch (e, s) {
_log.severe('Failed to seed RemoteConfig.', e, s);
rethrow;
}
}

/// Ensures that the necessary indexes exist on the collections.
///
/// This method is idempotent; it will only create indexes if they do not
Expand Down
Loading