Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add airtable fetch #116

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions api/src/graphql/airtable.sdl.ts
Original file line number Diff line number Diff line change
@@ -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
}
`
41 changes: 41 additions & 0 deletions api/src/services/airtable.ts
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
63 changes: 25 additions & 38 deletions web/src/components/Overlays/Info/PaymentsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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]
Expand All @@ -82,7 +69,7 @@ export const PaymentCard = ({ activeProjectData }) => {
setLoading(false)
}
fetchData()
}, [activeProjectData])
}, [activeProjectData, fiatData])

const fetchCryptoPayments = async () => {
let celoRecipients = []
Expand Down
23 changes: 22 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down