async def index(request: Request, db: DataBase = Depends(get_database)):
    async with db.pool.acquire() as conn:
        try:
            resp = await request.json()
            birthday_data = resp.get("data", {})
            logging.warning(f"response received {resp}")
            birthdays_today = await friends.get_friends_by_date(
                conn, birthday_data)
            client_map = group_friends_by_client_id(birthdays_today)
            phone_number_to_msg_map = dict()
            for client_id, friend_names in client_map.items():
                client = await clients.find_client_by_id(conn, client_id)
                if client:
                    phone_number_to_msg_map[
                        client.
                        phone_number] = constants.BIRTHDAY_REMINDER_MESSAGE(
                            friend_names)

            for phone_number, message in phone_number_to_msg_map.items():
                twilio_helper.send_direct_message(message, phone_number)

            return utils.create_aliased_response(
                {"data": phone_number_to_msg_map}, status.HTTP_202_ACCEPTED)
        except Exception as e:
            logging.error(e)
            return utils.create_aliased_response(
                {"error": str(e)},
                status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            )
Пример #2
0
async def delete_friends(friend_id: str, db: DataBase = Depends(get_database)):
    async with db.pool.acquire() as conn:
        try:
            await friends.delete_friend(conn, friend_id)
            resp = {"data": friend_id}
            return utils.create_aliased_response(resp,
                                                 status.HTTP_202_ACCEPTED)

        except Exception as e:
            logging.error(e)
            return utils.create_aliased_response(
                {"error": constants.FAILURE_MESSAGE},
                status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            )
Пример #3
0
async def get_single_friend(friend_id: str,
                            client_id: str,
                            db: DataBase = Depends(get_database)):
    async with db.pool.acquire() as conn:
        try:
            friend_in_db = await friends.get_friend_by_id_and_client_id(
                conn, friend_id, client_id)
            resp = utils.create_aliased_response({"data": friend_in_db.dict()})
            return resp

        except Exception as e:
            logging.error(e)
            return utils.create_aliased_response(
                {"error": constants.FAILURE_MESSAGE},
                status_code=status.HTTP_404_NOT_FOUND,
            )
Пример #4
0
async def index(client_id: str, db: DataBase = Depends(get_database)):
    async with db.pool.acquire() as conn:
        try:
            client_friends = await friends.get_all_friends_by_client_id(
                conn, client_id)
            friend_payload = client_friends if client_friends else []
            resp_data = utils.model_list_to_data_dict(friend_payload)
            resp = utils.create_aliased_response(resp_data)
            return resp

        except Exception as e:
            logging.error(e)
            return utils.create_aliased_response(
                {"error": constants.FAILURE_MESSAGE},
                status_code=status.HTTP_404_NOT_FOUND,
            )
async def test_delete_friend_success(conn, friend_in_db):
    resp = test_client.delete(
        f"{API_PREFIX}/{friend_in_db.id}?token={config.WEBHOOK_SECRET_TOKEN}")
    expected_resp_data = utils.create_aliased_response(
        {"data": friend_in_db.id})
    expected_resp = json.loads(expected_resp_data.body)

    assert resp.json() == expected_resp
    assert resp.status_code == 202
async def test_index_success(conn, client_in_db, friend_in_db):
    resp = test_client.get(
        f"{API_PREFIX}/{client_in_db.id}?token={config.WEBHOOK_SECRET_TOKEN}")
    expected_resp_data = utils.model_list_to_data_dict([friend_in_db])
    expected_resp_byte = utils.create_aliased_response(expected_resp_data)
    expected_resp = json.loads(expected_resp_byte.body)

    assert resp.json() == expected_resp
    assert resp.status_code == 200
Пример #7
0
async def update_friend(client_id: str,
                        friend_id: str,
                        payload: Dict = Body(...),
                        db: DataBase = Depends(get_database)):
    async with db.pool.acquire() as conn:
        try:
            friend = payload.get("data")
            friend_to_update = processors.update_friends_table_request(
                friend, friend_id)
            update_friend = await friends.update_friend_by_id(
                conn, friend_to_update)
            return utils.create_aliased_response({"data": update_friend},
                                                 status.HTTP_200_OK)

        except Exception as e:
            logging.error(e)
            return utils.create_aliased_response(
                {"error": constants.FAILURE_MESSAGE},
                status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            )
Пример #8
0
async def create_friends(client_id: str,
                         payload: Dict = Body(...),
                         db: DataBase = Depends(get_database)):
    async with db.pool.acquire() as conn:
        try:
            created_friends = []
            friends_list = payload.get("data")
            for friend in friends_list:
                new_friend = processors.friends_table_request(
                    friend, client_id)
                friend_in_db = await friends.create_friend(conn, new_friend)
                created_friends.append(friend_in_db)

            resp = utils.model_list_to_data_dict(created_friends)
            return utils.create_aliased_response(resp, status.HTTP_201_CREATED)

        except Exception as e:
            logging.error(e)
            return utils.create_aliased_response(
                {"error": constants.FAILURE_MESSAGE},
                status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            )