Add schema de users, sessions, plans y suscripción de tenant

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Carlos Narro
2026-05-30 19:37:35 +02:00
parent 2cc19147ff
commit 7b3b8457c1
4 changed files with 1270 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
CREATE TYPE "public"."subscription_status" AS ENUM('trial', 'activo', 'cancelado', 'vencido');--> statement-breakpoint
CREATE TYPE "public"."user_role" AS ENUM('reformista', 'admin');--> statement-breakpoint
CREATE TYPE "public"."user_status" AS ENUM('activo', 'deshabilitado');--> statement-breakpoint
CREATE TABLE "plans" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"slug" text NOT NULL,
"nombre" text NOT NULL,
"precio_mensual" integer NOT NULL,
"leads_incluidos" integer NOT NULL,
"features" jsonb DEFAULT '[]'::jsonb NOT NULL,
"activo" boolean DEFAULT true NOT NULL,
CONSTRAINT "plans_slug_unique" UNIQUE("slug")
);
--> statement-breakpoint
CREATE TABLE "sessions" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"user_id" uuid NOT NULL,
"token_hash" text NOT NULL,
"expires_at" timestamp with time zone NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "sessions_token_hash_unique" UNIQUE("token_hash")
);
--> statement-breakpoint
CREATE TABLE "users" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"email" text NOT NULL,
"password_hash" text NOT NULL,
"nombre" text,
"role" "user_role" DEFAULT 'reformista' NOT NULL,
"tenant_id" uuid,
"status" "user_status" DEFAULT 'activo' NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "users_email_unique" UNIQUE("email")
);
--> statement-breakpoint
ALTER TABLE "tenants" ADD COLUMN "plan_id" uuid;--> statement-breakpoint
ALTER TABLE "tenants" ADD COLUMN "subscription_status" "subscription_status" DEFAULT 'trial' NOT NULL;--> statement-breakpoint
ALTER TABLE "tenants" ADD COLUMN "trial_ends_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "tenants" ADD COLUMN "stripe_customer_id" text;--> statement-breakpoint
ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
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;--> statement-breakpoint
CREATE INDEX "sessions_user_idx" ON "sessions" USING btree ("user_id");--> statement-breakpoint
CREATE INDEX "users_tenant_idx" ON "users" USING btree ("tenant_id");--> statement-breakpoint
ALTER TABLE "tenants" ADD CONSTRAINT "tenants_plan_id_plans_id_fk" FOREIGN KEY ("plan_id") REFERENCES "public"."plans"("id") ON DELETE no action ON UPDATE no action;

File diff suppressed because it is too large Load Diff

View File

@@ -15,6 +15,13 @@
"when": 1780137082579, "when": 1780137082579,
"tag": "0001_bored_preak", "tag": "0001_bored_preak",
"breakpoints": true "breakpoints": true
},
{
"idx": 2,
"version": "7",
"when": 1780162638625,
"tag": "0002_overjoyed_the_renegades",
"breakpoints": true
} }
] ]
} }

View File

@@ -55,6 +55,15 @@ export const categoriaMaterial = pgEnum('categoria_material', [
export const unidadMedida = pgEnum('unidad_medida', ['m2', 'ml', 'ud']); export const unidadMedida = pgEnum('unidad_medida', ['m2', 'ml', 'ud']);
export const userRole = pgEnum('user_role', ['reformista', 'admin']);
export const userStatus = pgEnum('user_status', ['activo', 'deshabilitado']);
export const subscriptionStatus = pgEnum('subscription_status', [
'trial',
'activo',
'cancelado',
'vencido',
]);
// Reformista (cliente del SaaS). MVP = "Reformas Ejemplo" hardcoded. // Reformista (cliente del SaaS). MVP = "Reformas Ejemplo" hardcoded.
// Multi-tenant real es F1.5; la tabla ya queda lista para ello. // Multi-tenant real es F1.5; la tabla ya queda lista para ello.
export const tenants = pgTable('tenants', { export const tenants = pgTable('tenants', {
@@ -64,9 +73,53 @@ export const tenants = pgTable('tenants', {
logoUrl: text('logo_url'), logoUrl: text('logo_url'),
provincia: text('provincia'), provincia: text('provincia'),
whatsappBusiness: text('whatsapp_business'), whatsappBusiness: text('whatsapp_business'),
planId: uuid('plan_id').references((): any => plans.id),
subscriptionStatus: subscriptionStatus('subscription_status').notNull().default('trial'),
trialEndsAt: timestamp('trial_ends_at', { withTimezone: true }),
stripeCustomerId: text('stripe_customer_id'),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(), createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
}); });
export const plans = pgTable('plans', {
id: uuid('id').primaryKey().defaultRandom(),
slug: text('slug').notNull().unique(),
nombre: text('nombre').notNull(),
precioMensual: integer('precio_mensual').notNull(), // céntimos
leadsIncluidos: integer('leads_incluidos').notNull(),
features: jsonb('features').$type<string[]>().notNull().default([]),
activo: boolean('activo').notNull().default(true),
});
export const users = pgTable(
'users',
{
id: uuid('id').primaryKey().defaultRandom(),
email: text('email').notNull().unique(),
passwordHash: text('password_hash').notNull(),
nombre: text('nombre'),
role: userRole('role').notNull().default('reformista'),
tenantId: uuid('tenant_id').references(() => tenants.id, { onDelete: 'cascade' }),
status: userStatus('status').notNull().default('activo'),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
},
(table) => [index('users_tenant_idx').on(table.tenantId)]
);
export const sessions = pgTable(
'sessions',
{
id: uuid('id').primaryKey().defaultRandom(),
userId: uuid('user_id')
.notNull()
.references(() => users.id, { onDelete: 'cascade' }),
tokenHash: text('token_hash').notNull().unique(),
expiresAt: timestamp('expires_at', { withTimezone: true }).notNull(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
},
(table) => [index('sessions_user_idx').on(table.userId)]
);
export const leads = pgTable( export const leads = pgTable(
'leads', 'leads',
{ {
@@ -214,3 +267,7 @@ export type PrecisionHistory = typeof precisionHistory.$inferSelect;
export type PricingConfigRow = typeof pricingConfig.$inferSelect; export type PricingConfigRow = typeof pricingConfig.$inferSelect;
export type CatalogItemRow = typeof catalogItems.$inferSelect; export type CatalogItemRow = typeof catalogItems.$inferSelect;
export type NewCatalogItem = typeof catalogItems.$inferInsert; export type NewCatalogItem = typeof catalogItems.$inferInsert;
export type Plan = typeof plans.$inferSelect;
export type User = typeof users.$inferSelect;
export type NewUser = typeof users.$inferInsert;
export type Session = typeof sessions.$inferSelect;