5656 get_product_notification_data ,
5757 get_product_notification_type ,
5858)
59- from auth_api .utils .roles import CLIENT_ADMIN_ROLES , CLIENT_AUTH_ROLES , STAFF
59+ from auth_api .utils .roles import CLIENT_ADMIN_ROLES , CLIENT_AUTH_ROLES , GOV_ORG_TYPES , STAFF
6060from auth_api .utils .user_context import UserContext , user_context
6161
6262from .activity_log_publisher import ActivityLogPublisher
@@ -156,6 +156,41 @@ def resubmit_product_subscription(org_id, subscription_data: dict[str, Any], ski
156156
157157 return Product .get_all_product_subscription (org_id = org_id , skip_auth = True )
158158
159+ @staticmethod
160+ def _check_gov_org_add_product_previously_approved (
161+ org_id : int ,
162+ product_code : str
163+ ) -> tuple [bool , Any ]:
164+ """Check if GOV org's account fee product was previously approved (NEW_PRODUCT_FEE_REVIEW task)."""
165+ inactive_sub = ProductSubscriptionModel .find_by_org_id_product_code (
166+ org_id = org_id , product_code = product_code , valid_statuses = (ProductSubscriptionStatus .INACTIVE .value ,)
167+ )
168+ if not inactive_sub :
169+ return False , None
170+ task_add_product = TaskModel .find_by_task_relationship_id (
171+ inactive_sub .id , TaskRelationshipType .PRODUCT .value , TaskStatus .COMPLETED .value
172+ )
173+ task_create_org = TaskModel .find_by_task_relationship_id (
174+ org_id , TaskRelationshipType .ORG .value , TaskStatus .COMPLETED .value
175+ )
176+ product_invalid = (
177+ task_add_product is None or (
178+ task_add_product .action in (TaskAction .NEW_PRODUCT_FEE_REVIEW .value , TaskAction .PRODUCT_REVIEW .value )
179+ and task_add_product .relationship_status != TaskRelationshipStatus .ACTIVE .value
180+ )
181+ )
182+ if product_invalid :
183+ org_invalid = (
184+ task_create_org is None or (
185+ task_create_org .action in (TaskAction .AFFIDAVIT_REVIEW .value , TaskAction .ACCOUNT_REVIEW .value )
186+ and task_create_org .relationship_status != TaskRelationshipStatus .ACTIVE .value
187+ )
188+ )
189+
190+ if org_invalid :
191+ return False , None
192+ return True , inactive_sub
193+
159194 @staticmethod
160195 def _is_previously_approved (org_id : int , product_code : str ):
161196 """Check if this product has a task that was previously approved."""
@@ -170,7 +205,7 @@ def _is_previously_approved(org_id: int, product_code: str):
170205 )
171206 if task is None or (
172207 task .relationship_status != TaskRelationshipStatus .ACTIVE .value
173- and task .action == TaskAction .PRODUCT_REVIEW .value
208+ and task .action in ( TaskAction .PRODUCT_REVIEW .value , TaskAction . NEW_PRODUCT_FEE_REVIEW . value )
174209 ):
175210 return False , None
176211
@@ -183,6 +218,7 @@ def create_product_subscription(
183218 is_new_transaction : bool = True ,
184219 skip_auth = False ,
185220 auto_approve = False ,
221+ staff_review_for_create_org = False ,
186222 ):
187223 """Create product subscription for the user.
188224
@@ -198,6 +234,7 @@ def create_product_subscription(
198234
199235 subscriptions_list = subscription_data .get ("subscriptions" )
200236 for subscription in subscriptions_list :
237+ auto_approve_current = auto_approve
201238 product_code = subscription .get ("productCode" )
202239 if ProductSubscriptionModel .find_by_org_id_product_code (org_id , product_code ):
203240 raise BusinessException (Error .PRODUCT_SUBSCRIPTION_EXISTS , None )
@@ -206,11 +243,20 @@ def create_product_subscription(
206243 # Check if product requires system admin, if yes abort
207244 if product_model .need_system_admin :
208245 check_auth (system_required = True , org_id = org_id )
209- previously_approved , inactive_sub = Product ._is_previously_approved (org_id , product_code )
246+
247+ if org .access_type in GOV_ORG_TYPES and not staff_review_for_create_org :
248+ previously_approved , inactive_sub = Product ._check_gov_org_add_product_previously_approved (
249+ org .id , product_code
250+ )
251+ else :
252+ previously_approved , inactive_sub = Product ._is_previously_approved (org_id , product_code )
253+
210254 if previously_approved :
211- auto_approve = True
255+ auto_approve_current = True
212256
213- subscription_status = Product .find_subscription_status (org , product_model , auto_approve )
257+ subscription_status = Product .find_subscription_status (
258+ org , product_model , auto_approve_current , staff_review_for_create_org
259+ )
214260 product_subscription = Product ._subscribe_and_publish_activity (
215261 SubscriptionRequest (
216262 org_id = org_id ,
@@ -245,6 +291,7 @@ def create_product_subscription(
245291 ProductReviewTask (
246292 org_id = org .id ,
247293 org_name = org .name ,
294+ org_access_type = org .access_type ,
248295 product_code = product_subscription .product_code ,
249296 product_description = product_model .description ,
250297 product_subscription_id = product_subscription .id ,
@@ -374,11 +421,14 @@ def _reset_subscription_and_review_task(
374421 @staticmethod
375422 def _create_review_task (review_task : ProductReviewTask ):
376423 task_type = review_task .product_description
377- action_type = (
378- TaskAction .QUALIFIED_SUPPLIER_REVIEW .value
379- if review_task .product_code in QUALIFIED_SUPPLIER_PRODUCT_CODES
380- else TaskAction .PRODUCT_REVIEW .value
381- )
424+
425+ required_review_types = {AccessType .GOVM .value , AccessType .GOVN .value }
426+ if review_task .product_code in QUALIFIED_SUPPLIER_PRODUCT_CODES :
427+ action_type = TaskAction .QUALIFIED_SUPPLIER_REVIEW .value
428+ elif review_task .org_access_type in required_review_types :
429+ action_type = TaskAction .NEW_PRODUCT_FEE_REVIEW .value
430+ else :
431+ action_type = TaskAction .PRODUCT_REVIEW .value
382432
383433 task_info = {
384434 "name" : review_task .org_name ,
@@ -396,14 +446,13 @@ def _create_review_task(review_task: ProductReviewTask):
396446 TaskService .create_task (task_info , False )
397447
398448 @staticmethod
399- def find_subscription_status (org , product_model , auto_approve = False ):
449+ def find_subscription_status (org , product_model , auto_approve = False , staff_review_for_create_org = False ):
400450 """Return the subscriptions status based on org type."""
401- # GOVM accounts has default active subscriptions
402- skip_review_types = [AccessType .GOVM .value ]
403- if product_model .need_review and auto_approve is False :
451+ skip_review = org .access_type in GOV_ORG_TYPES and staff_review_for_create_org # prevent create second task when it's already added a staff review when creating org
452+ if (product_model .need_review or org .access_type in GOV_ORG_TYPES ) and not auto_approve :
404453 return (
405454 ProductSubscriptionStatus .ACTIVE .value
406- if ( org . access_type in skip_review_types )
455+ if skip_review
407456 else ProductSubscriptionStatus .PENDING_STAFF_REVIEW .value
408457 )
409458 return ProductSubscriptionStatus .ACTIVE .value
@@ -455,7 +504,10 @@ def get_all_product_subscription(org_id, skip_auth=False, **kwargs):
455504 check_auth (one_of_roles = (* CLIENT_AUTH_ROLES , STAFF ), org_id = org_id )
456505
457506 product_subscriptions : list [ProductSubscriptionModel ] = ProductSubscriptionModel .find_by_org_ids ([org_id ])
458- subscriptions_dict = {x .product_code : x .status_code for x in product_subscriptions }
507+ subscription_by_code = {
508+ sub .product_code : sub
509+ for sub in product_subscriptions
510+ }
459511
460512 # Include hidden products only for staff and SBC staff
461513 include_hidden = (
@@ -467,9 +519,9 @@ def get_all_product_subscription(org_id, skip_auth=False, **kwargs):
467519
468520 products = Product .get_products (include_hidden = include_hidden , staff_check = False )
469521 for product in products :
470- product [ "subscriptionStatus" ] = subscriptions_dict .get (
471- product . get ( "code" ) , ProductSubscriptionStatus .NOT_SUBSCRIBED .value
472- )
522+ sub = subscription_by_code .get (product . get ( "code" ))
523+ product [ "subscriptionStatus" ] = getattr ( sub , "status_code" , ProductSubscriptionStatus .NOT_SUBSCRIBED .value )
524+ product [ "id" ] = getattr ( sub , "id" , None )
473525
474526 return products
475527
@@ -483,7 +535,6 @@ def update_product_subscription(product_sub_info: ProductSubscriptionInfo, is_ne
483535 is_hold = product_sub_info .is_hold
484536 org_id = product_sub_info .org_id
485537 org_name = product_sub_info .org_name
486-
487538 # Approve/Reject Product subscription
488539 product_subscription : ProductSubscriptionModel = ProductSubscriptionModel .find_by_id (product_subscription_id )
489540
@@ -504,7 +555,6 @@ def update_product_subscription(product_sub_info: ProductSubscriptionInfo, is_ne
504555 product_model : ProductCodeModel = ProductCodeModel .find_by_code (product_subscription .product_code )
505556 # Find admin email addresses
506557 admin_emails = UserService .get_admin_emails_for_org (org_id )
507-
508558 if admin_emails != "" and not is_hold :
509559 Product .send_product_subscription_notification (
510560 ProductNotificationInfo (
0 commit comments