def create_membership(access_type, is_staff_admin, org, user_id): """Create membership account.""" if not is_staff_admin and access_type != AccessType.ANONYMOUS.value: membership = MembershipModel(org_id=org.id, user_id=user_id, membership_type_code='ADMIN', membership_type_status=Status.ACTIVE.value) membership.add_to_session() # Add the user to account_holders group KeycloakService.join_account_holders_group()
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)