示例#1
0
def SearchAccounts():
    req = SearchItemsReq(request.json)
    if (req.account_id):
        accounts = models.Accounts.query.filter(
            models.Accounts.account_id == req.account_id).all()
        info_accounts = []
        for account in ConvertModelListToDictList(accounts):
            user_info = {}
            if (account['role']['role_id'] == 3):  # customer
                search_customer_req = SearchCustomersReq(
                    {'account_id': account['account_id']})
                user_info = CustomerRep.SearchCustomers(search_customer_req)

            if (account['role']['role_id'] == 1
                    or account['role']['role_id'] == 2):  # admin, manager
                search_employee_req = SearchEmployeesReq(
                    {'account_id': account['account_id']})
                user_info = EmployeeRep.SearchEmployees(search_employee_req)

            account_info = user_info[0] if user_info else {'account': account}
            account_info['account_id'] = account['account_id']
            account_info['account_name'] = account['account_name']
            account_info['role'] = account['role']
            account_info['note'] = account['note']
            account_info['delete_at'] = account['delete_at']
            info_accounts.append(account_info)
        return jsonify((info_accounts))
    all_accounts = models.Accounts.query
    if req.account_name != None:
        all_accounts = all_accounts.filter(
            models.Accounts.account_name.contains(req.account_name))
    if req.role_id != None:
        all_accounts = all_accounts.filter(
            models.Accounts.role_id == (req.role_id))
    all_accounts = all_accounts.filter(models.Accounts.delete_at == None)
    accounts = ConvertModelListToDictList(all_accounts.all())
    info_accounts = []
    for account in accounts:
        user_info = {}
        if (account['role']['role_id'] == 3):  # customer
            search_customer_req = SearchCustomersReq(
                {'account_id': account['account_id']})
            user_info = CustomerRep.SearchCustomers(search_customer_req)

        if (account['role']['role_id'] == 1
                or account['role']['role_id'] == 2):  # admin, manager
            search_employee_req = SearchEmployeesReq(
                {'account_id': account['account_id']})
            user_info = EmployeeRep.SearchEmployees(search_employee_req)

        account_info = user_info[0] if user_info else {'account': account}
        account_info['account_id'] = account['account_id']
        account_info['account_name'] = account['account_name']
        account_info['role'] = account['role']
        account_info['note'] = account['note']
        account_info['delete_at'] = account['delete_at']
        info_accounts.append(account_info)
    return jsonify(info_accounts)
示例#2
0
def AuthenticateUser(acc: LoginReq):
    try:
        account = AccountRep.Authenticate(acc)
        if (account['role']['role_id'] == 3):  # customer
            search_customer_req = SearchCustomersReq(
                {'account_id': account['account_id']})
            user = CustomerRep.SearchCustomers(search_customer_req)

        if (account['role']['role_id'] == 1
                or account['role']['role_id'] == 2):  # admin, manager
            search_employee_req = SearchEmployeesReq(
                {'account_id': account['account_id']})
            user = EmployeeRep.SearchEmployees(search_employee_req)

        secect_key = app.config['SECRET_KEY']
        payload = {
            'account_id': account['account_id'],
            'iat': datetime.utcnow(),
            'exp': datetime.utcnow() + timedelta(minutes=30)
        }
        access_token = jwt.encode(payload, secect_key)
        result = {
            'access_token': access_token,
            'account': account,
            'user_info': user[0] if len(user) > 0 else None
        }
        return result
    except ErrorRsp as e:
        raise e
示例#3
0
    def _verify():
        auth_headers = request.headers.get('Authorization', '').split()

        invalid_msg = {
            'message': 'Token không hợp lệ.',
            'authenticated': False
        }
        expired_msg = {
            'message': 'Token hết hạn sử dụng.',
            'authenticated': False
        }

        if len(auth_headers) != 2:
            return jsonify(invalid_msg), 401
        try:
            token = auth_headers[1]
            data = jwt.decode(token, app.config['SECRET_KEY'])
            search_accounts_req = SearchAccountsReq(
                {'account_id': data['account_id']})
            account = AccountSvc.SearchAccounts(search_accounts_req)[0]

            search_employees_req = SearchEmployeesReq(
                {'account_id': account['account_id']})
            employee = EmployeeRep.SearchEmployees(
                search_employees_req)[0] if len(
                    EmployeeRep.SearchEmployees(
                        search_employees_req)) > 0 else None

            search_customers_req = SearchCustomersReq(
                {'account_id': account['account_id']})
            customer = CustomerRep.SearchCustomers(
                search_customers_req)[0] if len(
                    CustomerRep.SearchCustomers(
                        search_customers_req)) > 0 else None

            auth_info = {
                'account': account,
                'employee': employee,
                'customer': customer
            }
            return f(auth_info)
        except jwt.ExpiredSignatureError:
            return jsonify(
                expired_msg), 401  # 401 is Unauthorized HTTP status code
        except (jwt.InvalidTokenError) as e:
            return jsonify(invalid_msg), 401
def GetEmployeesByPage(req):
    has_next, has_prev, employees = EmployeeRep.GetEmployeesbyPage(req)
    result = {
        "has_next": has_next,
        "has_prev": has_prev,
        "employees": employees
    }
    return result
def SearchAccounts(acc_info):
    accounts = AccountRep.SearchAccounts(acc_info)
    info_accounts = []
    for account in accounts:
        user_info = {}
        if (account['role']['role_id'] == 3):  # customer
            search_customer_req = SearchCustomersReq(
                {'account_id': account['account_id']})
            user_info = CustomerRep.SearchCustomers(search_customer_req)

        if (account['role']['role_id'] == 1
                or account['role']['role_id'] == 2):  # admin, manager
            search_employee_req = SearchEmployeesReq(
                {'account_id': account['account_id']})
            user_info = EmployeeRep.SearchEmployees(search_employee_req)

        account_info = user_info[0] if user_info else {'account': account}
        info_accounts.append(account_info)

    return info_accounts
def SearchEmployee(req):
    search_employee = EmployeeRep.SearchEmployees(req)
    return search_employee
def DeleteEmployee(req):
    delete_employee = EmployeeRep.DeleteEmployee(req)
    return delete_employee
def UpdateEmployee(req):
    update_employee = EmployeeRep.UpdateEmployee(req)
    return update_employee
def CreateEmployee(req):
    create_employee = EmployeeRep.CreateEmployee(req)
    return create_employee