Exemple #1
0
def test_find_similar_org_by_name(session):  # pylint:disable=unused-argument
    """Assert that an Org can retrieved by its name."""
    org = factory_org_model(name='My Test Org', session=session)
    session.add(org)
    session.commit()

    found_org = OrgModel.find_similar_org_by_name(org.name)
    assert found_org
    assert found_org.name == org.name

    found_org = OrgModel.find_similar_org_by_name('Test Or')
    assert found_org is None
Exemple #2
0
    def update_org(
            self,
            org_info,
            token_info: Dict = None,  # pylint: disable=too-many-locals
            bearer_token: str = None):
        """Update the passed organization with the new info."""
        current_app.logger.debug('<update_org ')

        has_org_updates: bool = False  # update the org table if this variable is set true

        is_name_getting_updated = 'name' in org_info
        if is_name_getting_updated:
            existing_similar__org = OrgModel.find_similar_org_by_name(
                org_info['name'], self._model.id)
            if existing_similar__org is not None:
                raise BusinessException(Error.DATA_CONFLICT, None)
            has_org_updates = True

        # If the account is created using BCOL credential, verify its valid bc online account
        # If it's a valid account disable the current one and add a new one
        if bcol_credential := org_info.pop('bcOnlineCredential', None):
            bcol_response = Org.get_bcol_details(bcol_credential, bearer_token,
                                                 self._model.id).json()
            Org._map_response_to_org(bcol_response, org_info)
            has_org_updates = True
Exemple #3
0
    def update_org(
            self,
            org_info,
            token_info: Dict = None,  # pylint: disable=too-many-locals
            bearer_token: str = None):
        """Update the passed organization with the new info."""
        current_app.logger.debug('<update_org ')

        has_org_updates: bool = False  # update the org table if this variable is set true
        has_status_changing: bool = False

        org_model: OrgModel = self._model
        # to enforce necessary details for govm account creation
        is_govm_account = org_model.access_type == AccessType.GOVM.value
        is_govm_account_creation = is_govm_account and \
            org_model.status_code == OrgStatus.PENDING_INVITE_ACCEPT.value

        # govm name is not being updated now
        is_name_getting_updated = 'name' in org_info and not is_govm_account
        if is_name_getting_updated:
            existing_similar__org = OrgModel.find_similar_org_by_name(
                org_info['name'], self._model.id)
            if existing_similar__org is not None:
                raise BusinessException(Error.DATA_CONFLICT, None)
            has_org_updates = True

        # If the account is created using BCOL credential, verify its valid bc online account
        # If it's a valid account disable the current one and add a new one
        if bcol_credential := org_info.pop('bcOnlineCredential', None):
            bcol_response = Org.get_bcol_details(bcol_credential, bearer_token,
                                                 self._model.id).json()
            Org._map_response_to_org(bcol_response, org_info)
            has_org_updates = True
Exemple #4
0
    def update_org(self, org_info, bearer_token: str = None):
        """Update the passed organization with the new info."""
        current_app.logger.debug('<update_org ')

        if self._model.type_code != OrgType.PREMIUM.value:
            existing_similar__org = OrgModel.find_similar_org_by_name(
                org_info['name'], self._model.id)
            if existing_similar__org is not None:
                raise BusinessException(Error.DATA_CONFLICT, None)

        bcol_credential = org_info.pop('bcOnlineCredential', None)
        mailing_address = org_info.pop('mailingAddress', None)
        # If the account is created using BCOL credential, verify its valid bc online account
        # If it's a valid account disable the current one and add a new one
        if bcol_credential:
            self.add_bcol_to_account(bcol_credential, bearer_token, org_info)
        # Update mailing address
        if mailing_address:
            contact = self._model.contacts[0].contact
            contact.update_from_dict(**camelback2snake(mailing_address))
            contact.save()
        if self._model.type_code != OrgType.PREMIUM.value:
            self._model.update_org_from_dict(camelback2snake(org_info))
        current_app.logger.debug('>update_org ')
        return self
Exemple #5
0
def test_find_similar_org_by_name_inactive(session):  # pylint:disable=unused-argument
    """Assert that an inactive Org can not be retrieved by its name."""
    org = factory_org_model(name='My Test Org', session=session)
    session.add(org)
    session.commit()

    org.delete()

    found_org = OrgModel.find_similar_org_by_name(org.name)
    assert not found_org
Exemple #6
0
    def update_org(self, org_info):
        """Update the passed organization with the new info."""
        current_app.logger.debug('<update_org ')

        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)

        self._model.update_org_from_dict(camelback2snake(org_info))
        current_app.logger.debug('>update_org ')
        return self
