def update_product_subscription(product_subscription_id: int, is_approved: bool, org_id: int, is_new_transaction: bool = True): """Update Product Subscription.""" current_app.logger.debug('<update_task_product ') # Approve/Reject Product subscription product_subscription: ProductSubscriptionModel = ProductSubscriptionModel.find_by_id( product_subscription_id) if is_approved: product_subscription.status_code = ProductSubscriptionStatus.ACTIVE.value else: product_subscription.status_code = ProductSubscriptionStatus.REJECTED.value product_subscription.flush() if is_new_transaction: # Commit the transaction if it's a new transaction db.session.commit() # Get the org and to get admin mail address org: OrgModel = OrgModel.find_by_org_id(org_id) # Find admin email address admin_email = ContactLinkModel.find_by_user_id( org.members[0].user.id).contact.email product_model: ProductCodeModel = ProductCodeModel.find_by_code( product_subscription.product_code) Product.send_approved_product_subscription_notification( admin_email, product_model.description, product_subscription.status_code) if is_approved: ActivityLogPublisher.publish_activity( Activity(org_id, ActivityAction.ADD_PRODUCT_AND_SERVICE.value, name=product_model.description)) current_app.logger.debug('>update_task_product ')
def get_all_product_subscription(org_id, skip_auth=False): """Get a list of all products with their subscription details.""" org = OrgModel.find_by_org_id(org_id) if not org: raise BusinessException(Error.DATA_NOT_FOUND, None) # Check authorization for the user if not skip_auth: check_auth(one_of_roles=(*CLIENT_ADMIN_ROLES, STAFF), org_id=org_id) product_subscriptions: List[ ProductSubscriptionModel] = ProductSubscriptionModel.find_by_org_id( org_id) subscriptions_dict = { x.product_code: x.status_code for x in product_subscriptions } products = Product.get_products(include_hidden=False) for product in products: product['subscriptionStatus'] = subscriptions_dict.get( product.get('code'), ProductSubscriptionStatus.NOT_SUBSCRIBED.value) return products
def get_all_product_subscription(org_id, include_internal_products=True, token_info: Dict = None, skip_auth=False): """Get a list of all products with their subscription details.""" org = OrgModel.find_by_org_id(org_id) if not org: raise BusinessException(Error.DATA_NOT_FOUND, None) # Check authorization for the user if not skip_auth: check_auth(token_info, one_of_roles=(*CLIENT_ADMIN_ROLES, STAFF), org_id=org_id) product_subscriptions: List[ ProductSubscriptionModel] = ProductSubscriptionModel.find_by_org_id( org_id) subscriptions_dict = { x.product_code: x.status_code for x in product_subscriptions } # We only want to return products that have content configured, # so read configuration and merge merged_product_infos = [] products_config = current_app.config.get('PRODUCT_CONFIG') products: List[ProductCodeModel] = ProductCodeModel.get_all_products() if products: for product in products: if not include_internal_products and product.type_code == ProductTypeCode.INTERNAL.value: continue product_config = products_config.get(product.code) if product_config: merged_product_infos.append({ 'code': product.code, 'name': product.description, 'description': product_config.get('description'), 'url': product_config.get('url'), 'type': product.type_code, 'mdiIcon': product_config.get('mdiIcon'), 'subscriptionStatus': subscriptions_dict.get( product.code, ProductSubscriptionStatus.NOT_SUBSCRIBED.value) }) return merged_product_infos
def create_subscription_from_bcol_profile(org_id: int, bcol_profile_flags: List[str]): """Create product subscription from bcol profile flags.""" if not bcol_profile_flags: return for profile_flag in bcol_profile_flags: product_code = BCOL_PROFILE_PRODUCT_MAP.get(profile_flag, None) if product_code: # Check if account already have an entry for this product. subscription: ProductSubscriptionModel = ProductSubscriptionModel.find_by_org_id_product_code( org_id, product_code) if not subscription: ProductSubscriptionModel( org_id=org_id, product_code=product_code, status_code=ProductSubscriptionStatus.ACTIVE.value ).flush() elif subscription and \ (existing_sub := subscription[0]).status_code != ProductSubscriptionStatus.ACTIVE.value: existing_sub.status_code = ProductSubscriptionStatus.ACTIVE.value existing_sub.flush()
def create_product_subscription( org_id, subscription_data: Dict[str, Any], # pylint: disable=too-many-locals is_new_transaction: bool = True, skip_auth=False): """Create product subscription for the user. create product subscription first create the product role next if roles are given """ org: OrgModel = OrgModel.find_by_org_id(org_id) if not org: raise BusinessException(Error.DATA_NOT_FOUND, None) # Check authorization for the user if not skip_auth: check_auth(one_of_roles=(*CLIENT_ADMIN_ROLES, STAFF), org_id=org_id) subscriptions_list = subscription_data.get('subscriptions') for subscription in subscriptions_list: product_code = subscription.get('productCode') existing_product_subscriptions = ProductSubscriptionModel.find_by_org_id_product_code( org_id, product_code) if existing_product_subscriptions: raise BusinessException(Error.PRODUCT_SUBSCRIPTION_EXISTS, None) product_model: ProductCodeModel = ProductCodeModel.find_by_code( product_code) if product_model: # Check if product needs premium account, if yes skip and continue. if product_model.premium_only and org.type_code != OrgType.PREMIUM.value: continue subscription_status = Product.find_subscription_status( org, product_model) product_subscription = ProductSubscriptionModel( org_id=org_id, product_code=product_code, status_code=subscription_status).flush() if subscription_status == ProductSubscriptionStatus.ACTIVE.value: ActivityLogPublisher.publish_activity( Activity(org_id, ActivityAction.ADD_PRODUCT_AND_SERVICE.value, name=product_model.description)) # If there is a linked product, add subscription to that too. # This is to handle cases where Names and Business Registry is combined together. if product_model.linked_product_code: ProductSubscriptionModel( org_id=org_id, product_code=product_model.linked_product_code, status_code=subscription_status).flush() if subscription_status == ProductSubscriptionStatus.ACTIVE.value: ActivityLogPublisher.publish_activity( Activity( org_id, ActivityAction.ADD_PRODUCT_AND_SERVICE.value, name=product_model.description)) # create a staff review task for this product subscription if pending status if subscription_status == ProductSubscriptionStatus.PENDING_STAFF_REVIEW.value: user = UserModel.find_by_jwt_token() Product._create_review_task(org, product_model, product_subscription, user) else: raise BusinessException(Error.DATA_NOT_FOUND, None) if is_new_transaction: # Commit the transaction if it's a new transaction db.session.commit() return Product.get_all_product_subscription(org_id=org_id, skip_auth=True)
def create_product_subscription( org_id, subscription_data: Dict[str, Any], # pylint: disable=too-many-locals is_new_transaction: bool = True, token_info: Dict = None, skip_auth=False): """Create product subscription for the user. create product subscription first create the product role next if roles are given """ org: OrgModel = OrgModel.find_by_org_id(org_id) if not org: raise BusinessException(Error.DATA_NOT_FOUND, None) # Check authorization for the user if not skip_auth: check_auth(token_info, one_of_roles=(*CLIENT_ADMIN_ROLES, STAFF), org_id=org_id) subscriptions_list = subscription_data.get('subscriptions') # just used for returning all the models.. not ideal.. # todo remove this and may be return the subscriptions from db subscriptions_model_list = [] for subscription in subscriptions_list: product_code = subscription.get('productCode') existing_product_subscriptions = ProductSubscriptionModel.find_by_org_id_product_code( org_id, product_code) if existing_product_subscriptions: raise BusinessException(Error.PRODUCT_SUBSCRIPTION_EXISTS, None) product_model: ProductCodeModel = ProductCodeModel.find_by_code( product_code) if product_model: subscription_status = Product.find_subscription_status( org, product_model) product_subscription = ProductSubscriptionModel(org_id=org_id, product_code=product_code, status_code=subscription_status) \ .flush() # create a staff review task for this product subscription if pending status if subscription_status == ProductSubscriptionStatus.PENDING_STAFF_REVIEW.value: user = UserModel.find_by_jwt_token(token=token_info) task_type = product_model.description task_info = { 'name': org.name, 'relationshipId': product_subscription.id, 'relatedTo': user.id, 'dateSubmitted': datetime.today(), 'relationshipType': TaskRelationshipType.PRODUCT.value, 'type': task_type, 'status': TaskStatus.OPEN.value, 'accountId': org_id, 'relationship_status': TaskRelationshipStatus.PENDING_STAFF_REVIEW.value } do_commit = False TaskService.create_task(task_info, do_commit) subscriptions_model_list.append(product_subscription) else: raise BusinessException(Error.DATA_NOT_FOUND, None) if is_new_transaction: # Commit the transaction if it's a new transaction db.session.commit() # TODO return something better/useful.may be return the whole model from db return subscriptions_model_list