Last active
July 18, 2021 17:00
-
-
Save brotoo25/d824d5da7d32fbf7aea0ca8896251d90 to your computer and use it in GitHub Desktop.
Notion API - Update table with currency prices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dotenv = require('dotenv').config() | |
const { Client } = require('@notionhq/client') | |
const notion = new Client({ | |
auth: process.env.NOTION_TOKEN, | |
}) | |
const databaseId = process.env.NOTION_DATABASE_ID | |
const defaultCurrency = process.env.DEFAULT_CURRENCY | |
const refreshDatabase = async () => { | |
const payload = { | |
path: `databases/${databaseId}/query`, | |
method: 'POST' | |
} | |
const { results } = await notion.request(payload) | |
updateCryptoConversions(results) | |
updateCurrencyConversions(results) | |
} | |
async function updateCryptoConversions(notionPages) { | |
notionPages.map(async (page) => { | |
const coinType = page.properties.Crypto_ID.rich_text[0]?.text.content || "EMPTY" | |
if (coinType != "EMPTY") { | |
const cryptoValue = await fetchPriceOnCoinGecko(coinType, defaultCurrency) | |
_updateNotionTable(page.id, cryptoValue) | |
} | |
}) | |
} | |
async function updateCurrencyConversions(notionPages) { | |
notionPages.map(async (page) => { | |
const coinType = page.properties.Currency_ID.rich_text[0]?.text.content || "EMPTY" | |
if (coinType != "EMPTY") { | |
const currencyValue = await fetchCurrencyPrice(coinType, defaultCurrency) | |
_updateNotionTable(page.id, parseFloat(currencyValue)) | |
} | |
}) | |
} | |
async function _updateNotionTable(pageId, monetaryValue) { | |
notion.pages.update({ | |
page_id: pageId, | |
properties: { | |
USD: { | |
number: monetaryValue | |
} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment