Files
sprint/packages/backend/src/routes/organisation/create.ts
2026-01-13 15:32:31 +00:00

21 lines
859 B
TypeScript

import { OrgCreateRequestSchema } from "@issue/shared";
import type { AuthedRequest } from "../../auth/middleware";
import { createOrganisationWithOwner, getOrganisationBySlug } from "../../db/queries";
import { errorResponse, parseJsonBody } from "../../validation";
export default async function organisationCreate(req: AuthedRequest) {
const parsed = await parseJsonBody(req, OrgCreateRequestSchema);
if ("error" in parsed) return parsed.error;
const { name, slug, description } = parsed.data;
const existingOrganisation = await getOrganisationBySlug(slug);
if (existingOrganisation) {
return errorResponse(`organisation with slug "${slug}" already exists`, "SLUG_TAKEN", 409);
}
const organisation = await createOrganisationWithOwner(name, slug, req.userId, description);
return Response.json(organisation);
}