-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathroute.ts
More file actions
53 lines (50 loc) · 1.8 KB
/
Copy pathroute.ts
File metadata and controls
53 lines (50 loc) · 1.8 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
import { NextResponse } from "next/server"
import { getOrganizations } from "@/lib/db/queries"
import { getSessionUser } from "@/lib/auth"
import { toSessionUserDto } from "@/lib/session-user"
import { filterInstalledOrganizationsForAdmin } from "@/lib/github/admin-authorization"
export async function GET() {
const user = await getSessionUser()
if (!user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
}
try {
console.info("[api/orgs] Listing organizations for user", {
userId: user.id,
githubUsername: user.githubUsername,
})
const allOrgs = await getOrganizations()
const installedOrgs = allOrgs.filter((org) => org.installationId !== null)
console.info("[api/orgs] Retrieved organizations from DB", {
userId: user.id,
totalOrgs: allOrgs.length,
installedCount: installedOrgs.length,
installedOrgs: installedOrgs.map((org) => ({
slug: org.githubOrgSlug,
accountType: org.githubAccountType ?? "organization",
accountId: org.githubAccountId ?? null,
})),
})
const orgs = await filterInstalledOrganizationsForAdmin(user, allOrgs)
console.info("[api/orgs] Authorized organizations resolved", {
userId: user.id,
authorizedCount: orgs.length,
authorizedOrgs: orgs.map((org) => ({
slug: org.githubOrgSlug,
accountType: org.githubAccountType ?? "organization",
accountId: org.githubAccountId ?? null,
})),
})
return NextResponse.json({
orgs,
user: toSessionUserDto(user),
installedOrgsCount: orgs.length,
})
} catch (err) {
console.error("Failed to list authorized organizations:", err)
return NextResponse.json(
{ error: "Failed to verify GitHub installation admin access" },
{ status: 502 }
)
}
}