From 526f28e0e1c6ef45bf18195d24033173a7f79f5a Mon Sep 17 00:00:00 2001 From: Shams mosowi Date: Fri, 14 Feb 2020 10:27:32 +1100 Subject: [PATCH] algolia config on firetstore --- .gitignore | 1 + cloud_functions/functions/fetchConfig.ts | 5 +- .../functions/src/algolia/algoliaConfig.ts | 78 ------------------- .../functions/src/algolia/index.ts | 25 +++++- cloud_functions/functions/src/index.ts | 25 +++--- 5 files changed, 38 insertions(+), 96 deletions(-) delete mode 100644 cloud_functions/functions/src/algolia/algoliaConfig.ts diff --git a/.gitignore b/.gitignore index 052a1a54..f545dda7 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ cloud_functions/functions/lib cloud_functions/functions/src/collectionSync/config.json cloud_functions/functions/src/history/config.json +cloud_functions/functions/src/algolia/config.json cloud_functions/functions/firebase-credentials.json # misc diff --git a/cloud_functions/functions/fetchConfig.ts b/cloud_functions/functions/fetchConfig.ts index 68f6ad7d..4d1a53e0 100644 --- a/cloud_functions/functions/fetchConfig.ts +++ b/cloud_functions/functions/fetchConfig.ts @@ -25,11 +25,14 @@ const main = async () => { "_FIRETABLE_/_SETTINGS_/_CONFIG_/_HISTORY_", "./src/history/config.json" ); + await docConfig2json( + "_FIRETABLE_/_SETTINGS_/_CONFIG_/_ALGOLIA_", + "./src/algolia/config.json" + ); await docConfig2json( "_FIRETABLE_/_SETTINGS_/_CONFIG_/_COLLECTION_SYNC_", "./src/collectionSync/config.json" ); - return true; }; diff --git a/cloud_functions/functions/src/algolia/algoliaConfig.ts b/cloud_functions/functions/src/algolia/algoliaConfig.ts deleted file mode 100644 index f41882fa..00000000 --- a/cloud_functions/functions/src/algolia/algoliaConfig.ts +++ /dev/null @@ -1,78 +0,0 @@ -import * as _ from "lodash"; -const filterSnapshot = ( - field: { docPath: string; snapshot: any }, - preservedKeys: string[] -) => { - return { - docPath: field.docPath, - ...preservedKeys.reduce((acc: any, currentKey: string) => { - const value = _.get(field.snapshot, currentKey); - if (value) { - return { ...acc, snapshot: { [currentKey]: value, ...acc.snapshot } }; - } else return acc; - }, {}), - }; -}; -const algoliaConfig = [ - { - name: "founders", - fieldsToSync: [ - "firstName", - "lastName", - "founderType", - "cohort", - "employerLogos", - ], - }, - { - name: "advisors", - fieldsToSync: [ - "firstName", - "lastName", - "location", - "title", - "type", - "bio", - "linkedIn", - "expertise", - "profilePhoto", - "bookingLink", - "twitter", - "antlerPoc", - ], - }, - { - name: "teams", - fieldsToSync: [ - "createdAt", - "createdBy", - "updatedAt", - "updatedBy", - "cohort", - "teamName", - "trackOutDate", - { - fieldName: "teamMembers", - transformer: (teamMembers: any[]) => { - return teamMembers.map(member => - filterSnapshot(member, [ - "cohort", - "firstName", - "lastName", - "preferredName", - "profilePhoto", - ]) - ); - }, - }, - "focusArea", - "isDissolved", - "oneLineDescription", - ], - }, - { - name: "cohorts", - fieldsToSync: ["cohort", "location", "demoDay"], - }, -]; -export default algoliaConfig; diff --git a/cloud_functions/functions/src/algolia/index.ts b/cloud_functions/functions/src/algolia/index.ts index 803a41e7..e4e05c4e 100644 --- a/cloud_functions/functions/src/algolia/index.ts +++ b/cloud_functions/functions/src/algolia/index.ts @@ -1,6 +1,6 @@ import * as algoliasearch from "algoliasearch"; import * as functions from "firebase-functions"; - +import * as _ from "lodash"; import { env } from "../config"; const APP_ID = env.algolia.app; @@ -8,10 +8,25 @@ const ADMIN_KEY = env.algolia.key; const client = algoliasearch(APP_ID, ADMIN_KEY); +const filterSnapshot = ( + field: { docPath: string; snapshot: any }, + preservedKeys: string[] +) => { + return { + docPath: field.docPath, + ...preservedKeys.reduce((acc: any, currentKey: string) => { + const value = _.get(field.snapshot, currentKey); + if (value) { + return { ...acc, snapshot: { [currentKey]: value, ...acc.snapshot } }; + } else return acc; + }, {}), + }; +}; + // returns object of fieldsToSync const algoliaReducer = (docData: FirebaseFirestore.DocumentData) => ( acc: any, - curr: string | { fieldName: string; transformer: Function } + curr: string | { fieldName: string; snapshotFields: string[] } ) => { if (typeof curr === "string") { if (docData[curr] && typeof docData[curr].toDate === "function") { @@ -25,10 +40,12 @@ const algoliaReducer = (docData: FirebaseFirestore.DocumentData) => ( return acc; } } else { - if (docData[curr.fieldName]) { + if (docData[curr.fieldName] && curr.snapshotFields) { return { ...acc, - [curr.fieldName]: curr.transformer(docData[curr.fieldName]), + [curr.fieldName]: docData[curr.fieldName].map(snapshot => + filterSnapshot(snapshot, curr.snapshotFields) + ), }; } else { return acc; diff --git a/cloud_functions/functions/src/index.ts b/cloud_functions/functions/src/index.ts index edd8c2e4..cf74ca82 100644 --- a/cloud_functions/functions/src/index.ts +++ b/cloud_functions/functions/src/index.ts @@ -1,5 +1,5 @@ import algoliaFnsGenerator from "./algolia"; -import algoliaConfig from "./algolia/algoliaConfig"; +import * as algoliaConfig from "./algolia/config.json"; import collectionSyncFnsGenerator from "./collectionSync"; import * as collectionSyncConfig from "./collectionSync/config.json"; @@ -8,12 +8,12 @@ import * as collectionHistoryConfig from "./history/config.json"; export { exportTable } from "./export"; import * as callableFns from "./callable"; -export const callable = callableFns; -export const algolia = algoliaConfig.reduce((acc: any, collection) => { +const callable = callableFns; +const algolia = algoliaConfig.reduce((acc: any, collection) => { return { ...acc, [collection.name]: algoliaFnsGenerator(collection) }; }, {}); -export const sync = collectionSyncConfig.reduce((acc: any, collection) => { +const sync = collectionSyncConfig.reduce((acc: any, collection) => { return { ...acc, [`${collection.source}2${collection.target}`]: collectionSyncFnsGenerator( @@ -22,12 +22,11 @@ export const sync = collectionSyncConfig.reduce((acc: any, collection) => { }; }, {}); -export const history = collectionHistoryConfig.reduce( - (acc: any, collection) => { - return { - ...acc, - [collection.name]: collectionSnapshotFnsGenerator(collection), - }; - }, - {} -); +const history = collectionHistoryConfig.reduce((acc: any, collection) => { + return { + ...acc, + [collection.name]: collectionSnapshotFnsGenerator(collection), + }; +}, {}); + +export const FIRETABLE = { callable, algolia, sync, history };