Created
October 25, 2018 12:38
-
-
Save nkammah/9f00afe4eeafd74292ced56dc6e82c39 to your computer and use it in GitHub Desktop.
Zap - pull Altru Data and process new records only
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Altru Credentials are passed around encoded - here's how to encode them : | |
// 1) go to https://www.base64encode.org/ | |
// 2) paste the "username:password" | |
// 3) click encode | |
// 4) put the result below, between the qotes | |
var creds = '' | |
// This code pulls data from Altru via an OData link. The query used in Altru is "Ad-hoc Query: Daily MailChimp Welcome Email for Active Members" | |
// If you need to use a different query, update the OData link below | |
var odata_link = '' | |
// This script stores into zapier the membershipID's that were last processed so that we don't process the same records over and over every day. The credentials to store/retrieve data from the zapier store were generated randomely at https://store.zapier.com/api/secret | |
var storeSecret = ''; | |
//////////////////// DO NO CHANGE THE BELOW CODE ///////////////////////// | |
// Retrieve the last membership IDs processed | |
var store = StoreClient(storeSecret); | |
console.log("Retrieving last stored ids"); | |
var lastRecorded = await store.get('lastRecorded') || []; | |
console.log(lastRecorded) | |
// Fetch Altru records | |
var options={ | |
method : 'get', | |
contentType: 'application/json', | |
headers: {'Authorization' : "Basic " + creds} | |
} | |
const result = await fetch(odata_link,options) | |
const content = await result.json() | |
var altru_records = content['value'] | |
// Go over the altru records and save all the ID's into an array and the new records into another array for downstream processing | |
var new_records = []; | |
var altru_records_ids = [] | |
for (var i = 0; i < altru_records.length; i++) { | |
var rec = altru_records[i]; | |
var rec_id = rec['MembershipID']; | |
altru_records_ids.push(rec_id) | |
// is this record ID in the data that were processed last time the zap ran? | |
if (lastRecorded.indexOf(rec_id) == -1) { | |
// if not, it means it's new - so process it | |
new_records.push(rec); | |
} | |
} | |
// Store all the ids that were just seen | |
await store.set("lastRecorded", JSON.stringify(altru_records_ids)) | |
console.log("New Altru records") | |
console.log(new_records) | |
return new_records |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment