Wachato CRM: versión marca blanca configurable por entorno

CRM de WhatsApp, Instagram, Messenger y TikTok de Wachato, basado en Vocero
CRM (MIT), listo para desplegar un negocio por instancia:

- Marca blanca: nombre, color, moneda y giro del negocio desde el .env
  (BRAND_NAME, BRAND_ACCENT, BRAND_CURRENCY, BUSINESS_DESCRIPTION); se
  aplican una vez y luego manda Ajustes → Marca.
- Superadmin sembrado al arrancar (SUPERADMIN_*), idempotente.
- Servicios y precios, campos personalizados, exportación a Excel/PDF,
  respaldos, agenda, atribución de anuncios y transcripción de notas de voz.
- Diseño con contraste AAA, tema claro y oscuro.
- Despliegue: docker compose (Caddy opcional), plantillas para paneles con
  reverse proxy (deploy/aapanel), imagen publicada en GHCR por la CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Andres Corazon Santana AquinoandClaude Opus 5.5 committed 2026-12-04 15:54:58 -04:00
commit 6edc6020d5
1316 files changed
+308543

No files matched your search

@@ -0,0 +1,104 @@
# Backend API blueprint
Use this reference when the user wants concrete backend responsibilities and route design.
## Recommended routes
### 1. Code exchange endpoint
Example:
```text
POST /api/whatsapp/exchange-token
```
Input:
- `code`
- `waba_id`
- `phone_number_id`
Responsibilities:
- validate the request,
- exchange the code with Meta,
- fetch connected phone metadata,
- persist the active connection,
- optionally store `meta_user_id`.
## 2. WhatsApp webhook
Example:
```text
GET /api/whatsapp/webhook
POST /api/whatsapp/webhook
```
GET responsibilities:
- verify `hub.verify_token`,
- return `hub.challenge`.
POST responsibilities:
- parse each `entry`,
- parse each `change`,
- process `value.statuses`,
- process `value.messages`,
- upsert by `message_id`.
## 3. Send template message
Example:
```text
POST /api/whatsapp/send-template
```
Input:
- recipient phone
- approved template name
- language code
- template parameters
Responsibilities:
- normalize the phone,
- send the message through Meta,
- store an initial outbound event as `accepted`.
## 4. Send freeform text message
Example:
```text
POST /api/whatsapp/send-text
```
Use this only for open customer service windows.
## 5. Deauthorize callback
Example:
```text
GET /api/meta/deauthorize
POST /api/meta/deauthorize
```
Responsibilities:
- read `signed_request`,
- validate it with `HMAC-SHA256` and the app secret,
- extract the Meta user identifier,
- map it to the local user,
- purge or unlink WhatsApp data,
- return a confirmation payload.
## Important behavior
- Store only metadata and tokens server-side.
- Never trust the UI as source of truth for message delivery.
- Use idempotent writes for webhook processing.
@@ -0,0 +1,40 @@
# WhatsApp SaaS reference architecture
Use this reference when the user needs the system-level view.
## Core layers
1. Frontend
2. Backend
3. Meta Graph API / WhatsApp Cloud API
4. Webhook endpoint
5. Supabase
## Recommended flow
1. A business enters the SaaS.
2. The business connects its own WhatsApp Business account through Meta Embedded Signup.
3. Meta returns:
- authorization code,
- `waba_id`,
- `phone_number_id`.
4. The backend exchanges the code for a usable token.
5. The backend stores the connection in Supabase.
6. The app reads:
- connected phone number,
- WABA-linked assets,
- templates.
7. The app sends:
- approved templates,
- text replies when the 24-hour window is open.
8. Meta posts statuses and inbound messages to the webhook.
9. The backend stores those events in Supabase.
10. The frontend renders a truthful dashboard from those records.
## Design principle
The product should not behave like a thin wrapper around API calls. The dashboard must reflect real state from:
- Meta assets,
- webhook events,
- and the local persistence layer.
@@ -0,0 +1,57 @@
# Recommended implementation sequence
Use this reference when the user is building from zero or needs a safe order of operations.
## Phase 1: Meta app and Embedded Signup
- Create the Meta app.
- Configure the WhatsApp product.
- Configure the OAuth redirect URL.
- Implement the frontend launcher for Embedded Signup.
- Capture the returned `code`, `waba_id`, and `phone_number_id`.
## Phase 2: Backend token exchange
- Create a backend endpoint for the code exchange.
- Exchange the Meta code for a usable token.
- Fetch phone metadata after the exchange.
- Persist the connection in Supabase.
- Save `meta_user_id` if available so deauthorize can map back to the local user later.
## Phase 3: Supabase schema
- Create a table for active WhatsApp connections.
- Create a table for message events.
- Add columns for:
- `message_id`,
- `direction`,
- `message_text`,
- delivery errors,
- timestamps.
## Phase 4: Sending
- Implement template sending first.
- Implement freeform text sending second.
- Persist the initial outbound event as `accepted`.
## Phase 5: Webhooks
- Implement GET verification.
- Implement POST ingestion.
- Store both:
- `statuses`,
- `messages`.
## Phase 6: Dashboard truthfulness
- Show the connected number.
- Show real templates.
- Show recent message events.
- Build conversations from stored events.
## Phase 7: Compliance
- Add a public data deletion instructions page.
- Add a deauthorize callback route.
- Support automatic deletion or unlinking when Meta revokes authorization.
@@ -0,0 +1,77 @@
# Meta configuration checklist
Use this reference when the user needs the configuration values and URLs that must exist in Meta Developers.
## OAuth / Embedded Signup
The app should expose a public callback page such as:
```text
https://your-domain.com/oauth/callback
```
This URL should be:
- listed in valid OAuth redirect URIs,
- reachable in production,
- consistent with the runtime environment.
## WhatsApp webhook
Use a public route such as:
```text
https://your-domain.com/api/whatsapp/webhook
```
Requirements:
- GET for verification
- POST for webhook events
- verify token stored as an environment variable
- subscribe to the `messages` field
## Data deletion instructions URL
Use a public page such as:
```text
https://your-domain.com/data-deletion
```
This page should explain how users can request deletion and how your team processes it.
## Deauthorize callback URL
Use a backend route such as:
```text
https://your-domain.com/api/meta/deauthorize
```
This route should:
- receive Meta's deauthorize callback,
- validate `signed_request`,
- identify the local user,
- delete or unlink the user's WhatsApp data,
- return a confirmation response.
## Critical environment variables
- `FACEBOOK_APP_ID`
- `FACEBOOK_APP_SECRET`
- `FACEBOOK_CONFIG_ID`
- `OAUTH_REDIRECT_URI`
- `SUPABASE_URL`
- `SUPABASE_ANON_KEY`
- `SUPABASE_SERVICE_ROLE_KEY`
- `WHATSAPP_WEBHOOK_VERIFY_TOKEN`
## Common config failures
- redirect URI mismatch
- wrong app secret
- app ID, app secret, and config ID from different apps
- webhook URL not publicly reachable
- deauthorize callback exists in Meta but not in the product
@@ -0,0 +1,71 @@
# Supabase schema for a WhatsApp SaaS
Use this reference when the user needs a minimal but production-leaning data model.
## Table: whatsapp_connections
Purpose:
- store the current connected WhatsApp Business account for a SaaS user.
```sql
create extension if not exists "uuid-ossp";
create table if not exists public.whatsapp_connections (
id uuid primary key default uuid_generate_v4(),
user_id uuid,
waba_id text not null,
phone_number_id text not null,
display_phone_number text,
verified_name text,
access_token text not null,
is_active boolean not null default true,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
```
## Table: whatsapp_message_events
Purpose:
- store outbound sends,
- delivery statuses,
- inbound messages,
- debugging context.
```sql
create table if not exists public.whatsapp_message_events (
id uuid primary key default uuid_generate_v4(),
waba_id text,
phone_number_id text,
message_id text not null unique,
recipient_phone text,
template_name text,
direction text not null default 'outbound',
message_text text,
status text not null,
error_code text,
error_title text,
error_message text,
raw_payload jsonb,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now(),
last_event_at timestamptz not null default now()
);
```
## Why this schema works
- `phone_number_id` ties events to the connected sender.
- `message_id` supports upserts from webhook updates.
- `direction` supports conversation rendering.
- `message_text` supports inbound conversation truth.
- `raw_payload` keeps enough context for debugging.
## Suggested operational rules
- Insert outbound events immediately after a successful send response.
- Update the same row when webhook statuses arrive.
- Insert inbound messages as new rows with `direction = inbound`.
- Mark stale or broken connections as inactive if Meta says the asset is gone.