Skip to content

Commit 90049a0

Browse files
committed
refactor(api): update DefaultUserPreferenceLimitService for multi-role support
1 parent 894cb6d commit 90049a0

File tree

1 file changed

+57
-44
lines changed

1 file changed

+57
-44
lines changed

lib/src/services/default_user_preference_limit_service.dart

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,42 @@ class DefaultUserPreferenceLimitService implements UserPreferenceLimitService {
2929
final appConfig = await _appConfigRepository.read(id: _appConfigId);
3030
final limits = appConfig.userPreferenceLimits;
3131

32-
// 2. Determine the limit based on user role and item type
32+
// Admins have no limits.
33+
if (user.roles.contains(UserRoles.admin)) {
34+
return;
35+
}
36+
37+
// 2. Determine the limit based on the user's highest role.
3338
int limit;
34-
switch (user.role) {
35-
case UserRole.guestUser:
36-
if (itemType == 'headline') {
37-
limit = limits.guestSavedHeadlinesLimit;
38-
} else {
39-
// Applies to countries, sources, categories
40-
limit = limits.guestFollowedItemsLimit;
41-
}
42-
case UserRole.standardUser:
43-
if (itemType == 'headline') {
44-
limit = limits.authenticatedSavedHeadlinesLimit;
45-
} else {
46-
// Applies to countries, sources, categories
47-
limit = limits.authenticatedFollowedItemsLimit;
48-
}
49-
case UserRole.premiumUser:
50-
if (itemType == 'headline') {
51-
limit = limits.premiumSavedHeadlinesLimit;
52-
} else {
53-
limit = limits.premiumFollowedItemsLimit;
54-
}
55-
case UserRole.admin:
56-
// Admins have no limits
57-
return;
39+
String accountType;
40+
41+
if (user.roles.contains(UserRoles.premiumUser)) {
42+
accountType = 'premium';
43+
limit = (itemType == 'headline')
44+
? limits.premiumSavedHeadlinesLimit
45+
: limits.premiumFollowedItemsLimit;
46+
} else if (user.roles.contains(UserRoles.standardUser)) {
47+
accountType = 'standard';
48+
limit = (itemType == 'headline')
49+
? limits.authenticatedSavedHeadlinesLimit
50+
: limits.authenticatedFollowedItemsLimit;
51+
} else if (user.roles.contains(UserRoles.guestUser)) {
52+
accountType = 'guest';
53+
limit = (itemType == 'headline')
54+
? limits.guestSavedHeadlinesLimit
55+
: limits.guestFollowedItemsLimit;
56+
} else {
57+
// Fallback for users with unknown or no roles.
58+
throw const ForbiddenException(
59+
'Cannot determine preference limits for this user account.',
60+
);
5861
}
5962

6063
// 3. Check if adding the item would exceed the limit
6164
if (currentCount >= limit) {
6265
throw ForbiddenException(
6366
'You have reached the maximum number of $itemType items allowed '
64-
'for your account type (${user.role.name}).',
67+
'for your account type ($accountType).',
6568
);
6669
}
6770
} on HtHttpException {
@@ -86,48 +89,58 @@ class DefaultUserPreferenceLimitService implements UserPreferenceLimitService {
8689
final appConfig = await _appConfigRepository.read(id: _appConfigId);
8790
final limits = appConfig.userPreferenceLimits;
8891

89-
// 2. Determine limits based on user role
92+
// Admins have no limits.
93+
if (user.roles.contains(UserRoles.admin)) {
94+
return;
95+
}
96+
97+
// 2. Determine limits based on the user's highest role.
9098
int followedItemsLimit;
9199
int savedHeadlinesLimit;
100+
String accountType;
92101

93-
switch (user.role) {
94-
case UserRole.guestUser:
95-
followedItemsLimit = limits.guestFollowedItemsLimit;
96-
savedHeadlinesLimit = limits.guestSavedHeadlinesLimit;
97-
case UserRole.standardUser:
98-
followedItemsLimit = limits.authenticatedFollowedItemsLimit;
99-
savedHeadlinesLimit = limits.authenticatedSavedHeadlinesLimit;
100-
case UserRole.premiumUser:
101-
followedItemsLimit = limits.premiumFollowedItemsLimit;
102-
savedHeadlinesLimit = limits.premiumSavedHeadlinesLimit;
103-
case UserRole.admin:
104-
// Admins have no limits
105-
return;
102+
if (user.roles.contains(UserRoles.premiumUser)) {
103+
accountType = 'premium';
104+
followedItemsLimit = limits.premiumFollowedItemsLimit;
105+
savedHeadlinesLimit = limits.premiumSavedHeadlinesLimit;
106+
} else if (user.roles.contains(UserRoles.standardUser)) {
107+
accountType = 'standard';
108+
followedItemsLimit = limits.authenticatedFollowedItemsLimit;
109+
savedHeadlinesLimit = limits.authenticatedSavedHeadlinesLimit;
110+
} else if (user.roles.contains(UserRoles.guestUser)) {
111+
accountType = 'guest';
112+
followedItemsLimit = limits.guestFollowedItemsLimit;
113+
savedHeadlinesLimit = limits.guestSavedHeadlinesLimit;
114+
} else {
115+
// Fallback for users with unknown or no roles.
116+
throw const ForbiddenException(
117+
'Cannot determine preference limits for this user account.',
118+
);
106119
}
107120

108121
// 3. Check if proposed preferences exceed limits
109122
if (updatedPreferences.followedCountries.length > followedItemsLimit) {
110123
throw ForbiddenException(
111124
'You have reached the maximum number of followed countries allowed '
112-
'for your account type (${user.role.name}).',
125+
'for your account type ($accountType).',
113126
);
114127
}
115128
if (updatedPreferences.followedSources.length > followedItemsLimit) {
116129
throw ForbiddenException(
117130
'You have reached the maximum number of followed sources allowed '
118-
'for your account type (${user.role.name}).',
131+
'for your account type ($accountType).',
119132
);
120133
}
121134
if (updatedPreferences.followedCategories.length > followedItemsLimit) {
122135
throw ForbiddenException(
123136
'You have reached the maximum number of followed categories allowed '
124-
'for your account type (${user.role.name}).',
137+
'for your account type ($accountType).',
125138
);
126139
}
127140
if (updatedPreferences.savedHeadlines.length > savedHeadlinesLimit) {
128141
throw ForbiddenException(
129142
'You have reached the maximum number of saved headlines allowed '
130-
'for your account type (${user.role.name}).',
143+
'for your account type ($accountType).',
131144
);
132145
}
133146
} on HtHttpException {

0 commit comments

Comments
 (0)