Ejemplo n.º 1
0
def handler(event, context):
    """
        Update user profile with new data. Makes query for existing data and combines it with new data.
    """
    logging.info(json.dumps(event))
    body = event.get("body")
    if not body:
        return _create_http_response(400,
                                     {"failure_reason": "No body in message!"})
    parsed = _json_string_to_dict(body)

    if not parsed.get("user-id"):
        return _create_http_response(400,
                                     {"failure_reason": "No user ID in body"})

    if parsed.get("failure_reason"):
        return _create_http_response(
            400, {"failure_reason": parsed["failure_reason"]})

    old_data = _get_old_data_items(query_db(parsed["user-id"]))
    if old_data:
        combined_data = _combine_user_data(parsed, old_data)
        write_to_db(combined_data)
        return _create_http_response(200, combined_data)
    else:
        write_to_db(parsed)
        return _create_http_response(200, parsed)
Ejemplo n.º 2
0
def handler(event, context):
    """
        Receive chat messages and write them to DB.
    """
    body = _parse_request_body(event)

    if body.get("failure_reason"):
        return _send_http_response(400, {"reason": body["failure_reason"]})

    old_ride = _db_search_existing_ride(body["ride-id"])
    chat_history_old = _get_messages_from_result(old_ride)
    chat_history = _join_new_message_to_chat_history(chat_history_old, body)
    existing_chatters = _get_existing_chatters(old_ride)
    body["messages"] = json.dumps(chat_history)
    body["latest-message"] = datetime.utcnow().isoformat()
    body["people-in-chat"] = _update_new_chatter(existing_chatters,
                                                 body["first-name"],
                                                 body["sender-id"])
    logger.info(body)
    write_to_db(body)

    # Remove old ones from HTTP-response
    http_response = _remove_already_fetched_records(
        chat_history, get_short_user_id(body["sender-id"]))
    # For HTTP-response, only leave the new ones
    body["messages"] = json.dumps(http_response)
    return _send_http_response(200, body)
Ejemplo n.º 3
0
def handler(event, context):
    """"
        Write data from Cognito to Dynamo.
        Validate user data and return it to Dynamo.
    """
    logging.info(json.dumps(event))
    wanted_data = _parse_event(event)
    logger.info(json.dumps(wanted_data))
    write_to_db(wanted_data)

    return event
Ejemplo n.º 4
0
def handler(event, context):
    """
        Parse ride data from event and write it to database.
    """
    logging.info(json.dumps(event))
    body = _parse_event(event)
    logger.info(json.dumps(body))
    write_to_db(body, "requestedRidesTable")

    response = create_http_response(200, f"Uploaded new requested ride to database with requested ride id {event_body['requested-ride-id']}")

    logger.info(response)

    return response
Ejemplo n.º 5
0
def handler(event, context):
    """
        Parse customer reviews and write it to database.
    """
    logging.info(json.dumps(event))
    required_values = ["review-text", "star-review", "user-id"]
    response = validate_body_and_data(event, required_values)
    if validate_results(response):
        event_body = get_body_from_event(event)
        event_body["review-id"] = create_random_short_id()
        write_to_db("reviewTable", event_body)
        response = create_http_response(
            200,
            f"New review with review id event_body['review-id'] has been added to the database."
        )

    logger.info(json.dumps(response))

    return response