Ejemplo n.º 1
0
def create_user():
    userservices = UserServices()
    if request.method == 'POST':
        body = request.get_json()

        validate_json_format(body)

        try:
            data = {
                'full_name': body['full_name'],
                'cpf': str(body['cpf']),
                'email': body['email'],
                'password': str(body['password'])
            }

        except KeyError as error:
            return http_response(
                422, 'Missing {} key on JSON body'.format(str(error)), None)

        response = userservices.create_new_user(data)
        return http_response(response['http_code'], response['message'],
                             response['payload'])

    if request.method == 'GET':
        response = userservices.get_all_users()
        return http_response(response['http_code'], response['message'],
                             response['payload'])
Ejemplo n.º 2
0
    def decorated(*args, **kwargs):
        token = request.headers.get('authorization').split(' ')[1]

        if not token:
            return http_response(401, 'JWT token is missing', None)

        data = jwt.decode(token, current_app.config['SECRET_KEY'])
        user_check = mongo.find_one({'cpf': data['user']}, 'user')
        if not user_check:
            return http_response(401, 'JWT token is invalid!', None)

        return func(*args, **kwargs)
Ejemplo n.º 3
0
def login():
    userservices = UserServices()
    body = request.get_json()

    if not isinstance(body, dict):
        return http_response(400, 'Invalid request - Expecting a JSON body',
                             None)
    else:
        try:
            data = {'cpf': body['cpf'], 'password': body['password']}
        except KeyError as error:
            return http_response(
                422, 'Missing {} key on JSON body'.format(str(error)), None)

        response = userservices.authenticate_user(data)
        return http_response(response['http_code'], response['message'],
                             response['payload'])
Ejemplo n.º 4
0
def orders():
    orderservices = OrderServices()
    if request.method == 'GET':
        body = request.get_json()

        validate_json_format(body)

        try:
            data = {
                'seller_cpf': str(body['seller_cpf']),
                'start_date': body['start_date'],
                'end_date': body['end_date']
            }
        except KeyError as error:
            return http_response(
                422, 'Missing {} key on JSON body'.format(str(error)), None)

        response = orderservices.get_orders(data)

        return http_response(response['http_code'], response['message'],
                             response['payload'])

    if request.method == 'DELETE':
        body = request.get_json()

        validate_json_format(body)

        try:
            data = {
                'seller_cpf': str(body['cpf']),
                'order_code': str(body['order_code'])
            }
        except KeyError as error:
            return http_response(
                422, 'Missing {} key on JSON body'.format(str(error)), None)

        response = orderservices.delete_order(data)
        return http_response(response['http_code'], response['message'],
                             response['payload'])

    if request.method == 'POST':
        body = request.get_json()

        validate_json_format(body)

        try:
            data = {
                'order_code': str(body['order_code']),
                'order_value': body['order_value'],
                'order_date': body['order_date'],
                'seller_cpf': str(body['seller_cpf'])
            }
        except KeyError as error:
            return http_response(
                422, 'Missing {} key on JSON body'.format(str(error)), None)

        response = orderservices.create_new_order(data)

        return http_response(response['http_code'], response['message'],
                             response['payload'])

    if request.method == 'PUT':
        body = request.get_json()

        validate_json_format(body)

        try:
            data = {
                'order_code': str(body['order_code']),
                'seller_cpf': str(body['seller_cpf']),
                'updated_fields': body['updated_fields']
            }
        except KeyError as error:
            return http_response(
                422, 'Missing {} key on JSON body'.format(str(error)), None)

        response = orderservices.update_order(data)

        return http_response(response['http_code'], response['message'],
                             response['payload'])
Ejemplo n.º 5
0
def get_cashback():
    response = cashback_request()

    return http_response(response['http_code'], response['message'],
                         response['payload'])