|
| 1 | +import { fetchPageById, fetchTableData, fetchNotionUsers } from "../api/notion"; |
| 2 | +import { parsePageId, getNotionValue } from "../api/utils"; |
| 3 | +import { |
| 4 | + RowContentType, |
| 5 | + CollectionType, |
| 6 | + RowType, |
| 7 | + HandlerRequest, |
| 8 | +} from "../api/types"; |
| 9 | +import { createResponse } from "../response"; |
| 10 | + |
| 11 | +export const getCollectionData = async ( |
| 12 | + collection: CollectionType, |
| 13 | + collectionViewId: string, |
| 14 | + notionToken?: string, |
| 15 | + raw?: boolean |
| 16 | +) => { |
| 17 | + const table = await fetchTableData( |
| 18 | + collection.value.id, |
| 19 | + collectionViewId, |
| 20 | + notionToken |
| 21 | + ); |
| 22 | + |
| 23 | + |
| 24 | + const collectionRows = collection.value.schema; |
| 25 | + const collectionColKeys = Object.keys(collectionRows); |
| 26 | + |
| 27 | + const tableArr: RowType[] = table.result.blockIds.map( |
| 28 | + (id: string) => table.recordMap.block[id] |
| 29 | + ); |
| 30 | + |
| 31 | + const tableData = tableArr.filter( |
| 32 | + (b) => |
| 33 | + b.value && b.value.properties && b.value.parent_id === collection.value.id |
| 34 | + ); |
| 35 | + |
| 36 | + type Row = { id: string;[key: string]: RowContentType }; |
| 37 | + |
| 38 | + const rows: Row[] = []; |
| 39 | + |
| 40 | + for (const td of tableData) { |
| 41 | + let row: Row = { id: td.value.id }; |
| 42 | + |
| 43 | + for (const key of collectionColKeys) { |
| 44 | + const val = td.value.properties[key]; |
| 45 | + if (val) { |
| 46 | + const schema = collectionRows[key]; |
| 47 | + row[schema.name] = raw ? val : getNotionValue(val, schema.type, td); |
| 48 | + if (schema.type === "person" && row[schema.name]) { |
| 49 | + const users = await fetchNotionUsers(row[schema.name] as string[]); |
| 50 | + row[schema.name] = users as any; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + rows.push(row); |
| 55 | + } |
| 56 | + |
| 57 | + const name: String = collection.value.name.join('') |
| 58 | + |
| 59 | + return { rows, schema: collectionRows, name }; |
| 60 | +}; |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +export async function collectionRoute(req: HandlerRequest) { |
| 68 | + const pageId = parsePageId(req.params.pageId); |
| 69 | + const page = await fetchPageById(pageId!, req.notionToken); |
| 70 | + |
| 71 | + if (!page.recordMap.collection) |
| 72 | + return createResponse( |
| 73 | + JSON.stringify({ error: "No table found on Notion page: " + pageId }), |
| 74 | + {}, |
| 75 | + 401 |
| 76 | + ); |
| 77 | + |
| 78 | + const collection = Object.keys(page.recordMap.collection).map( |
| 79 | + (k) => page.recordMap.collection[k] |
| 80 | + )[0]; |
| 81 | + |
| 82 | + const views: any[] = [] |
| 83 | + |
| 84 | + const collectionView: { |
| 85 | + value: { id: CollectionType["value"]["id"] }; |
| 86 | + } = Object.keys(page.recordMap.collection_view).map((k) => { |
| 87 | + |
| 88 | + views.push(page.recordMap.collection_view[k]['value']) |
| 89 | + return page.recordMap.collection_view[k] |
| 90 | + })[0]; |
| 91 | + |
| 92 | + const tableData = await getCollectionData( |
| 93 | + collection, |
| 94 | + collectionView.value.id, |
| 95 | + req.notionToken |
| 96 | + ); |
| 97 | + |
| 98 | + // console.log('table data:', JSON.stringify(collectionView.value)) |
| 99 | + // console.log('view:', JSON.stringify(page.recordMap)) |
| 100 | + |
| 101 | + // clean up the table order |
| 102 | + const tableProps = views[0].format.table_properties |
| 103 | + tableProps.map((tableCol, i) => { |
| 104 | + tableProps[i] = { ...tableProps[i], ...tableData.schema[tableCol['property']] } |
| 105 | + }) |
| 106 | + |
| 107 | + return createResponse({ ...tableData, columns: tableProps, sort: views[0].page_sort, collection: collection }); |
| 108 | +} |
0 commit comments