feat: implement onboarding flow with complex creation and email verification, and add Zustand for state management
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { db } from '@/lib/prisma';
|
||||
import { createComplex } from '@/modules/complex/services/complex.service';
|
||||
import type { AppContext } from '@/types/hono';
|
||||
import type { CreateComplexInput } from '@repo/api-contract';
|
||||
@@ -6,16 +7,32 @@ export async function createComplexHandler(c: AppContext) {
|
||||
const payload = c.req.valid('json' as never) as CreateComplexInput;
|
||||
|
||||
const user = c.get('user');
|
||||
const adminEmail = user.email;
|
||||
let adminEmail = user?.email;
|
||||
let userId = user?.id;
|
||||
|
||||
if (!adminEmail) {
|
||||
return c.json({ message: 'El usuario autenticado no tiene email.' }, 400);
|
||||
const body = await c.req.json().catch(() => ({}));
|
||||
adminEmail = body.adminEmail;
|
||||
userId = body.userId;
|
||||
}
|
||||
|
||||
if (!adminEmail) {
|
||||
return c.json({ message: 'Se requiere email del administrador.' }, 400);
|
||||
}
|
||||
|
||||
if (!userId && adminEmail) {
|
||||
const existingUser = await db.user.findUnique({
|
||||
where: { email: adminEmail },
|
||||
select: { id: true },
|
||||
});
|
||||
userId = existingUser?.id;
|
||||
}
|
||||
|
||||
const complex = await createComplex({
|
||||
complexName: payload.complexName,
|
||||
physicalAddress: payload.physicalAddress,
|
||||
adminEmail,
|
||||
userId,
|
||||
planCode: payload.planCode,
|
||||
city: payload.city,
|
||||
state: payload.state,
|
||||
|
||||
@@ -6,6 +6,7 @@ export type CreateComplexInput = {
|
||||
complexName: string;
|
||||
physicalAddress: string;
|
||||
adminEmail: string;
|
||||
userId?: string;
|
||||
planCode?: string;
|
||||
city?: string;
|
||||
state?: string;
|
||||
@@ -64,20 +65,36 @@ async function buildUniqueSlug(source: string, excludeComplexId?: string): Promi
|
||||
}
|
||||
|
||||
export async function createComplex(input: CreateComplexInput) {
|
||||
const complexSlug = await buildUniqueSlug(input.complexName);
|
||||
const { userId, ...rest } = input;
|
||||
|
||||
return db.complex.create({
|
||||
data: {
|
||||
id: uuidv7(),
|
||||
complexName: input.complexName,
|
||||
physicalAddress: input.physicalAddress.trim(),
|
||||
city: input.city?.trim() || null,
|
||||
state: input.state?.trim() || null,
|
||||
country: input.country?.trim() || null,
|
||||
complexSlug,
|
||||
adminEmail: input.adminEmail,
|
||||
planCode: input.planCode,
|
||||
},
|
||||
return db.$transaction(async (tx) => {
|
||||
const complexSlug = await buildUniqueSlug(input.complexName);
|
||||
|
||||
const complex = await tx.complex.create({
|
||||
data: {
|
||||
id: uuidv7(),
|
||||
complexName: input.complexName,
|
||||
physicalAddress: input.physicalAddress.trim(),
|
||||
city: input.city?.trim() || null,
|
||||
state: input.state?.trim() || null,
|
||||
country: input.country?.trim() || null,
|
||||
complexSlug,
|
||||
adminEmail: input.adminEmail,
|
||||
planCode: input.planCode,
|
||||
},
|
||||
});
|
||||
|
||||
if (userId) {
|
||||
await tx.complexUser.create({
|
||||
data: {
|
||||
complexId: complex.id,
|
||||
userId,
|
||||
role: 'ADMIN',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return complex;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user