|
| 1 | +/** |
| 2 | + * HHMI compliance Airtable cache using the shared Object table. |
| 3 | + * - Webhook always upserts when it runs. |
| 4 | + * - Routes: read from cache; if object missing (cold start), may fetch and write once; if exists, never update. |
| 5 | + */ |
| 6 | + |
| 7 | +import { getPrismaClient } from '@curvenote/scms-server'; |
| 8 | +import type { NormalizedScientist } from './types.js'; |
| 9 | +import type { NormalizedJournal } from './airtable.journals.server.js'; |
| 10 | +import { fetchAllScientists } from './airtable.scientists.server.js'; |
| 11 | +import { fetchAllJournals } from './airtable.journals.server.js'; |
| 12 | + |
| 13 | +const HHMI_COMPLIANCE_CACHE_PREFIX = 'hhmi:compliance:'; |
| 14 | + |
| 15 | +/** Cache slot definitions. Add new entries here when caching additional queries. */ |
| 16 | +export const CACHE_KEYS = { |
| 17 | + scientists: { |
| 18 | + id: `${HHMI_COMPLIANCE_CACHE_PREFIX}scientists`, |
| 19 | + type: `${HHMI_COMPLIANCE_CACHE_PREFIX}scientists`, |
| 20 | + }, |
| 21 | + journals: { |
| 22 | + id: `${HHMI_COMPLIANCE_CACHE_PREFIX}journals`, |
| 23 | + type: `${HHMI_COMPLIANCE_CACHE_PREFIX}journals`, |
| 24 | + }, |
| 25 | +} as const; |
| 26 | + |
| 27 | +function nowIso(): string { |
| 28 | + return new Date().toISOString(); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Read a cached object by id. Returns the row or null. |
| 33 | + */ |
| 34 | +export async function getCached(id: string): Promise<{ data: unknown; date_modified: string } | null> { |
| 35 | + const prisma = await getPrismaClient(); |
| 36 | + const row = await prisma.object.findUnique({ |
| 37 | + where: { id }, |
| 38 | + select: { data: true, date_modified: true }, |
| 39 | + }); |
| 40 | + if (!row) return null; |
| 41 | + return { data: row.data, date_modified: row.date_modified }; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Upsert a cache row. Used by webhook (always) and by routes on cold start only. |
| 46 | + */ |
| 47 | +export async function setCached( |
| 48 | + id: string, |
| 49 | + type: string, |
| 50 | + data: unknown, |
| 51 | +): Promise<void> { |
| 52 | + const prisma = await getPrismaClient(); |
| 53 | + const now = nowIso(); |
| 54 | + await prisma.object.upsert({ |
| 55 | + where: { id }, |
| 56 | + create: { |
| 57 | + id, |
| 58 | + type, |
| 59 | + date_created: now, |
| 60 | + date_modified: now, |
| 61 | + data: data as object, |
| 62 | + occ: 0, |
| 63 | + }, |
| 64 | + update: { |
| 65 | + date_modified: now, |
| 66 | + data: data as object, |
| 67 | + }, |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Read scientists list from cache. Returns null if not present. |
| 73 | + */ |
| 74 | +export async function getScientistsFromCache(): Promise<NormalizedScientist[] | null> { |
| 75 | + const row = await getCached(CACHE_KEYS.scientists.id); |
| 76 | + if (!row || row.data == null) return null; |
| 77 | + const parsed = row.data as NormalizedScientist[]; |
| 78 | + return Array.isArray(parsed) ? parsed : null; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Get scientists from cache, or fetch from Airtable and write to cache once (cold start). |
| 83 | + * Routes use this; they may write only when the object does not exist. |
| 84 | + */ |
| 85 | +export async function getScientistsFromCacheOrFetch(): Promise<NormalizedScientist[]> { |
| 86 | + const cached = await getScientistsFromCache(); |
| 87 | + if (cached !== null) return cached; |
| 88 | + |
| 89 | + const scientists = await fetchAllScientists(); |
| 90 | + const prisma = await getPrismaClient(); |
| 91 | + const existing = await prisma.object.findUnique({ |
| 92 | + where: { id: CACHE_KEYS.scientists.id }, |
| 93 | + select: { id: true }, |
| 94 | + }); |
| 95 | + if (!existing) { |
| 96 | + await setCached(CACHE_KEYS.scientists.id, CACHE_KEYS.scientists.type, scientists); |
| 97 | + } |
| 98 | + return scientists; |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Read journals list from cache. Returns null if not present. |
| 103 | + */ |
| 104 | +export async function getJournalsFromCache(): Promise<NormalizedJournal[] | null> { |
| 105 | + const row = await getCached(CACHE_KEYS.journals.id); |
| 106 | + if (!row || row.data == null) return null; |
| 107 | + const parsed = row.data as NormalizedJournal[]; |
| 108 | + return Array.isArray(parsed) ? parsed : null; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Get journals from cache, or fetch from Airtable and write to cache once (cold start). |
| 113 | + */ |
| 114 | +export async function getJournalsFromCacheOrFetch(): Promise<NormalizedJournal[]> { |
| 115 | + const cached = await getJournalsFromCache(); |
| 116 | + if (cached !== null) return cached; |
| 117 | + |
| 118 | + const journals = await fetchAllJournals(); |
| 119 | + const prisma = await getPrismaClient(); |
| 120 | + const existing = await prisma.object.findUnique({ |
| 121 | + where: { id: CACHE_KEYS.journals.id }, |
| 122 | + select: { id: true }, |
| 123 | + }); |
| 124 | + if (!existing) { |
| 125 | + await setCached(CACHE_KEYS.journals.id, CACHE_KEYS.journals.type, journals); |
| 126 | + } |
| 127 | + return journals; |
| 128 | +} |
0 commit comments