@@ -27,6 +27,7 @@ class DatabaseSeedingService {
27
27
28
28
await _ensureIndexes ();
29
29
await _seedOverrideAdminUser ();
30
+ await _seedRemoteConfig ();
30
31
await _seedCollection <Country >(
31
32
collectionName: 'countries' ,
32
33
fixtureData: countriesFixturesData,
@@ -39,12 +40,6 @@ class DatabaseSeedingService {
39
40
getId: (item) => item.id,
40
41
toJson: (item) => item.toJson (),
41
42
);
42
- await _seedCollection <RemoteConfig >(
43
- collectionName: 'remote_configs' ,
44
- fixtureData: remoteConfigsFixturesData,
45
- getId: (item) => item.id,
46
- toJson: (item) => item.toJson (),
47
- );
48
43
await _seedCollection <LocalAd >(
49
44
collectionName: 'local_ads' ,
50
45
fixtureData: localAdsFixturesData,
@@ -86,7 +81,9 @@ class DatabaseSeedingService {
86
81
// Filter by the specific, deterministic _id.
87
82
'filter' : {'_id' : objectId},
88
83
// 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},
90
87
'upsert' : true ,
91
88
},
92
89
});
@@ -103,6 +100,53 @@ class DatabaseSeedingService {
103
100
}
104
101
}
105
102
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
+
106
150
/// Ensures that the necessary indexes exist on the collections.
107
151
///
108
152
/// This method is idempotent; it will only create indexes if they do not
0 commit comments