def test_create_task_product(session, keycloak_mock): # pylint:disable=unused-argument """Assert that a task can be created.""" user = factory_user_model() test_org = factory_org_model() test_product = factory_product_model(org_id=test_org.id) product: ProductCodeModel = ProductCodeModel.find_by_code( test_product.product_code) test_task_info = { 'name': test_org.name, 'relationshipId': test_product.id, 'relatedTo': user.id, 'dateSubmitted': datetime.today(), 'relationshipType': TaskRelationshipType.PRODUCT.value, 'type': product.description, 'status': [TaskStatus.OPEN.value], 'accountId': test_org.id, 'relationship_status': TaskRelationshipStatus.PENDING_STAFF_REVIEW.value } task = TaskService.create_task(test_task_info) assert task dictionary = task.as_dict() assert dictionary['name'] == test_task_info['name'] assert dictionary['account_id'] == test_org.id assert dictionary[ 'relationship_type'] == TaskRelationshipType.PRODUCT.value
def create_product_subscription(org_id, subscription_data: Dict[str, Any], is_new_transaction: bool = True): """Create product subscription for the user. create product subscription first create the product role next if roles are given """ org = OrgModel.find_by_org_id(org_id) if not org: raise BusinessException(Error.DATA_NOT_FOUND, None) 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') product = ProductCodeModel.find_by_code(product_code) if product: product_subscription = ProductSubscriptionModel( org_id=org_id, product_code=product_code).flush() subscriptions_model_list.append(product_subscription) else: raise BusinessException(Error.DATA_NOT_FOUND, None) Product._create_roles(is_new_transaction, product_code, product_subscription, subscription) # TODO return something better/useful.may be return the whole model from db return subscriptions_model_list
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 create_product_subscription(org_id, subscription_data: Tuple[Dict[str, Any]]): """Create product subscription for the user. create product subscription first create the product role next if roles are given """ org = OrgModel.find_by_org_id(org_id) if not org: raise BusinessException(Error.DATA_NOT_FOUND, None) 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') product = ProductCodeModel.find_by_code(product_code) if product: product_subscription = ProductSubscriptionModel( org_id=org_id, product_code=product_code).save() subscriptions_model_list.append(product_subscription) else: raise BusinessException(Error.DATA_NOT_FOUND, None) if subscription.get('product_roles') is not None: for role in subscription.get('productRoles'): product_role_code = ProductRoleCodeModel.find_by_code_and_product_code( role, product_code) if product_role_code: ProductSubscriptionRoleModel( product_subscription_id=product_subscription.id, product_role_id=product_role_code.id).save() # TODO return something better/useful.may be return the whole model from db return subscriptions_model_list
def find_product_type_by_code(code: str) -> str: """Find Product Type.""" code_from_cache = cache.get(code) if code_from_cache: return code_from_cache product_code_model: ProductCodeModel = ProductCodeModel.find_by_code( code) return getattr(product_code_model, 'type_code', '')
def test_put_task_product(client, jwt, session, keycloak_mock, monkeypatch): # pylint:disable=unused-argument """Assert that the task can be updated.""" # 1. Create User # 4. Create Product subscription # 5. Update the created task and the relationship # Post user, org and product subscription headers = factory_auth_header(jwt=jwt, claims=TestJwtClaims.staff_admin_role) user_with_token = TestUserInfo.user_staff_admin user_with_token['keycloak_guid'] = TestJwtClaims.public_user_role['sub'] user = factory_user_model_with_contact(user_with_token) patch_token_info( { 'sub': str(user_with_token['keycloak_guid']), 'username': '******', 'realm_access': { 'roles': ['edit'] } }, monkeypatch) affidavit_info = TestAffidavit.get_test_affidavit_with_contact() AffidavitService.create_affidavit(affidavit_info=affidavit_info) patch_token_info(TestJwtClaims.public_bceid_user, monkeypatch) org = OrgService.create_org(TestOrgInfo.org_with_mailing_address(), user_id=user.id) org_dict = org.as_dict() product_which_doesnt_need_approval = TestOrgProductsInfo.org_products1 rv_products = client.post( f"/api/v1/orgs/{org_dict.get('id')}/products", data=json.dumps(product_which_doesnt_need_approval), headers=headers, content_type='application/json') assert rv_products.status_code == http_status.HTTP_201_CREATED assert schema_utils.validate(rv_products.json, 'org_product_subscriptions_response')[0] tasks = TaskService.fetch_tasks(task_status=[TaskStatus.OPEN.value], page=1, limit=10) assert len(tasks['tasks']) == 1 product_which_needs_approval = TestOrgProductsInfo.org_products_vs rv_products = client.post(f"/api/v1/orgs/{org_dict.get('id')}/products", data=json.dumps(product_which_needs_approval), headers=headers, content_type='application/json') assert rv_products.status_code == http_status.HTTP_201_CREATED assert schema_utils.validate(rv_products.json, 'org_product_subscriptions_response')[0] tasks = TaskService.fetch_tasks(task_status=[TaskStatus.OPEN.value], page=1, limit=10) fetched_tasks = tasks['tasks'] fetched_task = fetched_tasks[1] assert fetched_task[ 'relationship_type'] == TaskRelationshipType.PRODUCT.value # Assert task name product: ProductCodeModel = ProductCodeModel.find_by_code( product_which_needs_approval['subscriptions'][0].get('productCode')) org_name = org_dict['name'] assert fetched_task['name'] == org_name assert fetched_task['type'] == product.description # Assert the task can be updated and the product status is changed to active update_task_payload = { 'relationshipStatus': ProductSubscriptionStatus.ACTIVE.value } headers = factory_auth_header(jwt=jwt, claims=TestJwtClaims.staff_role) rv = client.put('/api/v1/tasks/{}'.format(fetched_task['id']), data=json.dumps(update_task_payload), headers=headers, content_type='application/json') dictionary = json.loads(rv.data) assert rv.status_code == http_status.HTTP_200_OK assert dictionary['status'] == TaskStatus.COMPLETED.value assert dictionary[ 'relationshipStatus'] == TaskRelationshipStatus.ACTIVE.value
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