From 761f895b0f8fd03633442f85b56df10112131785 Mon Sep 17 00:00:00 2001 From: Shams mosowi Date: Tue, 14 Jul 2020 16:49:23 +1000 Subject: [PATCH] algolia added optional requiredFields --- .../functions/src/algolia/index.ts | 64 +++++++++++++++---- firetable | 1 - 2 files changed, 53 insertions(+), 12 deletions(-) delete mode 160000 firetable diff --git a/cloud_functions/functions/src/algolia/index.ts b/cloud_functions/functions/src/algolia/index.ts index 685a3ef1..b6b4c28d 100644 --- a/cloud_functions/functions/src/algolia/index.ts +++ b/cloud_functions/functions/src/algolia/index.ts @@ -8,6 +8,12 @@ const ADMIN_KEY = env.algolia.key; const client = algoliasearch(APP_ID, ADMIN_KEY); +const missingFieldsReducer = (data: any) => (acc: string[], curr: string) => { + if (data[curr] === undefined) { + return [...acc, curr]; + } else return acc; +}; + const filterSnapshot = ( field: { docPath: string; snapshot: any }, preservedKeys: string[] @@ -53,30 +59,55 @@ const algoliaReducer = (docData: FirebaseFirestore.DocumentData) => ( } }; -const addToAlgolia = (fieldsToSync: string[], indexName?: string) => ( - snapshot: FirebaseFirestore.DocumentSnapshot -) => { +const addToAlgolia = ( + fieldsToSync: string[], + requiredFields: string[], + indexName?: string +) => (snapshot: FirebaseFirestore.DocumentSnapshot) => { const collectionName = snapshot.ref.parent.id; const objectID = snapshot.id; const docData = snapshot.data(); if (!docData) return false; // returns if theres no data in the doc + const missingRequiredFields = requiredFields.reduce( + missingFieldsReducer(docData), + [] + ); + if (missingRequiredFields.length > 0) { + throw new Error( + `Missing required fields:${missingRequiredFields.join(", ")}` + ); + } const algoliaData = fieldsToSync.reduce(algoliaReducer(docData), {}); if (Object.keys(algoliaData).length === 0) return false; // returns if theres nothing to sync const index = client.initIndex(indexName ? indexName : collectionName); // initialize algolia index return index.addObject({ ...algoliaData, objectID }); // add new algolia entry }; -const updateAlgolia = (fieldsToSync: string[], indexName?: string) => ( - snapshot: functions.Change -) => { +const updateAlgolia = ( + fieldsToSync: string[], + requiredFields: string[], + indexName?: string +) => async (snapshot: functions.Change) => { const collectionName = snapshot.after.ref.parent.id; const objectID = snapshot.after.id; const docData = snapshot.after.data(); if (!docData) return false; // returns if theres no data in the doc + + const missingRequiredFields = requiredFields.reduce( + missingFieldsReducer(docData), + [] + ); + if (missingRequiredFields.length > 0) { + throw new Error( + `Missing required fields:${missingRequiredFields.join(", ")}` + ); + } const algoliaData = fieldsToSync.reduce(algoliaReducer(docData), {}); if (Object.keys(algoliaData).length === 0) return false; // returns if theres nothing to sync const index = client.initIndex(indexName ? indexName : collectionName); // initialize algolia index - return index.saveObject({ ...algoliaData, objectID }); // add update algolia entry + const algoliaTask = await index.saveObject({ ...algoliaData, objectID }); // add update algolia entry + + return algoliaTask; }; const deleteFromAlgolia = (indexName?: string) => ( @@ -100,10 +131,14 @@ const algoliaFnsGenerator = collection => ({ ? (snapshot: FirebaseFirestore.DocumentSnapshot) => Promise.all( collection.indices.map(index => - addToAlgolia(index.fieldsToSync, index.name)(snapshot) + addToAlgolia( + index.fieldsToSync, + collection.requiredFields ?? [], + index.name + )(snapshot) ) ) - : addToAlgolia(collection.fieldsToSync) + : addToAlgolia(collection.fieldsToSync, collection.requiredFields ?? []) ), onUpdate: functions.firestore .document(`${collection.name}/{docId}`) @@ -112,10 +147,17 @@ const algoliaFnsGenerator = collection => ({ ? snapshot => Promise.all( collection.indices.map(index => - updateAlgolia(index.fieldsToSync, index.name)(snapshot) + updateAlgolia( + index.fieldsToSync, + collection.requiredFields ?? [], + index.name + )(snapshot) ) ) - : updateAlgolia(collection.fieldsToSync) + : updateAlgolia( + collection.fieldsToSync, + collection.requiredFields ?? [] + ) ), onDelete: functions.firestore .document(`${collection.name}/{docId}`) diff --git a/firetable b/firetable deleted file mode 160000 index 655a4ec3..00000000 --- a/firetable +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 655a4ec3985e6359416a5b2501bf878ce7a8830b