@@ -295,90 +295,125 @@ class DatabaseSeedingService {
295
295
}
296
296
}
297
297
298
- /// Seeds the database with the initial AppConfig and the default admin user.
298
+ /// Seeds the database with the initial RemoteConfig and the default admin user.
299
299
///
300
300
/// This method is idempotent, using `ON CONFLICT DO NOTHING` to prevent
301
301
/// errors if the data already exists. It runs within a single transaction.
302
302
Future <void > seedInitialAdminAndConfig () async {
303
- _log.info ('Seeding initial AppConfig and admin user...' );
303
+ _log.info ('Seeding initial RemoteConfig and admin user...' );
304
304
try {
305
305
await _connection.execute ('BEGIN' );
306
306
try {
307
- // Seed AppConfig
308
- _log.fine ('Seeding AppConfig ...' );
309
- final appConfig = AppConfig .fromJson (appConfigFixtureData);
310
- // The `app_config ` table only has `id` and `user_preference_limits`.
311
- // We must provide an explicit map to avoid a "superfluous variables"
312
- // error from the postgres driver.
307
+ // Seed RemoteConfig
308
+ _log.fine ('Seeding RemoteConfig ...' );
309
+ final remoteConfig = RemoteConfig .fromJson (remoteConfigFixtureData);
310
+ // The `remote_config ` table has multiple JSONB columns. We must
311
+ // provide an explicit map with JSON-encoded values to avoid a
312
+ // "superfluous variables" error from the postgres driver.
313
313
await _connection.execute (
314
314
Sql .named (
315
- 'INSERT INTO app_config (id, user_preference_limits) '
316
- 'VALUES (@id, @user_preference_limits) '
315
+ 'INSERT INTO remote_config (id, user_preference_limits, ad_config, '
316
+ 'account_action_config, app_status) VALUES (@id, '
317
+ '@user_preference_limits, @ad_config, @account_action_config, '
318
+ '@app_status) '
317
319
'ON CONFLICT (id) DO NOTHING' ,
318
320
),
319
321
parameters: {
320
- 'id' : appConfig.id,
321
- 'user_preference_limits' : appConfig.userPreferenceLimits.toJson (),
322
+ 'id' : remoteConfig.id,
323
+ 'user_preference_limits' :
324
+ jsonEncode (remoteConfig.userPreferenceLimits.toJson ()),
325
+ 'ad_config' : jsonEncode (remoteConfig.adConfig.toJson ()),
326
+ 'account_action_config' :
327
+ jsonEncode (remoteConfig.accountActionConfig.toJson ()),
328
+ 'app_status' : jsonEncode (remoteConfig.appStatus.toJson ()),
322
329
},
323
330
);
324
331
325
332
// Seed Admin User
326
333
_log.fine ('Seeding admin user...' );
327
334
// Find the admin user in the fixture data.
328
- final adminUser = usersFixturesData.firstWhere (
329
- (user ) => user.roles. contains ( UserRoles . admin) ,
335
+ final adminUserData = usersFixturesData.firstWhere (
336
+ (data ) => data[ 'dashboard_role' ] == DashboardUserRole . admin.name ,
330
337
orElse: () => throw StateError ('Admin user not found in fixtures.' ),
331
338
);
332
- // The `users` table only has `id`, `email`, and `roles`. We must
333
- // provide an explicit map to avoid a "superfluous variables" error.
339
+ final adminUser = User .fromJson (adminUserData);
340
+
341
+ // The `users` table has specific columns for roles and status.
334
342
await _connection.execute (
335
343
Sql .named (
336
- 'INSERT INTO users (id, email, roles) '
337
- 'VALUES (@id, @email, @roles) '
344
+ 'INSERT INTO users (id, email, app_role, dashboard_role, '
345
+ 'feed_action_status) VALUES (@id, @email, @app_role, '
346
+ '@dashboard_role, @feed_action_status) '
338
347
'ON CONFLICT (id) DO NOTHING' ,
339
348
),
340
349
parameters: {
341
350
'id' : adminUser.id,
342
351
'email' : adminUser.email,
343
- 'roles' : jsonEncode (adminUser.roles),
352
+ 'app_role' : adminUser.appRole.name,
353
+ 'dashboard_role' : adminUser.dashboardRole.name,
354
+ 'feed_action_status' : jsonEncode (
355
+ adminUser.feedActionStatus
356
+ .map ((key, value) => MapEntry (key.name, value.toJson ())),
357
+ ),
344
358
},
345
359
);
346
360
347
361
// Seed default settings and preferences for the admin user.
348
- final adminSettings = UserAppSettings (id: adminUser.id);
349
- final adminPreferences = UserContentPreferences (id: adminUser.id);
362
+ final adminSettings = UserAppSettings .fromJson (
363
+ userAppSettingsFixturesData
364
+ .firstWhere ((data) => data['id' ] == adminUser.id),
365
+ );
366
+ final adminPreferences = UserContentPreferences .fromJson (
367
+ userContentPreferencesFixturesData
368
+ .firstWhere ((data) => data['id' ] == adminUser.id),
369
+ );
350
370
351
371
await _connection.execute (
352
372
Sql .named (
353
373
'INSERT INTO user_app_settings (id, user_id, display_settings, '
354
- 'language, feed_preferences, engagement_shown_counts, '
355
- 'engagement_last_shown_timestamps) VALUES (@id, @user_id, '
356
- '@display_settings, @language, @feed_preferences, '
357
- '@engagement_shown_counts, @engagement_last_shown_timestamps) '
374
+ 'language, feed_preferences) VALUES (@id, @user_id, '
375
+ '@display_settings, @language, @feed_preferences) '
358
376
'ON CONFLICT (id) DO NOTHING' ,
359
377
),
360
- parameters: adminSettings.toJson ()
361
- ..['user_id' ] = adminUser.id,
378
+ parameters: {
379
+ 'id' : adminSettings.id,
380
+ 'user_id' : adminUser.id,
381
+ 'display_settings' :
382
+ jsonEncode (adminSettings.displaySettings.toJson ()),
383
+ 'language' : adminSettings.language,
384
+ 'feed_preferences' :
385
+ jsonEncode (adminSettings.feedPreferences.toJson ()),
386
+ },
362
387
);
363
388
364
389
await _connection.execute (
365
390
Sql .named (
366
391
'INSERT INTO user_content_preferences (id, user_id, '
367
- 'followed_categories , followed_sources, followed_countries, '
368
- 'saved_headlines) VALUES (@id, @user_id, @followed_categories , '
392
+ 'followed_topics , followed_sources, followed_countries, '
393
+ 'saved_headlines) VALUES (@id, @user_id, @followed_topics , '
369
394
'@followed_sources, @followed_countries, @saved_headlines) '
370
395
'ON CONFLICT (id) DO NOTHING' ,
371
396
),
372
- // Use toJson() to correctly serialize the lists of complex objects
373
- // into a format the database driver can handle for JSONB columns.
374
- parameters: adminPreferences.toJson ()
375
- ..['user_id' ] = adminUser.id,
397
+ parameters: {
398
+ 'id' : adminPreferences.id,
399
+ 'user_id' : adminUser.id,
400
+ 'followed_topics' : jsonEncode (
401
+ adminPreferences.followedTopics.map ((e) => e.toJson ()).toList (),
402
+ ),
403
+ 'followed_sources' : jsonEncode (
404
+ adminPreferences.followedSources.map ((e) => e.toJson ()).toList (),
405
+ ),
406
+ 'followed_countries' : jsonEncode (
407
+ adminPreferences.followedCountries.map ((e) => e.toJson ()).toList (),
408
+ ),
409
+ 'saved_headlines' : jsonEncode (
410
+ adminPreferences.savedHeadlines.map ((e) => e.toJson ()).toList (),
411
+ ),
412
+ },
376
413
);
377
414
378
415
await _connection.execute ('COMMIT' );
379
- _log.info (
380
- 'Initial AppConfig and admin user seeding completed successfully.' ,
381
- );
416
+ _log.info ('Initial RemoteConfig and admin user seeding completed.' );
382
417
} catch (e) {
383
418
await _connection.execute ('ROLLBACK' );
384
419
rethrow ;
0 commit comments