Skip to content

Commit

Permalink
added checks before reduce and added warn
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax committed Nov 19, 2022
1 parent 46a2c23 commit 1e74c1a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ 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[] = []

const hasNordigen = Deno.env.get("NORDIGEN_KEY") && Deno.env.get("NORDIGEN_SECRET")
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")
}
9 changes: 8 additions & 1 deletion src/nordigen/fetcher.ts
Original file line number Diff line number Diff line change
@@ -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<INordigenPagination<INordigenRequisition>>(`/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`)

Expand Down Expand Up @@ -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<INordigenBank>(`/institutions/${id}`))
const data = await Promise.all(requests)
Expand Down
9 changes: 7 additions & 2 deletions src/util/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function sensibleData(str: string) {
final = final.replace(sensibleDataPath, "***...***")
if (sensibleDataQuery)
final = final.replace(sensibleDataQuery, "***...***")

return final
}

Expand All @@ -26,4 +26,9 @@ export function log(str: string) {
showSeparator = true
}
console.log(`${emoji} ${sensibleData(str)}${showSeparator ? separator : ''}`)
}
}

export function warn(str: string) {
const emoji = "⚠️"
console.log(`${emoji} ${sensibleData(str)}${separator}`)
}

0 comments on commit 1e74c1a

Please sign in to comment.