1+
2+ const config = require ( "./config.json" ) ;
3+ const { Client } = require ( "@notionhq/client" ) ;
4+
5+ const notion = new Client ( { auth : config . notionApiKey } ) ;
6+
7+ async function getOrCreateNotionDatabase ( ) {
8+ try {
9+ // List databases to check existence
10+ const databases = await notion . search ( {
11+ query : config . NOTION_DATABASE_NAME ,
12+ } ) ;
13+ if ( databases . results . length > 0 ) return databases . results [ 0 ] . id ;
14+
15+ // Create new database
16+ const response = await notion . databases . create ( {
17+ parent : { type : "page_id" , page_id : config . notionParentPageId } ,
18+ title : [ { type : "text" , text : { content : NOTION_DATABASE_NAME } } ] ,
19+ properties : {
20+ Name : { title : { } } ,
21+ Link : { url : { } } ,
22+ Status : {
23+ select : {
24+ options : [
25+ { name : "Accepted" , color : "green" } ,
26+ { name : "Wrong Answer" , color : "red" } ,
27+ ] ,
28+ } ,
29+ } ,
30+ Date : { date : { } } ,
31+ } ,
32+ } ) ;
33+
34+ return response . id ;
35+ } catch ( error ) {
36+ console . error ( "Error setting up Notion database:" , error ) ;
37+ throw error ;
38+ }
39+ }
40+
41+ async function addSubmissionToNotion ( databaseId , submission ) {
42+ try {
43+ await notion . pages . create ( {
44+ parent : { database_id : databaseId } ,
45+ properties : {
46+ // Correctly define the Problem Name field (as a title)
47+ "Problem Name" : {
48+ title : [
49+ {
50+ text : { content : submission . name || "Untitled Problem" } , // Provide fallback if name is missing
51+ } ,
52+ ] ,
53+ } ,
54+ // Correctly define the Problem Number field (as a number)
55+ "Problem Number" : {
56+ number : parseInt ( submission . problemNumber ) || null , // Ensure null if no problem number is provided
57+ } ,
58+ // Correctly define the Link field (as a URL)
59+ Link : {
60+ url : submission . link || null , // Ensure null if no link is provided
61+ } ,
62+ // Correctly define the Submission Date field (as a date)
63+ "Submission Date" : {
64+ date : submission . date
65+ ? { start : submission . date } // Ensure date is properly structured
66+ : null ,
67+ } ,
68+ // Correctly define the Tags field (as a multi-select)
69+ Tags : {
70+ multi_select : submission . tags
71+ ? submission . tags . map ( ( tag ) => ( { name : tag } ) )
72+ : [ ] ,
73+ } ,
74+ // Correctly define the Difficulty field (as a select)
75+ Difficulty : {
76+ select : submission . difficulty
77+ ? { name : submission . difficulty }
78+ : null ,
79+ } ,
80+ // Correctly define the Status field (as a select)
81+ Status : {
82+ select : submission . status ? { name : submission . status } : null ,
83+ } ,
84+ } ,
85+ } ) ;
86+
87+ console . log ( `Added "${ submission . name } " to Notion successfully.` ) ;
88+ } catch ( error ) {
89+ console . error ( "Error adding submission to Notion:" , error . message ) ;
90+ }
91+ }
92+
93+ async function addSubmissionsToNotion ( databaseId , submissions ) {
94+ for ( const submission of submissions ) {
95+ await addSubmissionToNotion ( databaseId , submission ) ;
96+ }
97+ }
98+
99+ module . exports = {
100+ getOrCreateNotionDatabase,
101+ addSubmissionsToNotion
102+ }
0 commit comments