def test_count_org_from_dict(session): # pylint:disable=unused-argument """Assert that an Org can be updated from a dictionary.""" user = factory_user_model() org = factory_org_model(name='My Test Org', session=session) org.created_by_id = user.id session.add(org) session.commit() assert OrgModel.get_count_of_org_created_by_user_id(user.id) == 1
def validate(is_fatal=False, **kwargs) -> ValidatorResponse: """Validate account limit for user.""" user: UserContext = kwargs['user'] validator_response = ValidatorResponse() if not user.is_staff_admin(): count = OrgModel.get_count_of_org_created_by_user_id(user.user_id) if count >= current_app.config.get('MAX_NUMBER_OF_ORGS'): validator_response.add_error(Error.MAX_NUMBER_OF_ORGS_LIMIT) if is_fatal: raise BusinessException(Error.MAX_NUMBER_OF_ORGS_LIMIT, None) return validator_response
def create_org(org_info: dict, user_id, token_info: Dict = None): """Create a new organization.""" current_app.logger.debug('<create_org ') is_staff_admin = token_info and 'staff_admin' in token_info.get( 'realm_access').get('roles') if not is_staff_admin: # staff can create any number of orgs count = OrgModel.get_count_of_org_created_by_user_id(user_id) if count >= current_app.config.get('MAX_NUMBER_OF_ORGS'): raise BusinessException(Error.MAX_NUMBER_OF_ORGS_LIMIT, None) if org_info.get('accessType', None) == AccessType.ANONYMOUS.value: raise BusinessException(Error.USER_CANT_CREATE_ANONYMOUS_ORG, None) existing_similar__org = OrgModel.find_similar_org_by_name( org_info['name']) if existing_similar__org is not None: raise BusinessException(Error.DATA_CONFLICT, None) org = OrgModel.create_from_dict(camelback2snake(org_info)) if is_staff_admin: org.access_type = AccessType.ANONYMOUS.value org.billable = False else: org.access_type = AccessType.BCSC.value org.billable = True org.save() current_app.logger.info(f'<created_org org_id:{org.id}') # create the membership record for this user if its not created by staff and access_type is anonymous if not is_staff_admin and org_info.get( 'access_type') != AccessType.ANONYMOUS: membership = MembershipModel( org_id=org.id, user_id=user_id, membership_type_code='OWNER', membership_type_status=Status.ACTIVE.value) membership.save() # Add the user to account_holders group KeycloakService.join_account_holders_group() # TODO Remove later, create payment settings now with default values AccountPaymentModel.create_from_dict({'org_id': org.id}) return Org(org)
def validate_account_limit(is_staff_admin, user_id): """Validate account limit.""" if not is_staff_admin: # staff can create any number of orgs count = OrgModel.get_count_of_org_created_by_user_id(user_id) if count >= current_app.config.get('MAX_NUMBER_OF_ORGS'): raise BusinessException(Error.MAX_NUMBER_OF_ORGS_LIMIT, None)
def create_org( org_info: dict, user_id, # pylint: disable=too-many-locals, too-many-statements token_info: Dict = None, bearer_token: str = None): """Create a new organization.""" current_app.logger.debug('<create_org ') bcol_credential = org_info.pop('bcOnlineCredential', None) mailing_address = org_info.pop('mailingAddress', None) bcol_account_number = None bcol_user_id = None # If the account is created using BCOL credential, verify its valid bc online account if bcol_credential: bcol_response = Org.get_bcol_details(bcol_credential, org_info, bearer_token).json() bcol_account_number = bcol_response.get('accountNumber') bcol_user_id = bcol_response.get('userId') org_info[ 'typeCode'] = OrgType.PREMIUM.value if bcol_account_number else OrgType.BASIC.value is_staff_admin = token_info and 'staff_admin' in token_info.get( 'realm_access').get('roles') if not is_staff_admin: # staff can create any number of orgs count = OrgModel.get_count_of_org_created_by_user_id(user_id) if count >= current_app.config.get('MAX_NUMBER_OF_ORGS'): raise BusinessException(Error.MAX_NUMBER_OF_ORGS_LIMIT, None) if org_info.get('accessType', None) == AccessType.ANONYMOUS.value: raise BusinessException(Error.USER_CANT_CREATE_ANONYMOUS_ORG, None) if not bcol_account_number: # Allow duplicate names if premium Org.raise_error_if_duplicate_name(org_info['name']) org = OrgModel.create_from_dict(camelback2snake(org_info)) org.add_to_session() if is_staff_admin: org.access_type = AccessType.ANONYMOUS.value org.billable = False else: org.access_type = AccessType.BCSC.value org.billable = True # If mailing address is provided, save it if mailing_address: Org.add_contact_to_org(mailing_address, org) # create the membership record for this user if its not created by staff and access_type is anonymous if not is_staff_admin and org_info.get( 'access_type') != AccessType.ANONYMOUS: membership = MembershipModel( org_id=org.id, user_id=user_id, membership_type_code='OWNER', membership_type_status=Status.ACTIVE.value) membership.add_to_session() # Add the user to account_holders group KeycloakService.join_account_holders_group() Org.add_payment_settings(org.id, bcol_account_number, bcol_user_id) org.save() current_app.logger.info(f'<created_org org_id:{org.id}') return Org(org)