1 | import { NextResponse } from 'next/server';
|
---|
2 | import prisma from 'src/lib/prisma';
|
---|
3 | import { tenantSchema } from 'src/schemas';
|
---|
4 | import { z } from 'zod';
|
---|
5 |
|
---|
6 | export async function GET() {
|
---|
7 | try {
|
---|
8 | const tenants = await prisma.tenant.findMany();
|
---|
9 | const tenant = tenants[0]; // Get first tenant since we're dealing with single tenant
|
---|
10 | console.log('tenant', tenant);
|
---|
11 | if (!tenant) {
|
---|
12 | return NextResponse.json({ error: 'No tenant found' }, { status: 404 });
|
---|
13 | }
|
---|
14 |
|
---|
15 | return NextResponse.json(tenant);
|
---|
16 | } catch (error) {
|
---|
17 | console.error('Error fetching tenant:', error);
|
---|
18 | return NextResponse.json({ error: 'Failed to fetch tenant' }, { status: 500 });
|
---|
19 | }
|
---|
20 | }
|
---|
21 |
|
---|
22 | export async function POST(request: Request) {
|
---|
23 | try {
|
---|
24 | const body = await request.json();
|
---|
25 |
|
---|
26 | // Validate request body
|
---|
27 | const validatedData = tenantSchema.parse(body);
|
---|
28 |
|
---|
29 | // Check if tenant already exists
|
---|
30 | const existingTenants = await prisma.tenant.findMany();
|
---|
31 | if (existingTenants.length > 0) {
|
---|
32 | return NextResponse.json({ error: 'Tenant already exists' }, { status: 400 });
|
---|
33 | }
|
---|
34 |
|
---|
35 | // Create new tenant
|
---|
36 | const tenant = await prisma.tenant.create({
|
---|
37 | data: validatedData,
|
---|
38 | });
|
---|
39 |
|
---|
40 | return NextResponse.json(tenant, { status: 201 });
|
---|
41 | } catch (error) {
|
---|
42 | if (error instanceof z.ZodError) {
|
---|
43 | return NextResponse.json(
|
---|
44 | { error: 'Invalid request data', details: error.errors },
|
---|
45 | { status: 400 }
|
---|
46 | );
|
---|
47 | }
|
---|
48 |
|
---|
49 | console.error('Error creating tenant:', error);
|
---|
50 | return NextResponse.json({ error: 'Failed to create tenant' }, { status: 500 });
|
---|
51 | }
|
---|
52 | }
|
---|