From f2b19ab719a4ccb183ee0bd501ec1304633e9a31 Mon Sep 17 00:00:00 2001 From: Carlos Narro Date: Thu, 4 Jun 2026 16:45:14 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1ade=20estructura=20aditiva=20del=20flujo?= =?UTF-8?q?=20WhatsApp/llamada=20+=20workers=20(DB=20=C3=BAnica)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Integra el esquema "reformix-full" del equipo de forma ADITIVA, sin tocar los enums ni columnas existentes de la app (una sola DB, Drizzle es el dueño): - Enums nuevos: estado_wa, canal_contacto, canal_origen, resultado_contacto, rol_mensaje, job_tipo, job_estado, nivel_calificacion, visita_estado. - Tablas nuevas: conversacion_whatsapp, intentos_contacto, lead_calificacion (score 0-100 + nivel A/B/C/D), visitas, worker_jobs (cola async de los workers de fotos/render/presupuesto). Referencian nuestros leads/users/tenants. - Columnas nuevas en leads (nullable, las rellena el bot/Luisa): estado_wa, canal_origen, espacio, rango_m2, estilo, presupuesto_declarado, viable, fotos_solicitadas_at. - Migración 0010 + db-schema/schema.sql regenerado. El bot/n8n escribe estas tablas en la DB única y usa nuestros leads (creados solo desde el form web). Pendiente: alinear valores de lead_estado/pipeline_stage. Co-Authored-By: Claude Opus 4.8 --- mvp/b2c/db-schema/schema.sql | 101 +- mvp/b2c/drizzle/0010_square_vulture.sql | 96 + mvp/b2c/drizzle/meta/0010_snapshot.json | 2446 +++++++++++++++++++++++ mvp/b2c/drizzle/meta/_journal.json | 7 + mvp/b2c/src/db/schema.ts | 164 ++ 5 files changed, 2813 insertions(+), 1 deletion(-) create mode 100644 mvp/b2c/drizzle/0010_square_vulture.sql create mode 100644 mvp/b2c/drizzle/meta/0010_snapshot.json diff --git a/mvp/b2c/db-schema/schema.sql b/mvp/b2c/db-schema/schema.sql index a07b17a..6a1aa91 100644 --- a/mvp/b2c/db-schema/schema.sql +++ b/mvp/b2c/db-schema/schema.sql @@ -1,9 +1,17 @@ CREATE TYPE "public"."calidad" AS ENUM('basica', 'media', 'premium'); +CREATE TYPE "public"."canal_contacto" AS ENUM('formulario', 'whatsapp', 'llamada'); +CREATE TYPE "public"."canal_origen" AS ENUM('formulario_web', 'whatsapp', 'llamada', 'referido', 'anuncio'); CREATE TYPE "public"."categoria_material" AS ENUM('suelo', 'pared', 'pintura', 'mobiliario'); CREATE TYPE "public"."envio_presupuesto_mode" AS ENUM('automatico', 'revision'); +CREATE TYPE "public"."estado_wa" AS ENUM('sin_enviar', 'enviado', 'entregado', 'leido', 'fallido'); CREATE TYPE "public"."foto_momento" AS ENUM('antes', 'despues'); +CREATE TYPE "public"."job_estado" AS ENUM('pendiente', 'procesando', 'completado', 'error'); +CREATE TYPE "public"."job_tipo" AS ENUM('analisis_fotos', 'render', 'presupuesto_ia'); CREATE TYPE "public"."lead_estado" AS ENUM('nuevo', 'contactado', 'visita_agendada', 'presupuesto_enviado', 'ganado', 'perdido'); +CREATE TYPE "public"."nivel_calificacion" AS ENUM('A', 'B', 'C', 'D'); CREATE TYPE "public"."pipeline_stage" AS ENUM('form_completado', 'fotos_subidas', 'prellamada_enviada', 'llamada_completada', 'render_generado', 'presupuesto_generado', 'whatsapp_entregado'); +CREATE TYPE "public"."resultado_contacto" AS ENUM('exitoso', 'no_contesta', 'ocupado', 'rechaza', 'error_tecnico'); +CREATE TYPE "public"."rol_mensaje" AS ENUM('user', 'assistant', 'system'); CREATE TYPE "public"."subscription_status" AS ENUM('trial', 'activo', 'cancelado', 'vencido'); CREATE TYPE "public"."testimonio_estado" AS ENUM('pendiente', 'publicado', 'oculto'); CREATE TYPE "public"."tipo_reforma" AS ENUM('cocina', 'bano', 'salon', 'comedor', 'integral', 'otro'); @@ -11,6 +19,7 @@ CREATE TYPE "public"."unidad_medida" AS ENUM('m2', 'ml', 'ud'); CREATE TYPE "public"."urgencia" AS ENUM('alta', 'media', 'baja'); CREATE TYPE "public"."user_role" AS ENUM('reformista', 'admin'); CREATE TYPE "public"."user_status" AS ENUM('activo', 'deshabilitado'); +CREATE TYPE "public"."visita_estado" AS ENUM('propuesta', 'confirmada', 'realizada', 'cancelada', 'reprogramada'); CREATE TABLE "catalog_items" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "tenant_id" uuid NOT NULL, @@ -24,6 +33,17 @@ CREATE TABLE "catalog_items" ( "sku" text NOT NULL ); +CREATE TABLE "conversacion_whatsapp" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "lead_id" uuid NOT NULL, + "rol" "rol_mensaje" NOT NULL, + "mensaje" text NOT NULL, + "media_type" text, + "media_url" text, + "transcripcion_audio" text, + "created_at" timestamp with time zone DEFAULT now() NOT NULL +); + CREATE TABLE "galeria_fotos" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "tenant_id" uuid NOT NULL, @@ -33,6 +53,32 @@ CREATE TABLE "galeria_fotos" ( "created_at" timestamp with time zone DEFAULT now() NOT NULL ); +CREATE TABLE "intentos_contacto" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "lead_id" uuid NOT NULL, + "canal" "canal_contacto" NOT NULL, + "resultado" "resultado_contacto", + "completado" boolean DEFAULT false NOT NULL, + "numero_intento" integer NOT NULL, + "duracion_seg" integer, + "intentado_at" timestamp with time zone DEFAULT now() NOT NULL, + "notas" text, + "metadata" jsonb +); + +CREATE TABLE "lead_calificacion" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "lead_id" uuid NOT NULL, + "score" integer, + "nivel" "nivel_calificacion", + "criterios" jsonb, + "notas_agente" text, + "calificado_por" uuid, + "calificado_at" timestamp with time zone DEFAULT now() NOT NULL, + CONSTRAINT "lead_calificacion_lead_id_unique" UNIQUE("lead_id"), + CONSTRAINT "lead_calificacion_score_check" CHECK ("lead_calificacion"."score" >= 0 AND "lead_calificacion"."score" <= 100) +); + CREATE TABLE "lead_estado_history" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "lead_id" uuid NOT NULL, @@ -94,12 +140,22 @@ CREATE TABLE "leads" ( "altura_techo" double precision, "calidad_global" "calidad", "estructural" boolean DEFAULT false NOT NULL, + "anterior_a_2000" boolean DEFAULT false NOT NULL, + "cambio_distribucion" boolean DEFAULT false NOT NULL, "material_selections" jsonb DEFAULT '{}'::jsonb NOT NULL, "desglose_snapshot" jsonb, "urgencia" "urgencia", "presupuesto_target" integer, "taste_text" text, - "preferences_snapshot" jsonb + "preferences_snapshot" jsonb, + "estado_wa" "estado_wa", + "canal_origen" "canal_origen", + "espacio" text, + "rango_m2" text, + "estilo" text, + "presupuesto_declarado" text, + "viable" boolean, + "fotos_solicitadas_at" timestamp with time zone ); CREATE TABLE "plans" ( @@ -128,6 +184,7 @@ CREATE TABLE "pricing_config" ( "altura_techo_default" double precision DEFAULT 2.5 NOT NULL, "factor_zona" jsonb DEFAULT '{}'::jsonb NOT NULL, "mano_obra" jsonb DEFAULT '{}'::jsonb NOT NULL, + "extras" jsonb DEFAULT '{"tuberias":0,"boletin":0,"distribucion":0}'::jsonb NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "pricing_config_tenant_id_unique" UNIQUE("tenant_id") ); @@ -203,8 +260,38 @@ CREATE TABLE "users" ( CONSTRAINT "users_email_unique" UNIQUE("email") ); +CREATE TABLE "visitas" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "lead_id" uuid NOT NULL, + "tenant_id" uuid NOT NULL, + "fecha_propuesta" timestamp with time zone, + "fecha_confirmada" timestamp with time zone, + "estado" "visita_estado" DEFAULT 'propuesta' NOT NULL, + "direccion" text, + "notas" text, + "created_at" timestamp with time zone DEFAULT now() NOT NULL +); + +CREATE TABLE "worker_jobs" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "lead_id" uuid NOT NULL, + "tipo" "job_tipo" NOT NULL, + "estado_job" "job_estado" DEFAULT 'pendiente' NOT NULL, + "payload" jsonb NOT NULL, + "webhook_url" text, + "resultado_url" text, + "intentos" integer DEFAULT 0 NOT NULL, + "error_msg" text, + "created_at" timestamp with time zone DEFAULT now() NOT NULL, + "completed_at" timestamp with time zone +); + ALTER TABLE "catalog_items" ADD CONSTRAINT "catalog_items_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE cascade ON UPDATE no action; +ALTER TABLE "conversacion_whatsapp" ADD CONSTRAINT "conversacion_whatsapp_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action; ALTER TABLE "galeria_fotos" ADD CONSTRAINT "galeria_fotos_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE cascade ON UPDATE no action; +ALTER TABLE "intentos_contacto" ADD CONSTRAINT "intentos_contacto_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action; +ALTER TABLE "lead_calificacion" ADD CONSTRAINT "lead_calificacion_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action; +ALTER TABLE "lead_calificacion" ADD CONSTRAINT "lead_calificacion_calificado_por_users_id_fk" FOREIGN KEY ("calificado_por") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action; ALTER TABLE "lead_estado_history" ADD CONSTRAINT "lead_estado_history_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action; ALTER TABLE "lead_fotos" ADD CONSTRAINT "lead_fotos_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action; ALTER TABLE "lead_notas" ADD CONSTRAINT "lead_notas_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action; @@ -218,12 +305,24 @@ ALTER TABLE "testimonio_fotos" ADD CONSTRAINT "testimonio_fotos_testimonio_id_te ALTER TABLE "testimonios" ADD CONSTRAINT "testimonios_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE cascade ON UPDATE no action; ALTER TABLE "testimonios" ADD CONSTRAINT "testimonios_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE set null ON UPDATE no action; ALTER TABLE "users" ADD CONSTRAINT "users_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE cascade ON UPDATE no action; +ALTER TABLE "visitas" ADD CONSTRAINT "visitas_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action; +ALTER TABLE "visitas" ADD CONSTRAINT "visitas_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE cascade ON UPDATE no action; +ALTER TABLE "worker_jobs" ADD CONSTRAINT "worker_jobs_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action; CREATE INDEX "catalog_tenant_idx" ON "catalog_items" USING btree ("tenant_id"); CREATE UNIQUE INDEX "catalog_tenant_sku_idx" ON "catalog_items" USING btree ("tenant_id","sku"); +CREATE INDEX "idx_conversacion_whatsapp_lead_id" ON "conversacion_whatsapp" USING btree ("lead_id"); +CREATE INDEX "idx_conversacion_whatsapp_created_at" ON "conversacion_whatsapp" USING btree ("created_at"); CREATE INDEX "galeria_tenant_idx" ON "galeria_fotos" USING btree ("tenant_id"); +CREATE INDEX "idx_intentos_contacto_lead_id" ON "intentos_contacto" USING btree ("lead_id"); +CREATE INDEX "idx_lead_calificacion_lead_id" ON "lead_calificacion" USING btree ("lead_id"); CREATE INDEX "leads_tenant_created_idx" ON "leads" USING btree ("tenant_id","created_at"); CREATE INDEX "leads_estado_idx" ON "leads" USING btree ("estado"); CREATE INDEX "sessions_user_idx" ON "sessions" USING btree ("user_id"); CREATE INDEX "testimonios_tenant_estado_idx" ON "testimonios" USING btree ("tenant_id","estado"); CREATE INDEX "testimonios_lead_idx" ON "testimonios" USING btree ("lead_id"); CREATE INDEX "users_tenant_idx" ON "users" USING btree ("tenant_id"); +CREATE INDEX "idx_visitas_lead_id" ON "visitas" USING btree ("lead_id"); +CREATE INDEX "idx_visitas_tenant_id" ON "visitas" USING btree ("tenant_id"); +CREATE INDEX "idx_worker_jobs_lead_id" ON "worker_jobs" USING btree ("lead_id"); +CREATE INDEX "idx_worker_jobs_estado" ON "worker_jobs" USING btree ("estado_job"); +CREATE INDEX "idx_worker_jobs_tipo" ON "worker_jobs" USING btree ("tipo"); diff --git a/mvp/b2c/drizzle/0010_square_vulture.sql b/mvp/b2c/drizzle/0010_square_vulture.sql new file mode 100644 index 0000000..98eb61c --- /dev/null +++ b/mvp/b2c/drizzle/0010_square_vulture.sql @@ -0,0 +1,96 @@ +CREATE TYPE "public"."canal_contacto" AS ENUM('formulario', 'whatsapp', 'llamada');--> statement-breakpoint +CREATE TYPE "public"."canal_origen" AS ENUM('formulario_web', 'whatsapp', 'llamada', 'referido', 'anuncio');--> statement-breakpoint +CREATE TYPE "public"."estado_wa" AS ENUM('sin_enviar', 'enviado', 'entregado', 'leido', 'fallido');--> statement-breakpoint +CREATE TYPE "public"."job_estado" AS ENUM('pendiente', 'procesando', 'completado', 'error');--> statement-breakpoint +CREATE TYPE "public"."job_tipo" AS ENUM('analisis_fotos', 'render', 'presupuesto_ia');--> statement-breakpoint +CREATE TYPE "public"."nivel_calificacion" AS ENUM('A', 'B', 'C', 'D');--> statement-breakpoint +CREATE TYPE "public"."resultado_contacto" AS ENUM('exitoso', 'no_contesta', 'ocupado', 'rechaza', 'error_tecnico');--> statement-breakpoint +CREATE TYPE "public"."rol_mensaje" AS ENUM('user', 'assistant', 'system');--> statement-breakpoint +CREATE TYPE "public"."visita_estado" AS ENUM('propuesta', 'confirmada', 'realizada', 'cancelada', 'reprogramada');--> statement-breakpoint +CREATE TABLE "conversacion_whatsapp" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "lead_id" uuid NOT NULL, + "rol" "rol_mensaje" NOT NULL, + "mensaje" text NOT NULL, + "media_type" text, + "media_url" text, + "transcripcion_audio" text, + "created_at" timestamp with time zone DEFAULT now() NOT NULL +); +--> statement-breakpoint +CREATE TABLE "intentos_contacto" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "lead_id" uuid NOT NULL, + "canal" "canal_contacto" NOT NULL, + "resultado" "resultado_contacto", + "completado" boolean DEFAULT false NOT NULL, + "numero_intento" integer NOT NULL, + "duracion_seg" integer, + "intentado_at" timestamp with time zone DEFAULT now() NOT NULL, + "notas" text, + "metadata" jsonb +); +--> statement-breakpoint +CREATE TABLE "lead_calificacion" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "lead_id" uuid NOT NULL, + "score" integer, + "nivel" "nivel_calificacion", + "criterios" jsonb, + "notas_agente" text, + "calificado_por" uuid, + "calificado_at" timestamp with time zone DEFAULT now() NOT NULL, + CONSTRAINT "lead_calificacion_lead_id_unique" UNIQUE("lead_id"), + CONSTRAINT "lead_calificacion_score_check" CHECK ("lead_calificacion"."score" >= 0 AND "lead_calificacion"."score" <= 100) +); +--> statement-breakpoint +CREATE TABLE "visitas" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "lead_id" uuid NOT NULL, + "tenant_id" uuid NOT NULL, + "fecha_propuesta" timestamp with time zone, + "fecha_confirmada" timestamp with time zone, + "estado" "visita_estado" DEFAULT 'propuesta' NOT NULL, + "direccion" text, + "notas" text, + "created_at" timestamp with time zone DEFAULT now() NOT NULL +); +--> statement-breakpoint +CREATE TABLE "worker_jobs" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "lead_id" uuid NOT NULL, + "tipo" "job_tipo" NOT NULL, + "estado_job" "job_estado" DEFAULT 'pendiente' NOT NULL, + "payload" jsonb NOT NULL, + "webhook_url" text, + "resultado_url" text, + "intentos" integer DEFAULT 0 NOT NULL, + "error_msg" text, + "created_at" timestamp with time zone DEFAULT now() NOT NULL, + "completed_at" timestamp with time zone +); +--> statement-breakpoint +ALTER TABLE "leads" ADD COLUMN "estado_wa" "estado_wa";--> statement-breakpoint +ALTER TABLE "leads" ADD COLUMN "canal_origen" "canal_origen";--> statement-breakpoint +ALTER TABLE "leads" ADD COLUMN "espacio" text;--> statement-breakpoint +ALTER TABLE "leads" ADD COLUMN "rango_m2" text;--> statement-breakpoint +ALTER TABLE "leads" ADD COLUMN "estilo" text;--> statement-breakpoint +ALTER TABLE "leads" ADD COLUMN "presupuesto_declarado" text;--> statement-breakpoint +ALTER TABLE "leads" ADD COLUMN "viable" boolean;--> statement-breakpoint +ALTER TABLE "leads" ADD COLUMN "fotos_solicitadas_at" timestamp with time zone;--> statement-breakpoint +ALTER TABLE "conversacion_whatsapp" ADD CONSTRAINT "conversacion_whatsapp_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint +ALTER TABLE "intentos_contacto" ADD CONSTRAINT "intentos_contacto_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint +ALTER TABLE "lead_calificacion" ADD CONSTRAINT "lead_calificacion_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint +ALTER TABLE "lead_calificacion" ADD CONSTRAINT "lead_calificacion_calificado_por_users_id_fk" FOREIGN KEY ("calificado_por") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint +ALTER TABLE "visitas" ADD CONSTRAINT "visitas_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint +ALTER TABLE "visitas" ADD CONSTRAINT "visitas_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint +ALTER TABLE "worker_jobs" ADD CONSTRAINT "worker_jobs_lead_id_leads_id_fk" FOREIGN KEY ("lead_id") REFERENCES "public"."leads"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint +CREATE INDEX "idx_conversacion_whatsapp_lead_id" ON "conversacion_whatsapp" USING btree ("lead_id");--> statement-breakpoint +CREATE INDEX "idx_conversacion_whatsapp_created_at" ON "conversacion_whatsapp" USING btree ("created_at");--> statement-breakpoint +CREATE INDEX "idx_intentos_contacto_lead_id" ON "intentos_contacto" USING btree ("lead_id");--> statement-breakpoint +CREATE INDEX "idx_lead_calificacion_lead_id" ON "lead_calificacion" USING btree ("lead_id");--> statement-breakpoint +CREATE INDEX "idx_visitas_lead_id" ON "visitas" USING btree ("lead_id");--> statement-breakpoint +CREATE INDEX "idx_visitas_tenant_id" ON "visitas" USING btree ("tenant_id");--> statement-breakpoint +CREATE INDEX "idx_worker_jobs_lead_id" ON "worker_jobs" USING btree ("lead_id");--> statement-breakpoint +CREATE INDEX "idx_worker_jobs_estado" ON "worker_jobs" USING btree ("estado_job");--> statement-breakpoint +CREATE INDEX "idx_worker_jobs_tipo" ON "worker_jobs" USING btree ("tipo"); \ No newline at end of file diff --git a/mvp/b2c/drizzle/meta/0010_snapshot.json b/mvp/b2c/drizzle/meta/0010_snapshot.json new file mode 100644 index 0000000..f9d156b --- /dev/null +++ b/mvp/b2c/drizzle/meta/0010_snapshot.json @@ -0,0 +1,2446 @@ +{ + "id": "0c432d14-a8db-4a0d-92bf-56153cc661e6", + "prevId": "9e6e7bd9-61c0-4fa2-826e-67a3d100f8d3", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.catalog_items": { + "name": "catalog_items", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "tenant_id": { + "name": "tenant_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "categoria": { + "name": "categoria", + "type": "categoria_material", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "nombre": { + "name": "nombre", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "calidad": { + "name": "calidad", + "type": "calidad", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "precio_unit": { + "name": "precio_unit", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "unidad": { + "name": "unidad", + "type": "unidad_medida", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "descriptor_render": { + "name": "descriptor_render", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "''" + }, + "es_default": { + "name": "es_default", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "sku": { + "name": "sku", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": { + "catalog_tenant_idx": { + "name": "catalog_tenant_idx", + "columns": [ + { + "expression": "tenant_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "catalog_tenant_sku_idx": { + "name": "catalog_tenant_sku_idx", + "columns": [ + { + "expression": "tenant_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "sku", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "catalog_items_tenant_id_tenants_id_fk": { + "name": "catalog_items_tenant_id_tenants_id_fk", + "tableFrom": "catalog_items", + "tableTo": "tenants", + "columnsFrom": [ + "tenant_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.conversacion_whatsapp": { + "name": "conversacion_whatsapp", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "lead_id": { + "name": "lead_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "rol": { + "name": "rol", + "type": "rol_mensaje", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "mensaje": { + "name": "mensaje", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "media_type": { + "name": "media_type", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "media_url": { + "name": "media_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "transcripcion_audio": { + "name": "transcripcion_audio", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "idx_conversacion_whatsapp_lead_id": { + "name": "idx_conversacion_whatsapp_lead_id", + "columns": [ + { + "expression": "lead_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_conversacion_whatsapp_created_at": { + "name": "idx_conversacion_whatsapp_created_at", + "columns": [ + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "conversacion_whatsapp_lead_id_leads_id_fk": { + "name": "conversacion_whatsapp_lead_id_leads_id_fk", + "tableFrom": "conversacion_whatsapp", + "tableTo": "leads", + "columnsFrom": [ + "lead_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.galeria_fotos": { + "name": "galeria_fotos", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "tenant_id": { + "name": "tenant_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "titulo": { + "name": "titulo", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "orden": { + "name": "orden", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "galeria_tenant_idx": { + "name": "galeria_tenant_idx", + "columns": [ + { + "expression": "tenant_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "galeria_fotos_tenant_id_tenants_id_fk": { + "name": "galeria_fotos_tenant_id_tenants_id_fk", + "tableFrom": "galeria_fotos", + "tableTo": "tenants", + "columnsFrom": [ + "tenant_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.intentos_contacto": { + "name": "intentos_contacto", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "lead_id": { + "name": "lead_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "canal": { + "name": "canal", + "type": "canal_contacto", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "resultado": { + "name": "resultado", + "type": "resultado_contacto", + "typeSchema": "public", + "primaryKey": false, + "notNull": false + }, + "completado": { + "name": "completado", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "numero_intento": { + "name": "numero_intento", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "duracion_seg": { + "name": "duracion_seg", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "intentado_at": { + "name": "intentado_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "notas": { + "name": "notas", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "idx_intentos_contacto_lead_id": { + "name": "idx_intentos_contacto_lead_id", + "columns": [ + { + "expression": "lead_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "intentos_contacto_lead_id_leads_id_fk": { + "name": "intentos_contacto_lead_id_leads_id_fk", + "tableFrom": "intentos_contacto", + "tableTo": "leads", + "columnsFrom": [ + "lead_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.lead_calificacion": { + "name": "lead_calificacion", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "lead_id": { + "name": "lead_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "score": { + "name": "score", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "nivel": { + "name": "nivel", + "type": "nivel_calificacion", + "typeSchema": "public", + "primaryKey": false, + "notNull": false + }, + "criterios": { + "name": "criterios", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "notas_agente": { + "name": "notas_agente", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "calificado_por": { + "name": "calificado_por", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "calificado_at": { + "name": "calificado_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "idx_lead_calificacion_lead_id": { + "name": "idx_lead_calificacion_lead_id", + "columns": [ + { + "expression": "lead_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "lead_calificacion_lead_id_leads_id_fk": { + "name": "lead_calificacion_lead_id_leads_id_fk", + "tableFrom": "lead_calificacion", + "tableTo": "leads", + "columnsFrom": [ + "lead_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "lead_calificacion_calificado_por_users_id_fk": { + "name": "lead_calificacion_calificado_por_users_id_fk", + "tableFrom": "lead_calificacion", + "tableTo": "users", + "columnsFrom": [ + "calificado_por" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "lead_calificacion_lead_id_unique": { + "name": "lead_calificacion_lead_id_unique", + "nullsNotDistinct": false, + "columns": [ + "lead_id" + ] + } + }, + "policies": {}, + "checkConstraints": { + "lead_calificacion_score_check": { + "name": "lead_calificacion_score_check", + "value": "\"lead_calificacion\".\"score\" >= 0 AND \"lead_calificacion\".\"score\" <= 100" + } + }, + "isRLSEnabled": false + }, + "public.lead_estado_history": { + "name": "lead_estado_history", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "lead_id": { + "name": "lead_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "estado": { + "name": "estado", + "type": "lead_estado", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "changed_at": { + "name": "changed_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "changed_by": { + "name": "changed_by", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "lead_estado_history_lead_id_leads_id_fk": { + "name": "lead_estado_history_lead_id_leads_id_fk", + "tableFrom": "lead_estado_history", + "tableTo": "leads", + "columnsFrom": [ + "lead_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.lead_fotos": { + "name": "lead_fotos", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "lead_id": { + "name": "lead_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "momento": { + "name": "momento", + "type": "foto_momento", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'antes'" + }, + "zona": { + "name": "zona", + "type": "tipo_reforma", + "typeSchema": "public", + "primaryKey": false, + "notNull": false + }, + "orden": { + "name": "orden", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "lead_fotos_lead_id_leads_id_fk": { + "name": "lead_fotos_lead_id_leads_id_fk", + "tableFrom": "lead_fotos", + "tableTo": "leads", + "columnsFrom": [ + "lead_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.lead_notas": { + "name": "lead_notas", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "lead_id": { + "name": "lead_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "zona": { + "name": "zona", + "type": "tipo_reforma", + "typeSchema": "public", + "primaryKey": false, + "notNull": false + }, + "texto": { + "name": "texto", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "origen": { + "name": "origen", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'ep'" + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "lead_notas_lead_id_leads_id_fk": { + "name": "lead_notas_lead_id_leads_id_fk", + "tableFrom": "lead_notas", + "tableTo": "leads", + "columnsFrom": [ + "lead_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.lead_pipeline_eventos": { + "name": "lead_pipeline_eventos", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "lead_id": { + "name": "lead_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "stage": { + "name": "stage", + "type": "pipeline_stage", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "occurred_at": { + "name": "occurred_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "lead_pipeline_eventos_lead_id_leads_id_fk": { + "name": "lead_pipeline_eventos_lead_id_leads_id_fk", + "tableFrom": "lead_pipeline_eventos", + "tableTo": "leads", + "columnsFrom": [ + "lead_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.leads": { + "name": "leads", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "tenant_id": { + "name": "tenant_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "nombre": { + "name": "nombre", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "telefono": { + "name": "telefono", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "provincia": { + "name": "provincia", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "tipo_reforma": { + "name": "tipo_reforma", + "type": "tipo_reforma", + "typeSchema": "public", + "primaryKey": false, + "notNull": false + }, + "consent_privacidad": { + "name": "consent_privacidad", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "consent_contratacion": { + "name": "consent_contratacion", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "pipeline_stage": { + "name": "pipeline_stage", + "type": "pipeline_stage", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'form_completado'" + }, + "estado": { + "name": "estado", + "type": "lead_estado", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'nuevo'" + }, + "presupuesto_estimado": { + "name": "presupuesto_estimado", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "transcripcion": { + "name": "transcripcion", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "entidades": { + "name": "entidades", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "render_url": { + "name": "render_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "pdf_url": { + "name": "pdf_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "audio_url": { + "name": "audio_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "notas": { + "name": "notas", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "testimonio_solicitado_at": { + "name": "testimonio_solicitado_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "m2_suelo": { + "name": "m2_suelo", + "type": "double precision", + "primaryKey": false, + "notNull": false + }, + "altura_techo": { + "name": "altura_techo", + "type": "double precision", + "primaryKey": false, + "notNull": false + }, + "calidad_global": { + "name": "calidad_global", + "type": "calidad", + "typeSchema": "public", + "primaryKey": false, + "notNull": false + }, + "estructural": { + "name": "estructural", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "anterior_a_2000": { + "name": "anterior_a_2000", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "cambio_distribucion": { + "name": "cambio_distribucion", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "material_selections": { + "name": "material_selections", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "desglose_snapshot": { + "name": "desglose_snapshot", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "urgencia": { + "name": "urgencia", + "type": "urgencia", + "typeSchema": "public", + "primaryKey": false, + "notNull": false + }, + "presupuesto_target": { + "name": "presupuesto_target", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "taste_text": { + "name": "taste_text", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "preferences_snapshot": { + "name": "preferences_snapshot", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "estado_wa": { + "name": "estado_wa", + "type": "estado_wa", + "typeSchema": "public", + "primaryKey": false, + "notNull": false + }, + "canal_origen": { + "name": "canal_origen", + "type": "canal_origen", + "typeSchema": "public", + "primaryKey": false, + "notNull": false + }, + "espacio": { + "name": "espacio", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "rango_m2": { + "name": "rango_m2", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "estilo": { + "name": "estilo", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "presupuesto_declarado": { + "name": "presupuesto_declarado", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "viable": { + "name": "viable", + "type": "boolean", + "primaryKey": false, + "notNull": false + }, + "fotos_solicitadas_at": { + "name": "fotos_solicitadas_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "leads_tenant_created_idx": { + "name": "leads_tenant_created_idx", + "columns": [ + { + "expression": "tenant_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "leads_estado_idx": { + "name": "leads_estado_idx", + "columns": [ + { + "expression": "estado", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "leads_tenant_id_tenants_id_fk": { + "name": "leads_tenant_id_tenants_id_fk", + "tableFrom": "leads", + "tableTo": "tenants", + "columnsFrom": [ + "tenant_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.plans": { + "name": "plans", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "slug": { + "name": "slug", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "nombre": { + "name": "nombre", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "precio_mensual": { + "name": "precio_mensual", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "leads_incluidos": { + "name": "leads_incluidos", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "features": { + "name": "features", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'[]'::jsonb" + }, + "activo": { + "name": "activo", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "plans_slug_unique": { + "name": "plans_slug_unique", + "nullsNotDistinct": false, + "columns": [ + "slug" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.precision_history": { + "name": "precision_history", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "lead_id": { + "name": "lead_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "estimated": { + "name": "estimated", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "final": { + "name": "final", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "delta_pct": { + "name": "delta_pct", + "type": "numeric(6, 2)", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "precision_history_lead_id_leads_id_fk": { + "name": "precision_history_lead_id_leads_id_fk", + "tableFrom": "precision_history", + "tableTo": "leads", + "columnsFrom": [ + "lead_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.pricing_config": { + "name": "pricing_config", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "tenant_id": { + "name": "tenant_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "altura_techo_default": { + "name": "altura_techo_default", + "type": "double precision", + "primaryKey": false, + "notNull": true, + "default": 2.5 + }, + "factor_zona": { + "name": "factor_zona", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "mano_obra": { + "name": "mano_obra", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "extras": { + "name": "extras", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{\"tuberias\":0,\"boletin\":0,\"distribucion\":0}'::jsonb" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "pricing_config_tenant_id_tenants_id_fk": { + "name": "pricing_config_tenant_id_tenants_id_fk", + "tableFrom": "pricing_config", + "tableTo": "tenants", + "columnsFrom": [ + "tenant_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "pricing_config_tenant_id_unique": { + "name": "pricing_config_tenant_id_unique", + "nullsNotDistinct": false, + "columns": [ + "tenant_id" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.sessions": { + "name": "sessions", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "user_id": { + "name": "user_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "token_hash": { + "name": "token_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "sessions_user_idx": { + "name": "sessions_user_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "sessions_user_id_users_id_fk": { + "name": "sessions_user_id_users_id_fk", + "tableFrom": "sessions", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "sessions_token_hash_unique": { + "name": "sessions_token_hash_unique", + "nullsNotDistinct": false, + "columns": [ + "token_hash" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.tenants": { + "name": "tenants", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "slug": { + "name": "slug", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "nombre_empresa": { + "name": "nombre_empresa", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "logo_url": { + "name": "logo_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "provincia": { + "name": "provincia", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "whatsapp_business": { + "name": "whatsapp_business", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "seo_title": { + "name": "seo_title", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "seo_description": { + "name": "seo_description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "about_enabled": { + "name": "about_enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "about_foto_url": { + "name": "about_foto_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "about_texto": { + "name": "about_texto", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "anios_experiencia": { + "name": "anios_experiencia", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "theme_preset": { + "name": "theme_preset", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'pizarra'" + }, + "theme_color": { + "name": "theme_color", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "cif": { + "name": "cif", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "direccion": { + "name": "direccion", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "telefono": { + "name": "telefono", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "web": { + "name": "web", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "plan_id": { + "name": "plan_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "subscription_status": { + "name": "subscription_status", + "type": "subscription_status", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'trial'" + }, + "envio_presupuesto": { + "name": "envio_presupuesto", + "type": "envio_presupuesto_mode", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'automatico'" + }, + "trial_ends_at": { + "name": "trial_ends_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "stripe_customer_id": { + "name": "stripe_customer_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "tenants_plan_id_plans_id_fk": { + "name": "tenants_plan_id_plans_id_fk", + "tableFrom": "tenants", + "tableTo": "plans", + "columnsFrom": [ + "plan_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "tenants_slug_unique": { + "name": "tenants_slug_unique", + "nullsNotDistinct": false, + "columns": [ + "slug" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.testimonio_fotos": { + "name": "testimonio_fotos", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "testimonio_id": { + "name": "testimonio_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "orden": { + "name": "orden", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "testimonio_fotos_testimonio_id_testimonios_id_fk": { + "name": "testimonio_fotos_testimonio_id_testimonios_id_fk", + "tableFrom": "testimonio_fotos", + "tableTo": "testimonios", + "columnsFrom": [ + "testimonio_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.testimonios": { + "name": "testimonios", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "tenant_id": { + "name": "tenant_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "lead_id": { + "name": "lead_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "nombre": { + "name": "nombre", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "contexto": { + "name": "contexto", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "rating": { + "name": "rating", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "texto": { + "name": "texto", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "estado": { + "name": "estado", + "type": "testimonio_estado", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'pendiente'" + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "testimonios_tenant_estado_idx": { + "name": "testimonios_tenant_estado_idx", + "columns": [ + { + "expression": "tenant_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "estado", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "testimonios_lead_idx": { + "name": "testimonios_lead_idx", + "columns": [ + { + "expression": "lead_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "testimonios_tenant_id_tenants_id_fk": { + "name": "testimonios_tenant_id_tenants_id_fk", + "tableFrom": "testimonios", + "tableTo": "tenants", + "columnsFrom": [ + "tenant_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "testimonios_lead_id_leads_id_fk": { + "name": "testimonios_lead_id_leads_id_fk", + "tableFrom": "testimonios", + "tableTo": "leads", + "columnsFrom": [ + "lead_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.users": { + "name": "users", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "password_hash": { + "name": "password_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "nombre": { + "name": "nombre", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "role": { + "name": "role", + "type": "user_role", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'reformista'" + }, + "tenant_id": { + "name": "tenant_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "user_status", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'activo'" + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "users_tenant_idx": { + "name": "users_tenant_idx", + "columns": [ + { + "expression": "tenant_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "users_tenant_id_tenants_id_fk": { + "name": "users_tenant_id_tenants_id_fk", + "tableFrom": "users", + "tableTo": "tenants", + "columnsFrom": [ + "tenant_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "users_email_unique": { + "name": "users_email_unique", + "nullsNotDistinct": false, + "columns": [ + "email" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.visitas": { + "name": "visitas", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "lead_id": { + "name": "lead_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "tenant_id": { + "name": "tenant_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "fecha_propuesta": { + "name": "fecha_propuesta", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "fecha_confirmada": { + "name": "fecha_confirmada", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "estado": { + "name": "estado", + "type": "visita_estado", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'propuesta'" + }, + "direccion": { + "name": "direccion", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "notas": { + "name": "notas", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "idx_visitas_lead_id": { + "name": "idx_visitas_lead_id", + "columns": [ + { + "expression": "lead_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_visitas_tenant_id": { + "name": "idx_visitas_tenant_id", + "columns": [ + { + "expression": "tenant_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "visitas_lead_id_leads_id_fk": { + "name": "visitas_lead_id_leads_id_fk", + "tableFrom": "visitas", + "tableTo": "leads", + "columnsFrom": [ + "lead_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "visitas_tenant_id_tenants_id_fk": { + "name": "visitas_tenant_id_tenants_id_fk", + "tableFrom": "visitas", + "tableTo": "tenants", + "columnsFrom": [ + "tenant_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.worker_jobs": { + "name": "worker_jobs", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "lead_id": { + "name": "lead_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "tipo": { + "name": "tipo", + "type": "job_tipo", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "estado_job": { + "name": "estado_job", + "type": "job_estado", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'pendiente'" + }, + "payload": { + "name": "payload", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "webhook_url": { + "name": "webhook_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "resultado_url": { + "name": "resultado_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "intentos": { + "name": "intentos", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "error_msg": { + "name": "error_msg", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "completed_at": { + "name": "completed_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "idx_worker_jobs_lead_id": { + "name": "idx_worker_jobs_lead_id", + "columns": [ + { + "expression": "lead_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_worker_jobs_estado": { + "name": "idx_worker_jobs_estado", + "columns": [ + { + "expression": "estado_job", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_worker_jobs_tipo": { + "name": "idx_worker_jobs_tipo", + "columns": [ + { + "expression": "tipo", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "worker_jobs_lead_id_leads_id_fk": { + "name": "worker_jobs_lead_id_leads_id_fk", + "tableFrom": "worker_jobs", + "tableTo": "leads", + "columnsFrom": [ + "lead_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": { + "public.calidad": { + "name": "calidad", + "schema": "public", + "values": [ + "basica", + "media", + "premium" + ] + }, + "public.canal_contacto": { + "name": "canal_contacto", + "schema": "public", + "values": [ + "formulario", + "whatsapp", + "llamada" + ] + }, + "public.canal_origen": { + "name": "canal_origen", + "schema": "public", + "values": [ + "formulario_web", + "whatsapp", + "llamada", + "referido", + "anuncio" + ] + }, + "public.categoria_material": { + "name": "categoria_material", + "schema": "public", + "values": [ + "suelo", + "pared", + "pintura", + "mobiliario" + ] + }, + "public.envio_presupuesto_mode": { + "name": "envio_presupuesto_mode", + "schema": "public", + "values": [ + "automatico", + "revision" + ] + }, + "public.estado_wa": { + "name": "estado_wa", + "schema": "public", + "values": [ + "sin_enviar", + "enviado", + "entregado", + "leido", + "fallido" + ] + }, + "public.foto_momento": { + "name": "foto_momento", + "schema": "public", + "values": [ + "antes", + "despues" + ] + }, + "public.job_estado": { + "name": "job_estado", + "schema": "public", + "values": [ + "pendiente", + "procesando", + "completado", + "error" + ] + }, + "public.job_tipo": { + "name": "job_tipo", + "schema": "public", + "values": [ + "analisis_fotos", + "render", + "presupuesto_ia" + ] + }, + "public.lead_estado": { + "name": "lead_estado", + "schema": "public", + "values": [ + "nuevo", + "contactado", + "visita_agendada", + "presupuesto_enviado", + "ganado", + "perdido" + ] + }, + "public.nivel_calificacion": { + "name": "nivel_calificacion", + "schema": "public", + "values": [ + "A", + "B", + "C", + "D" + ] + }, + "public.pipeline_stage": { + "name": "pipeline_stage", + "schema": "public", + "values": [ + "form_completado", + "fotos_subidas", + "prellamada_enviada", + "llamada_completada", + "render_generado", + "presupuesto_generado", + "whatsapp_entregado" + ] + }, + "public.resultado_contacto": { + "name": "resultado_contacto", + "schema": "public", + "values": [ + "exitoso", + "no_contesta", + "ocupado", + "rechaza", + "error_tecnico" + ] + }, + "public.rol_mensaje": { + "name": "rol_mensaje", + "schema": "public", + "values": [ + "user", + "assistant", + "system" + ] + }, + "public.subscription_status": { + "name": "subscription_status", + "schema": "public", + "values": [ + "trial", + "activo", + "cancelado", + "vencido" + ] + }, + "public.testimonio_estado": { + "name": "testimonio_estado", + "schema": "public", + "values": [ + "pendiente", + "publicado", + "oculto" + ] + }, + "public.tipo_reforma": { + "name": "tipo_reforma", + "schema": "public", + "values": [ + "cocina", + "bano", + "salon", + "comedor", + "integral", + "otro" + ] + }, + "public.unidad_medida": { + "name": "unidad_medida", + "schema": "public", + "values": [ + "m2", + "ml", + "ud" + ] + }, + "public.urgencia": { + "name": "urgencia", + "schema": "public", + "values": [ + "alta", + "media", + "baja" + ] + }, + "public.user_role": { + "name": "user_role", + "schema": "public", + "values": [ + "reformista", + "admin" + ] + }, + "public.user_status": { + "name": "user_status", + "schema": "public", + "values": [ + "activo", + "deshabilitado" + ] + }, + "public.visita_estado": { + "name": "visita_estado", + "schema": "public", + "values": [ + "propuesta", + "confirmada", + "realizada", + "cancelada", + "reprogramada" + ] + } + }, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/mvp/b2c/drizzle/meta/_journal.json b/mvp/b2c/drizzle/meta/_journal.json index 404b75c..bb00c86 100644 --- a/mvp/b2c/drizzle/meta/_journal.json +++ b/mvp/b2c/drizzle/meta/_journal.json @@ -71,6 +71,13 @@ "when": 1780569557328, "tag": "0009_white_agent_brand", "breakpoints": true + }, + { + "idx": 10, + "version": "7", + "when": 1780584271011, + "tag": "0010_square_vulture", + "breakpoints": true } ] } \ No newline at end of file diff --git a/mvp/b2c/src/db/schema.ts b/mvp/b2c/src/db/schema.ts index e555edd..375fdd5 100644 --- a/mvp/b2c/src/db/schema.ts +++ b/mvp/b2c/src/db/schema.ts @@ -11,8 +11,10 @@ import { index, doublePrecision, uniqueIndex, + check, type AnyPgColumn, } from 'drizzle-orm/pg-core'; +import { sql } from 'drizzle-orm'; // Estado comercial del lead — RF-D-03. Lo que el reformista gestiona a mano. export const leadEstado = pgEnum('lead_estado', [ @@ -78,6 +80,37 @@ export const envioPresupuestoMode = pgEnum('envio_presupuesto_mode', ['automatic // 'pendiente' = recién enviado por el cliente; 'publicado' = visible en la landing; 'oculto' = retirado. export const testimonioEstado = pgEnum('testimonio_estado', ['pendiente', 'publicado', 'oculto']); +// === Estructura del flujo WhatsApp/llamada + workers (esquema "reformix-full" del equipo). +// ADITIVO: enums/tablas/columnas nuevas que usará el bot (Luisa) y los workers en la DB única. +// NO modifica los enums ni columnas existentes de la app (lead_estado, pipeline_stage, etc.). +export const estadoWa = pgEnum('estado_wa', ['sin_enviar', 'enviado', 'entregado', 'leido', 'fallido']); +export const canalContacto = pgEnum('canal_contacto', ['formulario', 'whatsapp', 'llamada']); +export const canalOrigen = pgEnum('canal_origen', [ + 'formulario_web', + 'whatsapp', + 'llamada', + 'referido', + 'anuncio', +]); +export const resultadoContacto = pgEnum('resultado_contacto', [ + 'exitoso', + 'no_contesta', + 'ocupado', + 'rechaza', + 'error_tecnico', +]); +export const rolMensaje = pgEnum('rol_mensaje', ['user', 'assistant', 'system']); +export const jobTipo = pgEnum('job_tipo', ['analisis_fotos', 'render', 'presupuesto_ia']); +export const jobEstado = pgEnum('job_estado', ['pendiente', 'procesando', 'completado', 'error']); +export const nivelCalificacion = pgEnum('nivel_calificacion', ['A', 'B', 'C', 'D']); +export const visitaEstado = pgEnum('visita_estado', [ + 'propuesta', + 'confirmada', + 'realizada', + 'cancelada', + 'reprogramada', +]); + // Reformista (cliente del SaaS). MVP = "Reformas Ejemplo" hardcoded. // Multi-tenant real es F1.5; la tabla ya queda lista para ello. export const tenants = pgTable('tenants', { @@ -213,6 +246,17 @@ export const leads = pgTable( presupuestoTarget: integer('presupuesto_target'), // céntimos tasteText: text('taste_text'), preferencesSnapshot: jsonb('preferences_snapshot'), + + // --- Flujo WhatsApp/llamada (esquema reformix-full; aditivos, los rellena el bot/Luisa). + // estadoWa nullable: null = aún sin enviar (el "nuevo" del diagrama). + estadoWa: estadoWa('estado_wa'), + canalOrigen: canalOrigen('canal_origen'), + espacio: text('espacio'), // extracción en crudo de Luisa (se normaliza a tipoReforma) + rangoM2: text('rango_m2'), // crudo (se normaliza a m2Suelo) + estilo: text('estilo'), + presupuestoDeclarado: text('presupuesto_declarado'), // crudo (se normaliza a presupuestoTarget) + viable: boolean('viable'), + fotosSolicitadasAt: timestamp('fotos_solicitadas_at', { withTimezone: true }), }, (table) => [ index('leads_tenant_created_idx').on(table.tenantId, table.createdAt), @@ -374,6 +418,121 @@ export const catalogItems = pgTable( ] ); +// === Tablas del flujo WhatsApp/llamada + workers (esquema reformix-full; aditivas). +// Las escribe el bot (Luisa) y los workers; referencian nuestros leads/users/tenants en la DB única. + +// Historial de la conversación de WhatsApp del bot con el lead. +export const conversacionWhatsapp = pgTable( + 'conversacion_whatsapp', + { + id: uuid('id').primaryKey().defaultRandom(), + leadId: uuid('lead_id') + .notNull() + .references(() => leads.id, { onDelete: 'cascade' }), + rol: rolMensaje('rol').notNull(), + mensaje: text('mensaje').notNull(), + mediaType: text('media_type'), + mediaUrl: text('media_url'), + transcripcionAudio: text('transcripcion_audio'), + createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(), + }, + (table) => [ + index('idx_conversacion_whatsapp_lead_id').on(table.leadId), + index('idx_conversacion_whatsapp_created_at').on(table.createdAt), + ] +); + +// Intentos de contacto multicanal (formulario/whatsapp/llamada). +export const intentosContacto = pgTable( + 'intentos_contacto', + { + id: uuid('id').primaryKey().defaultRandom(), + leadId: uuid('lead_id') + .notNull() + .references(() => leads.id, { onDelete: 'cascade' }), + canal: canalContacto('canal').notNull(), + resultado: resultadoContacto('resultado'), + completado: boolean('completado').notNull().default(false), + numeroIntento: integer('numero_intento').notNull(), + duracionSeg: integer('duracion_seg'), + intentadoAt: timestamp('intentado_at', { withTimezone: true }).notNull().defaultNow(), + notas: text('notas'), + metadata: jsonb('metadata'), + }, + (table) => [index('idx_intentos_contacto_lead_id').on(table.leadId)] +); + +// Calificación del lead (score 0-100 + nivel A/B/C/D). Una por lead. +export const leadCalificacion = pgTable( + 'lead_calificacion', + { + id: uuid('id').primaryKey().defaultRandom(), + leadId: uuid('lead_id') + .notNull() + .unique() + .references(() => leads.id, { onDelete: 'cascade' }), + score: integer('score'), + nivel: nivelCalificacion('nivel'), + criterios: jsonb('criterios'), + notasAgente: text('notas_agente'), + calificadoPor: uuid('calificado_por').references(() => users.id, { onDelete: 'set null' }), + calificadoAt: timestamp('calificado_at', { withTimezone: true }).notNull().defaultNow(), + }, + (table) => [ + index('idx_lead_calificacion_lead_id').on(table.leadId), + check('lead_calificacion_score_check', sql`${table.score} >= 0 AND ${table.score} <= 100`), + ] +); + +// Visitas agendadas por el reformista. +export const visitas = pgTable( + 'visitas', + { + id: uuid('id').primaryKey().defaultRandom(), + leadId: uuid('lead_id') + .notNull() + .references(() => leads.id, { onDelete: 'cascade' }), + tenantId: uuid('tenant_id') + .notNull() + .references(() => tenants.id, { onDelete: 'cascade' }), + fechaPropuesta: timestamp('fecha_propuesta', { withTimezone: true }), + fechaConfirmada: timestamp('fecha_confirmada', { withTimezone: true }), + estado: visitaEstado('estado').notNull().default('propuesta'), + direccion: text('direccion'), + notas: text('notas'), + createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(), + }, + (table) => [ + index('idx_visitas_lead_id').on(table.leadId), + index('idx_visitas_tenant_id').on(table.tenantId), + ] +); + +// Cola de trabajos async de los workers (análisis de fotos, render, presupuesto IA). +export const workerJobs = pgTable( + 'worker_jobs', + { + id: uuid('id').primaryKey().defaultRandom(), + leadId: uuid('lead_id') + .notNull() + .references(() => leads.id, { onDelete: 'cascade' }), + tipo: jobTipo('tipo').notNull(), + estadoJob: jobEstado('estado_job').notNull().default('pendiente'), + payload: jsonb('payload').notNull(), + webhookUrl: text('webhook_url'), + resultadoUrl: text('resultado_url'), + intentos: integer('intentos').notNull().default(0), + errorMsg: text('error_msg'), + createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(), + completedAt: timestamp('completed_at', { withTimezone: true }), + }, + (table) => [ + index('idx_worker_jobs_lead_id').on(table.leadId), + index('idx_worker_jobs_estado').on(table.estadoJob), + index('idx_worker_jobs_tipo').on(table.tipo), + ] +); + export type Tenant = typeof tenants.$inferSelect; export type Lead = typeof leads.$inferSelect; export type NewLead = typeof leads.$inferInsert; @@ -381,6 +540,11 @@ export type LeadFoto = typeof leadFotos.$inferSelect; export type NewLeadFoto = typeof leadFotos.$inferInsert; export type LeadNota = typeof leadNotas.$inferSelect; export type NewLeadNota = typeof leadNotas.$inferInsert; +export type ConversacionWhatsapp = typeof conversacionWhatsapp.$inferSelect; +export type IntentoContacto = typeof intentosContacto.$inferSelect; +export type LeadCalificacion = typeof leadCalificacion.$inferSelect; +export type Visita = typeof visitas.$inferSelect; +export type WorkerJob = typeof workerJobs.$inferSelect; export type Testimonio = typeof testimonios.$inferSelect; export type NewTestimonio = typeof testimonios.$inferInsert; export type TestimonioFoto = typeof testimonioFotos.$inferSelect;