message: sync:optional forced updates and change comparson
diff: @@ -2,6 +2,7 @@ import * as functions from "firebase-functions";
  import { db } from "../config";
  +import * as _ from "lodash";
  // returns object of fieldsToSync
  const docReducer = (docData: FirebaseFirestore.DocumentData) => (
  acc: any,
  @@ -26,7 +27,7 @@ const cloneDoc = (targetCollection: string, fieldsToSync: string[]) => (
  if (!docData) return false; // returns if theres no data in the doc
  const syncData = fieldsToSync.reduce(docReducer(docData), {});
  if (Object.keys(syncData).length === 0) return false; // returns if theres nothing to sync
  - console.log(`creating new doc ${docId}`);
  + console.log(`creating new doc or forcing update ${docId}`);
  db.collection(targetCollection)
  .doc(docId)
  .set(syncData, { merge: true })
  @@ -71,11 +72,25 @@ const syncDoc = (targetCollection: string, fieldsToSync: string[]) => async (
  * @param targetCollection
  * @param fieldsToSync
  */
  -const syncDocOnUpdate = (targetCollection: string, fieldsToSync: string[]) => (
  - snapshot: functions.Change<FirebaseFirestore.DocumentSnapshot>
  -) => {
  +const syncDocOnUpdate = (
  + targetCollection: string,
  + fieldsToSync: string[],
  + forcedUpdate: boolean
  +) => (snapshot: functions.Change<FirebaseFirestore.DocumentSnapshot>) => {
  // TODO: compare before and after for fields to sync
  + const afterData = snapshot.after.data();
  + const beforeData = snapshot.after.data();
  + const hasChanged = !_.isEqual(afterData, beforeData);
  + if (hasChanged) {
  + if (forcedUpdate === true) {
  + return cloneDoc(targetCollection, fieldsToSync)(snapshot.after);
  + } else {
  return syncDoc(targetCollection, fieldsToSync)(snapshot.after);
  + }
  + } else {
  + console.warn("no change detected");
  + return false;
  + }
  };
  /**
  @@ -92,7 +107,13 @@ const collectionSyncFnsGenerator = collection =>
  onUpdate: collection.onUpdate
  ? functions.firestore
  .document(`${collection.source}/{docId}`)
  - .onUpdate(syncDocOnUpdate(collection.target, collection.fieldsToSync))
  + .onUpdate(
  + syncDocOnUpdate(
  + collection.target,
  + collection.fieldsToSync,
  + collection.forcedUpdate
  + )
  + )
  : null,
  }).reduce((a, [k, v]) => (v === null ? a : { ...a, [k]: v }), {});
  