diff --git a/.github/workflows/update-notion.js.yml b/.github/workflows/update-notion.js.yml new file mode 100644 index 0000000..b820c07 --- /dev/null +++ b/.github/workflows/update-notion.js.yml @@ -0,0 +1,34 @@ +name: Notion Update CI + +on: + push: + branches: ["development", "main", "feature/notion-db-automation"] + +jobs: + build: + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [16.x] + # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ + defaults: + run: + working-directory: ./scripts + steps: + - uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + cache: "npm" + + - run: npm ci + - run: | + touch .env.local + echo NOTION_INTEGRATION_SECRET=${{ secrets.NOTION_INTEGRATION_SECRET }} >> .env.local + echo NOTION_LEADS_DEVDB=${{ secrets.NOTION_LEADS_DEVDB }} >> .env.local + echo NOTION_LEADS_PRODDB=${{ secrets.NOTION_LEADS_PRODDB }} >> .env.local + + - run: npm run build --if-present + - run: npm run start diff --git a/.gitignore b/.gitignore index 1437c53..207a50b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ /node_modules /.pnp .pnp.js +scripts/node_modules # testing /coverage diff --git a/scripts/index.js b/scripts/index.js index 10e4106..360dc71 100644 --- a/scripts/index.js +++ b/scripts/index.js @@ -1,8 +1,9 @@ const { Client } = require("@notionhq/client"); require("dotenv").config({ - path: ".env.local", + path: "./.env.local", }); - +console.log(process.env.NOTION_LEADS_PRODDB); +console.log(process.env.NOTION_LEADS_DEVDB); // Initializing a client const notion = new Client({ auth: process.env.NOTION_INTEGRATION_SECRET, @@ -26,40 +27,38 @@ async function updateDb(sourceDbId, destDbId) { for (const item of source.results) { const properties = { - Name: { - title: [ - { - text: { - content: item.properties.Name.title[0].text.content, - }, + Name: { + title: [ + { + text: { + content: item.properties.Name.title[0].text.content, }, - ], - }, - - Position: { - select: { - name: item.properties.Position.select.name, }, - }, - - Avatar: { - files: [{ - name: item.properties.Avatar.files[0]?.name ?? "test.jpg", - type: item.properties.Avatar.files[0]?.type, - file: { - url: item.properties.Avatar.files[0]?.file.url, - - } - }] - } - } + ], + }, - if(item.properties.Avatar.files.length === 0) { + Position: { + select: { + name: item.properties.Position.select.name, + }, + }, - delete properties.Avatar + Avatar: { + files: [ + { + name: item.properties.Avatar.files[0]?.name ?? "test.jpg", + type: item.properties.Avatar.files[0]?.type, + file: { + url: item.properties.Avatar.files[0]?.file.url, + }, + }, + ], + }, + }; - - } + if (item.properties.Avatar.files.length === 0) { + delete properties.Avatar; + } await notion.pages.create({ parent: { @@ -67,8 +66,7 @@ async function updateDb(sourceDbId, destDbId) { database_id: destDbId, }, - properties - + properties, }); } } @@ -78,12 +76,16 @@ async function queryDb(databaseId) { database_id: databaseId, }); - console.log(response.results[response.results.length-1].properties.Avatar.files[0].name) + // console.log(response.results[response.results.length-1].properties.Avatar.files[0].name) return response; } - deleteRecords(process.env.NOTION_LEADS_PRODDB); - - // queryDb(`bbbd878ff5f6489c8c39e6f54166d31e`) +(async () => { + await deleteRecords(process.env.NOTION_LEADS_PRODDB); + await updateDb( + process.env.NOTION_LEADS_DEVDB, + process.env.NOTION_LEADS_PRODDB + ); +})(); - updateDb(process.env.NOTION_LEADS_DEVDB, process.env.NOTION_LEADS_PRODDB) +// queryDb(`bbbd878ff5f6489c8c39e6f54166d31e`)