45 lines
1.9 KiB
Python
45 lines
1.9 KiB
Python
"""Status queries: «استعلامهای در انتظار؟»، «کدامها منقضی میشوند؟» …"""
|
|
import logging
|
|
from datetime import date, timedelta
|
|
|
|
from app.api.client import ApiError
|
|
from app.texts import ERR_GENERIC, status_report, esc
|
|
|
|
logger = logging.getLogger("bot.queries")
|
|
|
|
EXPAND = "supplier,line_items"
|
|
|
|
|
|
async def handle_query(ctx, chat_id, user, patch: dict) -> None:
|
|
query = patch.get("query") or {}
|
|
kind = query.get("kind") or "pending"
|
|
supplier_name = query.get("supplier_name")
|
|
q = supplier_name or query.get("project_name")
|
|
|
|
try:
|
|
if kind == "expiring":
|
|
quotes, _ = await ctx.api.list_quotes(status="offered", expand=EXPAND,
|
|
q=q, sort="-quote_date", page_size=100)
|
|
deadline = date.today() + timedelta(days=query.get("days") or 7)
|
|
soon = []
|
|
for quote in quotes:
|
|
expiry = (quote.get("expiry_date") or "")[:10]
|
|
if expiry and expiry <= deadline.isoformat():
|
|
soon.append(quote)
|
|
soon.sort(key=lambda x: x.get("expiry_date") or "9999")
|
|
await ctx.send(chat_id, status_report("expiring", soon[:10]))
|
|
return
|
|
|
|
if kind in ("accepted", "arrived", "pending", "offered"):
|
|
status = "offered" if kind == "pending" else kind
|
|
quotes, _ = await ctx.api.list_quotes(status=status, expand=EXPAND, q=q,
|
|
sort="-quote_date", page_size=10)
|
|
await ctx.send(chat_id, status_report("pending" if kind == "offered" else kind, quotes))
|
|
return
|
|
|
|
# recent / anything else
|
|
quotes, meta = await ctx.api.list_quotes(expand=EXPAND, q=q, sort="-quote_date", page_size=5)
|
|
await ctx.send(chat_id, status_report("recent", quotes))
|
|
except ApiError as exc:
|
|
await ctx.send(chat_id, ERR_GENERIC.format(detail=esc(exc.message)))
|