6.9 KiB
n8n Flow For order_groups
Goal
Supabase stores the delivery state, generates the client link, and saves all timestamps.
n8n is responsible for sending SMS, retrying, and moving stalled groups into manual handling.
The short link is not needed on our side. We store the full delivery_link, and the SMS provider shortens it during delivery.
Source Of Truth
The main record is public.order_groups.
Important fields:
status- business readiness of the groupdelivery_status- delivery coordination state for the client and logisticsdelivery_link- full public link to/delivery/:tokendelivery_invitation_id- related invitation recordnotification_status- SMS orchestration state forn8nsms_attempts- how many SMS attempts were madefirst_sms_sent_at- timestamp of the first SMSsecond_sms_sent_at- timestamp of the second SMSlast_sms_error- last provider error textnext_notification_check_at- whenn8nshould revisit the recorddelivery_dateanddelivery_time- selected slot after client confirmation
Recommended Status Model
delivery_status
pending_confirmation- client has not selected a slot yetagreed- client selected a delivery slotmanual_confirmation_required- automatic flow failed, manager/logistics must continue manuallyassigned_to_driver- delivery is approved and handed over to driver planningout_for_delivery- driver is already working on itdelivered- delivery completedcancelled- group should no longer be processed
notification_status
not_started- link has not been prepared yetlink_ready- Supabase createddelivery_link,n8ncan send the first SMSfirst_sms_sent- first SMS was accepted by providersecond_sms_sent- reminder SMS was accepted by providerconfirmed- client selected a slotmanual_required- no confirmation after retriessend_failed- provider/API error, retry allowed
Supabase Responsibilities
1. Prepare the link
When an order_group is moved into the client-delivery flow:
status = 'ready_for_notification'delivery_status = 'pending_confirmation'
Supabase trigger order_groups_ensure_delivery_link should:
- create
delivery_invitationsrow withorder_group_id - generate token and full public URL
- write
delivery_linkintoorder_groups - set
delivery_invitation_id - set
notification_status = 'link_ready' - set
next_notification_check_at = now()
The SQL for this trigger lives in:
docs/sql/order-groups-auto-delivery-link.sql
n8n no longer has to call create-delivery-invitation for order_groups. It should wait until the row already has notification_status = 'link_ready' and delivery_link is not null.
2. Accept client choice
The public client page uses the token.
When the client confirms a slot, confirm-delivery-choice should:
- store
delivery_dateanddelivery_time - set
delivery_status = 'agreed' - set
notification_status = 'confirmed'
That change becomes the stop signal for all reminder workflows in n8n.
n8n Workflows
Workflow 1. First SMS sender
Trigger:
- Cron every 5-10 minutes
- Optional backup webhook trigger if you later want push-based start
Query:
status = 'ready_for_notification'delivery_status = 'pending_confirmation'notification_status = 'link_ready'delivery_link is not null
Action:
- send SMS with
delivery_link
On success update order_groups:
notification_status = 'first_sms_sent'sms_attempts = 1first_sms_sent_at = now()sms_sent_at = now()last_sms_error = nullnext_notification_check_at = now() + interval '1 hour'
On failure update order_groups:
notification_status = 'send_failed'last_sms_error = <provider error>next_notification_check_at = now() + interval '10 minutes'
Workflow 2. Delivery watchdog
Trigger:
- Cron every 10 minutes
Purpose:
- find records where first workflow did not finish cleanly
- retry failed first sends
Query candidates:
notification_status = 'send_failed'delivery_status = 'pending_confirmation'next_notification_check_at <= now()
Behavior:
- retry first SMS
- if success, move to
first_sms_sent - if repeated failures exceed your chosen threshold, move to
manual_required
Workflow 3. Reminder SMS
Trigger:
- Cron every 10 minutes
Query:
delivery_status = 'pending_confirmation'notification_status = 'first_sms_sent'next_notification_check_at <= now()
Action:
- send second SMS reminder with the same
delivery_link
On success update:
notification_status = 'second_sms_sent'sms_attempts = 2second_sms_sent_at = now()last_sms_error = nullnext_notification_check_at = now() + interval '3 hours'
On failure update:
notification_status = 'send_failed'last_sms_error = <provider error>next_notification_check_at = now() + interval '30 minutes'
Workflow 4. Manual handoff
Trigger:
- Cron every 10 minutes
Query:
delivery_status = 'pending_confirmation'notification_status = 'second_sms_sent'next_notification_check_at <= now()
Action:
- stop automatic reminders
- move the group into manual handling
Update:
delivery_status = 'manual_confirmation_required'notification_status = 'manual_required'
Workflow 5. Stop conditions
Every workflow must ignore rows where:
delivery_status in ('agreed', 'assigned_to_driver', 'out_for_delivery', 'delivered', 'cancelled')notification_status in ('confirmed', 'manual_required')
This prevents duplicate SMS after the client already responded or the case was handed to a person.
Suggested SMS Text
Example:
Ваш заказ готов к согласованию доставки.
Выберите удобные дату и время по ссылке:
{{delivery_link}}
Reminder:
Напоминаем: нужно выбрать дату и время доставки вашего заказа.
Ссылка:
{{delivery_link}}
What Frontend Needs
The frontend public page only needs:
- token from URL
get-delivery-invitationconfirm-delivery-choice
No SMS logic should live in the frontend. No link generation should live in the frontend.
Minimal Rollout Order
- Deploy updated
Supabaseschema anddocs/sql/order-groups-auto-delivery-link.sql. - Verify that insert/update in
order_groupswritesdelivery_linkandnotification_status = 'link_ready'. - Build
n8nworkflow for first SMS. - Build
n8nreminder workflow. - Build
n8nmanual-handoff workflow. - Test full cycle on one real
order_group.
Test Scenario
- Mark one
order_groupas ready for client delivery. - Confirm that
delivery_linkappeared inorder_groupsautomatically. - Let
n8nsend the first SMS. - Open the link and confirm a slot on the client page.
- Confirm that
delivery_status = 'agreed'andnotification_status = 'confirmed'. - Confirm that reminder workflows no longer touch this group.