def handle_appointment_agreement_response():
    if request.is_json:
        request_dict = request.get_json()
        appointee_nickname = request_dict.get('appointee_nickname')
        store_name = request_dict.get('store_name')
        appointment_agreement_response = request_dict.get(
            'appointment_agreement_response')
        if appointment_agreement_response == 1:
            appointment_agreement_response = AppointmentStatus.DECLINED
        if appointment_agreement_response == 2:
            appointment_agreement_response = AppointmentStatus.APPROVED
        response = StoreOwnerOrManagerRole.update_agreement_participants(
            appointee_nickname, store_name, appointment_agreement_response)
        if response:
            # check is the status of the agreement is approved already
            if response["response"]:
                status = StoreOwnerOrManagerRole.get_appointment_status(
                    appointee_nickname, store_name)
                if status == AppointmentStatus.APPROVED:
                    response = StoreOwnerOrManagerRole.appoint_additional_owner(
                        appointee_nickname, store_name)
                    add_subscriber_to_store(store_name, appointee_nickname)
                    msg = f"New owner {appointee_nickname} appointed at store {store_name}!"
                    notify_all(
                        store_name, {
                            'username': appointee_nickname,
                            'messages': msg,
                            'store': store_name
                        }, "agreement")
            return jsonify(msg=response["msg"])
    return jsonify(msg="Oops, communication error")
def edit_product():
    if request.is_json:
        errors = []
        request_dict = request.get_json()
        store_name = request_dict.get('store_name')
        product_name = request_dict.get('product_name')
        new_product_name = request_dict.get('new_product_name')
        amount = request_dict.get('amount')
        price = request_dict.get('price')
        category = request_dict.get('category')
        purchase_type = request_dict.get('purchase_type')

        # if new_product_name is not None and new_product_name == "":
        #     return jsonify(msg="Oops, product's name can't be an emtpy string.")
        # if amount is not None p amount < 0:
        #     return jsonify(msg="Oops, product's amount can't be smaller than 0.")
        # if price < 0:
        #     return jsonify(msg="Oops, product's amount can't be smaller than 0.")

        if amount is not None:
            if not StoreOwnerOrManagerRole.edit_product(
                    store_name, product_name, "amount", amount)["response"]:
                errors.append("Product Amount")
        if price is not None:
            if not StoreOwnerOrManagerRole.edit_product(
                    store_name, product_name, "price", price)["response"]:
                errors.append("Product Price")
        if category is not None:
            if not StoreOwnerOrManagerRole.edit_product(
                    store_name, product_name, "category",
                    category)["response"]:
                errors.append("Category")
        if purchase_type is not None:
            if not StoreOwnerOrManagerRole.edit_product(
                    store_name, product_name, "purchase_type",
                    purchase_type)["response"]:
                errors.append("Purchase Type")
        if new_product_name is not None:
            if not StoreOwnerOrManagerRole.edit_product(
                    store_name, product_name, "name",
                    new_product_name)["response"]:
                errors.append("Product Name")

        error_str = ""
        for error in errors:
            error_str += ", " + error

        if len(errors) == 0:
            return jsonify(msg="Congrats! Product was edited successfully!")
        return jsonify(msg="Oops, issue with product edit, couldn't update: " +
                       error_str)
def get_owners_appointees():
    if request.is_json:
        request_dict = request.get_json()
        store_name = request_dict.get('store_name')  # str
        response = StoreOwnerOrManagerRole.get_appointees(store_name, "OWNERS")
        return jsonify(data=response)
    return jsonify(data=[])
def view_store_purchases_history():
    if request.is_json:
        request_dict = request.get_json()
        store_name = request_dict.get('store_name')  # str
        response = StoreOwnerOrManagerRole.display_store_purchases(store_name)
        return jsonify(msg=response["msg"], data=response["response"])
    return jsonify(msg="Oops, communication error.", data=[])
def delete_policy():
    if request.is_json:
        request_dict = request.get_json()
        policy_type = request_dict.get('policy_type')
        store_name = request_dict.get('store_name')
        policy_name = request_dict.get('policy_name')

        if policy_type == 'discount':
            response = StoreOwnerOrManagerRole.delete_policy(
                store_name, policy_name)
        else:
            response = StoreOwnerOrManagerRole.define_purchase_policy(
                store_name, policy_name)
            # response = {'response': False, 'msg': 'need to implement delete_purchase_policy'}
        return jsonify(msg=response['msg'], data=response['response'])

    return jsonify(msg="Oops, communication error.")
