emberflow 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +40 -0
  3. package/lib/db-structure.d.ts +7 -0
  4. package/lib/db-structure.js +67 -0
  5. package/lib/db-structure.js.map +1 -0
  6. package/lib/index-utils.d.ts +20 -0
  7. package/lib/index-utils.js +324 -0
  8. package/lib/index-utils.js.map +1 -0
  9. package/lib/index.d.ts +23 -0
  10. package/lib/index.js +197 -0
  11. package/lib/index.js.map +1 -0
  12. package/lib/init-db-structure.d.ts +17 -0
  13. package/lib/init-db-structure.js +111 -0
  14. package/lib/init-db-structure.js.map +1 -0
  15. package/lib/logics/index.d.ts +2 -0
  16. package/lib/logics/index.js +77 -0
  17. package/lib/logics/index.js.map +1 -0
  18. package/lib/logics/view-logics.d.ts +3 -0
  19. package/lib/logics/view-logics.js +167 -0
  20. package/lib/logics/view-logics.js.map +1 -0
  21. package/lib/sample-custom/business-logics.d.ts +3 -0
  22. package/lib/sample-custom/business-logics.js +67 -0
  23. package/lib/sample-custom/business-logics.js.map +1 -0
  24. package/lib/sample-custom/db-structure.d.ts +29 -0
  25. package/lib/sample-custom/db-structure.js +36 -0
  26. package/lib/sample-custom/db-structure.js.map +1 -0
  27. package/lib/sample-custom/security.d.ts +2 -0
  28. package/lib/sample-custom/security.js +63 -0
  29. package/lib/sample-custom/security.js.map +1 -0
  30. package/lib/sample-custom/validators.d.ts +9 -0
  31. package/lib/sample-custom/validators.js +42 -0
  32. package/lib/sample-custom/validators.js.map +1 -0
  33. package/lib/tests/index-utils.test.d.ts +1 -0
  34. package/lib/tests/index-utils.test.js +600 -0
  35. package/lib/tests/index-utils.test.js.map +1 -0
  36. package/lib/tests/index.test.d.ts +1 -0
  37. package/lib/tests/index.test.js +496 -0
  38. package/lib/tests/index.test.js.map +1 -0
  39. package/lib/tests/init-db-structure.test.d.ts +1 -0
  40. package/lib/tests/init-db-structure.test.js +114 -0
  41. package/lib/tests/init-db-structure.test.js.map +1 -0
  42. package/lib/tests/logics/index.test.d.ts +1 -0
  43. package/lib/tests/logics/index.test.js +127 -0
  44. package/lib/tests/logics/index.test.js.map +1 -0
  45. package/lib/tests/logics/view-logics.test.d.ts +1 -0
  46. package/lib/tests/logics/view-logics.test.js +211 -0
  47. package/lib/tests/logics/view-logics.test.js.map +1 -0
  48. package/lib/tests/utils/misc.test.d.ts +1 -0
  49. package/lib/tests/utils/misc.test.js +31 -0
  50. package/lib/tests/utils/misc.test.js.map +1 -0
  51. package/lib/tests/utils/paths.test.d.ts +1 -0
  52. package/lib/tests/utils/paths.test.js +258 -0
  53. package/lib/tests/utils/paths.test.js.map +1 -0
  54. package/lib/tests/utils/query.test.d.ts +1 -0
  55. package/lib/tests/utils/query.test.js +72 -0
  56. package/lib/tests/utils/query.test.js.map +1 -0
  57. package/lib/tests/utils/sample.test.d.ts +1 -0
  58. package/lib/tests/utils/sample.test.js +52 -0
  59. package/lib/tests/utils/sample.test.js.map +1 -0
  60. package/lib/types.d.ts +78 -0
  61. package/lib/types.js +3 -0
  62. package/lib/types.js.map +1 -0
  63. package/lib/utils/db-structure.d.ts +2 -0
  64. package/lib/utils/db-structure.js +9 -0
  65. package/lib/utils/db-structure.js.map +1 -0
  66. package/lib/utils/misc.d.ts +1 -0
  67. package/lib/utils/misc.js +53 -0
  68. package/lib/utils/misc.js.map +1 -0
  69. package/lib/utils/paths.d.ts +16 -0
  70. package/lib/utils/paths.js +136 -0
  71. package/lib/utils/paths.js.map +1 -0
  72. package/lib/utils/query.d.ts +2 -0
  73. package/lib/utils/query.js +41 -0
  74. package/lib/utils/query.js.map +1 -0
  75. package/lib/utils/sample.d.ts +5 -0
  76. package/lib/utils/sample.js +37 -0
  77. package/lib/utils/sample.js.map +1 -0
  78. package/lib/utils.d.ts +12 -0
  79. package/lib/utils.js +67 -0
  80. package/lib/utils.js.map +1 -0
  81. package/package.json +37 -0
  82. package/src/sample-custom/business-logics.ts +51 -0
  83. package/src/sample-custom/db-structure.ts +33 -0
  84. package/src/sample-custom/security.ts +77 -0
  85. package/src/sample-custom/validators.ts +46 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Aris Isais
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Emberflow
2
+
3
+ Emberflow is a library for Firebase Functions that simplifies the process of setting up security, validation, and business logic.
4
+
5
+ ## Usage
6
+
7
+ To use Emberflow in your Firebase Functions project, follow these steps:
8
+
9
+ 1. Install Emberflow:
10
+
11
+ ```sh
12
+ npm install emberflow
13
+ ```
14
+
15
+ 2. Import and initialize Emberflow in your Firebase Functions `view-logics.ts` file:
16
+
17
+ ```typescript
18
+ import * as admin from "firebase-admin";
19
+ import {dbStructure, Entity} from "./db-structure";
20
+ import {initializeEmberFlow} from "@primeiq/emberflow";
21
+ import {securityConfig} from "./security";
22
+ import {validatorConfig} from "./validators";
23
+ import {logics} from "./business-logics";
24
+
25
+ admin.initializeApp();
26
+ const {functionsConfig} = initializeEmberFlow(
27
+ admin,
28
+ dbStructure,
29
+ Entity,
30
+ securityConfig,
31
+ validatorConfig,
32
+ logics
33
+ );
34
+
35
+ Object.entries(functionsConfig).forEach(([key, value]) => {
36
+ exports[key] = value;
37
+ });
38
+ ```
39
+ This example assumes you have the necessary files **(db-structure.ts, security.ts, validators.ts, and business-logics.ts)** in your project. For reference on how to set up these files, you can check the **src.sample-custom** folder in the Emberflow library.
40
+
@@ -0,0 +1,7 @@
1
+ export declare function initDbStructure(dbStructure: Record<string, object>, Entity: Record<string, string>): {
2
+ docPaths: Record<string, string>;
3
+ docPathsRegex: Record<string, RegExp>;
4
+ colPaths: {
5
+ [key: string]: string;
6
+ };
7
+ };
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.initDbStructure = void 0;
4
+ function traverseBFS(obj) {
5
+ const paths = [];
6
+ const queue = [];
7
+ // Enqueue the root object with an empty path
8
+ queue.push({ node: obj, path: "" });
9
+ while (queue.length > 0) {
10
+ // Dequeue the next node
11
+ const item = queue.shift();
12
+ if (!item)
13
+ continue;
14
+ const node = item.node;
15
+ const path = item.path;
16
+ // Only add non-blank paths to the paths array
17
+ if (path !== "") {
18
+ paths.push(path);
19
+ }
20
+ // Enqueue the child objects with their paths
21
+ for (const key in node) {
22
+ if (typeof node[key] === "object" && node[key] !== null) {
23
+ const childPath = path === "" ? key : `${path}/${key}`;
24
+ queue.push({ node: node[key], path: childPath });
25
+ }
26
+ }
27
+ }
28
+ return paths;
29
+ }
30
+ function mapDocPaths(paths, Entity) {
31
+ const docPathsMap = {};
32
+ for (const entityKey of Object.values(Entity)) {
33
+ const entityPaths = paths.filter((path) => path.endsWith(`/${entityKey}`));
34
+ if (entityPaths.length > 0) {
35
+ const entityPath = entityPaths[0];
36
+ docPathsMap[entityKey] = entityPath.split("/").map((element) => {
37
+ if (Object.values(Entity).includes(element)) {
38
+ return `{${element}Id}`;
39
+ }
40
+ else {
41
+ return element;
42
+ }
43
+ }).join("/");
44
+ }
45
+ }
46
+ return docPathsMap;
47
+ }
48
+ function mapColPaths(docPathsMap) {
49
+ const colPathsMap = {};
50
+ for (const [entityKey, docPath] of Object.entries(docPathsMap)) {
51
+ colPathsMap[entityKey] = docPath.split("/").slice(0, -1).join("/");
52
+ }
53
+ return colPathsMap;
54
+ }
55
+ function initDbStructure(dbStructure, Entity) {
56
+ const paths = traverseBFS(dbStructure);
57
+ const docPaths = mapDocPaths(paths, Entity);
58
+ const docPathsRegex = {};
59
+ for (const [key, value] of Object.entries(docPaths)) {
60
+ const regexPattern = value.replace(/{(\w+)Id}/g, "([^/]+)");
61
+ docPathsRegex[key] = new RegExp(`^${regexPattern}$`);
62
+ }
63
+ const colPaths = mapColPaths(docPaths);
64
+ return { docPaths, docPathsRegex, colPaths };
65
+ }
66
+ exports.initDbStructure = initDbStructure;
67
+ //# sourceMappingURL=db-structure.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"db-structure.js","sourceRoot":"","sources":["../src/db-structure.ts"],"names":[],"mappings":";;;AACA,SAAS,WAAW,CAAC,GAA2B;IAC9C,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,MAAM,KAAK,GAAqD,EAAE,CAAC;IAEnE,6CAA6C;IAC7C,KAAK,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,EAAC,CAAC,CAAC;IAElC,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE;QACvB,wBAAwB;QACxB,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;QAC3B,IAAI,CAAC,IAAI;YAAE,SAAS;QAEpB,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACvB,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,C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CAAC;iBAChB;YACH,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;SACd;KACF;IAED,OAAO,WAAW,CAAC;AACrB,CAAC;AAED,SAAS,WAAW,CAAC,WAAsC;IACzD,MAAM,WAAW,GAA8B,EAAE,CAAC;IAElD,KAAK,MAAM,CAAC,SAAS,EAAE,OAAO,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE;QAC9D,WAAW,CAAC,SAAS,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;KACpE;IAED,OAAO,WAAW,CAAC;AACrB,CAAC;AAED,SAAgB,eAAe,CAAC,WAAmC,EAAE,MAA8B;IACjG,MAAM,KAAK,GAAG,WAAW,CAAC,WAAW,CAAC,CAAC;IACvC,MAAM,QAAQ,GAAG,WAAW,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAC5C,MAAM,aAAa,GAA2B,EAA4B,CAAC;IAC3E,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;QACnD,MAAM,YAAY,GAAG,KAAK,CAAC,OAAO,CAAC,YAAY,EAAE,SAAS,CAAC,CAAC;QAC5D,aAAa,CAAC,GAAG,CAAC,GAAG,IAAI,MAAM,CAAC,IAAI,YAAY,GAAG,CAAC,CAAC;KACtD;IACD,MAAM,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC;IACvC,OAAO,EAAC,QAAQ,EAAE,aAAa,EAAE,QAAQ,EAAC,CAAC;AAC7C,CAAC;AAVD,0CAUC"}
@@ -0,0 +1,20 @@
1
+ import { Action, LogicActionType, LogicResult, LogicResultDoc, SecurityFn, ValidateFormResult } from "./types";
2
+ import { firestore } from "firebase-admin";
3
+ import DocumentData = firestore.DocumentData;
4
+ export declare const _mockable: {
5
+ getViewLogicsConfig: () => import("./types").ViewLogicConfig[];
6
+ };
7
+ export declare function distribute(docsByDstPath: Map<string, LogicResultDoc>): Promise<void>;
8
+ export declare function revertModificationsOutsideForm(document: FirebaseFirestore.DocumentData, beforeDocument: FirebaseFirestore.DocumentData | null | undefined, snapshot: FirebaseFirestore.DocumentSnapshot): Promise<void>;
9
+ export declare function validateForm(entity: string, document: FirebaseFirestore.DocumentData, docPath: string): Promise<ValidateFormResult>;
10
+ export declare function getFormModifiedFields(document: DocumentData): string[];
11
+ export declare function delayFormSubmissionAndCheckIfCancelled(delay: number, snapshot: firestore.DocumentSnapshot): Promise<boolean>;
12
+ export declare function runBusinessLogics(actionType: LogicActionType, formModifiedFields: string[], entity: string, action: Action): Promise<LogicResult[]>;
13
+ export declare function groupDocsByUserAndDstPath(docsByDstPath: Map<string, LogicResultDoc>, userId: string): {
14
+ userDocsByDstPath: Map<string, LogicResultDoc>;
15
+ otherUsersDocsByDstPath: Map<string, LogicResultDoc>;
16
+ };
17
+ export declare function getSecurityFn(entity: string): SecurityFn;
18
+ export declare function consolidateAndGroupByDstPath(logicResults: LogicResult[]): Map<string, LogicResultDoc>;
19
+ export declare function runViewLogics(dstPathLogicDocsMap: Map<string, LogicResultDoc>): Promise<LogicResult[]>;
20
+ export declare function runPeerSyncViews(userDocsByDstPath: Map<string, LogicResultDoc>): Promise<LogicResult[]>;
@@ -0,0 +1,324 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runPeerSyncViews = exports.runViewLogics = exports.consolidateAndGroupByDstPath = exports.getSecurityFn = exports.groupDocsByUserAndDstPath = exports.runBusinessLogics = exports.delayFormSubmissionAndCheckIfCancelled = exports.getFormModifiedFields = exports.validateForm = exports.revertModificationsOutsideForm = exports.distribute = exports._mockable = void 0;
4
+ const index_1 = require("./index");
5
+ const view_logics_1 = require("./logics/view-logics");
6
+ const paths_1 = require("./utils/paths");
7
+ const misc_1 = require("./utils/misc");
8
+ exports._mockable = {
9
+ getViewLogicsConfig: () => index_1.viewLogicConfigs,
10
+ };
11
+ async function commitBatchIfNeeded(batch, db, writeCount) {
12
+ writeCount++; // Increment writeCount for each write operation
13
+ if (writeCount === 500) { // Commit batch every 500 writes
14
+ console.log("Committing batch of 500 writes...");
15
+ await batch.commit();
16
+ batch = db.batch();
17
+ writeCount = 0;
18
+ }
19
+ return [batch, writeCount];
20
+ }
21
+ async function distribute(docsByDstPath) {
22
+ const db = index_1.admin.firestore();
23
+ let batch = db.batch();
24
+ let writeCount = 0;
25
+ const forCopy = [];
26
+ for (const dstPath of Array.from(docsByDstPath.keys()).sort()) {
27
+ console.log(`Documents for path ${dstPath}:`);
28
+ const resultDoc = docsByDstPath.get(dstPath);
29
+ if (!resultDoc)
30
+ continue;
31
+ const { action, doc, instructions, } = resultDoc;
32
+ if (action === "copy") {
33
+ forCopy.push(resultDoc);
34
+ }
35
+ else if (action === "delete") {
36
+ // Delete document at dstPath
37
+ const dstDocRef = db.doc(dstPath);
38
+ batch.delete(dstDocRef);
39
+ [batch, writeCount] = await commitBatchIfNeeded(batch, db, writeCount);
40
+ console.log(`Document deleted at ${dstPath}`);
41
+ }
42
+ else if (action === "merge") {
43
+ const updateData = Object.assign({}, doc);
44
+ if (instructions) {
45
+ for (const [property, instruction] of Object.entries(instructions)) {
46
+ if (instruction === "++") {
47
+ updateData[property] = index_1.admin.firestore.FieldValue.increment(1);
48
+ }
49
+ else if (instruction === "--") {
50
+ updateData[property] = index_1.admin.firestore.FieldValue.increment(-1);
51
+ }
52
+ else if (instruction.startsWith("+")) {
53
+ const incrementValue = parseInt(instruction.slice(1));
54
+ if (isNaN(incrementValue)) {
55
+ console.log(`Invalid increment value ${instruction} for property ${property}`);
56
+ }
57
+ else {
58
+ updateData[property] = index_1.admin.firestore.FieldValue.increment(incrementValue);
59
+ }
60
+ }
61
+ else if (instruction.startsWith("-")) {
62
+ const decrementValue = parseInt(instruction.slice(1));
63
+ if (isNaN(decrementValue)) {
64
+ console.log(`Invalid decrement value ${instruction} for property ${property}`);
65
+ }
66
+ else {
67
+ updateData[property] = index_1.admin.firestore.FieldValue.increment(-decrementValue);
68
+ }
69
+ }
70
+ else {
71
+ console.log(`Invalid instruction ${instruction} for property ${property}`);
72
+ }
73
+ }
74
+ }
75
+ // Merge document to dstPath
76
+ const dstColPath = dstPath.endsWith("/#") ? dstPath.slice(0, -2) : null;
77
+ let dstDocRef;
78
+ if (dstColPath) {
79
+ const dstColRef = db.collection(dstColPath);
80
+ dstDocRef = dstColRef.doc();
81
+ }
82
+ else {
83
+ dstDocRef = db.doc(dstPath);
84
+ }
85
+ batch.set(dstDocRef, updateData, { merge: true });
86
+ [batch, writeCount] = await commitBatchIfNeeded(batch, db, writeCount);
87
+ console.log(`Document merged to ${dstPath}`);
88
+ }
89
+ }
90
+ if (writeCount > 0) {
91
+ console.log(`Committing final batch of ${writeCount} writes...`);
92
+ await batch.commit();
93
+ writeCount = 0;
94
+ }
95
+ // Do copy after all other operations
96
+ for (const resultDoc of forCopy) {
97
+ const { srcPath, dstPath, skipEntityDuringRecursiveCopy = [], copyMode = "recursive", } = resultDoc;
98
+ if (!srcPath) {
99
+ continue;
100
+ }
101
+ const srcDocRef = db.doc(srcPath);
102
+ const dstDocRef = db.doc(dstPath);
103
+ batch.set(dstDocRef, (await srcDocRef.get()).data());
104
+ [batch, writeCount] = await commitBatchIfNeeded(batch, db, writeCount);
105
+ console.log(`Document copied from ${srcPath} to ${dstPath}`);
106
+ if (copyMode === "recursive") {
107
+ const subDocPaths = (0, paths_1.expandAndGroupDocPaths)(srcPath);
108
+ const pathsToCopy = [];
109
+ for (const [entity, paths] of Object.entries(subDocPaths)) {
110
+ if (!skipEntityDuringRecursiveCopy || !skipEntityDuringRecursiveCopy.includes(entity)) {
111
+ pathsToCopy.push(...paths);
112
+ }
113
+ }
114
+ for (const path of pathsToCopy) {
115
+ const srcDocRef = db.doc(path);
116
+ const dstDocRef = db.doc(path.replace(srcPath, dstPath));
117
+ batch.set(dstDocRef, (await srcDocRef.get()).data());
118
+ [batch, writeCount] = await commitBatchIfNeeded(batch, db, writeCount);
119
+ console.log(`Document copied from ${path} to ${dstDocRef.path}`);
120
+ }
121
+ }
122
+ }
123
+ if (writeCount > 0) {
124
+ console.log(`Committing final batch of ${writeCount} writes...`);
125
+ await batch.commit();
126
+ writeCount = 0;
127
+ }
128
+ }
129
+ exports.distribute = distribute;
130
+ async function revertModificationsOutsideForm(document, beforeDocument, snapshot) {
131
+ // Revert any changes made to document other than @form
132
+ const revertedValues = {};
133
+ if (beforeDocument) {
134
+ const modifiedFields = Object.keys(document !== null && document !== void 0 ? document : {}).filter((key) => !key.startsWith("@form"));
135
+ modifiedFields.forEach((field) => {
136
+ if (!(0, misc_1.deepEqual)(document === null || document === void 0 ? void 0 : document[field], beforeDocument[field])) {
137
+ revertedValues[field] = beforeDocument[field];
138
+ }
139
+ });
140
+ }
141
+ // if revertedValues is not empty, update the document
142
+ if (Object.keys(revertedValues).length > 0) {
143
+ console.log("Reverting document:\n", revertedValues);
144
+ await snapshot.ref.update(revertedValues);
145
+ }
146
+ }
147
+ exports.revertModificationsOutsideForm = revertModificationsOutsideForm;
148
+ async function validateForm(entity, document, docPath) {
149
+ let hasValidationError = false;
150
+ const validate = index_1.validatorConfig[entity];
151
+ const validationResult = await validate(document, docPath);
152
+ // Check if validation failed
153
+ if (validationResult && Object.keys(validationResult).length > 0) {
154
+ console.log(`Document validation failed: ${JSON.stringify(validationResult)}`);
155
+ hasValidationError = true;
156
+ }
157
+ return [hasValidationError, validationResult];
158
+ }
159
+ exports.validateForm = validateForm;
160
+ function getFormModifiedFields(document) {
161
+ var _a;
162
+ const formFields = Object.keys((_a = document === null || document === void 0 ? void 0 : document["@form"]) !== null && _a !== void 0 ? _a : {}).filter((key) => !key.startsWith("@"));
163
+ // compare value of each @form field with the value of the same field in the document to get modified fields
164
+ return formFields.filter((field) => { var _a; return !(0, misc_1.deepEqual)(document === null || document === void 0 ? void 0 : document[field], (_a = document === null || document === void 0 ? void 0 : document["@form"]) === null || _a === void 0 ? void 0 : _a[field]); });
165
+ }
166
+ exports.getFormModifiedFields = getFormModifiedFields;
167
+ async function delayFormSubmissionAndCheckIfCancelled(delay, snapshot) {
168
+ var _a;
169
+ let cancelFormSubmission = false;
170
+ console.log(`Delaying document for ${delay}ms...`);
171
+ await snapshot.ref.update({ "@form.@status": "delay" });
172
+ await new Promise((resolve) => setTimeout(resolve, delay));
173
+ // Re-fetch document from Firestore
174
+ const updatedSnapshot = await snapshot.ref.get();
175
+ const updatedDocument = updatedSnapshot.data();
176
+ console.log("Re-fetched document from Firestore after delay:\n", updatedDocument);
177
+ // Check if form status is "cancel"
178
+ if (((_a = updatedDocument === null || updatedDocument === void 0 ? void 0 : updatedDocument["@form"]) === null || _a === void 0 ? void 0 : _a["@status"]) === "cancel") {
179
+ cancelFormSubmission = true;
180
+ }
181
+ return cancelFormSubmission;
182
+ }
183
+ exports.delayFormSubmissionAndCheckIfCancelled = delayFormSubmissionAndCheckIfCancelled;
184
+ async function runBusinessLogics(actionType, formModifiedFields, entity, action) {
185
+ const matchingLogics = index_1.logicConfigs.filter((logic) => {
186
+ return ((logic.actionTypes === "all" || logic.actionTypes.includes(actionType)) &&
187
+ (logic.modifiedFields === "all" || logic.modifiedFields.some((field) => formModifiedFields === null || formModifiedFields === void 0 ? void 0 : formModifiedFields.includes(field))) &&
188
+ (logic.entities === "all" || logic.entities.includes(entity)));
189
+ });
190
+ // TODO: Handle errors
191
+ // TODO: Add logic for execTime
192
+ return await Promise.all(matchingLogics.map((logic) => logic.logicFn(action)));
193
+ }
194
+ exports.runBusinessLogics = runBusinessLogics;
195
+ function groupDocsByUserAndDstPath(docsByDstPath, userId) {
196
+ const userDocPath = index_1.docPaths["user"].replace("{userId}", userId);
197
+ const userDocsByDstPath = new Map();
198
+ const otherUsersDocsByDstPath = new Map();
199
+ for (const [key, value] of docsByDstPath.entries()) {
200
+ if (key.startsWith(userDocPath)) {
201
+ userDocsByDstPath.set(key, value);
202
+ }
203
+ else {
204
+ otherUsersDocsByDstPath.set(key, value);
205
+ }
206
+ }
207
+ return { userDocsByDstPath, otherUsersDocsByDstPath };
208
+ }
209
+ exports.groupDocsByUserAndDstPath = groupDocsByUserAndDstPath;
210
+ function getSecurityFn(entity) {
211
+ return index_1.securityConfig[entity];
212
+ }
213
+ exports.getSecurityFn = getSecurityFn;
214
+ function consolidateAndGroupByDstPath(logicResults) {
215
+ const consolidated = new Map();
216
+ function warnOverwritingKeys(existing, incoming, type, dstPath) {
217
+ for (const key in incoming) {
218
+ if (Object.prototype.hasOwnProperty.call(existing, key)) {
219
+ console.warn(`Overwriting key "${key}" in ${type} for dstPath "${dstPath}"`);
220
+ }
221
+ }
222
+ }
223
+ function processMerge(existingDoc, doc, dstPath) {
224
+ if (existingDoc) {
225
+ if (existingDoc.action === "merge") {
226
+ warnOverwritingKeys(existingDoc.doc, doc.doc, "doc", dstPath);
227
+ warnOverwritingKeys(existingDoc.instructions, doc.instructions, "instructions", dstPath);
228
+ existingDoc.instructions = Object.assign(Object.assign({}, existingDoc.instructions), doc.instructions);
229
+ existingDoc.doc = Object.assign(Object.assign({}, existingDoc.doc), doc.doc);
230
+ }
231
+ else {
232
+ console.warn(`Action "merge" ignored because a "${existingDoc.action}" for dstPath "${dstPath}" already exists`);
233
+ }
234
+ }
235
+ else {
236
+ consolidated.set(dstPath, doc);
237
+ }
238
+ }
239
+ function processDelete(existingDoc, doc, dstPath) {
240
+ if (existingDoc) {
241
+ if (existingDoc.action === "merge") {
242
+ console.warn(`Action "merge" for dstPath "${dstPath}" is being overwritten by action "delete"`);
243
+ consolidated.set(dstPath, doc);
244
+ }
245
+ else {
246
+ console.warn(`Action "delete" ignored because a "${existingDoc.action}" for dstPath "${dstPath}" already exists`);
247
+ }
248
+ }
249
+ else {
250
+ consolidated.set(dstPath, doc);
251
+ }
252
+ }
253
+ function processCopy(existingDoc, doc, dstPath) {
254
+ if (existingDoc) {
255
+ if (existingDoc.action === "copy") {
256
+ console.warn(`Action "copy" ignored because "copy" for dstPath "${dstPath}" already exists`);
257
+ }
258
+ else {
259
+ console.warn(`Action "${existingDoc.action}" for dstPath "${dstPath}" is being replaced by action "copy"`);
260
+ consolidated.set(dstPath, doc);
261
+ }
262
+ }
263
+ else {
264
+ consolidated.set(dstPath, doc);
265
+ }
266
+ }
267
+ for (const logicResult of logicResults) {
268
+ for (const doc of logicResult.documents) {
269
+ const { dstPath, action, } = doc;
270
+ const existingDoc = consolidated.get(dstPath);
271
+ if (action === "merge") {
272
+ processMerge(existingDoc, doc, dstPath);
273
+ }
274
+ else if (action === "delete") {
275
+ processDelete(existingDoc, doc, dstPath);
276
+ }
277
+ else if (action === "copy") {
278
+ processCopy(existingDoc, doc, dstPath);
279
+ }
280
+ }
281
+ }
282
+ return consolidated;
283
+ }
284
+ exports.consolidateAndGroupByDstPath = consolidateAndGroupByDstPath;
285
+ async function runViewLogics(dstPathLogicDocsMap) {
286
+ const logicResults = [];
287
+ for (const [dstPath, logicResultDoc] of dstPathLogicDocsMap.entries()) {
288
+ const { action, doc, instructions, } = logicResultDoc;
289
+ const modifiedFields = [];
290
+ if (doc) {
291
+ modifiedFields.push(...Object.keys(doc));
292
+ }
293
+ if (instructions) {
294
+ modifiedFields.push(...Object.keys(instructions));
295
+ }
296
+ const { entity } = (0, paths_1.findMatchingDocPathRegex)(dstPath);
297
+ if (!entity) {
298
+ console.error("Entity should not be blank");
299
+ continue;
300
+ }
301
+ const matchingLogics = exports._mockable.getViewLogicsConfig().filter((logic) => {
302
+ return ((action === "merge" &&
303
+ logic.modifiedFields.some((field) => modifiedFields.includes(field)) &&
304
+ logic.entity === entity) ||
305
+ (action === "delete" &&
306
+ logic.entity === entity));
307
+ });
308
+ // TODO: Handle errors
309
+ // TODO: Add logic for execTime
310
+ const results = await Promise.all(matchingLogics.map((logic) => logic.viewLogicFn(logicResultDoc)));
311
+ logicResults.push(...results);
312
+ }
313
+ return logicResults;
314
+ }
315
+ exports.runViewLogics = runViewLogics;
316
+ async function runPeerSyncViews(userDocsByDstPath) {
317
+ const logicResults = [];
318
+ for (const [_, logicResultDoc] of userDocsByDstPath.entries()) {
319
+ logicResults.push(await (0, view_logics_1.syncPeerViews)(logicResultDoc));
320
+ }
321
+ return logicResults;
322
+ }
323
+ exports.runPeerSyncViews = runPeerSyncViews;
324
+ //# sourceMappingURL=index-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index-utils.js","sourceRoot":"","sources":["../src/index-utils.ts"],"names":[],"mappings":";;;AASA,mCAAyG;AACzG,sDAAmD;AACnD,yCAA+E;AAC/E,uCAAuC;AAG1B,QAAA,SAAS,GAAG;IACvB,mBAAmB,EAAE,GAAG,EAAE,CAAC,wBAAgB;CAC5C,CAAC;AAEF,KAAK,UAAU,mBAAmB,CAChC,KAAmC,EACnC,EAA+B,EAC/B,UAAkB;IAClB,UAAU,EAAE,CAAC,CAAC,gDAAgD;IAC9D,IAAI,UAAU,KAAK,GAAG,EAAE,EAAE,gCAAgC;QACxD,OAAO,CAAC,GAAG,CAAC,mCAAmC,CAAC,CAAC;QACjD,MAAM,KAAK,CAAC,MAAM,EAAE,CAAC;QACrB,KAAK,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC;QACnB,UAAU,GAAG,CAAC,CAAC;KAChB;IACD,OAAO,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;AAC7B,CAAC;AAEM,KAAK,UAAU,UAAU,CAAC,aAA0C;IACzE,MAAM,EAAE,GAAG,aAAK,CAAC,SAAS,EAAE,CAAC;IAC7B,IAAI,KAAK,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC;IACvB,IAAI,UAAU,GAAG,CAAC,CAAC;IACnB,MAAM,OAAO,GAAqB,EAAE,CAAC;IAErC,KAAK,MAAM,OAAO,IAAI,KAAK,CAAC,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE;QAC7D,OAAO,CAAC,G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Ac,GAAG,iBAAS,CAAC,mBAAmB,EAAE,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACtE,OAAO,CACL,CACE,MAAM,KAAK,OAAO;gBAClB,KAAK,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,cAAc,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;gBACpE,KAAK,CAAC,MAAM,KAAK,MAAM,CACxB;gBACD,CACE,MAAM,KAAK,QAAQ;oBACjB,KAAK,CAAC,MAAM,KAAK,MAAM,CAC1B,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;QACH,sBAAsB;QACtB,+BAA+B;QAC/B,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,WAAW,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;QACpG,YAAY,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC;KAC/B;IACD,OAAO,YAAY,CAAC;AACtB,CAAC;AAvCD,sCAuCC;AAEM,KAAK,UAAU,gBAAgB,CAAC,iBAA8C;IACnF,MAAM,YAAY,GAAkB,EAAE,CAAC;IACvC,KAAK,MAAM,CAAC,CAAC,EAAE,cAAc,CAAC,IAAI,iBAAiB,CAAC,OAAO,EAAE,EAAE;QAC7D,YAAY,CAAC,IAAI,CAAC,MAAM,IAAA,2BAAa,EAAC,cAAc,CAAC,CAAC,CAAC;KACxD;IACD,OAAO,YAAY,CAAC;AACtB,CAAC;AAND,4CAMC"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ import * as functions from "firebase-functions";
2
+ import { FirebaseAdmin, LogicConfig, SecurityConfig, ValidatorConfig, ViewLogicConfig } from "./types";
3
+ export declare let admin: FirebaseAdmin;
4
+ export declare let dbStructure: Record<string, object>;
5
+ export declare let Entity: Record<string, string>;
6
+ export declare let securityConfig: SecurityConfig;
7
+ export declare let validatorConfig: ValidatorConfig;
8
+ export declare let logicConfigs: LogicConfig[];
9
+ export declare let docPaths: Record<string, string>;
10
+ export declare let colPaths: Record<string, string>;
11
+ export declare let docPathsRegex: Record<string, RegExp>;
12
+ export declare let viewLogicConfigs: ViewLogicConfig[];
13
+ export declare const functionsConfig: Record<string, any>;
14
+ export declare const _mockable: {
15
+ createNowTimestamp: () => FirebaseFirestore.Timestamp;
16
+ };
17
+ export declare function initializeEmberFlow(adminInstance: FirebaseAdmin, customDbStructure: Record<string, object>, CustomEntity: Record<string, string>, customSecurityConfig: SecurityConfig, customValidatorConfig: ValidatorConfig, customLogicConfigs: LogicConfig[]): {
18
+ docPaths: Record<string, string>;
19
+ colPaths: Record<string, string>;
20
+ docPathsRegex: Record<string, RegExp>;
21
+ functionsConfig: Record<string, any>;
22
+ };
23
+ export declare function onDocChange(entity: string, change: functions.Change<functions.firestore.DocumentSnapshot | null>, context: functions.EventContext, event: "create" | "update" | "delete"): Promise<void>;