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:
commit
6edc6020d5
1316 files changed
+308543
No files matched your search
@@ -0,0 +1,254 @@
|
||||
CREATE TABLE "account" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"account_id" text NOT NULL,
|
||||
"provider_id" text NOT NULL,
|
||||
"user_id" text NOT NULL,
|
||||
"access_token" text,
|
||||
"refresh_token" text,
|
||||
"id_token" text,
|
||||
"access_token_expires_at" timestamp,
|
||||
"refresh_token_expires_at" timestamp,
|
||||
"scope" text,
|
||||
"password" text,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "agent_profile" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"enabled" boolean DEFAULT false NOT NULL,
|
||||
"name" text DEFAULT 'Asistente' NOT NULL,
|
||||
"tone" text,
|
||||
"instructions" text,
|
||||
"escalation_rules" text,
|
||||
"greeting" text,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "agent_test_case" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"run_id" text NOT NULL,
|
||||
"persona" text NOT NULL,
|
||||
"conversation_id" text,
|
||||
"transcript" jsonb,
|
||||
"veredicto" text,
|
||||
"hallazgos" jsonb,
|
||||
"status" text DEFAULT 'pending' NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "agent_test_run" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"status" text DEFAULT 'running' NOT NULL,
|
||||
"score" integer,
|
||||
"error" text,
|
||||
"started_at" timestamp DEFAULT now() NOT NULL,
|
||||
"finished_at" timestamp
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "contact" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"phone" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"notes" text,
|
||||
"archived_at" timestamp,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "conversation" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"contact_id" text NOT NULL,
|
||||
"is_test" boolean DEFAULT false NOT NULL,
|
||||
"ai_enabled" boolean DEFAULT true NOT NULL,
|
||||
"handoff_at" timestamp,
|
||||
"handoff_reason" text,
|
||||
"last_inbound_at" timestamp,
|
||||
"last_message_at" timestamp,
|
||||
"unread_count" integer DEFAULT 0 NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "invitation" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"email" text NOT NULL,
|
||||
"role" text,
|
||||
"status" text DEFAULT 'pending' NOT NULL,
|
||||
"expires_at" timestamp NOT NULL,
|
||||
"inviter_id" text NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "kb_entry" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"kind" text NOT NULL,
|
||||
"question" text,
|
||||
"answer" text,
|
||||
"content" text,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "lead" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"contact_id" text NOT NULL,
|
||||
"stage_id" text NOT NULL,
|
||||
"position" integer DEFAULT 0 NOT NULL,
|
||||
"last_activity_at" timestamp,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "member" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"user_id" text NOT NULL,
|
||||
"role" text DEFAULT 'member' NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "message" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"conversation_id" text NOT NULL,
|
||||
"wa_message_id" text,
|
||||
"direction" text NOT NULL,
|
||||
"type" text DEFAULT 'text' NOT NULL,
|
||||
"text" text,
|
||||
"status" text DEFAULT 'pending' NOT NULL,
|
||||
"error" text,
|
||||
"ai_generated" boolean DEFAULT false NOT NULL,
|
||||
"wa_timestamp" timestamp,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "message_wa_message_id_unique" UNIQUE("wa_message_id")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "meta_credentials" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"waba_id" text NOT NULL,
|
||||
"phone_number_id" text NOT NULL,
|
||||
"display_phone_number" text,
|
||||
"verified_name" text,
|
||||
"token_cipher" text NOT NULL,
|
||||
"token_iv" text NOT NULL,
|
||||
"token_tag" text NOT NULL,
|
||||
"status" text DEFAULT 'connected' NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "organization" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"slug" text,
|
||||
"logo" text,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"metadata" text,
|
||||
CONSTRAINT "organization_slug_unique" UNIQUE("slug")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "pipeline_stage" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"position" integer NOT NULL,
|
||||
"kind" text DEFAULT 'open' NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "session" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"expires_at" timestamp NOT NULL,
|
||||
"token" text NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL,
|
||||
"ip_address" text,
|
||||
"user_agent" text,
|
||||
"user_id" text NOT NULL,
|
||||
"active_organization_id" text,
|
||||
CONSTRAINT "session_token_unique" UNIQUE("token")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "template" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"language" text NOT NULL,
|
||||
"category" text NOT NULL,
|
||||
"body" text NOT NULL,
|
||||
"status" text DEFAULT 'draft' NOT NULL,
|
||||
"rejection_reason" text,
|
||||
"wa_template_id" text,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "user" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"email" text NOT NULL,
|
||||
"email_verified" boolean DEFAULT false NOT NULL,
|
||||
"image" text,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "user_email_unique" UNIQUE("email")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "verification" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"identifier" text NOT NULL,
|
||||
"value" text NOT NULL,
|
||||
"expires_at" timestamp NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "agent_profile" ADD CONSTRAINT "agent_profile_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "agent_test_case" ADD CONSTRAINT "agent_test_case_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "agent_test_case" ADD CONSTRAINT "agent_test_case_run_id_agent_test_run_id_fk" FOREIGN KEY ("run_id") REFERENCES "public"."agent_test_run"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "agent_test_case" ADD CONSTRAINT "agent_test_case_conversation_id_conversation_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversation"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "agent_test_run" ADD CONSTRAINT "agent_test_run_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "contact" ADD CONSTRAINT "contact_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "conversation" ADD CONSTRAINT "conversation_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "conversation" ADD CONSTRAINT "conversation_contact_id_contact_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contact"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "invitation" ADD CONSTRAINT "invitation_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "invitation" ADD CONSTRAINT "invitation_inviter_id_user_id_fk" FOREIGN KEY ("inviter_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "kb_entry" ADD CONSTRAINT "kb_entry_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "lead" ADD CONSTRAINT "lead_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "lead" ADD CONSTRAINT "lead_contact_id_contact_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contact"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "lead" ADD CONSTRAINT "lead_stage_id_pipeline_stage_id_fk" FOREIGN KEY ("stage_id") REFERENCES "public"."pipeline_stage"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "member" ADD CONSTRAINT "member_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "member" ADD CONSTRAINT "member_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "message" ADD CONSTRAINT "message_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "message" ADD CONSTRAINT "message_conversation_id_conversation_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversation"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "meta_credentials" ADD CONSTRAINT "meta_credentials_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "pipeline_stage" ADD CONSTRAINT "pipeline_stage_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "template" ADD CONSTRAINT "template_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "agent_profile_org_uq" ON "agent_profile" USING btree ("organization_id");--> statement-breakpoint
|
||||
CREATE INDEX "test_case_run_idx" ON "agent_test_case" USING btree ("run_id");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "test_run_org_running_uq" ON "agent_test_run" USING btree ("organization_id") WHERE "agent_test_run"."status" = 'running';--> statement-breakpoint
|
||||
CREATE INDEX "test_run_org_idx" ON "agent_test_run" USING btree ("organization_id","started_at");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "contact_org_phone_uq" ON "contact" USING btree ("organization_id","phone");--> statement-breakpoint
|
||||
CREATE INDEX "contact_org_name_idx" ON "contact" USING btree ("organization_id","name");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "conversation_org_contact_real_uq" ON "conversation" USING btree ("organization_id","contact_id") WHERE "conversation"."is_test" = false;--> statement-breakpoint
|
||||
CREATE INDEX "conversation_org_last_idx" ON "conversation" USING btree ("organization_id","last_message_at");--> statement-breakpoint
|
||||
CREATE INDEX "kb_org_idx" ON "kb_entry" USING btree ("organization_id");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "lead_contact_uq" ON "lead" USING btree ("contact_id");--> statement-breakpoint
|
||||
CREATE INDEX "lead_org_stage_idx" ON "lead" USING btree ("organization_id","stage_id","position");--> statement-breakpoint
|
||||
CREATE INDEX "message_org_conv_idx" ON "message" USING btree ("organization_id","conversation_id","created_at");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "meta_credentials_org_uq" ON "meta_credentials" USING btree ("organization_id");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "meta_credentials_phone_uq" ON "meta_credentials" USING btree ("phone_number_id");--> statement-breakpoint
|
||||
CREATE INDEX "stage_org_pos_idx" ON "pipeline_stage" USING btree ("organization_id","position");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "template_org_name_lang_uq" ON "template" USING btree ("organization_id","name","language");
|
||||
@@ -0,0 +1,70 @@
|
||||
-- 003 Identidad resiliente de contacto (BSUID).
|
||||
-- Editada a mano sobre la generada: añade backfill + dedup para instancias con
|
||||
-- datos (en una instancia fresca los pasos de backfill son no-op). Corre en una
|
||||
-- transacción (migrator de drizzle), re-ejecutable vía IF EXISTS/IF NOT EXISTS.
|
||||
|
||||
ALTER TABLE "contact" ADD COLUMN IF NOT EXISTS "wa_identity" text;--> statement-breakpoint
|
||||
ALTER TABLE "contact" ADD COLUMN IF NOT EXISTS "wa_user_id" text;--> statement-breakpoint
|
||||
|
||||
-- Backfill: la identidad es el teléfono NORMALIZADO (troncal MX 521→52) — la
|
||||
-- misma regla que normalizeMx() en src/lib/meta/client.ts.
|
||||
UPDATE "contact"
|
||||
SET "wa_identity" = CASE
|
||||
WHEN "phone" ~ '^521[0-9]{10}$' THEN '52' || substring("phone" from 4)
|
||||
ELSE "phone"
|
||||
END
|
||||
WHERE "wa_identity" IS NULL;--> statement-breakpoint
|
||||
|
||||
-- Dedup de colisiones post-normalización (contactos 521/52 duplicados hoy):
|
||||
-- la fila más antigua es canónica; se re-apunta lo colgado y se borra la dup.
|
||||
CREATE TEMPORARY TABLE "_contact_dedup" ON COMMIT DROP AS
|
||||
SELECT id AS dup_id, keep_id FROM (
|
||||
SELECT id,
|
||||
first_value(id) OVER (
|
||||
PARTITION BY organization_id, wa_identity
|
||||
ORDER BY created_at, id
|
||||
) AS keep_id
|
||||
FROM "contact"
|
||||
) ranked
|
||||
WHERE id <> keep_id;--> statement-breakpoint
|
||||
|
||||
-- Conversaciones no-test del duplicado cuando la canónica YA tiene una:
|
||||
-- mover los mensajes a la conversación canónica y borrar la duplicada.
|
||||
UPDATE "message" m
|
||||
SET conversation_id = ck.id
|
||||
FROM "conversation" cd
|
||||
JOIN "_contact_dedup" d ON cd.contact_id = d.dup_id
|
||||
JOIN "conversation" ck ON ck.contact_id = d.keep_id AND ck.is_test = false
|
||||
WHERE m.conversation_id = cd.id AND cd.is_test = false;--> statement-breakpoint
|
||||
|
||||
DELETE FROM "conversation" cd
|
||||
USING "_contact_dedup" d, "conversation" ck
|
||||
WHERE cd.contact_id = d.dup_id AND cd.is_test = false
|
||||
AND ck.contact_id = d.keep_id AND ck.is_test = false;--> statement-breakpoint
|
||||
|
||||
-- Resto de conversaciones del duplicado (canónica sin conversación, o de test):
|
||||
-- re-apuntar a la canónica.
|
||||
UPDATE "conversation" cd
|
||||
SET contact_id = d.keep_id
|
||||
FROM "_contact_dedup" d
|
||||
WHERE cd.contact_id = d.dup_id;--> statement-breakpoint
|
||||
|
||||
-- Leads: si ambas filas tienen lead, sobrevive el de la canónica.
|
||||
DELETE FROM "lead" l
|
||||
USING "_contact_dedup" d, "lead" lk
|
||||
WHERE l.contact_id = d.dup_id AND lk.contact_id = d.keep_id;--> statement-breakpoint
|
||||
|
||||
UPDATE "lead" l
|
||||
SET contact_id = d.keep_id
|
||||
FROM "_contact_dedup" d
|
||||
WHERE l.contact_id = d.dup_id;--> statement-breakpoint
|
||||
|
||||
DELETE FROM "contact" c
|
||||
USING "_contact_dedup" d
|
||||
WHERE c.id = d.dup_id;--> statement-breakpoint
|
||||
|
||||
ALTER TABLE "contact" ALTER COLUMN "wa_identity" SET NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "contact" ALTER COLUMN "phone" DROP NOT NULL;--> statement-breakpoint
|
||||
DROP INDEX IF EXISTS "contact_org_phone_uq";--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "contact_org_wa_identity_uq" ON "contact" USING btree ("organization_id","wa_identity");--> statement-breakpoint
|
||||
CREATE INDEX IF NOT EXISTS "contact_org_wa_user_id_idx" ON "contact" USING btree ("organization_id","wa_user_id");
|
||||
@@ -0,0 +1,26 @@
|
||||
CREATE TABLE "media_asset" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"kind" text NOT NULL,
|
||||
"wa_media_id" text,
|
||||
"mime_type" text,
|
||||
"file_name" text,
|
||||
"file_size" integer,
|
||||
"caption" text,
|
||||
"payload" jsonb,
|
||||
"storage_path" text,
|
||||
"fetch_status" text DEFAULT 'pending' NOT NULL,
|
||||
"fetch_error" text,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "message" ADD COLUMN "origin" text DEFAULT 'operator' NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "message" ADD COLUMN "media_asset_id" text;--> statement-breakpoint
|
||||
ALTER TABLE "media_asset" ADD CONSTRAINT "media_asset_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE INDEX "media_asset_org_idx" ON "media_asset" USING btree ("organization_id","created_at");--> statement-breakpoint
|
||||
CREATE INDEX "media_asset_wa_media_idx" ON "media_asset" USING btree ("wa_media_id");--> statement-breakpoint
|
||||
ALTER TABLE "message" ADD CONSTRAINT "message_media_asset_id_media_asset_id_fk" FOREIGN KEY ("media_asset_id") REFERENCES "public"."media_asset"("id") ON DELETE set null ON UPDATE no action;
|
||||
--> statement-breakpoint
|
||||
UPDATE "message" SET "origin" = 'ai' WHERE "direction" = 'out' AND "ai_generated" = true AND "origin" = 'operator';--> statement-breakpoint
|
||||
UPDATE "message" SET "origin" = 'template' WHERE "direction" = 'out' AND "type" = 'template' AND "origin" = 'operator';
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE "contact" ADD COLUMN "ficha" jsonb;
|
||||
@@ -0,0 +1,90 @@
|
||||
CREATE TABLE "lead_stage_event" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"lead_id" text NOT NULL,
|
||||
"contact_id" text NOT NULL,
|
||||
"from_stage_id" text,
|
||||
"from_stage_name" text,
|
||||
"to_stage_id" text,
|
||||
"to_stage_name" text NOT NULL,
|
||||
"to_stage_kind" text DEFAULT 'open' NOT NULL,
|
||||
"occurred_at" timestamp DEFAULT now() NOT NULL,
|
||||
"actor_user_id" text,
|
||||
"source" text DEFAULT 'dueno' NOT NULL,
|
||||
"approximate" boolean DEFAULT false NOT NULL,
|
||||
"loss_reason" text,
|
||||
"loss_note" text,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "lse_loss_reason_ck" CHECK ("lead_stage_event"."to_stage_kind" <> 'lost' OR "lead_stage_event"."approximate" = true OR "lead_stage_event"."loss_reason" IS NOT NULL)
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "lead_stage_event" ADD CONSTRAINT "lead_stage_event_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "lead_stage_event" ADD CONSTRAINT "lead_stage_event_lead_id_lead_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."lead"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "lead_stage_event" ADD CONSTRAINT "lead_stage_event_contact_id_contact_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contact"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "lead_stage_event" ADD CONSTRAINT "lead_stage_event_from_stage_id_pipeline_stage_id_fk" FOREIGN KEY ("from_stage_id") REFERENCES "public"."pipeline_stage"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "lead_stage_event" ADD CONSTRAINT "lead_stage_event_to_stage_id_pipeline_stage_id_fk" FOREIGN KEY ("to_stage_id") REFERENCES "public"."pipeline_stage"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "lead_stage_event" ADD CONSTRAINT "lead_stage_event_actor_user_id_user_id_fk" FOREIGN KEY ("actor_user_id") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE INDEX "lse_org_occurred_idx" ON "lead_stage_event" USING btree ("organization_id","occurred_at");--> statement-breakpoint
|
||||
CREATE INDEX "lse_lead_occurred_idx" ON "lead_stage_event" USING btree ("lead_id","occurred_at");--> statement-breakpoint
|
||||
CREATE INDEX "lse_org_kind_occurred_idx" ON "lead_stage_event" USING btree ("organization_id","to_stage_kind","occurred_at");--> statement-breakpoint
|
||||
-- Siembra de la bitácora para los leads que ya existían.
|
||||
-- El pasado NO se reconstruye: se marca `approximate = true` para que estos
|
||||
-- eventos cuenten en los TOTALES pero nunca en los promedios de tiempo.
|
||||
-- Idempotente: el id se deriva del lead con md5, así que re-ejecutar la
|
||||
-- migración no duplica.
|
||||
--
|
||||
-- 1) "Nació aquí": todo lead entra por la primera etapa del tablero, que es
|
||||
-- exactamente donde lo pone la ingesta. La fecha SÍ es confiable
|
||||
-- (`created_at` no se pisa).
|
||||
INSERT INTO "lead_stage_event" (
|
||||
"id","organization_id","lead_id","contact_id",
|
||||
"from_stage_id","from_stage_name","to_stage_id","to_stage_name","to_stage_kind",
|
||||
"occurred_at","actor_user_id","source","approximate","created_at"
|
||||
)
|
||||
SELECT
|
||||
'lse_' || substr(md5(l."id" || ':nacio'), 1, 20),
|
||||
l."organization_id", l."id", l."contact_id",
|
||||
NULL, NULL,
|
||||
primera."id", primera."name", primera."kind",
|
||||
l."created_at", NULL, 'migracion', true, now()
|
||||
FROM "lead" l
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT s."id", s."name", s."kind"
|
||||
FROM "pipeline_stage" s
|
||||
WHERE s."organization_id" = l."organization_id"
|
||||
ORDER BY s."position" ASC
|
||||
LIMIT 1
|
||||
) AS primera
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM "lead_stage_event" e
|
||||
WHERE e."id" = 'lse_' || substr(md5(l."id" || ':nacio'), 1, 20)
|
||||
);--> statement-breakpoint
|
||||
-- 2) "Llegó hasta aquí": un segundo evento hacia la etapa ACTUAL para los
|
||||
-- leads que ya no están en la primera. Sin él, el embudo diría que ningún
|
||||
-- lead viejo pasó de la primera columna. La fecha es `updated_at`, que
|
||||
-- cualquier edición pisa: por eso va como aproximada.
|
||||
INSERT INTO "lead_stage_event" (
|
||||
"id","organization_id","lead_id","contact_id",
|
||||
"from_stage_id","from_stage_name","to_stage_id","to_stage_name","to_stage_kind",
|
||||
"occurred_at","actor_user_id","source","approximate","created_at"
|
||||
)
|
||||
SELECT
|
||||
'lse_' || substr(md5(l."id" || ':actual'), 1, 20),
|
||||
l."organization_id", l."id", l."contact_id",
|
||||
primera."id", primera."name",
|
||||
actual."id", actual."name", actual."kind",
|
||||
GREATEST(l."updated_at", l."created_at"), NULL, 'migracion', true, now()
|
||||
FROM "lead" l
|
||||
JOIN "pipeline_stage" actual ON actual."id" = l."stage_id"
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT s."id", s."name"
|
||||
FROM "pipeline_stage" s
|
||||
WHERE s."organization_id" = l."organization_id"
|
||||
ORDER BY s."position" ASC
|
||||
LIMIT 1
|
||||
) AS primera
|
||||
WHERE actual."id" <> primera."id"
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM "lead_stage_event" e
|
||||
WHERE e."id" = 'lse_' || substr(md5(l."id" || ':actual'), 1, 20)
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE "contact" ADD COLUMN "source" text;
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE "lead" ADD COLUMN "amount_cents" integer;--> statement-breakpoint
|
||||
ALTER TABLE "lead" ADD COLUMN "currency" text;
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE "lead" ADD COLUMN "priority" text;--> statement-breakpoint
|
||||
ALTER TABLE "lead" ADD COLUMN "priority_updated_at" timestamp;
|
||||
@@ -0,0 +1,30 @@
|
||||
CREATE TABLE IF NOT EXISTS "instagram_credentials" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"source" text NOT NULL,
|
||||
"ig_user_id" text NOT NULL,
|
||||
"account_ref" text,
|
||||
"username" text,
|
||||
"token_cipher" text NOT NULL,
|
||||
"token_iv" text NOT NULL,
|
||||
"token_tag" text NOT NULL,
|
||||
"webhook_secret" text,
|
||||
"status" text DEFAULT 'connected' NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "contact" ADD COLUMN "channel" text DEFAULT 'whatsapp' NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "conversation" ADD COLUMN "channel" text DEFAULT 'whatsapp' NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "conversation" ADD COLUMN "channel_thread_ref" text;--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "instagram_credentials" ADD CONSTRAINT "instagram_credentials_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
DROP INDEX IF EXISTS "contact_org_wa_identity_uq";--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "contact_org_channel_identity_uq" ON "contact" USING btree ("organization_id","channel","wa_identity");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "instagram_credentials_org_uq" ON "instagram_credentials" USING btree ("organization_id");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "instagram_credentials_ig_user_uq" ON "instagram_credentials" USING btree ("ig_user_id");--> statement-breakpoint
|
||||
CREATE INDEX IF NOT EXISTS "instagram_credentials_account_ref_idx" ON "instagram_credentials" USING btree ("account_ref");
|
||||
@@ -0,0 +1,135 @@
|
||||
-- 015 Motor de agendamiento universal (agenda, citas, memoria de lo ofrecido
|
||||
-- y credenciales de los conectores).
|
||||
--
|
||||
-- Editada a mano sobre la generada para ser RE-EJECUTABLE (Constitución IV):
|
||||
-- IF NOT EXISTS en tablas e índices, y DO-block en cada clave foránea.
|
||||
--
|
||||
-- Es puramente ADITIVA: no toca ninguna tabla existente, así que aplicarla
|
||||
-- sobre una instancia con datos no puede perder nada. Se aplica SIEMPRE, con
|
||||
-- la bandera AGENDA encendida o apagada: unas tablas vacías son inertes y a
|
||||
-- cambio todas las instancias comparten la misma estructura (ADR-001).
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "calendar_settings" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"weekly_hours" jsonb NOT NULL,
|
||||
"slot_minutes" integer DEFAULT 30 NOT NULL,
|
||||
"buffer_minutes" integer DEFAULT 0 NOT NULL,
|
||||
"min_notice_hours" integer DEFAULT 2 NOT NULL,
|
||||
"max_days_ahead" integer DEFAULT 7 NOT NULL,
|
||||
"timezone" text DEFAULT 'America/Mexico_City' NOT NULL,
|
||||
"connector" text DEFAULT 'enlace-fijo' NOT NULL,
|
||||
"meeting_link" text,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "booking" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"kind" text DEFAULT 'session' NOT NULL,
|
||||
"status" text DEFAULT 'agendada' NOT NULL,
|
||||
"source" text DEFAULT 'manual' NOT NULL,
|
||||
"contact_id" text,
|
||||
"conversation_id" text,
|
||||
"lead_id" text,
|
||||
"scheduled_at" timestamp NOT NULL,
|
||||
"duration_minutes" integer NOT NULL,
|
||||
"connector" text,
|
||||
"external_ref" text,
|
||||
"meeting_link" text,
|
||||
"link_pending" boolean DEFAULT false NOT NULL,
|
||||
"is_test" boolean DEFAULT false NOT NULL,
|
||||
"notes" text,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "offered_slot" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"conversation_id" text NOT NULL,
|
||||
"start_utc" timestamp NOT NULL,
|
||||
"label" text NOT NULL,
|
||||
"offered_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "zoom_credentials" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"account_id" text NOT NULL,
|
||||
"client_id" text NOT NULL,
|
||||
"secret_cipher" text NOT NULL,
|
||||
"secret_iv" text NOT NULL,
|
||||
"secret_tag" text NOT NULL,
|
||||
"status" text DEFAULT 'connected' NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "google_credentials" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"client_id" text NOT NULL,
|
||||
"client_secret_cipher" text NOT NULL,
|
||||
"client_secret_iv" text NOT NULL,
|
||||
"client_secret_tag" text NOT NULL,
|
||||
"refresh_token_cipher" text NOT NULL,
|
||||
"refresh_token_iv" text NOT NULL,
|
||||
"refresh_token_tag" text NOT NULL,
|
||||
"calendar_id" text DEFAULT 'primary' NOT NULL,
|
||||
"status" text DEFAULT 'connected' NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "calendar_settings" ADD CONSTRAINT "calendar_settings_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "booking" ADD CONSTRAINT "booking_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "booking" ADD CONSTRAINT "booking_contact_id_contact_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contact"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "booking" ADD CONSTRAINT "booking_conversation_id_conversation_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversation"("id") ON DELETE set null ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "booking" ADD CONSTRAINT "booking_lead_id_lead_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."lead"("id") ON DELETE set null ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "offered_slot" ADD CONSTRAINT "offered_slot_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "offered_slot" ADD CONSTRAINT "offered_slot_conversation_id_conversation_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversation"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "zoom_credentials" ADD CONSTRAINT "zoom_credentials_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "google_credentials" ADD CONSTRAINT "google_credentials_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "calendar_settings_org_uq" ON "calendar_settings" USING btree ("organization_id");--> statement-breakpoint
|
||||
CREATE INDEX IF NOT EXISTS "booking_org_when_idx" ON "booking" USING btree ("organization_id","scheduled_at");--> statement-breakpoint
|
||||
CREATE INDEX IF NOT EXISTS "booking_org_status_idx" ON "booking" USING btree ("organization_id","status");--> statement-breakpoint
|
||||
|
||||
-- Anti doble-booking ATÓMICO: dos confirmaciones simultáneas sobre el mismo
|
||||
-- instante no pueden ganar las dos; la perdedora recibe un 23505 que el
|
||||
-- servicio traduce a `slot_taken` con alternativas frescas. Las citas de
|
||||
-- prueba del Laboratorio quedan fuera: no consumen la agenda real.
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "booking_org_active_slot_uq" ON "booking" USING btree ("organization_id","scheduled_at") WHERE "booking"."status" in ('agendada','realizada') and "booking"."is_test" = false;--> statement-breakpoint
|
||||
|
||||
CREATE INDEX IF NOT EXISTS "offered_slot_conv_idx" ON "offered_slot" USING btree ("conversation_id","start_utc");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "zoom_credentials_org_uq" ON "zoom_credentials" USING btree ("organization_id");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "google_credentials_org_uq" ON "google_credentials" USING btree ("organization_id");
|
||||
@@ -0,0 +1,82 @@
|
||||
-- 016 Atribucion de anuncios Click-to-WhatsApp y Conversions API de Meta
|
||||
-- (origen del anuncio, eventos reportados y la conexion del negocio).
|
||||
--
|
||||
-- Editada a mano sobre la generada para ser RE-EJECUTABLE (Constitucion IV):
|
||||
-- IF NOT EXISTS en tablas e indices, y DO-block en cada clave foranea.
|
||||
--
|
||||
-- Es puramente ADITIVA: no toca ninguna tabla existente, asi que aplicarla
|
||||
-- sobre una instancia con datos no puede perder nada. Se aplica SIEMPRE, con
|
||||
-- la bandera ATRIBUCION encendida o apagada: unas tablas vacias son inertes y
|
||||
-- a cambio todas las instancias comparten la misma estructura (ADR-001).
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "ad_attribution" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"contact_id" text NOT NULL,
|
||||
"conversation_id" text NOT NULL,
|
||||
"ctwa_clid" text,
|
||||
"source_id" text,
|
||||
"source_type" text,
|
||||
"source_url" text,
|
||||
"headline" text,
|
||||
"body" text,
|
||||
"media_type" text,
|
||||
"raw" jsonb NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "capi_settings" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"dataset_id" text NOT NULL,
|
||||
"token_cipher" text NOT NULL,
|
||||
"token_iv" text NOT NULL,
|
||||
"token_tag" text NOT NULL,
|
||||
"qualified_stage_id" text,
|
||||
"status" text DEFAULT 'connected' NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE IF NOT EXISTS "conversion_event" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"conversation_id" text NOT NULL,
|
||||
"attribution_id" text,
|
||||
"event_name" text NOT NULL,
|
||||
"status" text DEFAULT 'pending' NOT NULL,
|
||||
"error" text,
|
||||
"fb_trace_id" text,
|
||||
"sent_at" timestamp,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "ad_attribution" ADD CONSTRAINT "ad_attribution_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "ad_attribution" ADD CONSTRAINT "ad_attribution_contact_id_contact_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contact"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "ad_attribution" ADD CONSTRAINT "ad_attribution_conversation_id_conversation_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversation"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "capi_settings" ADD CONSTRAINT "capi_settings_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "capi_settings" ADD CONSTRAINT "capi_settings_qualified_stage_id_pipeline_stage_id_fk" FOREIGN KEY ("qualified_stage_id") REFERENCES "public"."pipeline_stage"("id") ON DELETE set null ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "conversion_event" ADD CONSTRAINT "conversion_event_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "conversion_event" ADD CONSTRAINT "conversion_event_conversation_id_conversation_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversation"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "conversion_event" ADD CONSTRAINT "conversion_event_attribution_id_ad_attribution_id_fk" FOREIGN KEY ("attribution_id") REFERENCES "public"."ad_attribution"("id") ON DELETE set null ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "ad_attribution_org_conversation_uq" ON "ad_attribution" USING btree ("organization_id","conversation_id");--> statement-breakpoint
|
||||
CREATE INDEX IF NOT EXISTS "ad_attribution_org_contact_idx" ON "ad_attribution" USING btree ("organization_id","contact_id");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "capi_settings_org_uq" ON "capi_settings" USING btree ("organization_id");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "conversion_event_org_conv_name_uq" ON "conversion_event" USING btree ("organization_id","conversation_id","event_name");--> statement-breakpoint
|
||||
CREATE INDEX IF NOT EXISTS "conversion_event_org_created_idx" ON "conversion_event" USING btree ("organization_id","created_at");
|
||||
@@ -0,0 +1,21 @@
|
||||
CREATE TABLE IF NOT EXISTS "messenger_credentials" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"page_id" text NOT NULL,
|
||||
"page_name" text,
|
||||
"token_cipher" text NOT NULL,
|
||||
"token_iv" text NOT NULL,
|
||||
"token_tag" text NOT NULL,
|
||||
"status" text DEFAULT 'connected' NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "messenger_credentials" ADD CONSTRAINT "messenger_credentials_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "messenger_credentials_org_uq" ON "messenger_credentials" USING btree ("organization_id");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "messenger_credentials_page_uq" ON "messenger_credentials" USING btree ("page_id");
|
||||
@@ -0,0 +1,5 @@
|
||||
ALTER TABLE "messenger_credentials" ALTER COLUMN "page_id" DROP NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "messenger_credentials" ADD COLUMN IF NOT EXISTS "source" text DEFAULT 'meta' NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "messenger_credentials" ADD COLUMN IF NOT EXISTS "account_ref" text;--> statement-breakpoint
|
||||
ALTER TABLE "messenger_credentials" ADD COLUMN IF NOT EXISTS "webhook_secret" text;--> statement-breakpoint
|
||||
CREATE INDEX IF NOT EXISTS "messenger_credentials_account_ref_idx" ON "messenger_credentials" USING btree ("account_ref");
|
||||
@@ -0,0 +1,17 @@
|
||||
-- 003/#51 - De donde viene el nombre de un contacto.
|
||||
--
|
||||
-- `perfil` = lo trajo WhatsApp y se mantiene al dia solo.
|
||||
-- `manual` = lo escribio una persona en el CRM y no se pisa nunca.
|
||||
--
|
||||
-- Las filas que ya existian quedan en 'perfil' A PROPOSITO, y tiene un coste
|
||||
-- que conviene saber: un contacto al que el operador renombro a mano ANTES de
|
||||
-- esta migracion se actualizara una vez con el nombre de su perfil de
|
||||
-- WhatsApp. Se elige asi porque la inmensa mayoria de los contactos se
|
||||
-- crearon con el nombre del perfil y nunca se tocaron: dejarlos en 'manual'
|
||||
-- haria que la correccion no arreglara el caso reportado para practicamente
|
||||
-- nadie. En cuanto alguien vuelva a editar un nombre, queda 'manual' para
|
||||
-- siempre.
|
||||
--
|
||||
-- Si se prefiere lo conservador, basta correr esto ANTES de desplegar:
|
||||
-- UPDATE "contact" SET "name_source" = 'manual';
|
||||
ALTER TABLE "contact" ADD COLUMN "name_source" text DEFAULT 'perfil' NOT NULL;
|
||||
@@ -0,0 +1,17 @@
|
||||
-- 018 - De que anuncio llego cada conversacion: la imagen del creativo.
|
||||
--
|
||||
-- Editada a mano sobre la generada para ser RE-EJECUTABLE (Constitucion IV):
|
||||
-- IF NOT EXISTS en columna e indice, y bloque DO en la clave foranea.
|
||||
--
|
||||
-- Puramente ADITIVA: una columna nula y un indice sobre ad_attribution (016).
|
||||
-- Las filas que ya existen quedan sin imagen; la app la repara al abrir el
|
||||
-- contacto con la URL que ya guarda su raw, mientras Meta no la caduque.
|
||||
--
|
||||
-- Mismos nombres de columna, clave foranea e indice que la 0023 de Vocero
|
||||
-- Cloud (spec 212): los dos repos comparten la forma de la tabla.
|
||||
|
||||
ALTER TABLE "ad_attribution" ADD COLUMN IF NOT EXISTS "image_asset_id" text;--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "ad_attribution" ADD CONSTRAINT "ad_attribution_image_asset_id_media_asset_id_fk" FOREIGN KEY ("image_asset_id") REFERENCES "public"."media_asset"("id") ON DELETE set null ON UPDATE no action;
|
||||
EXCEPTION WHEN duplicate_object THEN null; END $$;--> statement-breakpoint
|
||||
CREATE INDEX IF NOT EXISTS "ad_attribution_org_source_idx" ON "ad_attribution" USING btree ("organization_id","source_id");
|
||||
@@ -0,0 +1,19 @@
|
||||
CREATE TABLE "ai_credentials" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"provider" text DEFAULT 'openrouter' NOT NULL,
|
||||
"base_url" text NOT NULL,
|
||||
"model" text NOT NULL,
|
||||
"token_cipher" text NOT NULL,
|
||||
"token_iv" text NOT NULL,
|
||||
"token_tag" text NOT NULL,
|
||||
"token_last4" text NOT NULL,
|
||||
"status" text DEFAULT 'active' NOT NULL,
|
||||
"status_reason" text,
|
||||
"status_changed_at" timestamp,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "ai_credentials" ADD CONSTRAINT "ai_credentials_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "ai_credentials_org_uq" ON "ai_credentials" USING btree ("organization_id");
|
||||
@@ -0,0 +1,20 @@
|
||||
CREATE TABLE "dispatch" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"conversation_id" text NOT NULL,
|
||||
"status" text DEFAULT 'pendiente' NOT NULL,
|
||||
"attempts" integer DEFAULT 0 NOT NULL,
|
||||
"not_before" timestamp DEFAULT now() NOT NULL,
|
||||
"first_message_at" timestamp NOT NULL,
|
||||
"last_message_at" timestamp NOT NULL,
|
||||
"leased_until" timestamp,
|
||||
"last_error" text,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "dispatch" ADD CONSTRAINT "dispatch_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "dispatch" ADD CONSTRAINT "dispatch_conversation_id_conversation_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversation"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "dispatch_conversation_vivo_uq" ON "dispatch" USING btree ("conversation_id") WHERE "dispatch"."status" in ('pendiente', 'en_vuelo');--> statement-breakpoint
|
||||
CREATE INDEX "dispatch_org_status_updated_idx" ON "dispatch" USING btree ("organization_id","status","updated_at");--> statement-breakpoint
|
||||
CREATE INDEX "dispatch_status_notbefore_idx" ON "dispatch" USING btree ("status","not_before");
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE "dispatch" ADD COLUMN "message_ids" jsonb;
|
||||
@@ -0,0 +1,18 @@
|
||||
CREATE TABLE "tiktok_credentials" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"source" text DEFAULT 'zernio' NOT NULL,
|
||||
"account_ref" text NOT NULL,
|
||||
"username" text,
|
||||
"token_cipher" text NOT NULL,
|
||||
"token_iv" text NOT NULL,
|
||||
"token_tag" text NOT NULL,
|
||||
"webhook_secret" text,
|
||||
"status" text DEFAULT 'connected' NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "tiktok_credentials" ADD CONSTRAINT "tiktok_credentials_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "tiktok_credentials_org_uq" ON "tiktok_credentials" USING btree ("organization_id");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "tiktok_credentials_account_ref_uq" ON "tiktok_credentials" USING btree ("account_ref");
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE "contact" ADD COLUMN "ai_paused" boolean DEFAULT false NOT NULL;
|
||||
@@ -0,0 +1,9 @@
|
||||
ALTER TABLE "instagram_credentials" ADD COLUMN "webhook_secret_cipher" text;--> statement-breakpoint
|
||||
ALTER TABLE "instagram_credentials" ADD COLUMN "webhook_secret_iv" text;--> statement-breakpoint
|
||||
ALTER TABLE "instagram_credentials" ADD COLUMN "webhook_secret_tag" text;--> statement-breakpoint
|
||||
ALTER TABLE "messenger_credentials" ADD COLUMN "webhook_secret_cipher" text;--> statement-breakpoint
|
||||
ALTER TABLE "messenger_credentials" ADD COLUMN "webhook_secret_iv" text;--> statement-breakpoint
|
||||
ALTER TABLE "messenger_credentials" ADD COLUMN "webhook_secret_tag" text;--> statement-breakpoint
|
||||
ALTER TABLE "tiktok_credentials" ADD COLUMN "webhook_secret_cipher" text;--> statement-breakpoint
|
||||
ALTER TABLE "tiktok_credentials" ADD COLUMN "webhook_secret_iv" text;--> statement-breakpoint
|
||||
ALTER TABLE "tiktok_credentials" ADD COLUMN "webhook_secret_tag" text;
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE "agent_profile" ADD COLUMN "role" text DEFAULT 'calificador' NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "agent_profile" ADD COLUMN "tools" jsonb;
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE "message" ADD COLUMN IF NOT EXISTS "reply_to_wa_message_id" text;
|
||||
@@ -0,0 +1,20 @@
|
||||
CREATE TABLE "webhook_event" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"fuente" text NOT NULL,
|
||||
"payload" jsonb NOT NULL,
|
||||
"huella" text NOT NULL,
|
||||
"status" text DEFAULT 'pendiente' NOT NULL,
|
||||
"attempts" integer DEFAULT 0 NOT NULL,
|
||||
"not_before" timestamp DEFAULT now() NOT NULL,
|
||||
"leased_until" timestamp,
|
||||
"last_error" text,
|
||||
"processed_at" timestamp,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "webhook_event" ADD CONSTRAINT "webhook_event_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "webhook_event_huella_uq" ON "webhook_event" USING btree ("huella");--> statement-breakpoint
|
||||
CREATE INDEX "webhook_event_barrido_idx" ON "webhook_event" USING btree ("status","not_before") WHERE "webhook_event"."status" in ('pendiente', 'en_proceso');--> statement-breakpoint
|
||||
CREATE INDEX "webhook_event_org_status_idx" ON "webhook_event" USING btree ("organization_id","status");
|
||||
@@ -0,0 +1,11 @@
|
||||
CREATE TABLE "quick_reply" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"atajo" text NOT NULL,
|
||||
"texto" text NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "quick_reply" ADD CONSTRAINT "quick_reply_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "quick_reply_org_atajo_uq" ON "quick_reply" USING btree ("organization_id","atajo");
|
||||
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE "conversation_note" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"conversation_id" text NOT NULL,
|
||||
"author_user_id" text,
|
||||
"texto" text NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "conversation_note" ADD CONSTRAINT "conversation_note_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "conversation_note" ADD CONSTRAINT "conversation_note_conversation_id_conversation_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversation"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "conversation_note" ADD CONSTRAINT "conversation_note_author_user_id_user_id_fk" FOREIGN KEY ("author_user_id") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE INDEX "conversation_note_conv_idx" ON "conversation_note" USING btree ("organization_id","conversation_id","created_at");
|
||||
@@ -0,0 +1,3 @@
|
||||
-- 065: la Cuenta de mensajes del split de 2026 (plantillas y pago). Aditiva y
|
||||
-- anulable: una instancia con una sola ID sigue igual.
|
||||
ALTER TABLE "meta_credentials" ADD COLUMN IF NOT EXISTS "messaging_account_id" text;
|
||||
@@ -0,0 +1,65 @@
|
||||
CREATE TABLE "api_key" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"prefix" text NOT NULL,
|
||||
"last4" text NOT NULL,
|
||||
"hash" text NOT NULL,
|
||||
"scopes" text[] NOT NULL,
|
||||
"created_by_user_id" text NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"last_used_at" timestamp,
|
||||
"request_count" integer DEFAULT 0 NOT NULL,
|
||||
"revoked_at" timestamp
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "oauth_client" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"redirect_uris" text[] NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "oauth_grant" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"client_id" text NOT NULL,
|
||||
"user_id" text NOT NULL,
|
||||
"scopes" text[] NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"last_used_at" timestamp,
|
||||
"request_count" integer DEFAULT 0 NOT NULL,
|
||||
"revoked_at" timestamp
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "oauth_token" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"grant_id" text NOT NULL,
|
||||
"kind" text NOT NULL,
|
||||
"hash" text NOT NULL,
|
||||
"scopes" text[] NOT NULL,
|
||||
"code_challenge" text,
|
||||
"redirect_uri" text,
|
||||
"expires_at" timestamp NOT NULL,
|
||||
"used_at" timestamp,
|
||||
"revoked_at" timestamp,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "api_key" ADD CONSTRAINT "api_key_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "api_key" ADD CONSTRAINT "api_key_created_by_user_id_user_id_fk" FOREIGN KEY ("created_by_user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "oauth_client" ADD CONSTRAINT "oauth_client_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "oauth_grant" ADD CONSTRAINT "oauth_grant_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "oauth_grant" ADD CONSTRAINT "oauth_grant_client_id_oauth_client_id_fk" FOREIGN KEY ("client_id") REFERENCES "public"."oauth_client"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "oauth_grant" ADD CONSTRAINT "oauth_grant_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "oauth_token" ADD CONSTRAINT "oauth_token_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "oauth_token" ADD CONSTRAINT "oauth_token_grant_id_oauth_grant_id_fk" FOREIGN KEY ("grant_id") REFERENCES "public"."oauth_grant"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "api_key_hash_uq" ON "api_key" USING btree ("hash");--> statement-breakpoint
|
||||
CREATE INDEX "api_key_org_idx" ON "api_key" USING btree ("organization_id");--> statement-breakpoint
|
||||
CREATE INDEX "oauth_client_org_idx" ON "oauth_client" USING btree ("organization_id");--> statement-breakpoint
|
||||
CREATE INDEX "oauth_grant_org_idx" ON "oauth_grant" USING btree ("organization_id");--> statement-breakpoint
|
||||
CREATE INDEX "oauth_grant_client_user_idx" ON "oauth_grant" USING btree ("client_id","user_id");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "oauth_token_hash_uq" ON "oauth_token" USING btree ("hash");--> statement-breakpoint
|
||||
CREATE INDEX "oauth_token_grant_idx" ON "oauth_token" USING btree ("grant_id");
|
||||
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE "conversation_pin" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"user_id" text NOT NULL,
|
||||
"conversation_id" text NOT NULL,
|
||||
"pinned_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "conversation_pin" ADD CONSTRAINT "conversation_pin_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "conversation_pin" ADD CONSTRAINT "conversation_pin_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "conversation_pin" ADD CONSTRAINT "conversation_pin_conversation_id_conversation_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversation"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "conversation_pin_user_conv_uq" ON "conversation_pin" USING btree ("user_id","conversation_id");--> statement-breakpoint
|
||||
CREATE INDEX "conversation_pin_org_user_idx" ON "conversation_pin" USING btree ("organization_id","user_id");
|
||||
@@ -0,0 +1,18 @@
|
||||
CREATE TABLE "message_reaction" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"conversation_id" text NOT NULL,
|
||||
"message_id" text NOT NULL,
|
||||
"actor" text NOT NULL,
|
||||
"user_id" text,
|
||||
"emoji" text NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "message_reaction" ADD CONSTRAINT "message_reaction_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "message_reaction" ADD CONSTRAINT "message_reaction_conversation_id_conversation_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversation"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "message_reaction" ADD CONSTRAINT "message_reaction_message_id_message_id_fk" FOREIGN KEY ("message_id") REFERENCES "public"."message"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "message_reaction" ADD CONSTRAINT "message_reaction_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "message_reaction_message_actor_uq" ON "message_reaction" USING btree ("message_id","actor");--> statement-breakpoint
|
||||
CREATE INDEX "message_reaction_conv_idx" ON "message_reaction" USING btree ("organization_id","conversation_id");
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE "message" ADD COLUMN "edited_at" timestamp;--> statement-breakpoint
|
||||
ALTER TABLE "message" ADD COLUMN "original_text" text;
|
||||
@@ -0,0 +1,5 @@
|
||||
ALTER TABLE "dispatch" ADD COLUMN "relaunch_count" integer DEFAULT 0 NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "dispatch" ADD COLUMN "safety_net_at" timestamp;--> statement-breakpoint
|
||||
-- Lo entregado antes de esta versión ya no lo revisa la red de seguridad: al actualizar no se relanza ningún turno viejo.
|
||||
UPDATE "dispatch" SET "safety_net_at" = now() WHERE "status" = 'entregado' AND "safety_net_at" IS NULL;--> statement-breakpoint
|
||||
CREATE INDEX "dispatch_red_seguridad_idx" ON "dispatch" USING btree ("updated_at") WHERE "dispatch"."status" = 'entregado' and "dispatch"."safety_net_at" is null;
|
||||
@@ -0,0 +1,30 @@
|
||||
CREATE TABLE "ia_vuelta" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"conversation_id" text NOT NULL,
|
||||
"motivo" text NOT NULL,
|
||||
"horas" integer,
|
||||
"pausada_desde" timestamp,
|
||||
"motivo_pausa" text,
|
||||
"occurred_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "agent_profile" ADD COLUMN "reactivate_after_hours" integer;--> statement-breakpoint
|
||||
ALTER TABLE "conversation" ADD COLUMN "ai_paused_until" timestamp;--> statement-breakpoint
|
||||
ALTER TABLE "conversation" ADD COLUMN "last_team_out_at" timestamp;--> statement-breakpoint
|
||||
ALTER TABLE "ia_vuelta" ADD CONSTRAINT "ia_vuelta_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "ia_vuelta" ADD CONSTRAINT "ia_vuelta_conversation_id_conversation_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."conversation"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE INDEX "ia_vuelta_org_conv_idx" ON "ia_vuelta" USING btree ("organization_id","conversation_id","occurred_at");--> statement-breakpoint
|
||||
-- 076: la última salida del equipo en las conversaciones que ya existían.
|
||||
-- Desde aquí la anota quien envía; para lo de antes se toma cualquier saliente
|
||||
-- que no escribió la IA. Cuenta de más (un recordatorio pasa por persona), y
|
||||
-- es a propósito: equivocarse así solo retrasa que la IA vuelva.
|
||||
UPDATE "conversation" AS c
|
||||
SET "last_team_out_at" = s.ultimo
|
||||
FROM (
|
||||
SELECT "organization_id", "conversation_id", max("created_at") AS ultimo
|
||||
FROM "message"
|
||||
WHERE "direction" = 'out' AND "ai_generated" = false AND "status" <> 'failed'
|
||||
GROUP BY "organization_id", "conversation_id"
|
||||
) AS s
|
||||
WHERE c."organization_id" = s."organization_id" AND c."id" = s."conversation_id";
|
||||
@@ -0,0 +1,53 @@
|
||||
CREATE TABLE "custom_field" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"target_entity" text DEFAULT 'contact' NOT NULL,
|
||||
"key" text NOT NULL,
|
||||
"label" text NOT NULL,
|
||||
"field_type" text NOT NULL,
|
||||
"options" jsonb DEFAULT '[]'::jsonb NOT NULL,
|
||||
"required" boolean DEFAULT false NOT NULL,
|
||||
"position" integer DEFAULT 0 NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "custom_field_value" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"field_id" text NOT NULL,
|
||||
"entity_id" text NOT NULL,
|
||||
"value" jsonb,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "service_item" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"organization_id" text NOT NULL,
|
||||
"category" text NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"description" text,
|
||||
"specifications" text,
|
||||
"pricing_type" text DEFAULT 'quote_required' NOT NULL,
|
||||
"price_cents" integer,
|
||||
"currency" text DEFAULT 'MXN' NOT NULL,
|
||||
"unit_label" text,
|
||||
"delivery_time" text,
|
||||
"is_active" boolean DEFAULT true NOT NULL,
|
||||
"position" integer DEFAULT 0 NOT NULL,
|
||||
"created_at" timestamp DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp DEFAULT now() NOT NULL,
|
||||
CONSTRAINT "service_item_price_ck" CHECK ("service_item"."price_cents" is null or "service_item"."price_cents" >= 0)
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "contact" ADD COLUMN "tags" jsonb DEFAULT '[]'::jsonb NOT NULL;--> statement-breakpoint
|
||||
ALTER TABLE "pipeline_stage" ADD COLUMN "capi_event" text;--> statement-breakpoint
|
||||
ALTER TABLE "custom_field" ADD CONSTRAINT "custom_field_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "custom_field_value" ADD CONSTRAINT "custom_field_value_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "custom_field_value" ADD CONSTRAINT "custom_field_value_field_id_custom_field_id_fk" FOREIGN KEY ("field_id") REFERENCES "public"."custom_field"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "service_item" ADD CONSTRAINT "service_item_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "custom_field_org_entity_key_uq" ON "custom_field" USING btree ("organization_id","target_entity","key");--> statement-breakpoint
|
||||
CREATE INDEX "custom_field_org_pos_idx" ON "custom_field" USING btree ("organization_id","position");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "custom_field_value_field_entity_uq" ON "custom_field_value" USING btree ("field_id","entity_id");--> statement-breakpoint
|
||||
CREATE INDEX "custom_field_value_org_entity_idx" ON "custom_field_value" USING btree ("organization_id","entity_id");--> statement-breakpoint
|
||||
CREATE INDEX "service_item_org_pos_idx" ON "service_item" USING btree ("organization_id","position");
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,244 @@
|
||||
{
|
||||
"version": "7",
|
||||
"dialect": "postgresql",
|
||||
"entries": [
|
||||
{
|
||||
"idx": 0,
|
||||
"version": "7",
|
||||
"when": 1783657511753,
|
||||
"tag": "0000_absurd_the_santerians",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 1,
|
||||
"version": "7",
|
||||
"when": 1785464517783,
|
||||
"tag": "0001_old_sabra",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 2,
|
||||
"version": "7",
|
||||
"when": 1785872764254,
|
||||
"tag": "0002_closed_millenium_guard",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 3,
|
||||
"version": "7",
|
||||
"when": 1786835958011,
|
||||
"tag": "0003_flippant_mastermind",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 4,
|
||||
"version": "7",
|
||||
"when": 1786849014992,
|
||||
"tag": "0004_reflective_the_enforcers",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 5,
|
||||
"version": "7",
|
||||
"when": 1786850050625,
|
||||
"tag": "0005_glossy_excalibur",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 6,
|
||||
"version": "7",
|
||||
"when": 1786855153169,
|
||||
"tag": "0006_keen_thor_girl",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 7,
|
||||
"version": "7",
|
||||
"when": 1786856261073,
|
||||
"tag": "0007_confused_zaran",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 8,
|
||||
"version": "7",
|
||||
"when": 1787000000000,
|
||||
"tag": "0008_canal_instagram",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 9,
|
||||
"version": "7",
|
||||
"when": 1787796724315,
|
||||
"tag": "0009_motor_agenda",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 10,
|
||||
"version": "7",
|
||||
"when": 1787960052077,
|
||||
"tag": "0010_atribucion_capi",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 11,
|
||||
"version": "7",
|
||||
"when": 1788309200954,
|
||||
"tag": "0011_canal_messenger",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 12,
|
||||
"version": "7",
|
||||
"when": 1788310823966,
|
||||
"tag": "0012_messenger_zernio",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 13,
|
||||
"version": "7",
|
||||
"when": 1788731118597,
|
||||
"tag": "0013_nombre_del_contacto",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 14,
|
||||
"version": "7",
|
||||
"when": 1790048470717,
|
||||
"tag": "0014_anuncio_de_origen",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 15,
|
||||
"version": "7",
|
||||
"when": 1791426906573,
|
||||
"tag": "0015_token_de_ia",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 16,
|
||||
"version": "7",
|
||||
"when": 1791469451742,
|
||||
"tag": "0016_despacho_al_cerebro",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 17,
|
||||
"version": "7",
|
||||
"when": 1791914626123,
|
||||
"tag": "0017_dispatch_message_ids",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 18,
|
||||
"version": "7",
|
||||
"when": 1791918147335,
|
||||
"tag": "0018_canal_tiktok",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 19,
|
||||
"version": "7",
|
||||
"when": 1791947815503,
|
||||
"tag": "0019_contact_ai_paused",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 20,
|
||||
"version": "7",
|
||||
"when": 1791987903930,
|
||||
"tag": "0020_secreto_zernio_cifrado",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 21,
|
||||
"version": "7",
|
||||
"when": 1792517498460,
|
||||
"tag": "0021_roles_como_datos",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 22,
|
||||
"version": "7",
|
||||
"when": 1792608627646,
|
||||
"tag": "0022_responder_citando",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 23,
|
||||
"version": "7",
|
||||
"when": 1792758207285,
|
||||
"tag": "0023_webhook_event",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 24,
|
||||
"version": "7",
|
||||
"when": 1792778191260,
|
||||
"tag": "0024_quick_reply",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 25,
|
||||
"version": "7",
|
||||
"when": 1792779395979,
|
||||
"tag": "0025_conversation_note",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 26,
|
||||
"version": "7",
|
||||
"when": 1793222749562,
|
||||
"tag": "0026_cuenta_de_mensajes",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 27,
|
||||
"version": "7",
|
||||
"when": 1795471203210,
|
||||
"tag": "0027_api_v1_mcp",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 28,
|
||||
"version": "7",
|
||||
"when": 1795479066722,
|
||||
"tag": "0028_fijar_chats",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 29,
|
||||
"version": "7",
|
||||
"when": 1795523513693,
|
||||
"tag": "0029_reacciones",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 30,
|
||||
"version": "7",
|
||||
"when": 1795525506312,
|
||||
"tag": "0030_que_no_se_pierda_nada",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 31,
|
||||
"version": "7",
|
||||
"when": 1795526831299,
|
||||
"tag": "0031_una_respuesta",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 32,
|
||||
"version": "7",
|
||||
"when": 1795545644836,
|
||||
"tag": "0032_la_ia_vuelve_sola",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 33,
|
||||
"version": "7",
|
||||
"when": 1796406172977,
|
||||
"tag": "0033_servicios_campos",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in new issue
Block a user