-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathroute.ts
More file actions
61 lines (55 loc) · 2.12 KB
/
Copy pathroute.ts
File metadata and controls
61 lines (55 loc) · 2.12 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
import { NextResponse } from "next/server"
import { getSignaturesByUser, getOrganizations } from "@/lib/db/queries"
import { getSessionUser } from "@/lib/auth"
import { toSessionUserDto } from "@/lib/session-user"
export async function GET() {
const user = await getSessionUser()
if (!user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
}
const mySignatures = await getSignaturesByUser(user.id)
const allOrgs = await getOrganizations()
const signaturesBySignedAtDesc = [...mySignatures].sort((a, b) =>
b.signedAt.localeCompare(a.signedAt)
)
const orgById = new Map(allOrgs.map((org) => [org.id, org]))
const latestSignatureByOrg = new Map<string, (typeof signaturesBySignedAtDesc)[number]>()
const orgsWithCurrentSignature = new Set<string>()
for (const sig of signaturesBySignedAtDesc) {
if (!latestSignatureByOrg.has(sig.orgId)) {
latestSignatureByOrg.set(sig.orgId, sig)
}
const org = orgById.get(sig.orgId)
if (org && sig.claSha256 === org.claTextSha256) {
orgsWithCurrentSignature.add(sig.orgId)
}
}
// Enrich signatures with org data and status for latest vs history.
const enriched = signaturesBySignedAtDesc.map((sig) => {
const org = orgById.get(sig.orgId)
const isCurrentVersion = sig.claSha256 === org?.claTextSha256
const isLatestForOrg = latestSignatureByOrg.get(sig.orgId)?.id === sig.id
const orgHasCurrentSignature = orgsWithCurrentSignature.has(sig.orgId)
const orgNeedsResign = !orgHasCurrentSignature
return {
...sig,
orgName: org?.name ?? "Unknown",
orgSlug: org?.githubOrgSlug ?? "",
orgAvatarUrl: org?.avatarUrl ?? "",
orgIsActive: org?.isActive ?? false,
isCurrentVersion,
isLatestForOrg,
orgHasCurrentSignature,
orgNeedsResign,
signedVersionLabel: sig.claSha256.slice(0, 7),
}
})
return NextResponse.json({
user: toSessionUserDto(user),
signatures: enriched,
signedOrgCount: latestSignatureByOrg.size,
outdatedOrgCount: [...latestSignatureByOrg.keys()].filter(
(orgId) => !orgsWithCurrentSignature.has(orgId)
).length,
})
}