def get_manager_permissions():
    if request.is_json:
        request_dict = request.get_json()
        store_name = request_dict.get('store_name')  # str
        response = StoreOwnerOrManagerRole.get_manager_permissions(store_name)
        numlist = [enum.value for enum in response]
        return jsonify(data=numlist)
    return jsonify(data=[])
def get_policies():
    if request.is_json:
        request_dict = request.get_json()
        store_name = request_dict.get('store_name')  # str
        policy_type = request_dict.get('policy_type')  # str
        response = StoreOwnerOrManagerRole.get_policies(
            policy_type, store_name)
        return jsonify(msg=response["msg"], data=response["response"])

    return jsonify(msg="Oops, communication error.")
def remove_product():
    if request.is_json:
        request_dict = request.get_json()
        store_name = request_dict.get('store_name')
        products_name = request_dict.get('product_name')
        response = StoreOwnerOrManagerRole.remove_products(
            store_name, [products_name])
        if response:
            return jsonify(msg="Congrats! Product was removed!")
    return jsonify(msg="Oops, product wasn't removed")
def add_product():
    if request.is_json:
        request_dict = request.get_json()
        store_name = request_dict.get('store_name')
        products_details = request_dict.get('products_details')
        response = StoreOwnerOrManagerRole.add_products(
            store_name, products_details)
        if response["response"]:
            return jsonify(msg="Congrats! Product was added!")
        return jsonify(msg="Oops, product wasn't added")
Example #10
0
 def setUp(self) -> None:
     if not ("testing" in rel_path):
         raise ReferenceError(
             "The Data Base is not the testing data base.\n"
             "\t\t\t\tPlease go to src.main.DataAccessLayer.ConnectionProxy.RealDb.rel_path\n"
             "\t\t\t\t and change rel_path to test_rel_path.\n"
             "\t\t\t\tThanks :D")
     self.__store_owner_or_manager_role: StoreOwnerOrManagerRole = StoreOwnerOrManagerRole(
     )
     self.__trade_control_mock: TradeControl = TradeControl.get_instance()
     self.store_mock = Store("store1")
def appoint_store_manager():
    if request.is_json:
        request_dict = request.get_json()
        appointee_nickname = request_dict.get('appointee_nickname')  # str
        store_name = request_dict.get('store_name')  # str
        permissions = request_dict.get('permissions')  # list of tuples
        response = StoreOwnerOrManagerRole.appoint_store_manager(
            appointee_nickname, store_name, permissions)
        if response:
            return jsonify(msg=response["msg"], data=response["response"])
    return jsonify(msg="Oops, communication error")
def get_purchase_operator():
    if request.is_json:
        request_dict = request.get_json()
        store_name = request_dict.get('store_name')  # str
        response = StoreOwnerOrManagerRole.get_purchase_operator(store_name)
        if response == "xor" or response == "and" or response == "or":
            return jsonify(
                msg="Retrieved purchases policies operator successfully!",
                data=response)

    return jsonify(msg="Oops, communication error.")
def remove_manager():
    if request.is_json:
        request_dict = request.get_json()
        appointee_nickname = request_dict.get('nickname')  # str
        store_name = request_dict.get('store_name')  # str
        response = StoreOwnerOrManagerRole.remove_manager(
            store_name, appointee_nickname)
        if response:
            return jsonify(msg="Removed manager " + appointee_nickname +
                           " successfully!")
    return jsonify(msg="Oops, communication error")
def remove_owner():
    if request.is_json:
        request_dict = request.get_json()
        appointee_nickname = request_dict.get('nickname')  # str
        store_name = request_dict.get('store_name')  # str
        response = StoreOwnerOrManagerRole.remove_owner(
            appointee_nickname, store_name)
        if response:
            handle_remove_owner_msg(appointee_nickname, store_name)
            return jsonify(msg=response['msg'], data=response['response'])
    return jsonify(msg="Oops, communication error--")
