Add ciclo de vida de sesión y helpers de usuario actual
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
47
mvp/b2c/src/db/auth-queries.ts
Normal file
47
mvp/b2c/src/db/auth-queries.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { db } from './index';
|
||||
import { users, tenants } from './schema';
|
||||
import { sessionExpiry } from '@/lib/auth/tokens';
|
||||
|
||||
export async function getUserByEmail(email: string) {
|
||||
const [row] = await db.select().from(users).where(eq(users.email, email)).limit(1);
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
export async function getUserById(id: string) {
|
||||
const [row] = await db.select().from(users).where(eq(users.id, id)).limit(1);
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
export async function createTenantWithOwner(input: {
|
||||
nombreEmpresa: string;
|
||||
slug: string;
|
||||
provincia: string | null;
|
||||
email: string;
|
||||
passwordHash: string;
|
||||
nombre: string | null;
|
||||
}) {
|
||||
const [tenant] = await db
|
||||
.insert(tenants)
|
||||
.values({
|
||||
slug: input.slug,
|
||||
nombreEmpresa: input.nombreEmpresa,
|
||||
provincia: input.provincia,
|
||||
subscriptionStatus: 'trial',
|
||||
trialEndsAt: sessionExpiry(new Date()),
|
||||
})
|
||||
.returning();
|
||||
|
||||
const [user] = await db
|
||||
.insert(users)
|
||||
.values({
|
||||
email: input.email,
|
||||
passwordHash: input.passwordHash,
|
||||
nombre: input.nombre,
|
||||
role: 'reformista',
|
||||
tenantId: tenant.id,
|
||||
})
|
||||
.returning();
|
||||
|
||||
return { tenant, user };
|
||||
}
|
||||
Reference in New Issue
Block a user