Customer check fix (#401)
* refactor: streamline MSSP license check logic for customer provisioning * precommit fixes * fix: improve MSSP license check logic for customer provisioning * precommit fixes
taylor_socfortress committed
Jan 30, 2025 at 08:50 UTC
4bef864eb8cb700a9f7ddb5fd092f8d2f0586674
1 file changed
+90
-46
backend/app/customers/routes/customers.py
+90
-46
@@ -73,65 +73,109 @@ async def verify_unique_customer_code(
73
)
74
75
76
+# async def mssp_license_check(session: AsyncSession):
77
+# """
78
+# Check if the current number of provisioned customers is within the allowed range based on the MSSP license type.
79
+# Customer 0 is free, 1-5 customers require an "MSSP 1-5" license, and 6-10 customers require an "MSSP 6-10" license.
80
+
81
+# Args:
82
+# session (AsyncSession): The database session.
83
+
84
+# Raises:
85
+# HTTPException: If the MSSP is not allowed to provision more customers.
86
+# """
87
+# # Select all customers to check the number of provisioned customers
88
+# stmt = select(Customers)
89
+# result = await session.execute(stmt)
90
+# customers = result.scalars().all()
91
+# provisioned_customers = len(customers)
92
+# logger.info(f"Provisioned customers: {provisioned_customers}")
93
+
94
+# if 1 <= provisioned_customers <= 5:
95
+# # Check the license of the MSSP if the number of provisioned customers is between 1 and 5
96
+# try:
97
+# await is_feature_enabled(
98
+# "MSSP 5",
99
+# session,
100
+# message="You have reached the maximum number of customers allowed for your license type. Please upgrade your license to provision more customers.",
101
+# )
102
+# except HTTPException as e:
103
+# if e.status_code == 400:
104
+# # If MSSP 1-5 license check fails, check for MSSP 6-10 or MSSP Unlimited license
105
+# try:
106
+# await is_feature_enabled(
107
+# "MSSP 10",
108
+# session,
109
+# message="You have reached the maximum number of customers allowed for your license type. Please upgrade your license to provision more customers.",
110
+# )
111
+# except HTTPException as e2:
112
+# if e2.status_code == 400:
113
+# await is_feature_enabled(
114
+# "MSSP Unlimited",
115
+# session,
116
+# message="You have reached the maximum number of customers allowed for your license type. Please upgrade your license to provision more customers.",
117
+# )
118
+# else:
119
+# raise e2
120
+# else:
121
+# raise e
122
+# elif 6 <= provisioned_customers <= 10:
123
+# # Check the license of the MSSP if the number of provisioned customers is between 6 and 10
124
+# await is_feature_enabled(
125
+# "MSSP 10",
126
+# session,
127
+# message="You have reached the maximum number of customers allowed for your license type. Please upgrade your license to provision more customers.",
128
+# )
129
+# elif provisioned_customers > 10:
130
+# await is_feature_enabled(
131
+# "MSSP Unlimited",
132
+# session,
133
+# message="You have reached the maximum number of customers allowed for your license type. Please upgrade your license to provision more customers.",
134
+# )
135
+
136
+
137
async def mssp_license_check(session: AsyncSession):
138
"""
139
Check if the current number of provisioned customers is within the allowed range based on the MSSP license type.
79
- Customer 0 is free, 1-5 customers require an "MSSP 1-5" license, and 6-10 customers require an "MSSP 6-10" license.
80
-
81
- Args:
82
- session (AsyncSession): The database session.
83
-
84
- Raises:
85
- HTTPException: If the MSSP is not allowed to provision more customers.
140
+ - MSSP Unlimited: No limit
141
+ - MSSP 10: Up to 10 customers (0-9 current customers)
142
+ - MSSP 5: Up to 5 customers (0-4 current customers)
143
"""
87
- # Select all customers to check the number of provisioned customers
144
stmt = select(Customers)
145
result = await session.execute(stmt)
146
customers = result.scalars().all()
147
provisioned_customers = len(customers)
148
logger.info(f"Provisioned customers: {provisioned_customers}")
149
94
- if 1 <= provisioned_customers <= 5:
95
- # Check the license of the MSSP if the number of provisioned customers is between 1 and 5
150
+ error_message = "You have reached the maximum number of customers allowed for your license type. Please upgrade your license to provision more customers."
151
+
152
+ # Try most permissive license first
153
+ try:
154
+ await is_feature_enabled("MSSP Unlimited", session, message=error_message)
155
+ return # License check passed
156
+ except HTTPException as e:
157
+ if e.status_code != 400:
158
+ raise e
159
+
160
+ # Check MSSP 10 license - adding new customer must not exceed 10
161
+ if provisioned_customers < 10:
162
try:
97
- await is_feature_enabled(
98
- "MSSP 5",
99
- session,
100
- message="You have reached the maximum number of customers allowed for your license type. Please upgrade your license to provision more customers.",
101
- )
163
+ await is_feature_enabled("MSSP 10", session, message=error_message)
164
+ return # License check passed
165
except HTTPException as e:
103
- if e.status_code == 400:
104
- # If MSSP 1-5 license check fails, check for MSSP 6-10 or MSSP Unlimited license
105
- try:
106
- await is_feature_enabled(
107
- "MSSP 10",
108
- session,
109
- message="You have reached the maximum number of customers allowed for your license type. Please upgrade your license to provision more customers.",
110
- )
111
- except HTTPException as e2:
112
- if e2.status_code == 400:
113
- await is_feature_enabled(
114
- "MSSP Unlimited",
115
- session,
116
- message="You have reached the maximum number of customers allowed for your license type. Please upgrade your license to provision more customers.",
117
- )
118
- else:
119
- raise e2
120
- else:
166
+ if e.status_code != 400:
167
raise e
122
- elif 6 <= provisioned_customers <= 10:
123
- # Check the license of the MSSP if the number of provisioned customers is between 6 and 10
124
- await is_feature_enabled(
125
- "MSSP 10",
126
- session,
127
- message="You have reached the maximum number of customers allowed for your license type. Please upgrade your license to provision more customers.",
128
- )
129
- elif provisioned_customers > 10:
130
- await is_feature_enabled(
131
- "MSSP Unlimited",
132
- session,
133
- message="You have reached the maximum number of customers allowed for your license type. Please upgrade your license to provision more customers.",
134
- )
168
+
169
+ # Check MSSP 5 license - adding new customer must not exceed 5
170
+ if provisioned_customers < 5:
171
+ try:
172
+ await is_feature_enabled("MSSP 5", session, message=error_message)
173
+ return # License check passed
174
+ except HTTPException as e:
175
+ if e.status_code != 400:
176
+ raise e
177
+
178
+ raise HTTPException(status_code=400, detail=error_message)
179
180
181
@customers_router.post(