(notion: Client, databaseId: string, dataSourceId: string)
| 85 | } |
| 86 | |
| 87 | async function createViews(notion: Client, databaseId: string, dataSourceId: string): Promise<void> { |
| 88 | const ds = await notion.request<any>({ path: `data_sources/${dataSourceId}`, method: "get" }); |
| 89 | const propIds: Record<string, string> = {}; |
| 90 | for (const [name, def] of Object.entries((ds?.properties ?? {}) as Record<string, any>)) { |
| 91 | if (def?.id) propIds[name] = def.id; |
| 92 | } |
| 93 | |
| 94 | const ourViewIds = new Set<string>(); |
| 95 | for (const v of viewPayloads(databaseId, dataSourceId, propIds)) { |
| 96 | try { |
| 97 | const created = await notion.request<any>({ path: "views", method: "post", body: v }); |
| 98 | if (created?.id) ourViewIds.add(created.id); |
| 99 | ok(`View "${v.name}"`); |
| 100 | } catch (e: any) { |
| 101 | fail(`View "${v.name}" failed: ${e.message}`); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Remove the auto-created default view so users land on "All Repos". |
| 106 | try { |
| 107 | const listed = await notion.request<any>({ path: `views?database_id=${databaseId}`, method: "get" }); |
| 108 | for (const v of (listed?.results ?? []) as Array<{ id: string; name?: string }>) { |
| 109 | if (ourViewIds.has(v.id)) continue; |
| 110 | await notion.request({ path: `views/${v.id}`, method: "delete" }); |
| 111 | ok(`Removed default view`); |
| 112 | } |
| 113 | } catch { /* non-fatal */ } |
| 114 | } |
| 115 | |
| 116 | // ---------- Env-state builder --------------------------------------------- |
| 117 |
no test coverage detected