コード例 #1
0
 def create_default_product_subscriptions(org: OrgModel,
                                          bcol_profile_flags: List[str],
                                          is_new_transaction: bool = True):
     """Create default product subscriptions for the account."""
     internal_product_codes = ProductCodeModel.find_by_type_code(
         type_code=ProductTypeCode.INTERNAL.value)
     for product_code in internal_product_codes:
         # Add PPR only if the account is premium.
         if product_code.code == ProductCode.PPR.value:
             if org.type_code == OrgType.PREMIUM.value:
                 ProductSubscriptionModel(
                     org_id=org.id,
                     product_code=product_code.code,
                     status_code=product_code.default_subscription_status
                 ).flush()
         else:
             ProductSubscriptionModel(org_id=org.id,
                                      product_code=product_code.code,
                                      status_code=product_code.
                                      default_subscription_status).flush()
     # Now add or update the product subscription based on bcol profiles.
     Product.create_subscription_from_bcol_profile(org.id,
                                                   bcol_profile_flags)
     if is_new_transaction:  # Commit the transaction if it's a new transaction
         db.session.commit()
コード例 #2
0
 def create_default_product_subscriptions(org: OrgModel, is_new_transaction: bool = True):
     """Create default product subscriptions for the account."""
     internal_product_codes = ProductCodeModel.find_by_type_code(type_code=ProductTypeCode.INTERNAL.value)
     for product_code in internal_product_codes:
         # Add PPR only if the account is premium.
         if product_code.code == ProductCode.PPR.value:
             if org.type_code == OrgType.PREMIUM.value:
                 ProductSubscriptionModel(org_id=org.id, product_code=product_code.code).flush()
         else:
             ProductSubscriptionModel(org_id=org.id, product_code=product_code.code).flush()
     if is_new_transaction:  # Commit the transaction if it's a new transaction
         db.session.commit()
コード例 #3
0
    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
コード例 #4
0
ファイル: org.py プロジェクト: severinbeauvais/sbc-auth
    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
コード例 #5
0
ファイル: products.py プロジェクト: stevenc987/sbc-auth
 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()
コード例 #6
0
ファイル: products.py プロジェクト: stevenc987/sbc-auth
    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)
コード例 #7
0
    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