a tool for shared writing and social publishing
at feature/recommend 60 lines 1.9 kB view raw
1import { GetLeafletDataReturnType } from "app/api/rpc/[command]/get_leaflet_data"; 2import { Json } from "supabase/database.types"; 3 4/** 5 * Return type for publication metadata extraction. 6 * Note: `publications.record` and `documents.data` are raw JSON from the database. 7 * Consumers should use `normalizePublicationRecord()` and `normalizeDocumentRecord()` 8 * from `src/utils/normalizeRecords` to get properly typed data. 9 */ 10export type PublicationMetadata = { 11 description: string; 12 title: string; 13 leaflet: string; 14 doc: string | null; 15 publications: { 16 identity_did: string; 17 name: string; 18 indexed_at: string; 19 /** Raw record - use normalizePublicationRecord() to get typed data */ 20 record: Json | null; 21 uri: string; 22 } | null; 23 documents: { 24 /** Raw data - use normalizeDocumentRecord() to get typed data */ 25 data: Json; 26 indexed_at: string; 27 uri: string; 28 } | null; 29} | null; 30 31export function getPublicationMetadataFromLeafletData( 32 data?: GetLeafletDataReturnType["result"]["data"], 33): PublicationMetadata { 34 if (!data) return null; 35 36 let pubData: 37 | NonNullable<PublicationMetadata> 38 | undefined 39 | null = 40 data?.leaflets_in_publications?.[0] || 41 data?.permission_token_rights[0].entity_sets?.permission_tokens?.find( 42 (p) => p.leaflets_in_publications?.length, 43 )?.leaflets_in_publications?.[0]; 44 45 // If not found, check for standalone documents 46 let standaloneDoc = 47 data?.leaflets_to_documents?.[0] || 48 data?.permission_token_rights[0].entity_sets?.permission_tokens.find( 49 (p) => p.leaflets_to_documents?.length, 50 )?.leaflets_to_documents?.[0]; 51 if (!pubData && standaloneDoc) { 52 // Transform standalone document data to match the expected format 53 pubData = { 54 ...standaloneDoc, 55 publications: null, // No publication for standalone docs 56 doc: standaloneDoc.document, 57 }; 58 } 59 return pubData || null; 60}