Skip to content

Commit 4880179

Browse files
committed
feat(registry): add data operations for engagement and report
- Register read, readAll, create, update, and delete operations for Engagement and Report entities - Implement custom creators for engagement and report with security and limit checks - Update existing updaters and deleters for other entities - Rename UserPreferenceLimitService to UserActionLimitService for better naming consistency
1 parent 7fd22a2 commit 4880179

File tree

1 file changed

+76
-4
lines changed

1 file changed

+76
-4
lines changed

lib/src/registry/data_operation_registry.dart

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:flutter_news_app_api_server_full_source_code/src/rbac/permission
99
import 'package:flutter_news_app_api_server_full_source_code/src/services/country_query_service.dart';
1010
import 'package:flutter_news_app_api_server_full_source_code/src/services/dashboard_summary_service.dart';
1111
import 'package:flutter_news_app_api_server_full_source_code/src/services/push_notification_service.dart';
12-
import 'package:flutter_news_app_api_server_full_source_code/src/services/user_preference_limit_service.dart';
12+
import 'package:flutter_news_app_api_server_full_source_code/src/services/user_action_limit_service.dart';
1313
import 'package:logging/logging.dart';
1414

1515
// --- Typedefs for Data Operations ---
@@ -128,6 +128,10 @@ class DataOperationRegistry {
128128
'push_notification_device': (c, id) => c
129129
.read<DataRepository<PushNotificationDevice>>()
130130
.read(id: id, userId: null),
131+
'engagement': (c, id) =>
132+
c.read<DataRepository<Engagement>>().read(id: id, userId: null),
133+
'report': (c, id) =>
134+
c.read<DataRepository<Report>>().read(id: id, userId: null),
131135
});
132136

133137
// --- Register "Read All" Readers ---
@@ -191,6 +195,19 @@ class DataOperationRegistry {
191195
sort: s,
192196
pagination: p,
193197
),
198+
'engagement': (c, uid, f, s, p) =>
199+
c.read<DataRepository<Engagement>>().readAll(
200+
userId: uid,
201+
filter: f,
202+
sort: s,
203+
pagination: p,
204+
),
205+
'report': (c, uid, f, s, p) => c.read<DataRepository<Report>>().readAll(
206+
userId: uid,
207+
filter: f,
208+
sort: s,
209+
pagination: p,
210+
),
194211
});
195212

196213
// --- Register Item Creators ---
@@ -279,6 +296,49 @@ class DataOperationRegistry {
279296
userId: null,
280297
);
281298
},
299+
'engagement': (context, item, uid) async {
300+
_log.info('Executing custom creator for engagement.');
301+
final authenticatedUser = context.read<User>();
302+
final userActionLimitService = context.read<UserActionLimitService>();
303+
final engagementToCreate = item as Engagement;
304+
305+
// Security Check
306+
if (engagementToCreate.userId != authenticatedUser.id) {
307+
throw const ForbiddenException(
308+
'You can only create engagements for your own account.',
309+
);
310+
}
311+
312+
// Limit Check: Delegate to the centralized service.
313+
await userActionLimitService.checkEngagementCreationLimit(
314+
user: authenticatedUser,
315+
engagement: engagementToCreate,
316+
);
317+
318+
return context.read<DataRepository<Engagement>>().create(
319+
item: engagementToCreate,
320+
userId: null,
321+
);
322+
},
323+
'report': (context, item, uid) async {
324+
_log.info('Executing custom creator for report.');
325+
final authenticatedUser = context.read<User>();
326+
final userActionLimitService = context.read<UserActionLimitService>();
327+
final reportToCreate = item as Report;
328+
329+
// Security Check
330+
if (reportToCreate.reporterUserId != authenticatedUser.id) {
331+
throw const ForbiddenException(
332+
'You can only create reports for your own account.',
333+
);
334+
}
335+
336+
// Limit Check
337+
await userActionLimitService.checkReportCreationLimit(
338+
user: authenticatedUser);
339+
340+
return context.read<DataRepository<Report>>().create(item: item);
341+
},
282342
});
283343

284344
// --- Register Item Updaters ---
@@ -398,8 +458,7 @@ class DataOperationRegistry {
398458
);
399459
final authenticatedUser = context.read<User>();
400460
final permissionService = context.read<PermissionService>();
401-
final userPreferenceLimitService = context
402-
.read<UserPreferenceLimitService>();
461+
final userActionLimitService = context.read<UserActionLimitService>();
403462
final userContentPreferencesRepository = context
404463
.read<DataRepository<UserContentPreferences>>();
405464

@@ -416,7 +475,7 @@ class DataOperationRegistry {
416475
'User ${authenticatedUser.id} has bypass permission. Skipping limit checks.',
417476
);
418477
} else {
419-
await userPreferenceLimitService.checkUserContentPreferencesLimits(
478+
await userActionLimitService.checkUserContentPreferencesLimits(
420479
user: authenticatedUser,
421480
updatedPreferences: preferencesToUpdate,
422481
);
@@ -440,6 +499,15 @@ class DataOperationRegistry {
440499
id: id,
441500
item: item as InAppNotification,
442501
),
502+
'engagement': (c, id, item, uid) =>
503+
c.read<DataRepository<Engagement>>().update(
504+
id: id,
505+
item: item as Engagement,
506+
),
507+
'report': (c, id, item, uid) => c.read<DataRepository<Report>>().update(
508+
id: id,
509+
item: item as Report,
510+
),
443511
});
444512

445513
// --- Register Item Deleters ---
@@ -467,6 +535,10 @@ class DataOperationRegistry {
467535
'in_app_notification': (c, id, uid) => c
468536
.read<DataRepository<InAppNotification>>()
469537
.delete(id: id, userId: uid),
538+
'engagement': (c, id, uid) =>
539+
c.read<DataRepository<Engagement>>().delete(id: id, userId: uid),
540+
'report': (c, id, uid) =>
541+
c.read<DataRepository<Report>>().delete(id: id, userId: uid),
470542
});
471543
}
472544
}

0 commit comments

Comments
 (0)