Skip to content

Commit 449f49d

Browse files
committed
fix(app): correct data migration logic in demo mode
Ensures that the `DemoDataInitializerService` is not called when a data migration from an anonymous to an authenticated user is in progress. Previously, when an anonymous user linked their account, the `DemoDataInitializerService` would create fresh fixture data for the new authenticated user ID. Immediately after, the `DemoDataMigrationService` would run, but its migrated data was being overwritten or made redundant by the initialization step. This change makes the initialization and migration paths mutually exclusive. The `DemoDataInitializerService` will now only run for a new user if it is not a migration scenario, fixing the regression and ensuring that an anonymous user's data is correctly preserved upon account linking.
1 parent 8dd8d04 commit 449f49d

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,15 @@ class AppBloc extends Bloc<AppEvent, AppState> {
336336
// If a new user is present, handle their data.
337337
if (newUser != null) {
338338
// In demo mode, ensure essential user-specific data (settings,
339-
// preferences, and the user object itself in the data client)
340-
// are initialized if they don't already exist. This prevents
341-
// NotFoundException during subsequent reads.
342-
if (_environment == local_config.AppEnvironment.demo &&
343-
demoDataInitializerService != null) {
339+
// preferences) are initialized if they don't already exist.
340+
final isDemoMode = _environment == local_config.AppEnvironment.demo;
341+
final isMigration =
342+
oldUser != null &&
343+
oldUser.appRole == AppUserRole.guestUser &&
344+
newUser.appRole == AppUserRole.standardUser;
345+
346+
// Initialize data for a new user, but ONLY if it's not a migration.
347+
if (isDemoMode && !isMigration && demoDataInitializerService != null) {
344348
_logger.info(
345349
'[AppBloc] Demo mode: Initializing user-specific data for '
346350
'user: ${newUser.id}',
@@ -376,9 +380,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
376380
}
377381

378382
// Handle data migration if an anonymous user signs in.
379-
if (oldUser != null &&
380-
oldUser.appRole == AppUserRole.guestUser &&
381-
newUser.appRole == AppUserRole.standardUser) {
383+
if (isMigration) {
382384
_logger.info(
383385
'[AppBloc] Anonymous user ${oldUser.id} transitioned to '
384386
'authenticated user ${newUser.id}. Attempting data migration.',
@@ -414,10 +416,11 @@ class AppBloc extends Bloc<AppEvent, AppState> {
414416
}
415417
}
416418

417-
// If not in demo mode, or if the demo initializer is not used,
418-
// perform the standard data fetch. In demo mode, this call is now
419-
// redundant as it's handled immediately after initialization.
420-
if (_environment != local_config.AppEnvironment.demo) {
419+
// If not in demo mode, or if it was a migration (where data is already
420+
// fetched post-migration), perform the standard data fetch.
421+
// This avoids a redundant fetch in the demo initialization path,
422+
// which already fetches data.
423+
if (!isDemoMode || isMigration) {
421424
await _fetchAndSetUserData(newUser, emit);
422425
}
423426
} else {

0 commit comments

Comments
 (0)