Skip to content

Commit

Permalink
fix: Enable callback timeout for calls only, not API, nor SMS
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlesne committed Dec 12, 2024
1 parent 24a2cbc commit 5c0c0e1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ async def call_get(call_id_or_phone_number: str) -> CallGetModel:
# Second, try to get by phone number
phone_number = PhoneNumber(call_id_or_phone_number)
call = await _db.call_search_one(
callback_timeout=False,
phone_number=phone_number,
scheduler=scheduler,
)
Expand Down Expand Up @@ -498,6 +499,7 @@ async def sms_event(
async with get_scheduler() as scheduler:
# Get call
call = await _db.call_search_one(
callback_timeout=False,
phone_number=phone_number,
scheduler=scheduler,
)
Expand Down Expand Up @@ -987,6 +989,7 @@ async def twilio_sms_post(
async with get_scheduler() as scheduler:
# Get call
call = await _db.call_search_one(
callback_timeout=False,
phone_number=From,
scheduler=scheduler,
)
Expand Down
8 changes: 7 additions & 1 deletion app/persistence/cosmos_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ async def call_search_one(
self,
phone_number: str,
scheduler: Scheduler,
callback_timeout: bool = True,
) -> CallStateModel | None:
logger.debug("Loading last call for %s", phone_number)

Expand All @@ -255,14 +256,19 @@ async def call_search_one(
except ValidationError:
logger.debug("Parsing error", exc_info=True)

# Filter by timeout if needed
extra_where = ""
if callback_timeout:
extra_where = f"AND c.created_at >= DATETIMEADD('hh', -{await callback_timeout_hour(scheduler)}, GETCURRENTDATETIME())"

# Try live
call = None
try:
with suppress(StopAsyncIteration):
async with self._use_client() as db:
items = db.query_items(
max_item_count=1,
query=f"SELECT * FROM c WHERE (STRINGEQUALS(c.initiate.phone_number, @phone_number, true) OR STRINGEQUALS(c.claim.policyholder_phone, @phone_number, true)) AND c.created_at >= DATETIMEADD('hh', -{await callback_timeout_hour(scheduler)}, GETCURRENTDATETIME()) ORDER BY c.created_at DESC",
query=f"SELECT * FROM c WHERE (STRINGEQUALS(c.initiate.phone_number, @phone_number, true) OR STRINGEQUALS(c.claim.policyholder_phone, @phone_number, true)) {extra_where} ORDER BY c.created_at DESC",
parameters=[
{
"name": "@phone_number",
Expand Down
1 change: 1 addition & 0 deletions app/persistence/istore.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ async def call_search_one(
self,
phone_number: str,
scheduler: Scheduler,
callback_timeout: bool = True,
) -> CallStateModel | None:
pass

Expand Down

0 comments on commit 5c0c0e1

Please sign in to comment.