-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroute.ts
More file actions
63 lines (54 loc) · 1.62 KB
/
route.ts
File metadata and controls
63 lines (54 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { type NextRequest, NextResponse } from "next/server";
import { db } from "@/db/drizzle";
import { orgs } from "@/db/schema";
import { eq } from "drizzle-orm";
import { z } from "zod";
import { getOrgIdFromNameId } from "../../service";
import { getOrgByOrgId } from "./service";
import { invalidateCacheKey } from "@/lib/cache/utils";
const updateOrgSchema = z.object({
name: z.string().optional(),
about: z.string().optional(),
avatar: z.string().optional(),
});
export async function GET(
req: NextRequest,
{ params }: { params: { orgId: string } },
) {
// Get numeric ID from nameId
const orgId = await getOrgIdFromNameId(params.orgId);
if (!orgId) {
return NextResponse.json(
{ message: "Organization not found" },
{ status: 404 },
);
}
const org = getOrgByOrgId(orgId);
return org
? NextResponse.json(org)
: NextResponse.json({ message: "Organization not found" }, { status: 404 });
}
export async function PATCH(
req: NextRequest,
{ params }: { params: { orgId: string } },
) {
// Get numeric ID from nameId
const orgId = await getOrgIdFromNameId(params.orgId);
if (!orgId) {
return NextResponse.json(
{ message: "Organization not found" },
{ status: 404 },
);
}
const body = await req.json();
const validatedData = updateOrgSchema.parse(body);
const [updatedOrg] = await db
.update(orgs)
.set(validatedData)
.where(eq(orgs.id, orgId))
.returning();
await invalidateCacheKey(`org:${orgId}`);
return updatedOrg
? NextResponse.json(updatedOrg)
: NextResponse.json({ message: "Organization not found" }, { status: 404 });
}