From 1e74c1a06c52c6d6250aa4deae050e5c17578eb4 Mon Sep 17 00:00:00 2001 From: maximo Date: Sat, 19 Nov 2022 04:18:06 +0000 Subject: [PATCH] added checks before reduce and added warn --- src/main.ts | 7 ++++--- src/nordigen/fetcher.ts | 9 ++++++++- src/util/logger.ts | 9 +++++++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/main.ts b/src/main.ts index c5ed102..ddadacf 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,6 +2,7 @@ import { getCoinbaseInfo } from "./coinbase/fetcher.ts"; import { getBanks } from "./nordigen/fetcher.ts"; import { syncDatabases } from "./notion/notion.ts"; import { type Bank } from "./types/custom.d.ts"; +import { warn } from "./util/logger.ts"; const banks: Bank[] = [] @@ -9,19 +10,19 @@ const hasNordigen = Deno.env.get("NORDIGEN_KEY") && Deno.env.get("NORDIGEN_SECRE if (hasNordigen) { banks.push(...await getBanks()) } else { - console.log("⚠️ Ignoring Nordigen because of missing env variables"); + warn("Ignoring Nordigen because of missing env variables") } const hasCoinbase = Deno.env.get("COINBASE_KEY") && Deno.env.get("COINBASE_SECRET") if (hasCoinbase) { banks.push(await getCoinbaseInfo()) } else { - console.log("⚠️ Ignoring Coinbase because of missing env variables"); + warn("Ignoring Coinbase because of missing env variables") } const hasNotion = Deno.env.get("NOTION_TOKEN") && Deno.env.get("NOTION_DATABASE_ID") if (hasNotion) { await syncDatabases(banks) } else { - console.log("⚠️ Ignoring Notion because of missing env variables"); + warn("Ignoring Notion because of missing env variables") } \ No newline at end of file diff --git a/src/nordigen/fetcher.ts b/src/nordigen/fetcher.ts index 0950276..6a17b9d 100644 --- a/src/nordigen/fetcher.ts +++ b/src/nordigen/fetcher.ts @@ -1,11 +1,16 @@ import type { INordigenPagination, INordigenBank, INordigenRequisition, INordigenTransactions, INordigenBankAccount, INordigenBankAccountDetail, Bank, Account, INordigenBankAccountBalances, Transaction } from "../types/index.d.ts"; import { get } from "./util.ts"; -import { log } from "../util/logger.ts"; +import { log, warn } from "../util/logger.ts"; async function getRequisitions() { const allData = await get>(`/requisitions/`) const data = allData.results.filter(d => d.status === "LN" && d.accounts.length > 0) + if (data.length === 0) { + warn("We couldn't fetch your requisitions. Make sure you have linked at least one account in Nordigen.") + return [] + } + const nAccounts = data.map(d => d.accounts.length).reduce((a, b) => a + b) log(`Fetched ${data.length} requisitions with ${nAccounts} accounts`) @@ -49,6 +54,8 @@ async function getAccount(accountId: string) { export async function getBanks() { const requisitions = await getRequisitions() + if (requisitions.length === 0) return [] + const bankIds = requisitions.map(r => r.institution_id) const requests = bankIds.map((id) => get(`/institutions/${id}`)) const data = await Promise.all(requests) diff --git a/src/util/logger.ts b/src/util/logger.ts index ce2e850..a2b83cb 100644 --- a/src/util/logger.ts +++ b/src/util/logger.ts @@ -8,7 +8,7 @@ function sensibleData(str: string) { final = final.replace(sensibleDataPath, "***...***") if (sensibleDataQuery) final = final.replace(sensibleDataQuery, "***...***") - + return final } @@ -26,4 +26,9 @@ export function log(str: string) { showSeparator = true } console.log(`${emoji} ${sensibleData(str)}${showSeparator ? separator : ''}`) -} \ No newline at end of file +} + +export function warn(str: string) { + const emoji = "⚠️" + console.log(`${emoji} ${sensibleData(str)}${separator}`) +}