From e8ec23b03f5aa1cc75e80fd551b78f15a7147900 Mon Sep 17 00:00:00 2001 From: dpsgit Date: Fri, 12 Jul 2024 07:53:54 -0700 Subject: [PATCH 1/3] restore airtable netlify function --- .gitignore | 5 +-- .netlify/functions/airtable.js | 60 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 .netlify/functions/airtable.js diff --git a/.gitignore b/.gitignore index 316c7eb..b17547d 100644 --- a/.gitignore +++ b/.gitignore @@ -32,7 +32,4 @@ npm-debug.log* yarn-debug.log* yarn-error.log* -.env* - -# Local Netlify folder -.netlify +.env* \ No newline at end of file diff --git a/.netlify/functions/airtable.js b/.netlify/functions/airtable.js new file mode 100644 index 0000000..fa8d8d3 --- /dev/null +++ b/.netlify/functions/airtable.js @@ -0,0 +1,60 @@ +// WIP :| + +exports.handler = function (event, context, callback) { + const AIRTABLE_API_KEY = process.env.AIRTABLE_API_KEY; + const AIRTABLE_BASE_ID = process.env.AIRTABLE_BASE_ID; + const Airtable = require("airtable"); + Airtable.configure({ + apiKey: AIRTABLE_API_KEY, + endpointUrl: "https://api.airtable.com", + }); + const base = Airtable.base(AIRTABLE_BASE_ID); + + const allRecords = []; + base("Content Creation") + .select({ + // Selecting the first 3 records in Interview Tracking: + view: "Developer View", + }) + .eachPage( + function page(records, fetchNextPage) { + // This function (`page`) will get called for each page of records. + + records + .filter( + (i) => + i.fields["Latitude"] && + i.fields["Longitude"] && + i.fields["ENTRY COMPLETED"] + ) + .forEach(function (record) { + if ( + record.fields["Level of Anonymity Requested"] === + "Complete Anonymity" + ) + delete record.fields["Name of Interviewee or Anonymous"]; + allRecords.push(record); + }); + + // To fetch the next page of records, call `fetchNextPage`. + // If there are more records, `page` will get called again. + // If there are no more records, `done` will get called. + fetchNextPage(); + }, + function done(err) { + if (err) { + callback(err); + } else { + const response = { + statusCode: 200, + body: JSON.stringify({ records: allRecords }), + headers: { + "content-type": "application/json", + "cache-control": "Cache-Control: max-age=60, public", + }, + }; + callback(null, response); + } + } + ); +}; \ No newline at end of file From 521f4253f99860e3a0728766fff78aa4849e4d67 Mon Sep 17 00:00:00 2001 From: dpsgit Date: Fri, 12 Jul 2024 08:12:14 -0700 Subject: [PATCH 2/3] move dir --- {.netlify/functions => functions}/airtable.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {.netlify/functions => functions}/airtable.js (100%) diff --git a/.netlify/functions/airtable.js b/functions/airtable.js similarity index 100% rename from .netlify/functions/airtable.js rename to functions/airtable.js From 0c706652110b7918e8faa3ef307f8680b77894b6 Mon Sep 17 00:00:00 2001 From: dpsgit Date: Mon, 15 Jul 2024 07:46:14 -0700 Subject: [PATCH 3/3] change to access token --- functions/airtable.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/functions/airtable.js b/functions/airtable.js index fa8d8d3..2c9d4ee 100644 --- a/functions/airtable.js +++ b/functions/airtable.js @@ -1,11 +1,13 @@ // WIP :| exports.handler = function (event, context, callback) { - const AIRTABLE_API_KEY = process.env.AIRTABLE_API_KEY; + // Airtable has transitioned from API keys to access tokens, + // see here: https://airtable.com/developers/web/api/authentication + const AIRTABLE_ACCESS_TOKEN = process.env.AIRTABLE_ACCESS_TOKEN; const AIRTABLE_BASE_ID = process.env.AIRTABLE_BASE_ID; const Airtable = require("airtable"); Airtable.configure({ - apiKey: AIRTABLE_API_KEY, + apiKey: AIRTABLE_ACCESS_TOKEN, endpointUrl: "https://api.airtable.com", }); const base = Airtable.base(AIRTABLE_BASE_ID);