# LyfStyle order alerts

## Current state

Recipients confirmed by the owner: **kwabylogic2@gmail.com** and **WhatsApp +233595556350**.

**Email alerts are active as of 29 October 2026.** Resend confirmed the activation email was delivered to `kwabylogic2@gmail.com` (provider ID `01a189d1-97c1-76eb-9424-895eb30c6671`). The sender is `LyfStyle Orders <onboarding@resend.dev>`, limited to the Resend account owner's email until a store domain is verified. No test customer order was created.

The `lyfstyle_order_email_alert` database trigger queues a request after a new order is saved. The private header secret is stored in Supabase Vault and Edge Function Secrets. JWT verification remains enabled; the trigger also sends the existing public anon JWT. No private sending keys are stored in this repository.

**WhatsApp automation is not activated.** The order confirmation includes a WhatsApp button that customers may tap and send themselves. This is a manual handoff.

## Email first

1. Create a [Resend account](https://resend.com/signup), verify the account, and configure a sender permitted by Resend. For a permanent store sender, verify a domain you own. A Gmail address can receive alerts; do not use it as the API sender unless the provider explicitly permits it.
2. Create a sending API key in Resend. Enter it directly in **Supabase → Edge Functions → Secrets**; never paste it in chat or browser code.
3. Set these function secrets:

| Secret | Value |
| --- | --- |
| `RESEND_API_KEY` | The sending API key from Resend |
| `RESEND_FROM` | The complete permitted sender, for example `LyfStyle Orders <orders@your-verified-domain>` |
| `STORE_OWNER_EMAIL` | `kwabylogic2@gmail.com` |
| `ORDER_WEBHOOK_SECRET` | A strong randomly generated private secret; use the same value in the webhook header below |

The Edge Function also uses Supabase's server-only `SUPABASE_URL` and `SUPABASE_SERVICE_ROLE_KEY` environment values. Never put the service-role key in the storefront configuration.

## Deploy and connect orders

1. Run `supabase-migration-order-alerts.sql` in Supabase SQL Editor. It creates a private delivery ledger and a service-only function for claiming each notification. Normal customer sessions cannot read or change it.
2. Deploy the complete `supabase/functions/order-notification` folder to the existing Supabase project. With the Supabase CLI, use `supabase functions deploy order-notification --project-ref mwzkvrejquikmvzuwshe`. Both `index.ts` and `handler.mjs` must be deployed. JWT verification remains enabled, and the handler also validates a private webhook secret.
3. The existing project already has its INSERT trigger. For another installation, use `supabase-migration-order-alert-webhook.sql`, replacing placeholders privately. Do not rerun its trigger or Vault creation statements on the live project. Do not also create a second Dashboard webhook for the same orders.
4. Requests include `Content-Type: application/json`, `Authorization: Bearer <existing public anon JWT>`, the public `apikey`, and `x-lyfstyle-webhook-secret`. The database loads the private header value from Vault and uses a 30-second timeout.
5. With the owner's approval, POST `{"type":"TEST"}` with these authenticated headers. It sends a clearly labelled test email without placing an order. Check Resend for delivery, not merely API acceptance. This activation check has passed for the existing project.

The webhook runs after an order is saved. A notification failure does not undo or discard the customer's order. The function accepts only new-order events, looks up the canonical saved order, and keeps the recipient server-controlled. Emails include customer contact/delivery details, ordered items, amount, and payment reference. They remind the owner to verify the recorded payment status before dispatch.

## Optional automatic WhatsApp

Configure a WhatsApp Business Platform sender that can message **233595556350**, then have the following template approved and enabled:

> New LyfStyle order {{1}}. Total: {{2}}. Customer: {{3}}. Open your admin dashboard to review the order and verify payment.

Use three body variables in exactly that order. Set these Edge Function secrets after approval:

| Secret | Value |
| --- | --- |
| `WHATSAPP_ACCESS_TOKEN` | Server-side token with permission to send messages |
| `WHATSAPP_PHONE_NUMBER_ID` | The sending phone-number ID from Meta, not the receiving phone number |
| `WHATSAPP_GRAPH_VERSION` | The supported Graph API version configured for your Meta app, such as `vXX.X` (replace with a real supported version) |
| `WHATSAPP_TEMPLATE_NAME` | The approved template name |
| `WHATSAPP_TEMPLATE_LANGUAGE` | The template's approved language, default `en_US` |
| `STORE_OWNER_WHATSAPP` | `233595556350` |

Do not set WhatsApp secrets until the sender and template are ready. Email can operate on its own. WhatsApp alerts use the approved template rather than sending unsolicited free-form messages.

## Failed deliveries and retries

Inspect `order_alert_deliveries` and function logs for failures. Invoke the authenticated webhook again with the same INSERT event to retry a failed channel. Already-sent channels are skipped, and email requests use a stable Resend idempotency key. In-flight jobs have a two-minute lease. The ledger reduces duplicates; a provider accepting a WhatsApp message immediately before a process failure can still lead to a duplicate on a later retry. Do not assume Supabase Database Webhooks will automatically retry every failed call.

## Provider references

- [Supabase Database Webhooks](https://supabase.com/docs/guides/database/webhooks)
- [Resend API reference](https://resend.com/docs/api-reference/emails/send-email)
- [WhatsApp template approvals](https://www.twilio.com/docs/whatsapp/tutorial/message-template-approvals-statuses)
