feat: an approved order nobody paid expires in 24 h (front 9)
All checks were successful
Build and deploy / Validate source (push) Successful in 1m15s
Build and deploy / Integration suite on a real stack (push) Successful in 5m10s
Build and deploy / Secret scan and release gate (push) Successful in 11s
Build and deploy / Publish images (push) Successful in 1m29s

An approved quote with no payment expires on its own after 24 h, never
while a Pix or card attempt is still live: the worker marks
quotes.expired_at, frees its files and e-mails the customer that the
order expired and can be sent again on the Site. The backlog from before
this front expires silently.

Kanban: Cotações › Aguardando pagamento shows when each one expires and
the review gets an "Abrir conversa" WhatsApp button; Números gets
"Pedidos sem pagamento" (made, paid, expired, waiting, abandonment rate).

release_files moves to app/payments.py so the worker can use it; an
expired quote no longer counts as open there. New tests/unpaid_test.py
in the integration job.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-09 10:01:26 -03:00
parent 448a7fa8c3
commit 1a9611942d
11 changed files with 269 additions and 32 deletions

72
app/unpaid.py Normal file
View File

@@ -0,0 +1,72 @@
"""Orders made on the Site and never paid (Dropstar's Fase 2, front 9, 05/11/2026).
An approved quote is an order the customer did not pay yet: Kanban's
Cotações › "Aguardando pagamento" lists it, with how long ago it came and
when it expires, so the seller can charge it by WhatsApp. After
QUOTE_VALID_HOURS (24 h) without payment it expires on its own: its files
are freed for deletion and the customer gets an e-mail ("seu pedido expirou,
suba de novo quando quiser"). Never while a payment attempt is still live
(a Pix not yet expired, a card being processed), so a late payment always
finds its files. Números counts orders made, paid and expired, and the
abandonment rate.
"""
from datetime import datetime, timedelta, timezone
from . import mail, payments
from .payments import QUOTE_VALID_HOURS
# An intent in one of these no longer waits for money: approved (it became an
# order), rejected, cancelled, expired or refunded (app/payments.py).
FINAL_INTENT = ('approved', 'rejected', 'cancelled', 'expired', 'refunded')
def expire(c, origin):
"""Expire the approved, unpaid quotes past QUOTE_VALID_HOURS: set
quotes.expired_at, free their files (payments.release_files) and queue the
customer's e-mail (mail.quote_expired). Returns how many expired."""
rows = c.execute('''SELECT q.* FROM dtf_local.quotes q
WHERE q.approved IS NOT NULL AND q.discarded_at IS NULL AND q.expired_at IS NULL
AND q.approved_at <= now() - make_interval(hours => %s)
AND NOT EXISTS (SELECT 1 FROM dtf_local.orders o WHERE o.quote_id=q.id)
AND NOT EXISTS (
SELECT 1 FROM dtf_local.payment_intents i
WHERE i.quote_id=q.id
AND i.id = (SELECT i2.id FROM dtf_local.payment_intents i2
WHERE i2.quote_id=q.id ORDER BY i2.created_at DESC LIMIT 1)
AND i.status <> ALL(%s)
AND i.created_at > now() - make_interval(hours => %s)
AND (i.response->>'expires_at' IS NULL
OR (i.response->>'expires_at')::timestamptz > now()))
ORDER BY q.approved_at LIMIT 50 FOR UPDATE OF q SKIP LOCKED''',
(QUOTE_VALID_HOURS, list(FINAL_INTENT), QUOTE_VALID_HOURS + 1)).fetchall()
for row in rows:
c.execute('UPDATE dtf_local.quotes SET expired_at=now() WHERE id=%s', (row['id'],))
# expired_at is set first: release_files must no longer see this quote
# as open, or its own files would stay held.
payments.release_files(c, payments.quote_uploads(row['approved']))
# Only a quote that just ran out is news to the customer: the backlog
# from before this front existed expires silently on the first pass.
if row['approved_at'] > datetime.now(timezone.utc) - timedelta(hours=2 * QUOTE_VALID_HOURS):
mail.quote_expired(c, row, origin)
return len(rows)
def figures(c, start, end):
"""Números, for quotes approved between the local days `start` and `end`
(inclusive): {'made': n, 'paid': n, 'expired': n, 'waiting': n,
'abandon_pct': float | None}. Paid has an order; expired has expired_at;
waiting is neither yet. abandon_pct is expired / (paid + expired) * 100,
one decimal, None when both are 0."""
row = c.execute('''SELECT count(*) AS made,
count(*) FILTER (WHERE EXISTS (SELECT 1 FROM dtf_local.orders o WHERE o.quote_id=q.id)) AS paid,
count(*) FILTER (WHERE q.expired_at IS NOT NULL) AS expired,
count(*) FILTER (WHERE q.expired_at IS NULL
AND NOT EXISTS (SELECT 1 FROM dtf_local.orders o WHERE o.quote_id=q.id)) AS waiting
FROM dtf_local.quotes q
WHERE q.discarded_at IS NULL AND q.approved_at IS NOT NULL
AND (q.approved_at AT TIME ZONE 'America/Sao_Paulo')::date BETWEEN %s AND %s''',
(start, end)).fetchone()
paid, expired = row['paid'], row['expired']
decided = paid + expired
return {'made': row['made'], 'paid': paid, 'expired': expired, 'waiting': row['waiting'],
'abandon_pct': round(expired / decided * 100, 1) if decided else None}