New API + API Token Updates

This commit is contained in:
2026-01-16 13:20:11 -07:00
parent 756ab2a38f
commit 4412229990
26 changed files with 1661 additions and 1012 deletions

View File

@@ -1,38 +1,57 @@
import type { APIRoute } from 'astro';
import { db } from '../../../db';
import { clients, members } from '../../../db/schema';
import { eq } from 'drizzle-orm';
import { nanoid } from 'nanoid';
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 });
return new Response("Unauthorized", { status: 401 });
}
const formData = await request.formData();
const name = formData.get('name')?.toString();
const email = formData.get('email')?.toString();
let name: string | undefined;
let email: string | undefined;
if (request.headers.get("Content-Type")?.includes("application/json")) {
const body = await request.json();
name = body.name;
email = body.email;
} else {
const formData = await request.formData();
name = formData.get("name")?.toString();
email = formData.get("email")?.toString();
}
if (!name) {
return new Response('Name is required', { status: 400 });
return new Response("Name is required", { status: 400 });
}
const userOrg = await db.select()
const userOrg = await db
.select()
.from(members)
.where(eq(members.userId, user.id))
.get();
if (!userOrg) {
return new Response('No organization found', { status: 400 });
return new Response("No organization found", { status: 400 });
}
const id = nanoid();
await db.insert(clients).values({
id: nanoid(),
id,
organizationId: userOrg.organizationId,
name,
email: email || null,
});
return redirect('/dashboard/clients');
if (locals.scopes) {
return new Response(JSON.stringify({ id, name, email: email || null }), {
status: 201,
headers: { "Content-Type": "application/json" },
});
}
return redirect("/dashboard/clients");
};