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

@@ -55,6 +55,15 @@ export const categoriaMaterial = pgEnum('categoria_material', [
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.
// Multi-tenant real es F1.5; la tabla ya queda lista para ello.
export const tenants = pgTable('tenants', {
@@ -64,9 +73,53 @@ export const tenants = pgTable('tenants', {
logoUrl: text('logo_url'),
provincia: text('provincia'),
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(),
});
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(
'leads',
{
@@ -214,3 +267,7 @@ export type PrecisionHistory = typeof precisionHistory.$inferSelect;
export type PricingConfigRow = typeof pricingConfig.$inferSelect;
export type CatalogItemRow = typeof catalogItems.$inferSelect;
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;