Skip to content

Commit 023a3c0

Browse files
committed
feat(engagement): implement single engagement per user and entity
- Add a business logic check to prevent multiple engagements by the same user for the same entity - Query existing engagements for the authenticated user and the entity in question - Throw a ConflictException if an existing engagement is found - Log a warning message when attempting to create a duplicate engagement
1 parent 60e6725 commit 023a3c0

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lib/src/registry/data_operation_registry.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,24 @@ class DataOperationRegistry {
318318
);
319319
}
320320

321+
// Business Logic Check: Ensure a user can only have one engagement
322+
// per headline to prevent duplicate reactions or comments.
323+
final engagementRepository = context.read<DataRepository<Engagement>>();
324+
final existingEngagements = await engagementRepository.readAll(
325+
filter: {
326+
'userId': authenticatedUser.id,
327+
'entityId': engagementToCreate.entityId,
328+
'entityType': engagementToCreate.entityType.name,
329+
},
330+
);
331+
332+
if (existingEngagements.items.isNotEmpty) {
333+
_log.warning(
334+
'User ${authenticatedUser.id} attempted to create a second engagement for entity ${engagementToCreate.entityId}.',
335+
);
336+
throw const ConflictException('An engagement for this item already exists.');
337+
}
338+
321339
// Limit Check: Delegate to the centralized service.
322340
await userActionLimitService.checkEngagementCreationLimit(
323341
user: authenticatedUser,

0 commit comments

Comments
 (0)