Example #1
0
def execute_day_off_validation_rule(day_off):

    balance = execute_balance_calculation_rule(ws_id=day_off.workspace_id,
                                               user_id=day_off.user_id)

    ws_user = WorkspaceUser.find(user_id=day_off.user_id,
                                 ws_id=day_off.workspace_id)

    if not ws_user:
        raise Exception('User is not added to the specified workspace')

    ws_roles = WorkspaceUserRole.find_all(ws_id=day_off.workspace_id,
                                          user_id=day_off.user_id)

    rule_payload = DayOffValidationPayload(user_start_date=ws_user.start_date,
                                           leave=day_off,
                                           balance=balance,
                                           user_roles=list(
                                               map(lambda r: r.role,
                                                   ws_roles)))

    intellect = Intellect()
    intellect.policy.append_child(
        _get_rule_node(ws_id=day_off.workspace_id,
                       type=WorkspaceRuleTypes.DAY_OFF_VALIDATION))

    for leave_day in _leaves_to_leave_days([day_off]):
        intellect.learn_fact(leave_day)

    intellect.learn_fact(rule_payload)

    intellect.reason()

    return rule_payload
Example #2
0
def execute_balance_calculation_rule(ws_id, user_id):

    leaves = DayOff.query(). \
        filter(DayOff.user_id == user_id). \
        filter(DayOff.workspace_id == ws_id). \
        all()

    ws_user = WorkspaceUser.find(user_id=user_id, ws_id=ws_id)

    rule_payload = BalanceCalculationRulePayload(start_date=ws_user.start_date)

    node = _get_rule_node(ws_id=ws_id,
                          type=WorkspaceRuleTypes.BALANCE_CALCULATION)

    intellect = Intellect()
    intellect.policy.append_child(node)

    for leave_day in _leaves_to_leave_days(leaves):
        intellect.learn_fact(leave_day)

    intellect.learn_fact(rule_payload)

    intellect.reason()

    return rule_payload