From 720bbdd0cecdbfa630498c88790f754c9d6769d9 Mon Sep 17 00:00:00 2001 From: Shams mosowi Date: Tue, 21 Jul 2020 15:57:29 +1000 Subject: [PATCH] FT_algolia log doc id of error --- .../functions/src/algolia/index.ts | 38 +++++++++++-------- .../functions/src/functionConfig.ts | 17 ++++++++- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/cloud_functions/functions/src/algolia/index.ts b/cloud_functions/functions/src/algolia/index.ts index 5c8d9a46..ecfd4f20 100644 --- a/cloud_functions/functions/src/algolia/index.ts +++ b/cloud_functions/functions/src/algolia/index.ts @@ -90,26 +90,32 @@ const updateAlgolia = ( 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 + try { + const collectionName = snapshot.after.ref.parent.id; - const missingRequiredFields = requiredFields.reduce( - missingFieldsReducer(docData), - [] - ); - if (missingRequiredFields.length > 0) { - throw new Error( - `Missing required fields:${missingRequiredFields.join(", ")}` + const docData = snapshot.after.data(); + if (!docData) return false; // returns if theres no data in the doc + + const missingRequiredFields = requiredFields.reduce( + missingFieldsReducer(docData), + [] ); - } - 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 - const algoliaTask = await index.saveObject({ ...algoliaData, objectID }); // add update algolia entry + 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 + const algoliaTask = await index.saveObject({ ...algoliaData, objectID }); // add update algolia entry - return algoliaTask; + return algoliaTask; + } catch (error) { + console.error(JSON.stringify({ error, objectID })); + return false; + } }; const deleteFromAlgolia = (indexName?: string) => ( diff --git a/cloud_functions/functions/src/functionConfig.ts b/cloud_functions/functions/src/functionConfig.ts index 44462430..0873676d 100644 --- a/cloud_functions/functions/src/functionConfig.ts +++ b/cloud_functions/functions/src/functionConfig.ts @@ -1,2 +1,15 @@ -export const collectionPath = "teams"; -export default []; +export default { + onUpdate: true, + onCreate: false, + source: "advisors", + fieldsToSync: [ + "firstName", + "lastName", + "preferredName", + "profilePhoto", + "email", + "location", + ], + target: "coaches", +}; +export const collectionPath = "";