First pass
This commit is contained in:
34
src/pages/api/organizations/create.ts
Normal file
34
src/pages/api/organizations/create.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import type { APIRoute } from 'astro';
|
||||
import { db } from '../../../db';
|
||||
import { organizations, members } from '../../../db/schema';
|
||||
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();
|
||||
|
||||
if (!name) {
|
||||
return new Response('Name is required', { status: 400 });
|
||||
}
|
||||
|
||||
// Create organization
|
||||
const orgId = nanoid();
|
||||
await db.insert(organizations).values({
|
||||
id: orgId,
|
||||
name,
|
||||
});
|
||||
|
||||
// Add user as owner
|
||||
await db.insert(members).values({
|
||||
userId: user.id,
|
||||
organizationId: orgId,
|
||||
role: 'owner',
|
||||
});
|
||||
|
||||
return redirect('/dashboard');
|
||||
};
|
||||
Reference in New Issue
Block a user