fix: manual_campaign EXISTS check — no manual_required without SMS sent

Scenario 3 (link_ready + next_check expired) now requires EXISTS
in sms_campaign_log. Prevents groups from being moved to
manual_required before first SMS is ever sent.
This commit is contained in:
root 2026-07-03 12:18:28 +00:00
parent db57624626
commit e55a4cafcc
1 changed files with 8 additions and 1 deletions

View File

@ -123,9 +123,16 @@ def get_groups_to_manual(conn):
AND (og.next_notification_check_at IS NULL OR og.next_notification_check_at <= NOW())
AND og.first_sms_sent_at < NOW() - INTERVAL '3 hours'
AND og.second_sms_sent_at IS NOT NULL)
-- 3. link_ready + next_check в прошлом + нет активных SMS в логе
-- 3. link_ready + next_check в прошлом + SMS уже отправлялась хотя бы раз
-- ВАЖНО: если SMS никогда не отправлялась (нет записи в логе), не переводить в manual --
-- возможно first campaign ещё не успел отправить (БД была недоступна и т.п.)
OR (COALESCE(og.notification_status, '') = 'link_ready'
AND (og.next_notification_check_at IS NULL OR og.next_notification_check_at <= NOW())
AND EXISTS (
SELECT 1 FROM sms_campaign_log scl
WHERE scl.order_group_id = og.id
AND scl.status IN ('sent', 'checking', 'delivered')
)
AND NOT EXISTS (
SELECT 1 FROM sms_campaign_log scl
WHERE scl.order_group_id = og.id