-
Notifications
You must be signed in to change notification settings - Fork 480
/
index.ts
40 lines (35 loc) · 1.23 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import * as functions from "firebase-functions";
import * as _ from "lodash";
import { db } from "../config";
import config from "../functionConfig"; // generated using generateConfig.ts
const functionConfig: any = config;
const historySnapshot = (trackedFields: string[]) => async (
change: functions.Change<FirebaseFirestore.DocumentSnapshot>
) => {
const before = change.before.data();
const after = change.after.data();
const docPath = change.after.ref.path;
if (!before || !after) return false;
const trackedChanges: any = {};
trackedFields.forEach((field) => {
if (!_.isEqual(before[field], after[field]))
trackedChanges[field] = after[field];
});
if (!_.isEmpty(trackedChanges)) {
await db
.doc(docPath)
.collection("historySnapshots")
.add({ ...before, archivedAt: new Date() });
return true;
} else return false;
};
const historySnapshotFnsGenerator = (collection) =>
functions.firestore
.document(`${collection.name}/{docId}`)
.onUpdate(historySnapshot(collection.trackedFields));
//export default historySnapshotFnsGenerator;
export const FT_history = {
[functionConfig.name
.replace(/\//g, "_")
.replace(/_{.*?}_/g, "_")]: historySnapshotFnsGenerator(functionConfig),
};