|
| 1 | +import { NextResponse, NextRequest } from "next/server"; |
| 2 | +import { createClient } from "~/utils/supabase/server"; |
| 3 | +import { asPostgrestFailure } from "@repo/database/lib/contextFunctions"; |
| 4 | +import { |
| 5 | + defaultOptionsHandler, |
| 6 | + createApiResponse, |
| 7 | +} from "~/utils/supabase/apiUtils"; |
| 8 | +import { asJsonLD, wrapJsonLd } from "~/utils/conversion/jsonld"; |
| 9 | +import { Tables } from "@repo/database/dbTypes"; |
| 10 | +import { PostgrestMaybeSingleResponse } from "@supabase/supabase-js"; |
| 11 | + |
| 12 | +type Concept = Tables<"Concept">; |
| 13 | +type Content = Tables<"Content">; |
| 14 | +type Space = Tables<"Space">; |
| 15 | +type PlatformAccount = Tables<"PlatformAccount">; |
| 16 | + |
| 17 | +export type SegmentDataType = { params: Promise<Record<string, string>> }; |
| 18 | + |
| 19 | +export const GET = async ( |
| 20 | + request: NextRequest, |
| 21 | + segmentData: SegmentDataType, |
| 22 | +): Promise<NextResponse> => { |
| 23 | + const { space_id, resource_id } = await segmentData.params; |
| 24 | + const targetFormat = request.nextUrl.searchParams.get("format") ?? "html"; |
| 25 | + const withContext = request.nextUrl.searchParams.get("context"); |
| 26 | + const withSchema = request.nextUrl.searchParams.get("schema"); |
| 27 | + const spaceIdN = Number.parseInt(space_id || "NaN"); |
| 28 | + if (isNaN(spaceIdN)) { |
| 29 | + return createApiResponse( |
| 30 | + request, |
| 31 | + asPostgrestFailure(`${space_id} is not a number`, "type"), |
| 32 | + ); |
| 33 | + } |
| 34 | + const resourceIdN = Number.parseInt(resource_id || "NaN"); |
| 35 | + if (isNaN(resourceIdN)) { |
| 36 | + return createApiResponse( |
| 37 | + request, |
| 38 | + asPostgrestFailure(`${resource_id} is not a number`, "type"), |
| 39 | + ); |
| 40 | + } |
| 41 | + const supabase = await createClient(); |
| 42 | + const spaceResponse = await supabase |
| 43 | + .from("Space") |
| 44 | + .select() |
| 45 | + .eq("id", spaceIdN) |
| 46 | + .maybeSingle(); |
| 47 | + if (spaceResponse.error) { |
| 48 | + return createApiResponse(request, spaceResponse); |
| 49 | + } |
| 50 | + if (!spaceResponse.data) { |
| 51 | + // consideration: We may not see it because we don't have access, |
| 52 | + // so it would be worth re-fetching as superuser to see if I should redirect to login. |
| 53 | + return createApiResponse( |
| 54 | + request, |
| 55 | + asPostgrestFailure("Space not found", "401", 401), |
| 56 | + ); |
| 57 | + } |
| 58 | + const space: Space = spaceResponse.data; |
| 59 | + const conceptResponse = await supabase |
| 60 | + .from("Concept") |
| 61 | + .select() |
| 62 | + .eq("id", resourceIdN) |
| 63 | + .maybeSingle(); |
| 64 | + if (conceptResponse.error) { |
| 65 | + return createApiResponse(request, conceptResponse); |
| 66 | + } |
| 67 | + const concept = conceptResponse.data; |
| 68 | + if (!concept) { |
| 69 | + return createApiResponse( |
| 70 | + request, |
| 71 | + asPostgrestFailure("Resource not found", "401", 401), |
| 72 | + ); |
| 73 | + } |
| 74 | + const contentResponse = await supabase |
| 75 | + .from("Content") |
| 76 | + .select() |
| 77 | + .eq("source_local_id", concept.source_local_id!); |
| 78 | + if (contentResponse.error) { |
| 79 | + return createApiResponse(request, conceptResponse); |
| 80 | + } |
| 81 | + const contents: Content[] = contentResponse.data; |
| 82 | + const fullContentsArray = contents.filter((c) => c.variant === "full"); |
| 83 | + const fullContents = fullContentsArray.length |
| 84 | + ? fullContentsArray[0] |
| 85 | + : undefined; |
| 86 | + const titleArray = contents.filter((c) => c.variant === "direct"); |
| 87 | + const title = titleArray.length ? titleArray[0] : undefined; |
| 88 | + |
| 89 | + const requestUrlParts = request.url.split("/"); |
| 90 | + const baseUrl = requestUrlParts |
| 91 | + .slice(0, requestUrlParts.length - 1) |
| 92 | + .join("/"); |
| 93 | + const rootUrl = baseUrl.split("/").slice(0, 3).join("/"); |
| 94 | + const pageUrl = `${rootUrl}/api/content/${baseUrl.split("/")[5]}/${concept.id}#`; |
| 95 | + let schemas: Record<number, Concept> = {}; |
| 96 | + let authors: Record<number, PlatformAccount> = {}; |
| 97 | + let relations: Concept[] = []; |
| 98 | + let schemaIds = new Set<number>(); |
| 99 | + if (withContext) { |
| 100 | + const relationsResult = (await supabase |
| 101 | + .from("Concept") |
| 102 | + .select("relations:concept_in_relations!inner(*)") |
| 103 | + .eq("id", concept.id) |
| 104 | + .maybeSingle()) as PostgrestMaybeSingleResponse<{ relations: Concept[] }>; |
| 105 | + if (relationsResult.data?.relations?.length) { |
| 106 | + relations = relationsResult.data.relations; |
| 107 | + if (withSchema) |
| 108 | + schemaIds = new Set( |
| 109 | + relations.map((c) => c.schema_id).filter((id) => id !== null), |
| 110 | + ); |
| 111 | + } |
| 112 | + } |
| 113 | + if (concept.schema_id) schemaIds.add(concept.schema_id); |
| 114 | + if (schemaIds.size > 0) { |
| 115 | + const schemaResponse = await supabase |
| 116 | + .from("Concept") |
| 117 | + .select() |
| 118 | + .in("id", [...schemaIds]); |
| 119 | + if (schemaResponse.error) { |
| 120 | + return createApiResponse(request, schemaResponse); |
| 121 | + } |
| 122 | + if (!schemaResponse.data) { |
| 123 | + return createApiResponse( |
| 124 | + request, |
| 125 | + asPostgrestFailure("Resource schema not found", "401", 401), |
| 126 | + ); |
| 127 | + } |
| 128 | + schemas = Object.fromEntries(schemaResponse.data.map((s) => [s.id, s])); |
| 129 | + } |
| 130 | + const authorIds = new Set<number>([ |
| 131 | + ...relations.map((r) => r.author_id).filter((id) => id !== null), |
| 132 | + ...Object.values(schemas) |
| 133 | + .map((s) => s.author_id) |
| 134 | + .filter((id) => id !== null), |
| 135 | + ]); |
| 136 | + if (concept.author_id) authorIds.add(concept.author_id); |
| 137 | + if (authorIds.size > 0) { |
| 138 | + const authorsResponse = await supabase |
| 139 | + .from("PlatformAccount") |
| 140 | + .select() |
| 141 | + .in("id", [...authorIds]); |
| 142 | + if (authorsResponse.error) { |
| 143 | + return createApiResponse(request, authorsResponse); |
| 144 | + } |
| 145 | + if (!authorsResponse.data) { |
| 146 | + return createApiResponse( |
| 147 | + request, |
| 148 | + asPostgrestFailure("Resource schema not found", "401", 401), |
| 149 | + ); |
| 150 | + } |
| 151 | + authors = Object.fromEntries(authorsResponse.data.map((a) => [a.id, a])); |
| 152 | + } |
| 153 | + |
| 154 | + const relationsJLD = withContext |
| 155 | + ? await Promise.all( |
| 156 | + relations.map((c) => |
| 157 | + asJsonLD({ |
| 158 | + space, |
| 159 | + concept: c, |
| 160 | + baseUrl, |
| 161 | + schema: c.schema_id ? schemas[c.schema_id] : undefined, |
| 162 | + author: c.author_id ? authors[c.author_id] : undefined, |
| 163 | + }), |
| 164 | + ), |
| 165 | + ) |
| 166 | + : []; |
| 167 | + const schemasJLD = withSchema |
| 168 | + ? await Promise.all( |
| 169 | + Object.values(schemas).map((c) => |
| 170 | + asJsonLD({ |
| 171 | + space, |
| 172 | + concept: c, |
| 173 | + baseUrl, |
| 174 | + author: c.author_id ? authors[c.author_id] : undefined, |
| 175 | + }), |
| 176 | + ), |
| 177 | + ) |
| 178 | + : []; |
| 179 | + |
| 180 | + const baseJLDData = await asJsonLD({ |
| 181 | + space, |
| 182 | + concept, |
| 183 | + baseUrl, |
| 184 | + title, |
| 185 | + schema: concept.schema_id ? schemas[concept.schema_id] : undefined, |
| 186 | + content: targetFormat === "none" ? undefined : fullContents, |
| 187 | + author: concept.author_id ? authors[concept.author_id] : undefined, |
| 188 | + targetFormat, |
| 189 | + }); |
| 190 | + |
| 191 | + const jsonLdData = |
| 192 | + relationsJLD.length > 0 || schemasJLD.length > 0 |
| 193 | + ? [baseJLDData, ...relationsJLD, ...schemasJLD] |
| 194 | + : baseJLDData; |
| 195 | + return NextResponse.json(wrapJsonLd(jsonLdData, baseUrl, pageUrl), { |
| 196 | + status: 200, |
| 197 | + headers: { "Content-Type": "application/ld+json" }, |
| 198 | + }); |
| 199 | +}; |
| 200 | + |
| 201 | +export const OPTIONS = defaultOptionsHandler; |
0 commit comments