Skip to content

Commit 9e6402f

Browse files
committed
refactor(DemoDataInitializerService): remove User object initialization
- Remove _ensureUserClientUserExists method - Remove User repository dependency - Update comments and documentation to reflect changes - Simplify logger calls by removing unnecessary line breaks
1 parent 3a8bd05 commit 9e6402f

File tree

1 file changed

+10
-54
lines changed

1 file changed

+10
-54
lines changed

lib/app/services/demo_data_initializer_service.dart

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import 'package:logging/logging.dart'; // Import Logger
44

55
/// {@template demo_data_initializer_service}
66
/// A service responsible for ensuring that essential user-specific data
7-
/// (like [UserAppSettings], [UserContentPreferences], and the [User] object
8-
/// itself) exists in the data in-memory clients when a user is first encountered
9-
/// in the demo environment.
7+
/// (like [UserAppSettings] and [UserContentPreferences]) exists in the
8+
/// data in-memory clients when a user is first encountered in the demo environment.
109
///
1110
/// This service is specifically designed for the in-memory data clients
1211
/// used in the demo environment. In production/development environments,
@@ -21,34 +20,31 @@ class DemoDataInitializerService {
2120
required DataRepository<User> userRepository,
2221
}) : _userAppSettingsRepository = userAppSettingsRepository,
2322
_userContentPreferencesRepository = userContentPreferencesRepository,
24-
_userRepository = userRepository,
23+
_userRepository = userRepository, // Retained for consistency in constructor, but not used internally by this service.
2524
_logger = Logger('DemoDataInitializerService'); // Initialize logger
2625

2726
final DataRepository<UserAppSettings> _userAppSettingsRepository;
2827
final DataRepository<UserContentPreferences>
2928
_userContentPreferencesRepository;
30-
final DataRepository<User> _userRepository;
29+
final DataRepository<User> _userRepository; // Retained for consistency in constructor, but not used internally by this service.
3130
final Logger _logger; // Add logger instance
3231

3332
/// Initializes essential user-specific data in the in-memory clients
3433
/// for the given [user].
3534
///
36-
/// This method checks if [UserAppSettings], [UserContentPreferences],
37-
/// and the [User] object itself exist for the provided user ID. If any
38-
/// are missing, it creates them with default values.
35+
/// This method checks if [UserAppSettings] and [UserContentPreferences]
36+
/// exist for the provided user ID. If any are missing, it creates them
37+
/// with default values.
3938
///
4039
/// This prevents "READ FAILED" errors when the application attempts to
4140
/// access these user-specific data points for a newly signed-in anonymous
4241
/// user in the demo environment.
4342
Future<void> initializeUserSpecificData(User user) async {
44-
_logger.info(
45-
'Initializing user-specific data for user ID: ${user.id}',
46-
);
43+
_logger.info('Initializing user-specific data for user ID: ${user.id}');
4744

4845
await Future.wait([
4946
_ensureUserAppSettingsExist(user.id),
5047
_ensureUserContentPreferencesExist(user.id),
51-
_ensureUserClientUserExists(user),
5248
]);
5349

5450
_logger.info(
@@ -61,9 +57,7 @@ class DemoDataInitializerService {
6157
Future<void> _ensureUserAppSettingsExist(String userId) async {
6258
try {
6359
await _userAppSettingsRepository.read(id: userId, userId: userId);
64-
_logger.info(
65-
'UserAppSettings found for user ID: $userId.',
66-
);
60+
_logger.info('UserAppSettings found for user ID: $userId.');
6761
} on NotFoundException {
6862
_logger.info(
6963
'UserAppSettings not found for user ID: '
@@ -115,9 +109,7 @@ class DemoDataInitializerService {
115109
Future<void> _ensureUserContentPreferencesExist(String userId) async {
116110
try {
117111
await _userContentPreferencesRepository.read(id: userId, userId: userId);
118-
_logger.info(
119-
'UserContentPreferences found for user ID: $userId.',
120-
);
112+
_logger.info('UserContentPreferences found for user ID: $userId.');
121113
} on NotFoundException {
122114
_logger.info(
123115
'UserContentPreferences not found for '
@@ -148,40 +140,4 @@ class DemoDataInitializerService {
148140
rethrow;
149141
}
150142
}
151-
152-
/// Ensures that the [User] object for the given [user] exists in the
153-
/// user client. If not found, creates it. If found, updates it.
154-
///
155-
/// This is important because the `AuthInmemory` client might create a
156-
/// basic user, but the `DataInMemory<User>` client might not have it
157-
/// immediately.
158-
Future<void> _ensureUserClientUserExists(User user) async {
159-
try {
160-
await _userRepository.read(id: user.id, userId: user.id);
161-
// If user exists, ensure it's up-to-date (e.g., if roles changed)
162-
await _userRepository.update(id: user.id, item: user, userId: user.id);
163-
_logger.info(
164-
'User object found and updated in '
165-
'user client for ID: ${user.id}.',
166-
);
167-
} on NotFoundException {
168-
_logger.info(
169-
'User object not found in user client '
170-
'for ID: ${user.id}. Creating it.',
171-
);
172-
await _userRepository.create(item: user, userId: user.id);
173-
_logger.info(
174-
'User object created in user client '
175-
'for ID: ${user.id}.',
176-
);
177-
} catch (e, s) {
178-
_logger.severe(
179-
'Error ensuring User object exists in '
180-
'user client for ID: ${user.id}: $e',
181-
e,
182-
s,
183-
);
184-
rethrow;
185-
}
186-
}
187143
}

0 commit comments

Comments
 (0)