|
| 1 | +import { and, eq, sql } from 'drizzle-orm' |
| 2 | + |
| 3 | +import { db, schema } from '~/db/client.server' |
| 4 | +import { buildArkUrl, DEFAULT_NAAN } from '~/lib/ark' |
| 5 | + |
| 6 | +export async function getCollectionPageData(owner: string, slug: string, userId?: string) { |
| 7 | + const [result] = await db |
| 8 | + .select({ |
| 9 | + id: schema.collections.id, |
| 10 | + slug: schema.collections.slug, |
| 11 | + name: schema.collections.name, |
| 12 | + description: schema.collections.description, |
| 13 | + public: schema.collections.public, |
| 14 | + ownerSlug: schema.organization.slug, |
| 15 | + ownerName: schema.organization.name, |
| 16 | + createdAt: schema.collections.createdAt, |
| 17 | + updatedAt: schema.collections.updatedAt, |
| 18 | + }) |
| 19 | + .from(schema.collections) |
| 20 | + .innerJoin(schema.organization, eq(schema.collections.organizationId, schema.organization.id)) |
| 21 | + .where(and(eq(schema.organization.slug, owner), eq(schema.collections.slug, slug))) |
| 22 | + .limit(1) |
| 23 | + |
| 24 | + if (!result) return null |
| 25 | + |
| 26 | + if (!result.public) { |
| 27 | + const [org] = await db |
| 28 | + .select({ id: schema.organization.id }) |
| 29 | + .from(schema.organization) |
| 30 | + .where(eq(schema.organization.slug, owner)) |
| 31 | + .limit(1) |
| 32 | + |
| 33 | + if (!org) return null |
| 34 | + |
| 35 | + let hasAccess = false |
| 36 | + if (userId) { |
| 37 | + const [membership] = await db |
| 38 | + .select() |
| 39 | + .from(schema.member) |
| 40 | + .where(and(eq(schema.member.organizationId, org.id), eq(schema.member.userId, userId))) |
| 41 | + .limit(1) |
| 42 | + hasAccess = !!membership |
| 43 | + } |
| 44 | + if (!hasAccess) return null |
| 45 | + } |
| 46 | + |
| 47 | + const [latestVersion] = await db |
| 48 | + .select({ |
| 49 | + id: schema.versions.id, |
| 50 | + number: schema.versions.number, |
| 51 | + semver: schema.versions.semver, |
| 52 | + recordCount: schema.versions.recordCount, |
| 53 | + fileCount: schema.versions.fileCount, |
| 54 | + totalBytes: schema.versions.totalBytes, |
| 55 | + createdAt: schema.versions.createdAt, |
| 56 | + message: schema.versions.message, |
| 57 | + readme: schema.versions.readme, |
| 58 | + }) |
| 59 | + .from(schema.versions) |
| 60 | + .where(eq(schema.versions.collectionId, result.id)) |
| 61 | + .orderBy(sql`${schema.versions.number} desc`) |
| 62 | + .limit(1) |
| 63 | + |
| 64 | + let typeCounts: { type: string; count: number }[] = [] |
| 65 | + if (latestVersion) { |
| 66 | + const rows = await db |
| 67 | + .select({ |
| 68 | + type: schema.records.type, |
| 69 | + count: sql<number>`count(*)::int`, |
| 70 | + }) |
| 71 | + .from(schema.records) |
| 72 | + .where(eq(schema.records.versionId, latestVersion.id)) |
| 73 | + .groupBy(schema.records.type) |
| 74 | + typeCounts = rows.map((r) => ({ type: r.type, count: r.count })) |
| 75 | + } |
| 76 | + |
| 77 | + let ark: string | null = null |
| 78 | + try { |
| 79 | + const [arkRow] = await db |
| 80 | + .select({ |
| 81 | + arkId: schema.arkCollections.arkId, |
| 82 | + enabled: schema.arkCollections.enabled, |
| 83 | + shoulder: schema.arkShoulders.shoulder, |
| 84 | + ownerNaan: schema.organization.arkNaan, |
| 85 | + }) |
| 86 | + .from(schema.arkCollections) |
| 87 | + .innerJoin(schema.collections, eq(schema.arkCollections.collectionId, schema.collections.id)) |
| 88 | + .innerJoin(schema.organization, eq(schema.collections.organizationId, schema.organization.id)) |
| 89 | + .innerJoin( |
| 90 | + schema.arkShoulders, |
| 91 | + eq(schema.arkShoulders.organizationId, schema.organization.id), |
| 92 | + ) |
| 93 | + .where(eq(schema.arkCollections.collectionId, result.id)) |
| 94 | + .limit(1) |
| 95 | + if (arkRow?.enabled) { |
| 96 | + ark = buildArkUrl(arkRow.ownerNaan ?? DEFAULT_NAAN, arkRow.shoulder, arkRow.arkId) |
| 97 | + } |
| 98 | + } catch { |
| 99 | + // Non-fatal |
| 100 | + } |
| 101 | + |
| 102 | + const { id: _id, ...collectionData } = result |
| 103 | + const { id: _vid, ...latestVersionData } = latestVersion ?? { id: undefined } |
| 104 | + return { |
| 105 | + ...collectionData, |
| 106 | + ark, |
| 107 | + latestVersion: latestVersion ? { ...latestVersionData, typeCounts } : null, |
| 108 | + } |
| 109 | +} |
0 commit comments