def add_and_update_dicount_policy():
    if request.is_json:
        request_dict = request.get_json()
        action_type = request_dict.get('action_type')
        store_name = request_dict.get('store_name')
        policy_name = request_dict.get('policy_name')
        product_name = request_dict.get('product_name')
        date = parse(request_dict.get('date'))
        percentage = request_dict.get('percentage')
        product = request_dict.get('product')
        min_amount = request_dict.get('min_amount')
        min_purchase_price = request_dict.get('min_purchase_price')

        discount_details = {'name': policy_name, 'product': product_name}
        discount_precondition = {
            'product': product,
            'min_amount': min_amount,
            'min_basket_price': min_purchase_price
        }

        if product is None and min_amount is None and min_purchase_price is None:
            discount_precondition = None
        if action_type == 'add':
            response = StoreOwnerOrManagerRole.define_discount_policy(
                store_name, percentage, date, discount_details,
                discount_precondition)
        else:
            new_policy_name = request_dict.get('new_policy_name')
            discount_details = {
                'name': new_policy_name,
                'product': product_name
            }
            response = StoreOwnerOrManagerRole.update_discount_policy(
                store_name, policy_name, percentage, date, discount_details,
                discount_precondition)
        return jsonify(msg=response['msg'], data=response['response'])

    return jsonify(msg="Oops, communication error.")
def add_and_update_purchase_policy():
    if request.is_json:
        request_dict = request.get_json()
        action_type = request_dict.get('action_type')
        store_name = request_dict.get('store_name')
        policy_name = request_dict.get('policy_name')
        products = request_dict.get('products')
        min_amount = request_dict.get('min_amount')
        max_amount = request_dict.get('max_amount')
        bundle = request_dict.get('bundle')
        string_dates = request_dict.get('dates')
        dates = []
        # convert string to dates
        if string_dates is None:
            dates = None
        else:
            for date in string_dates:
                dates += [parse(date)]

        details = {
            "name": policy_name,
            "products": products,
            "min_amount": min_amount,
            "max_amount": max_amount,
            "dates": dates,
            "bundle": bundle
        }
        if action_type == 'add':
            response = StoreOwnerOrManagerRole.define_purchase_policy(
                store_name, details)
        else:
            response = StoreOwnerOrManagerRole.update_purchase_policy(
                store_name, details)
        return jsonify(msg=response['msg'], data=response['response'])

    return jsonify(msg="Oops, communication error.")
def edit_manager_permissions():
    if request.is_json:
        request_dict = request.get_json()
        store_name = request_dict.get('store_name')  # str
        appointee_nickname = request_dict.get('appointee_nickname')  # str
        permissions = request_dict.get('permissions')  # str
        response = StoreOwnerOrManagerRole.edit_manager_permissions(
            store_name, appointee_nickname, permissions)
        if response:
            return jsonify(data=response,
                           msg="Permissions of manager " + appointee_nickname +
                           " were updated successfully!")
        else:
            return jsonify(data=False, msg="Oops, update permissions failed.")
    return jsonify(data=False, msg="Oops, communication error.")
def add_composite_dicount_policy():
    if request.is_json:
        request_dict = request.get_json()
        store_name = request_dict.get('store_name')
        policy1 = request_dict.get('policy1')
        policy2 = request_dict.get('policy2')
        date = parse(request_dict.get('date'))
        percentage = request_dict.get('percentage')
        new_policy_name = request_dict.get('new_policy_name')
        operator = request_dict.get('operator')

        response = StoreOwnerOrManagerRole.define_composite_policy(
            store_name, policy1, policy2, operator, percentage,
            new_policy_name, date)

        return jsonify(msg=response['msg'], data=response['response'])

    return jsonify(msg="Oops, communication error.")
def appoint_store_owner():
    if request.is_json:
        request_dict = request.get_json()
        appointee_nickname = request_dict.get('appointee_nickname')  # str
        store_name = request_dict.get('store_name')  # str
        response = StoreOwnerOrManagerRole.appoint_additional_owner(
            appointee_nickname, store_name)
        if response:
            if response["msg"] == "The request is pending approval":
                # ----------------appointment agreement----------------------
                msg = f"New owner appointment at store {store_name} - action required!"
                notify_all(
                    store_name, {
                        'username': appointee_nickname,
                        'messages': msg,
                        'store': store_name
                    }, "agreement")
            elif response["response"]:
                if add_subscriber_to_store(store_name, appointee_nickname):
                    # TODO - we need to take into consideration LOGIN/LOGOUT
                    print(_users)
                    join_room(store_name, _users[appointee_nickname])
            return jsonify(msg=response["msg"], data=response["response"])
    return jsonify(msg="Oops, communication error")
def get_owned_stores():
    response = StoreOwnerOrManagerRole.get_owned_stores()
    return jsonify(data=response)