Exemple #7
0
def validate(is_fatal=False, **kwargs) -> ValidatorResponse:
    """Validate and return org name."""
    name = kwargs.get('name')
    branch_name = kwargs.get('branch_name')
    org_id = kwargs.get('org_id', None)
    validator_response = ValidatorResponse()
    existing_similar_orgs = OrgModel.find_similar_org_by_name(
        name, org_id=org_id, branch_name=branch_name)
    if existing_similar_orgs:
        validator_response.add_error(Error.DATA_CONFLICT)
        if is_fatal:
            raise BusinessException(Error.DATA_CONFLICT, None)
    return validator_response
Exemple #8
0
    def create_org(org_info: dict, user_id):
        """Create a new organization."""
        current_app.logger.debug('<create_org ')
        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))
        org.save()
        current_app.logger.info(f'<created_org org_id:{org.id}')
        # create the membership record for this user
        membership = MembershipModel(org_id=org.id, user_id=user_id, membership_type_code='OWNER',
                                     membership_type_status=Status.ACTIVE.value)
        membership.save()

        return Org(org)
Exemple #9
0
    def find_by_org_name(org_name, branch_name=None):
        """Find and return an existing organization with the provided name."""
        if org_name is None:
            return None

        org_model = OrgModel.find_similar_org_by_name(org_name,
                                                      org_id=None,
                                                      branch_name=branch_name)
        if not org_model:
            return None

        orgs = {'orgs': []}

        for org in org_model:
            orgs['orgs'].append(Org(org).as_dict())

        return orgs
Exemple #10
0
    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)
Exemple #11
0
 def search_orgs(**kwargs):
     """Search for orgs based on input parameters."""
     orgs = {'orgs': []}
     if kwargs.get('business_identifier', None):
         affiliation: AffiliationModel = AffiliationModel. \
             find_affiliations_by_business_identifier(kwargs.get('business_identifier'))
         if affiliation:
             orgs['orgs'].append(
                 Org(OrgModel.find_by_org_id(affiliation.org_id)).as_dict())
     elif kwargs.get('org_type', None):
         org_models = OrgModel.find_by_org_access_type(
             kwargs.get('org_type'))
         for org in org_models:
             orgs['orgs'].append(Org(org).as_dict())
     elif kwargs.get('name', None):
         org_model = OrgModel.find_similar_org_by_name(kwargs.get('name'))
         if org_model is not None:
             orgs['orgs'].append(Org(org_model).as_dict())
     return orgs
Exemple #12
0
    def update_org(self, org_info, bearer_token: str = None):
        """Update the passed organization with the new info."""
        current_app.logger.debug('<update_org ')

        if self._model.type_code != OrgType.PREMIUM.value:
            existing_similar__org = OrgModel.find_similar_org_by_name(
                org_info['name'], self._model.id)
            if existing_similar__org is not None:
                raise BusinessException(Error.DATA_CONFLICT, None)
        is_premium = False

        bcol_credential = org_info.pop('bcOnlineCredential', None)
        mailing_address = org_info.pop('mailingAddress', None)
        selected_payment_method = org_info.pop('paymentMethod', None)
        # If the account is created using BCOL credential, verify its valid bc online account
        # If it's a valid account disable the current one and add a new one
        if bcol_credential:
            bcol_response = Org.get_bcol_details(bcol_credential, org_info,
                                                 bearer_token,
                                                 self._model.id).json()
            Org._map_response_to_org(bcol_response, org_info)
            is_premium = True

        # Update mailing address Or create new one
        if mailing_address:
            contacts = self._model.contacts
            if len(contacts) > 0:
                contact = self._model.contacts[0].contact
                contact.update_from_dict(**camelback2snake(mailing_address))
                contact.save()
            else:
                Org.add_contact_to_org(mailing_address, self._model)

        if self._model.type_code != OrgType.PREMIUM.value:
            self._model.update_org_from_dict(camelback2snake(org_info))

        org_type: OrgType = OrgType.PREMIUM if is_premium else OrgType.BASIC
        payment_type = Org._validate_and_get_payment_method(
            selected_payment_method, org_type)
        Org._create_payment_settings(self._model, payment_type, False)
        current_app.logger.debug('>update_org ')
        return self
Exemple #13
0
 def raise_error_if_duplicate_name(name, branch_name=None):
     """Raise error if there is duplicate org name already."""
     existing_similar__org = OrgModel.find_similar_org_by_name(
         name, branch_name=branch_name)
     if existing_similar__org is not None:
         raise BusinessException(Error.DATA_CONFLICT, None)