First pass
This commit is contained in:
40
src/pages/api/clients/create.ts
Normal file
40
src/pages/api/clients/create.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import type { APIRoute } from 'astro';
|
||||
import { db } from '../../../db';
|
||||
import { clients, members } from '../../../db/schema';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { nanoid } from 'nanoid';
|
||||
|
||||
export const POST: APIRoute = async ({ request, locals, redirect }) => {
|
||||
const user = locals.user;
|
||||
if (!user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const formData = await request.formData();
|
||||
const name = formData.get('name')?.toString();
|
||||
const email = formData.get('email')?.toString();
|
||||
|
||||
if (!name) {
|
||||
return new Response('Name is required', { status: 400 });
|
||||
}
|
||||
|
||||
// Get user's first organization
|
||||
const userOrg = await db.select()
|
||||
.from(members)
|
||||
.where(eq(members.userId, user.id))
|
||||
.get();
|
||||
|
||||
if (!userOrg) {
|
||||
return new Response('No organization found', { status: 400 });
|
||||
}
|
||||
|
||||
// Create client
|
||||
await db.insert(clients).values({
|
||||
id: nanoid(),
|
||||
organizationId: userOrg.organizationId,
|
||||
name,
|
||||
email: email || null,
|
||||
});
|
||||
|
||||
return redirect('/dashboard/clients');
|
||||
};
|
||||
Reference in New Issue
Block a user