83 lines
2.2 KiB
TypeScript
83 lines
2.2 KiB
TypeScript
'use server';
|
|
|
|
import { revalidatePath } from 'next/cache';
|
|
import { prisma, getTenantPrisma } from '@/lib/prisma';
|
|
|
|
export async function createDemoTenantAction(formData: FormData): Promise<void> {
|
|
const name = (formData.get('name') as string) || 'Iron Fitness Gym';
|
|
const slug = (formData.get('slug') as string) || `gym-${Date.now().toString().slice(-4)}`;
|
|
|
|
// 1. Create Tenant
|
|
const tenant = await prisma.tenant.create({
|
|
data: {
|
|
name,
|
|
slug,
|
|
domain: `${slug}.fitnessapp.local`,
|
|
},
|
|
});
|
|
|
|
// 2. Use Tenant-Scoped Client Extension to create user and class session
|
|
const tenantPrisma = getTenantPrisma(tenant.id);
|
|
|
|
const instructor = await tenantPrisma.user.create({
|
|
data: {
|
|
tenantId: tenant.id,
|
|
email: `coach@${slug}.com`,
|
|
name: 'Coach Marcus',
|
|
role: 'INSTRUCTOR',
|
|
},
|
|
});
|
|
|
|
const client = await tenantPrisma.user.create({
|
|
data: {
|
|
tenantId: tenant.id,
|
|
email: `athlete@${slug}.com`,
|
|
name: 'Alex Rivera',
|
|
role: 'CLIENT',
|
|
},
|
|
});
|
|
|
|
const session = await tenantPrisma.classSession.create({
|
|
data: {
|
|
tenantId: tenant.id,
|
|
title: 'HIIT & Functional Strength',
|
|
description: 'High intensity interval training with kettlebells and plyometrics',
|
|
startTime: new Date(Date.now() + 24 * 3600 * 1000),
|
|
endTime: new Date(Date.now() + 25 * 3600 * 1000),
|
|
capacity: 12,
|
|
jitsiRoomId: `room-${tenant.id.slice(0, 8)}`,
|
|
},
|
|
});
|
|
|
|
await tenantPrisma.booking.create({
|
|
data: {
|
|
tenantId: tenant.id,
|
|
userId: client.id,
|
|
classSessionId: session.id,
|
|
status: 'CONFIRMED',
|
|
signedWaiverAt: new Date(),
|
|
},
|
|
});
|
|
|
|
await tenantPrisma.eWaiver.create({
|
|
data: {
|
|
tenantId: tenant.id,
|
|
userId: client.id,
|
|
parqStatus: 'CLEARED',
|
|
signedAt: new Date(),
|
|
ipAddress: '127.0.0.1',
|
|
},
|
|
});
|
|
|
|
revalidatePath('/');
|
|
}
|
|
|
|
export async function clearDemoDataAction(): Promise<void> {
|
|
await prisma.eWaiver.deleteMany();
|
|
await prisma.booking.deleteMany();
|
|
await prisma.classSession.deleteMany();
|
|
await prisma.mediaAsset.deleteMany();
|
|
await prisma.user.deleteMany();
|
|
await prisma.tenant.deleteMany();
|
|
revalidatePath('/');
|
|
} |