Skip to content

Instantly share code, notes, and snippets.

@timschafli
Last active May 16, 2021 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timschafli/72900660b8c2f1ff154b390244eab5de to your computer and use it in GitHub Desktop.
Save timschafli/72900660b8c2f1ff154b390244eab5de to your computer and use it in GitHub Desktop.
// **How to set up**
//
// Create two inputData fields: dedupeKey and storageKey
// Generate a unique UUID here: https://www.uuidgenerator.net/ and paste it into the storageKey field
// Use a new and unique storageKey for each deduper you set up as it will use the maximum capacity of a storage account
// Map the ID (or other unique value) from your Zap's Trigger into the dedupeKey field
// Add a Filter after this Code Step to only continue the Zap if 'previouslySeenValue' is false
const { dedupeKey, storageKey } = inputData;
const url = 'https://store.zapier.com/api/records';
const authHeaders = {
'X-Secret': storageKey,
};
// Get all Values from Storage
const getOptions = {
method: 'GET',
headers: authHeaders
};
const getRes = await fetch(url, getOptions);
const getBody = await getRes.json();
// Check current dedupeKey against existing values, return 'previouslySeenValue:true' if there is a match, ending Code Step
const existingDedupeIDs = Object.values(getBody);
if (existingDedupeIDs.includes(dedupeKey)) {
return { dedupeKey, previouslySeenValue: true, storedDedupeKeys: getBody};
}
// If there are 500 keys, prune 1 (Storage can only hold 500)
if (existingDedupeIDs.length > 499) existingDedupeIDs.shift();
// Add current dedupeKey as next key in the array
existingDedupeIDs.push(dedupeKey);
// Write keys back to Storage
const writeBody = {};
existingDedupeIDs.forEach((dedupeKey, index) => {
// we start at 100 for the key name so that all the names have
// 3 characters and stay in the same sort order
const keyName = `${index + 100}`;
Object.assign(writeBody, { [keyName]: dedupeKey });
});
const postOptions = {
method: 'POST',
headers: authHeaders,
body: JSON.stringify(writeBody),
};
const postRes = await fetch(url, postOptions);
const postBody = await postRes.json();
return { dedupeKey, previouslySeenValue: false, storedDedupeKeys: postBody };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment