﻿# Customer order emails

## Current status

Customer emails are still off. The connected Resend account currently has no verified sending domains, so it cannot send customer messages from a LyfStyle address yet. Keep `window.LYFSTYLE_CUSTOMER_EMAILS_ENABLED` set to `false` until a domain you own is verified and the database/function setup below is complete. Existing owner order alerts are separate and remain active.

The `onboarding@resend.dev` test sender is restricted to the Resend account owner's inbox. It cannot be used to send order updates to shoppers.

## Customer choice

Only two transactional messages:
1. Order received, immediately after placing an order. This is not a payment receipt.
2. Order shipped, when the admin changes its status to shipped.

No messages for payment changes, confirmed, completed or cancelled statuses. Each order receives at most one of each message, even if a status is changed back and forth.

When customer emails are enabled, MoMo checkout asks for an email address and stores it with the order. Paystack checkout always asks for an email for its payment receipt; it also receives LyfStyle order updates when enabled. With the feature off, MoMo checkout hides the email field and leaves the email column out of its insert.

## Sending-domain requirement

The current onboarding@resend.dev test sender can only send to the Resend account owner's address. Verify a domain you own and set CUSTOMER_EMAIL_FROM to an address at that verified domain. A Netlify subdomain does not provide DNS control for email verification.

Official instructions: https://resend.com/docs/knowledge-base/403-error-resend-dev-domain

## Activation after approval

1. Add a domain you own in Resend and publish the DNS records it provides. Wait until Resend marks the domain verified. Do not put API keys in chat or website files.
2. Deploy supabase/functions/customer-order-notification with JWT verification ON. Existing RESEND_API_KEY, SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY, ORDER_WEBHOOK_SECRET and STORE_OWNER_EMAIL stay server-side.
3. Set CUSTOMER_EMAIL_FROM (verified domain) and CUSTOMER_ORDER_EMAILS_ENABLED=true in Supabase function secrets. The function rejects the shared resend.dev test sender for customer emails.
4. Apply supabase-migration-customer-order-emails.sql, replacing the public anon key/JWT placeholders privately as with the existing owner webhook. It uses the existing Vault lyfstyle_order_webhook secret. The private event ledger is inaccessible to browser clients.
5. Set `window.LYFSTYLE_CUSTOMER_EMAILS_ENABLED=true` in `supabase-config.js` only after the schema, function, secrets and verified sending service are ready, then publish. The flag defaults to false. Paystack still collects an email for its receipt; MoMo only asks for one once customer order emails are enabled.
6. Place an approved test order and verify an order confirmation reaches the customer test inbox. Change that test order to shipped and check the second email. Verify other status changes do not send messages.

Owner order alerts continue through their existing, separate function and ledger.

## Delivery and retry behaviour

The private event table records status, attempts, provider ID and HTTP queue request ID. A provider accepting a message is not proof that it reached an inbox; check Resend delivery logs.

The queue is durable, but this migration does not install an automatic scheduler. A failed HTTP call or email remains pending/failed until explicitly retried by the operator. Retry a specific event in the SQL editor:

    select public.queue_customer_order_email('EVENT_UUID_HERE');

Retries use the same provider idempotency key and private sent-state check. Avoid manually resetting sent rows. Resend's idempotency window is not permanent; if a provider success could not be recorded for an extended period, inspect its delivery log before retrying to prevent duplicates.

Legacy orders without a customer email remain usable and do not receive emails. Changing an old order to shipped only produces an email if it has a saved customer email. Historical order-confirmation messages are never backfilled automatically.
