From 7db38bd2dfd60acb4aad5acedbc6ded0cdf9098e Mon Sep 17 00:00:00 2001 From: Zach Lefler Date: Fri, 7 Jun 2024 14:08:39 -0700 Subject: [PATCH] add airtable fetch --- api/src/graphql/airtable.sdl.ts | 18 ++++++ api/src/services/airtable.ts | 41 ++++++++++++ web/package.json | 1 + .../components/Overlays/Info/PaymentsCard.tsx | 63 ++++++++----------- yarn.lock | 23 ++++++- 5 files changed, 107 insertions(+), 39 deletions(-) create mode 100644 api/src/graphql/airtable.sdl.ts create mode 100644 api/src/services/airtable.ts diff --git a/api/src/graphql/airtable.sdl.ts b/api/src/graphql/airtable.sdl.ts new file mode 100644 index 00000000..d2de2307 --- /dev/null +++ b/api/src/graphql/airtable.sdl.ts @@ -0,0 +1,18 @@ +export const schema = gql` + type AirtableTransaction { + platform: String + originalAmount: Float + currency: String + amountInUsd: Float + hash: String + timestamp: String + orgName: String + id: Int + recipientName: String + communityMemberId: Int + } + + type Query { + findEntry(orgName: String): [AirtableTransaction] @skipAuth + } +` diff --git a/api/src/services/airtable.ts b/api/src/services/airtable.ts new file mode 100644 index 00000000..15f41533 --- /dev/null +++ b/api/src/services/airtable.ts @@ -0,0 +1,41 @@ +import Airtable from 'airtable' +const base = new Airtable({ + apiKey: process.env.AIRTABLE_TRANSACTIONS_TOKEN, +}).base('appekZyMlwje7377w') + +export const createNewEntry = async ({ id }) => { + return new Promise((resolve, reject) => { + base('Samples').create( + [{ fields: { 'Sample ID': id } }], + function (err, records) { + if (err) { + console.error(err) + reject(err) + } else { + const record = records[0] + resolve({ id: record.getId() }) + } + } + ) + }) +} + +export const findEntry = async (orgName) => { + const encodedFormula = encodeURIComponent(`{orgName} = '${orgName}'`) + + const url = `https://api.airtable.com/v0/appekZyMlwje7377w/Fiat%20Transactions?filterByFormula=${encodedFormula}` + + const res = await fetch(url, { + headers: { + Authorization: 'Bearer ' + process.env.AIRTABLE_TRANSACTIONS_TOKEN, + }, + }) + const data = await res.json() + console.log(data) + if (data.records.length === 0) { + return null + } + const fields = data.records.map((record) => record.fields) + + return fields +} diff --git a/web/package.json b/web/package.json index 519b8654..975de7a5 100644 --- a/web/package.json +++ b/web/package.json @@ -18,6 +18,7 @@ "@redwoodjs/forms": "4.3.1", "@redwoodjs/router": "4.3.1", "@redwoodjs/web": "4.3.1", + "airtable": "^0.12.2", "prop-types": "15.8.1", "react": "17.0.2", "react-dom": "17.0.2", diff --git a/web/src/components/Overlays/Info/PaymentsCard.tsx b/web/src/components/Overlays/Info/PaymentsCard.tsx index 7d671fcf..93112e50 100644 --- a/web/src/components/Overlays/Info/PaymentsCard.tsx +++ b/web/src/components/Overlays/Info/PaymentsCard.tsx @@ -2,6 +2,8 @@ import { useEffect, useState } from 'react' import useAxios from 'axios-hooks' +import { useQuery } from '@redwoodjs/web' + import { CELO_EAS_SCAN_API } from 'src/utils/apiUrls' import ThemedSkeleton from '../../Map/components/Skeleton' @@ -25,49 +27,34 @@ export const PaymentCard = ({ activeProjectData }) => { }, { manual: true } ) + const FETCH_FIAT_TRANSACTIONS = gql` + query findEntry($orgName: String!) { + findEntry(orgName: $orgName) { + platform + originalAmount + currency + amountInUsd + hash + timestamp + orgName + id + recipientName + communityMemberId + } + } + ` + const { data: fiatData } = useQuery(FETCH_FIAT_TRANSACTIONS, { + variables: { orgName: activeProjectData.project.name }, + skip: !activeProjectData.name, + }) useEffect(() => { const fetchData = async () => { - const fetchFiatPayments = async () => { - const res = await fetch( - `${process.env.GAINFOREST_ENDPOINT}/api/graphql`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - query: ` - query { - fiatTransactionsByProjectId(id:"${activeProjectData.project.id}") { - amountInUsd - timestamp - currency - originalAmount - firstName - lastName - profileUrl - } - } - `, - }), - } - ) - const result = await res.json() - return result.data.fiatTransactionsByProjectId.map((payment) => { - return { - ...payment, - amount: payment.amountInUsd, - currency: `Fiat (${payment.currency})`, - } - }) - } - setLoading(true) + const fiatPayments = await fiatData const cryptoPayments = await fetchCryptoPayments() - const fiatPayments = await fetchFiatPayments() - if (fiatPayments.length > 0) { + if (fiatPayments?.length > 0) { setShowFiatMessage(true) } const allPayments = [...cryptoPayments, ...fiatPayments] @@ -82,7 +69,7 @@ export const PaymentCard = ({ activeProjectData }) => { setLoading(false) } fetchData() - }, [activeProjectData]) + }, [activeProjectData, fiatData]) const fetchCryptoPayments = async () => { let celoRecipients = [] diff --git a/yarn.lock b/yarn.lock index 1f1ddf90..ce2ccb00 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7416,6 +7416,13 @@ __metadata: languageName: node linkType: hard +"@types/node@npm:>=8.0.0 <15": + version: 14.18.63 + resolution: "@types/node@npm:14.18.63" + checksum: 626a371419a6a0e11ca460b22bb4894abe5d75c303739588bc96267e380aa8b90ba5a87bc552400584f0ac2a84b5c458dadcbcf0dfd2396ebeb765f7a7f95893 + languageName: node + linkType: hard + "@types/node@npm:^10.1.0": version: 10.17.60 resolution: "@types/node@npm:10.17.60" @@ -8562,7 +8569,7 @@ __metadata: languageName: node linkType: hard -"abortcontroller-polyfill@npm:^1.7.3": +"abortcontroller-polyfill@npm:^1.4.0, abortcontroller-polyfill@npm:^1.7.3": version: 1.7.5 resolution: "abortcontroller-polyfill@npm:1.7.5" checksum: d7a5ab6fda4f9a54f22ddeb233a2564d2f4f857ec17be25fee21a91bb5090bee57c630c454634b5c4b93fc06bd90d592d1f2fc69f77cd28791ac0fe361feb7d2 @@ -8717,6 +8724,19 @@ __metadata: languageName: node linkType: hard +"airtable@npm:^0.12.2": + version: 0.12.2 + resolution: "airtable@npm:0.12.2" + dependencies: + "@types/node": ">=8.0.0 <15" + abort-controller: ^3.0.0 + abortcontroller-polyfill: ^1.4.0 + lodash: ^4.17.21 + node-fetch: ^2.6.7 + checksum: 80a23da4826c57676bfa4835675463c951e3e6578301655ed190f1a7237774425f8e333b2ea8eacf68ea71b6b1987b277e984bb1cff9ef40baabf2301e3c3adf + languageName: node + linkType: hard + "ajv-errors@npm:^1.0.0": version: 1.0.1 resolution: "ajv-errors@npm:1.0.1" @@ -26038,6 +26058,7 @@ __metadata: "@redwoodjs/forms": 4.3.1 "@redwoodjs/router": 4.3.1 "@redwoodjs/web": 4.3.1 + airtable: ^0.12.2 prop-types: 15.8.1 react: 17.0.2 react-dom: 17.0.2