@friggframework/core 0.2.31-v1-alpha-package-update.0 → 1.0.1-canary.a414ea2.0
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.
- package/assertions/CHANGELOG.md +87 -0
- package/assertions/LICENSE.md +9 -0
- package/assertions/README.md +3 -0
- package/assertions/bump.txt +1 -0
- package/assertions/get.js +139 -0
- package/assertions/index.js +19 -0
- package/assertions/is-equal.js +17 -0
- package/associations/LICENSE.md +9 -0
- package/associations/README.md +3 -0
- package/associations/association.js +78 -0
- package/associations/bump3.txt +0 -0
- package/associations/jest.config.js +5 -0
- package/associations/model.js +54 -0
- package/bump0.txt +1 -0
- package/core/.eslintrc.json +3 -0
- package/{Delegate.js → core/Delegate.js} +1 -1
- package/core/LICENSE.md +9 -0
- package/{Worker.js → core/Worker.js} +2 -2
- package/core/bump3.txt +0 -0
- package/{create-handler.js → core/create-handler.js} +2 -2
- package/core/index.js +6 -0
- package/core/jest.config.js +5 -0
- package/database/.eslintrc.json +3 -0
- package/database/CHANGELOG.md +97 -0
- package/database/LICENSE.md +9 -0
- package/database/README.md +3 -0
- package/database/bump3.txt +0 -0
- package/database/index.js +23 -0
- package/database/jest.config.js +5 -0
- package/database/models/IndividualUser.js +76 -0
- package/database/models/OrganizationUser.js +29 -0
- package/database/models/State.js +9 -0
- package/database/models/Token.js +70 -0
- package/database/models/UserModel.js +7 -0
- package/database/mongo.js +45 -0
- package/database/mongoose.js +5 -0
- package/encrypt/.eslintrc.json +3 -0
- package/encrypt/CHANGELOG.md +78 -0
- package/encrypt/Cryptor.js +236 -0
- package/encrypt/Cryptor.test.js +32 -0
- package/encrypt/LICENSE.md +9 -0
- package/encrypt/README.md +13 -0
- package/encrypt/aes.js +27 -0
- package/encrypt/bump3.txt +0 -0
- package/encrypt/encrypt.js +132 -0
- package/encrypt/encrypt.test.js +1069 -0
- package/encrypt/index.js +4 -0
- package/encrypt/jest.config.js +5 -0
- package/encrypt/test-encrypt.js +107 -0
- package/errors/.eslintrc.json +3 -0
- package/errors/CHANGELOG.md +44 -0
- package/errors/LICENSE.md +9 -0
- package/errors/README.md +3 -0
- package/errors/base-error.js +23 -0
- package/errors/base-error.test.js +32 -0
- package/errors/bump.txt +1 -0
- package/errors/bump3.txt +0 -0
- package/errors/fetch-error.js +76 -0
- package/errors/fetch-error.test.js +79 -0
- package/errors/halt-error.js +10 -0
- package/errors/halt-error.test.js +11 -0
- package/errors/index.js +15 -0
- package/errors/jest.config.js +5 -0
- package/errors/validation-errors.js +23 -0
- package/errors/validation-errors.test.js +120 -0
- package/index.js +123 -5
- package/integrations/.eslintrc.json +3 -0
- package/integrations/CHANGELOG.md +244 -0
- package/integrations/LICENSE.md +9 -0
- package/integrations/README.md +3 -0
- package/integrations/bump3.txt +0 -0
- package/integrations/create-frigg-backend.js +31 -0
- package/integrations/index.js +19 -0
- package/integrations/integration-base.js +162 -0
- package/integrations/integration-factory.js +166 -0
- package/integrations/integration-mapping.js +43 -0
- package/integrations/integration-model.js +42 -0
- package/integrations/integration-router.js +367 -0
- package/integrations/integration-user.js +144 -0
- package/integrations/jest-setup.js +2 -0
- package/integrations/jest-teardown.js +2 -0
- package/integrations/jest.config.js +12 -0
- package/integrations/options.js +54 -0
- package/integrations/test/integration-base.test.js +143 -0
- package/lambda/README.md +3 -0
- package/lambda/TimeoutCatcher.js +43 -0
- package/lambda/TimeoutCatcher.test.js +68 -0
- package/lambda/bump3.txt +0 -0
- package/lambda/index.js +3 -0
- package/lambda/jest.config.js +3 -0
- package/logs/.eslintrc.json +3 -0
- package/logs/CHANGELOG.md +57 -0
- package/logs/LICENSE.md +9 -0
- package/logs/README.md +3 -0
- package/logs/bump3.txt +0 -0
- package/logs/index.js +7 -0
- package/logs/jest.config.js +5 -0
- package/logs/logger.js +69 -0
- package/logs/logger.test.js +76 -0
- package/module-plugin/.eslintrc.json +3 -0
- package/module-plugin/CHANGELOG.md +224 -0
- package/module-plugin/LICENSE.md +9 -0
- package/module-plugin/ModuleConstants.js +10 -0
- package/module-plugin/README.md +3 -0
- package/module-plugin/auther.js +342 -0
- package/module-plugin/bump3.txt +0 -0
- package/module-plugin/credential.js +22 -0
- package/module-plugin/entity-manager.js +70 -0
- package/module-plugin/entity.js +46 -0
- package/module-plugin/index.js +25 -0
- package/module-plugin/jest-setup.js +3 -0
- package/module-plugin/jest-teardown.js +2 -0
- package/module-plugin/jest.config.js +20 -0
- package/module-plugin/manager.js +169 -0
- package/module-plugin/module-factory.js +60 -0
- package/module-plugin/requester/api-key.js +36 -0
- package/module-plugin/requester/basic.js +43 -0
- package/module-plugin/requester/oauth-2.js +219 -0
- package/module-plugin/requester/requester.js +151 -0
- package/module-plugin/requester/requester.test.js +28 -0
- package/module-plugin/test/auther.test.js +97 -0
- package/module-plugin/test/mock-api/api.js +29 -0
- package/module-plugin/test/mock-api/definition.js +48 -0
- package/module-plugin/test/mock-api/mocks/hubspot.js +43 -0
- package/package.json +28 -12
- package/syncs/README.md +3 -0
- package/syncs/bump3.txt +0 -0
- package/syncs/jest.config.js +5 -0
- package/syncs/manager.js +464 -0
- package/syncs/model.js +62 -0
- package/syncs/sync.js +114 -0
- package/types/CHANGELOG.md +49 -0
- package/types/README.md +24 -0
- package/types/assertions/index.d.ts +83 -0
- package/types/associations/index.d.ts +74 -0
- package/types/bump3.txt +0 -0
- package/types/core/index.d.ts +54 -0
- package/types/database/index.d.ts +3 -0
- package/types/encrypt/index.d.ts +5 -0
- package/types/errors/index.d.ts +66 -0
- package/types/eslint-config/index.d.ts +41 -0
- package/types/index.d.ts +14 -0
- package/types/integrations/index.d.ts +191 -0
- package/types/lambda/index.d.ts +31 -0
- package/types/logs/index.d.ts +5 -0
- package/types/module-plugin/index.d.ts +293 -0
- package/types/prettier-config/index.d.ts +6 -0
- package/types/syncs/index.d.ts +128 -0
- package/types/test-environment/index.d.ts +17 -0
- package/types/tsconfig.json +103 -0
- /package/{.eslintrc.json → assertions/.eslintrc.json} +0 -0
- /package/{bump3.txt → assertions/bump3.txt} +0 -0
- /package/{jest.config.js → assertions/jest.config.js} +0 -0
- /package/{CHANGELOG.md → core/CHANGELOG.md} +0 -0
- /package/{README.md → core/README.md} +0 -0
- /package/{load-installed-modules.js → core/load-installed-modules.js} +0 -0
package/syncs/manager.js
ADDED
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
const _ = require("lodash");
|
|
2
|
+
const moment = require("moment");
|
|
3
|
+
const mongoose = require("mongoose");
|
|
4
|
+
const SyncObject = require("./sync");
|
|
5
|
+
const { debug } = require("packages/logs");
|
|
6
|
+
const { get } = require("../assertions");
|
|
7
|
+
const { Sync } = require("./model");
|
|
8
|
+
|
|
9
|
+
class SyncManager {
|
|
10
|
+
constructor(params) {
|
|
11
|
+
// TODO verify type????????
|
|
12
|
+
// this.primaryModule = getAndVerifyType(params, 'primary', ModuleManager);
|
|
13
|
+
// this.secondaryModule = getAndVerifyType(
|
|
14
|
+
// params,
|
|
15
|
+
// 'secondary',
|
|
16
|
+
// ModuleManager
|
|
17
|
+
// );
|
|
18
|
+
this.SyncObjectClass = getAndVerifyType(
|
|
19
|
+
params,
|
|
20
|
+
"syncObjectClass",
|
|
21
|
+
SyncObject
|
|
22
|
+
);
|
|
23
|
+
this.ignoreEmptyMatchValues = get(params, "ignoreEmptyMatchValues", true);
|
|
24
|
+
this.isUnidirectionalSync = get(params, "isUnidirectionalSync", false);
|
|
25
|
+
this.useFirstMatchingDuplicate = get(
|
|
26
|
+
params,
|
|
27
|
+
"useFirstMatchingDuplicate",
|
|
28
|
+
true
|
|
29
|
+
);
|
|
30
|
+
this.omitEmptyStringsFromData = get(
|
|
31
|
+
params,
|
|
32
|
+
"omitEmptyStringsFromData",
|
|
33
|
+
true
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
this.integration = get(params, "integration", null); // TODO Change to type validation
|
|
37
|
+
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// calls getAllSyncObjects() on the modules and then finds the difference between each. The Primary Module
|
|
41
|
+
// takes precedence unless the field is an empty string or null
|
|
42
|
+
async initialSync() {
|
|
43
|
+
const time0 = parseInt(moment().format("x"));
|
|
44
|
+
const primaryEntityId = await this.primaryModule.entity.id;
|
|
45
|
+
const secondaryEntityId = await this.secondaryModule.entity.id;
|
|
46
|
+
|
|
47
|
+
// get array of sync objects
|
|
48
|
+
let primaryArr = await this.primaryModule.getAllSyncObjects(
|
|
49
|
+
this.SyncObjectClass
|
|
50
|
+
);
|
|
51
|
+
const primaryArrayInitialCount = primaryArr.length;
|
|
52
|
+
const time1 = parseInt(moment().format("x"));
|
|
53
|
+
debug(
|
|
54
|
+
`${primaryArr.length} number of ${
|
|
55
|
+
this.SyncObjectClass.name
|
|
56
|
+
} retrieved from ${this.primaryModule.constructor.getName()} in ${
|
|
57
|
+
time1 - time0
|
|
58
|
+
} ms`
|
|
59
|
+
);
|
|
60
|
+
let secondaryArr = await this.secondaryModule.getAllSyncObjects(
|
|
61
|
+
this.SyncObjectClass
|
|
62
|
+
);
|
|
63
|
+
const secondaryArrayInitialCount = secondaryArr.length;
|
|
64
|
+
const time2 = parseInt(moment().format("x"));
|
|
65
|
+
debug(
|
|
66
|
+
`${secondaryArr.length} number of ${
|
|
67
|
+
this.SyncObjectClass.name
|
|
68
|
+
} retrieved from ${this.secondaryModule.constructor.getName()} in ${
|
|
69
|
+
time2 - time1
|
|
70
|
+
} ms`
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
// ignore the empty match values
|
|
74
|
+
if (this.ignoreEmptyMatchValues) {
|
|
75
|
+
const primaryCountBefore = primaryArr.length;
|
|
76
|
+
primaryArr = primaryArr.filter((obj) => !obj.missingMatchData);
|
|
77
|
+
const primaryCountAfter = primaryArr.length;
|
|
78
|
+
const secondaryCountBefore = secondaryArr.length;
|
|
79
|
+
secondaryArr = secondaryArr.filter((obj) => !obj.missingMatchData);
|
|
80
|
+
const secondaryCountAfter = secondaryArr.length;
|
|
81
|
+
debug(
|
|
82
|
+
`Ignoring ${primaryCountBefore - primaryCountAfter} ${
|
|
83
|
+
this.SyncObjectClass.name
|
|
84
|
+
} objects from ${this.primaryModule.constructor.getName()}`
|
|
85
|
+
);
|
|
86
|
+
debug(
|
|
87
|
+
`Ignoring ${secondaryCountBefore - secondaryCountAfter} ${
|
|
88
|
+
this.SyncObjectClass.name
|
|
89
|
+
} objects from ${this.secondaryModule.constructor.getName()}`
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
if (this.useFirstMatchingDuplicate) {
|
|
93
|
+
primaryArr = _.uniqBy(primaryArr, "matchHash");
|
|
94
|
+
debug(
|
|
95
|
+
`${primaryArr.length} Objects remaining after removing duplicates from Primary Array`
|
|
96
|
+
);
|
|
97
|
+
secondaryArr = _.uniqBy(secondaryArr, "matchHash");
|
|
98
|
+
debug(
|
|
99
|
+
`${secondaryArr.length} Objects remaining after removing duplicates from Secondary Array`
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
const primaryUpdate = [];
|
|
103
|
+
const secondaryUpdate = [];
|
|
104
|
+
// PrimaryIntersection is an array where at least one matching object was found inside
|
|
105
|
+
// SecondaryArray that matched the inspected object from Primary.
|
|
106
|
+
// The only catch is, there will definitely be duplicates unless self filtered
|
|
107
|
+
const primaryIntersection = primaryArr.filter((e1) =>
|
|
108
|
+
secondaryArr.some((e2) => e1.equals(e2))
|
|
109
|
+
);
|
|
110
|
+
// SecondaryIntersection is an array where at least one matching object was found inside
|
|
111
|
+
// primaryIntersection that matched the inspected object from secondaryArray.
|
|
112
|
+
// The only catch is, there will definitely be duplicates unless self filtered
|
|
113
|
+
const secondaryIntersection = secondaryArr.filter((e1) =>
|
|
114
|
+
primaryIntersection.some((e2) => e1.equals(e2))
|
|
115
|
+
);
|
|
116
|
+
const secondaryCreate = primaryArr.filter(
|
|
117
|
+
(e1) => !secondaryArr.some((e2) => e1.equals(e2))
|
|
118
|
+
);
|
|
119
|
+
const primaryCreate = secondaryArr.filter(
|
|
120
|
+
(e1) => !primaryArr.some((e2) => e1.equals(e2))
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
// process the intersections and see which ones need to be updated.
|
|
124
|
+
for (const primaryObj of primaryIntersection) {
|
|
125
|
+
const secondaryObj = secondaryIntersection.find((e1) =>
|
|
126
|
+
e1.equals(primaryObj)
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
let primaryUpdated = false;
|
|
130
|
+
let secondaryUpdated = false;
|
|
131
|
+
|
|
132
|
+
for (const key in primaryObj.data) {
|
|
133
|
+
let valuesAreNotEquivalent = true; // Default to this just to be safe
|
|
134
|
+
// Make sure we're not comparing a number 0 to a empty string/null/undefined.
|
|
135
|
+
if (_.isEqual(primaryObj.data[key], secondaryObj.data[key])) {
|
|
136
|
+
// This should basically tell us if both values are falsy, in which case we're good
|
|
137
|
+
valuesAreNotEquivalent = false;
|
|
138
|
+
} else if (
|
|
139
|
+
typeof primaryObj.data[key] === "number" ||
|
|
140
|
+
typeof secondaryObj.data[key] === "number"
|
|
141
|
+
) {
|
|
142
|
+
// This should try comparing if at least one of the two are numbers
|
|
143
|
+
valuesAreNotEquivalent =
|
|
144
|
+
primaryObj.data[key] !== secondaryObj.data[key];
|
|
145
|
+
} else if (!primaryObj.data[key] && !secondaryObj.data[key]) {
|
|
146
|
+
valuesAreNotEquivalent = false;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (valuesAreNotEquivalent) {
|
|
150
|
+
if (
|
|
151
|
+
primaryObj.dataKeyIsReplaceable(key) &&
|
|
152
|
+
!secondaryObj.dataKeyIsReplaceable(key) &&
|
|
153
|
+
!this.isUnidirectionalSync
|
|
154
|
+
) {
|
|
155
|
+
primaryObj.data[key] = secondaryObj.data[key];
|
|
156
|
+
primaryUpdated = true;
|
|
157
|
+
} else if (!primaryObj.dataKeyIsReplaceable(key)) {
|
|
158
|
+
secondaryObj.data[key] = primaryObj.data[key];
|
|
159
|
+
secondaryUpdated = true;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (primaryUpdated && !this.isUnidirectionalSync) {
|
|
164
|
+
primaryUpdate.push(primaryObj);
|
|
165
|
+
}
|
|
166
|
+
if (secondaryUpdated) {
|
|
167
|
+
secondaryUpdate.push(secondaryObj);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const createdObj = await this.createSyncDBObject(
|
|
171
|
+
[primaryObj, secondaryObj],
|
|
172
|
+
[primaryEntityId, secondaryEntityId]
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
primaryObj.setSyncId(createdObj.id);
|
|
176
|
+
secondaryObj.setSyncId(createdObj.id);
|
|
177
|
+
}
|
|
178
|
+
debug(
|
|
179
|
+
`Found ${
|
|
180
|
+
primaryUpdate.length
|
|
181
|
+
} for updating in ${this.primaryModule.constructor.getName()}`
|
|
182
|
+
);
|
|
183
|
+
debug(
|
|
184
|
+
`Found ${
|
|
185
|
+
primaryCreate.length
|
|
186
|
+
} for creating in ${this.primaryModule.constructor.getName()}`
|
|
187
|
+
);
|
|
188
|
+
debug(
|
|
189
|
+
`Found ${
|
|
190
|
+
secondaryUpdate.length
|
|
191
|
+
} for updating in ${this.secondaryModule.constructor.getName()}`
|
|
192
|
+
);
|
|
193
|
+
debug(
|
|
194
|
+
`Found ${
|
|
195
|
+
secondaryCreate.length
|
|
196
|
+
} for creating in ${this.secondaryModule.constructor.getName()}`
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
const time3 = parseInt(moment().format("x"));
|
|
200
|
+
debug(`Sorting complete in ${time3 - time2} ms`);
|
|
201
|
+
|
|
202
|
+
// create the database entries for the
|
|
203
|
+
if (!this.isUnidirectionalSync) {
|
|
204
|
+
for (const secondaryObj of primaryCreate) {
|
|
205
|
+
const createdObj = await this.createSyncDBObject(
|
|
206
|
+
[secondaryObj],
|
|
207
|
+
[secondaryEntityId, primaryEntityId]
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
secondaryObj.setSyncId(createdObj.id);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
for (const primaryObj of secondaryCreate) {
|
|
215
|
+
const createdObj = await this.createSyncDBObject(
|
|
216
|
+
[primaryObj],
|
|
217
|
+
[primaryEntityId, secondaryEntityId]
|
|
218
|
+
);
|
|
219
|
+
primaryObj.setSyncId(createdObj.id);
|
|
220
|
+
}
|
|
221
|
+
const time4 = parseInt(moment().format("x"));
|
|
222
|
+
debug(`Sync objects create in DB in ${time4 - time3} ms`);
|
|
223
|
+
|
|
224
|
+
// call the batch update/creates
|
|
225
|
+
let time5 = parseInt(moment().format("x"));
|
|
226
|
+
let time6 = parseInt(moment().format("x"));
|
|
227
|
+
if (!this.isUnidirectionalSync) {
|
|
228
|
+
await this.primaryModule.batchUpdateSyncObjects(primaryUpdate, this);
|
|
229
|
+
time5 = parseInt(moment().format("x"));
|
|
230
|
+
debug(
|
|
231
|
+
`Updated ${primaryUpdate.length} ${
|
|
232
|
+
this.SyncObjectClass.name
|
|
233
|
+
}s in ${this.primaryModule.constructor.getName()} in ${
|
|
234
|
+
time5 - time4
|
|
235
|
+
} ms`
|
|
236
|
+
);
|
|
237
|
+
await this.primaryModule.batchCreateSyncObjects(primaryCreate, this);
|
|
238
|
+
time6 = parseInt(moment().format("x"));
|
|
239
|
+
debug(
|
|
240
|
+
`Created ${primaryCreate.length} ${
|
|
241
|
+
this.SyncObjectClass.name
|
|
242
|
+
}s in ${this.primaryModule.constructor.getName()} in ${
|
|
243
|
+
time6 - time5
|
|
244
|
+
} ms`
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
await this.secondaryModule.batchUpdateSyncObjects(secondaryUpdate, this);
|
|
249
|
+
const time7 = parseInt(moment().format("x"));
|
|
250
|
+
debug(
|
|
251
|
+
`Updated ${secondaryUpdate.length} ${
|
|
252
|
+
this.SyncObjectClass.name
|
|
253
|
+
}s in ${this.secondaryModule.constructor.getName()} in ${
|
|
254
|
+
time7 - time6
|
|
255
|
+
} ms`
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
await this.secondaryModule.batchCreateSyncObjects(secondaryCreate, this);
|
|
259
|
+
const time8 = parseInt(moment().format("x"));
|
|
260
|
+
debug(
|
|
261
|
+
`${primaryArrayInitialCount} number of ${
|
|
262
|
+
this.SyncObjectClass.name
|
|
263
|
+
} objects retrieved from ${this.primaryModule.constructor.getName()} in ${
|
|
264
|
+
time1 - time0
|
|
265
|
+
} ms`
|
|
266
|
+
);
|
|
267
|
+
debug(
|
|
268
|
+
`${secondaryArrayInitialCount} number of ${
|
|
269
|
+
this.SyncObjectClass.name
|
|
270
|
+
} objects retrieved from ${this.secondaryModule.constructor.getName()} in ${
|
|
271
|
+
time2 - time1
|
|
272
|
+
} ms`
|
|
273
|
+
);
|
|
274
|
+
debug(`Sorting complete in ${time3 - time2} ms`);
|
|
275
|
+
debug(`Sync objects create in DB in ${time4 - time3} ms`);
|
|
276
|
+
debug(
|
|
277
|
+
`Updated ${primaryUpdate.length} ${
|
|
278
|
+
this.SyncObjectClass.name
|
|
279
|
+
}s in ${this.primaryModule.constructor.getName()} in ${time5 - time4} ms`
|
|
280
|
+
);
|
|
281
|
+
debug(
|
|
282
|
+
`Created ${primaryCreate.length} ${
|
|
283
|
+
this.SyncObjectClass.name
|
|
284
|
+
}s in ${this.primaryModule.constructor.getName()} in ${time6 - time5} ms`
|
|
285
|
+
);
|
|
286
|
+
debug(
|
|
287
|
+
`Updated ${secondaryUpdate.length} ${
|
|
288
|
+
this.SyncObjectClass.name
|
|
289
|
+
}s in ${this.secondaryModule.constructor.getName()} in ${
|
|
290
|
+
time7 - time6
|
|
291
|
+
} ms`
|
|
292
|
+
);
|
|
293
|
+
debug(
|
|
294
|
+
`Created ${secondaryCreate.length} ${
|
|
295
|
+
this.SyncObjectClass.name
|
|
296
|
+
}s in ${this.secondaryModule.constructor.getName()} in ${
|
|
297
|
+
time8 - time7
|
|
298
|
+
} ms`
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
async createSyncDBObject(objArr, entities) {
|
|
303
|
+
const entityIds = entities.map(
|
|
304
|
+
(ent) => ({ $elemMatch: { $eq: mongoose.Types.ObjectId(ent) } })
|
|
305
|
+
// return {"$elemMatch": {"$eq": ent}};
|
|
306
|
+
);
|
|
307
|
+
const dataIdentifiers = [];
|
|
308
|
+
for (const index in objArr) {
|
|
309
|
+
dataIdentifiers.push({
|
|
310
|
+
entity: entities[index],
|
|
311
|
+
id: objArr[index].dataIdentifier,
|
|
312
|
+
hash: objArr[index].dataIdentifierHash,
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
const primaryObj = objArr[0];
|
|
316
|
+
|
|
317
|
+
const createSyncObj = {
|
|
318
|
+
name: primaryObj.getName(),
|
|
319
|
+
entities,
|
|
320
|
+
hash: primaryObj.getHashData({
|
|
321
|
+
omitEmptyStringsFromData: this.omitEmptyStringsFromData,
|
|
322
|
+
}),
|
|
323
|
+
dataIdentifiers,
|
|
324
|
+
};
|
|
325
|
+
const filter = {
|
|
326
|
+
name: primaryObj.getName(),
|
|
327
|
+
dataIdentifiers: {
|
|
328
|
+
$elemMatch: {
|
|
329
|
+
id: primaryObj.dataIdentifier,
|
|
330
|
+
entity: entities[0],
|
|
331
|
+
},
|
|
332
|
+
},
|
|
333
|
+
entities: { $all: entityIds },
|
|
334
|
+
// entities
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
return await Sync.upsert(filter, createSyncObj);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Automatically syncs the objects with the secondary module if the object was updated
|
|
341
|
+
async sync(syncObjects) {
|
|
342
|
+
const batchUpdates = [];
|
|
343
|
+
const batchCreates = [];
|
|
344
|
+
const noChange = [];
|
|
345
|
+
const primaryEntityId = await this.primaryModule.entity.id;
|
|
346
|
+
const secondaryEntityId = await this.secondaryModule.entity.id;
|
|
347
|
+
|
|
348
|
+
const secondaryModuleName = this.secondaryModule.constructor.getName();
|
|
349
|
+
for (const primaryObj of syncObjects) {
|
|
350
|
+
const dataHash = primaryObj.getHashData({
|
|
351
|
+
omitEmptyStringsFromData: this.omitEmptyStringsFromData,
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
// get the sync object in the database if it exists
|
|
355
|
+
let syncObj = await Sync.getSyncObject(
|
|
356
|
+
primaryObj.getName(),
|
|
357
|
+
primaryObj.dataIdentifier,
|
|
358
|
+
primaryEntityId
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
if (syncObj) {
|
|
362
|
+
debug("Sync object found, evaluating...");
|
|
363
|
+
const hashMatch = syncObj.hash === dataHash;
|
|
364
|
+
const dataIdentifierLength = syncObj.dataIdentifiers.length;
|
|
365
|
+
|
|
366
|
+
if (!hashMatch && dataIdentifierLength > 1) {
|
|
367
|
+
debug(
|
|
368
|
+
"Previously successful sync, but hashes don't match. Updating."
|
|
369
|
+
);
|
|
370
|
+
const secondaryObj = new this.SyncObjectClass({
|
|
371
|
+
data: primaryObj.data,
|
|
372
|
+
dataIdentifier: Sync.getEntityObjIdForEntityIdFromObject(
|
|
373
|
+
syncObj,
|
|
374
|
+
secondaryEntityId
|
|
375
|
+
),
|
|
376
|
+
moduleName: secondaryModuleName,
|
|
377
|
+
useMapping: false,
|
|
378
|
+
});
|
|
379
|
+
secondaryObj.setSyncId(syncObj.id);
|
|
380
|
+
batchUpdates.push(secondaryObj);
|
|
381
|
+
} else if (hashMatch && dataIdentifierLength > 1) {
|
|
382
|
+
debug(
|
|
383
|
+
"Data hashes match, no updates or creates needed for this one."
|
|
384
|
+
);
|
|
385
|
+
noChange.push(syncObj);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if (dataIdentifierLength === 1) {
|
|
389
|
+
debug(
|
|
390
|
+
"We have only one data Identifier, which means we don't have a record in the secondary app for whatever reason (failure or filter). So, creating."
|
|
391
|
+
);
|
|
392
|
+
primaryObj.setSyncId(syncObj.id);
|
|
393
|
+
batchCreates.push(primaryObj);
|
|
394
|
+
}
|
|
395
|
+
} else {
|
|
396
|
+
debug(
|
|
397
|
+
"No sync object, so we'll try creating, first creating an object"
|
|
398
|
+
);
|
|
399
|
+
syncObj = await this.createSyncDBObject(
|
|
400
|
+
[primaryObj],
|
|
401
|
+
[primaryEntityId, secondaryEntityId]
|
|
402
|
+
);
|
|
403
|
+
primaryObj.setSyncId(syncObj.id);
|
|
404
|
+
batchCreates.push(primaryObj);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
const updateRes =
|
|
408
|
+
batchUpdates.length > 0
|
|
409
|
+
? await this.secondaryModule.batchUpdateSyncObjects(batchUpdates, this)
|
|
410
|
+
: [];
|
|
411
|
+
const createRes =
|
|
412
|
+
batchCreates.length > 0
|
|
413
|
+
? await this.secondaryModule.batchCreateSyncObjects(batchCreates, this)
|
|
414
|
+
: [];
|
|
415
|
+
return updateRes.concat(createRes).concat(noChange);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// takes in:
|
|
419
|
+
// 1. the Sync Id of an object in our database
|
|
420
|
+
// 2. the object Id in the form of a json object for example:
|
|
421
|
+
// {
|
|
422
|
+
// companyId: 12,
|
|
423
|
+
// saleId:524
|
|
424
|
+
// }
|
|
425
|
+
// 3. the module manager calling the function
|
|
426
|
+
async confirmCreate(syncObj, createdId, moduleManager) {
|
|
427
|
+
const dataIdentifier = {
|
|
428
|
+
entity: await moduleManager.entity.id,
|
|
429
|
+
id: createdId,
|
|
430
|
+
hash: this.SyncObjectClass.hashJSON(createdId),
|
|
431
|
+
};
|
|
432
|
+
// No matter what, save the hash because why not?
|
|
433
|
+
// TODO this is suboptimal because it does 2 DB requests where only 1 is needed
|
|
434
|
+
// TODO If you want to get even more optimized, batch any/all updates together.
|
|
435
|
+
// Also this is only needed because of the case where an "update" becomes a "create" when we find only
|
|
436
|
+
// 1 data identifier. So, during `sync()`, if we see that the hashes don't match, we check for DataIDs and
|
|
437
|
+
// decide to create in the "target" or "secondary" because we know it failed for some reason. We also want
|
|
438
|
+
// to hold off on updating the hash in case the create fails for some reason again.
|
|
439
|
+
|
|
440
|
+
await Sync.update(syncObj.syncId, {
|
|
441
|
+
hash: syncObj.getHashData({
|
|
442
|
+
omitEmptyStringsFromData: this.omitEmptyStringsFromData,
|
|
443
|
+
}),
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
const result = await Sync.addDataIdentifier(syncObj.syncId, dataIdentifier);
|
|
447
|
+
|
|
448
|
+
return result;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
async confirmUpdate(syncObj) {
|
|
452
|
+
debug("Successfully updated secondaryObject. Updating the hash in the DB");
|
|
453
|
+
const result = await Sync.update(syncObj.syncId, {
|
|
454
|
+
hash: syncObj.getHashData({
|
|
455
|
+
omitEmptyStringsFromData: this.omitEmptyStringsFromData,
|
|
456
|
+
}),
|
|
457
|
+
});
|
|
458
|
+
debug("Success");
|
|
459
|
+
|
|
460
|
+
return result;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
module.exports = SyncManager;
|
package/syncs/model.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const mongoose = require("mongoose");
|
|
2
|
+
|
|
3
|
+
const schema = new mongoose.Schema({
|
|
4
|
+
entities: [
|
|
5
|
+
{ type: mongoose.Schema.Types.ObjectId, ref: "Entity", required: true },
|
|
6
|
+
],
|
|
7
|
+
hash: { type: String, required: true },
|
|
8
|
+
name: { type: String, required: true },
|
|
9
|
+
dataIdentifiers: [
|
|
10
|
+
{
|
|
11
|
+
entity: {
|
|
12
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
13
|
+
ref: "Entity",
|
|
14
|
+
required: true,
|
|
15
|
+
},
|
|
16
|
+
id: { type: Object, required: true },
|
|
17
|
+
hash: { type: String, required: true },
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
schema.statics({
|
|
23
|
+
getSyncObject: async function (name, dataIdentifier, entity) {
|
|
24
|
+
// const syncList = await this.list({name:name,entities: {"$in": entities}, "entityIds.idHash":entityIdHash });
|
|
25
|
+
const syncList = await this.find({
|
|
26
|
+
name: name,
|
|
27
|
+
dataIdentifiers: { $elemMatch: { id: dataIdentifier, entity } },
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
if (syncList.length === 1) {
|
|
31
|
+
return syncList[0];
|
|
32
|
+
} else if (syncList.length === 0) {
|
|
33
|
+
return null;
|
|
34
|
+
} else {
|
|
35
|
+
throw new Error(
|
|
36
|
+
`There are multiple sync objects with the name ${name}, for entities [${syncList[0].entities}] [${syncList[1].entities}]`
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
addDataIdentifier: async function (id, dataIdentifier) {
|
|
42
|
+
return await this.update(
|
|
43
|
+
{ _id: id },
|
|
44
|
+
{},
|
|
45
|
+
{ dataIdentifiers: dataIdentifier }
|
|
46
|
+
);
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
getEntityObjIdForEntityIdFromObject: function (syncObj, entityId) {
|
|
50
|
+
for (let dataIdentifier of syncObj.dataIdentifiers) {
|
|
51
|
+
if (dataIdentifier.entity.toString() === entityId) {
|
|
52
|
+
return dataIdentifier.id;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
throw new Error(
|
|
56
|
+
`Sync object does not have DataIdentifier for entityId: ${entityId}`
|
|
57
|
+
);
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const Sync = mongoose.models.Sync || mongoose.model("Sync", schema);
|
|
62
|
+
module.exports = { Sync };
|
package/syncs/sync.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
const md5 = require("md5");
|
|
2
|
+
const ModuleManager = require('../module-plugin');
|
|
3
|
+
const { debug } = require("packages/logs");
|
|
4
|
+
const { get } = require("packages/assertions");
|
|
5
|
+
|
|
6
|
+
class Sync {
|
|
7
|
+
static Config = {
|
|
8
|
+
name: "Sync",
|
|
9
|
+
|
|
10
|
+
// an array of keys we will use to form an object and then hash it. Order matters here
|
|
11
|
+
// because it will effect how the hash results
|
|
12
|
+
keys: [],
|
|
13
|
+
|
|
14
|
+
// matchOn is an array of keys that make the variable unique when combined together
|
|
15
|
+
// and is used to sync with the other objects
|
|
16
|
+
// matchOn keys _have_ to have a value, otherwise the object is not considered a match
|
|
17
|
+
matchOn: [],
|
|
18
|
+
|
|
19
|
+
// a key value mapping of module to then a list of keys that will map to
|
|
20
|
+
// an a function that takes in the module object and return the value from it
|
|
21
|
+
// format as follows:
|
|
22
|
+
// {
|
|
23
|
+
// ModuleName:{
|
|
24
|
+
// firstName:(moduleObject)=>{moduleObject['name'][0]},
|
|
25
|
+
// lastName:(moduleObject)=>{moduleObject['name'][1]},
|
|
26
|
+
// },
|
|
27
|
+
// ....
|
|
28
|
+
// }
|
|
29
|
+
moduleMap: {},
|
|
30
|
+
reverseModuleMap: {},
|
|
31
|
+
};
|
|
32
|
+
constructor(params) {
|
|
33
|
+
this.data = {};
|
|
34
|
+
|
|
35
|
+
let data = get(params, "data");
|
|
36
|
+
this.moduleName = get(params, "moduleName");
|
|
37
|
+
this.dataIdentifier = get(params, "dataIdentifier");
|
|
38
|
+
this.useMapping = get(params, "useMapping", true); // Use with caution...
|
|
39
|
+
|
|
40
|
+
this.dataIdentifierHash = this.constructor.hashJSON(this.dataIdentifier);
|
|
41
|
+
|
|
42
|
+
if (this.useMapping) {
|
|
43
|
+
for (let key of this.constructor.Config.keys) {
|
|
44
|
+
this.data[key] =
|
|
45
|
+
this.constructor.Config.moduleMap[this.moduleName][key](data);
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
this.data = data;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// matchHash is used to find matches between two sync objects
|
|
52
|
+
// Match data _has_ to have a value
|
|
53
|
+
const matchHashData = [];
|
|
54
|
+
this.missingMatchData = false;
|
|
55
|
+
for (const key of this.constructor.Config.matchOn) {
|
|
56
|
+
if (!this.data[key]) {
|
|
57
|
+
this.missingMatchData = true;
|
|
58
|
+
debug(`Data key of ${key} was missing from MatchOn`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
matchHashData.push(this.data[key]);
|
|
62
|
+
}
|
|
63
|
+
this.matchHash = this.constructor.hashJSON(matchHashData);
|
|
64
|
+
|
|
65
|
+
this.syncId = null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
equals(syncObj) {
|
|
69
|
+
return this.matchHash === syncObj.matchHash;
|
|
70
|
+
}
|
|
71
|
+
dataKeyIsReplaceable(key) {
|
|
72
|
+
return this.data[key] === null || this.data[key] === "";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
isModuleInMap(moduleName) {
|
|
76
|
+
return this.constructor.Config.moduleMap[name];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
getName() {
|
|
80
|
+
return this.constructor.Config.name;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
getHashData(params) {
|
|
84
|
+
let omitEmptyStringsFromData = get(
|
|
85
|
+
params,
|
|
86
|
+
"omitEmptyStringsFromData",
|
|
87
|
+
false
|
|
88
|
+
);
|
|
89
|
+
let orderedData = [];
|
|
90
|
+
for (let key of this.constructor.Config.keys) {
|
|
91
|
+
if (omitEmptyStringsFromData && this.data[key] === "") {
|
|
92
|
+
this.data[key] = undefined;
|
|
93
|
+
}
|
|
94
|
+
orderedData.push(this.data[key]);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return this.constructor.hashJSON(orderedData);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
setSyncId(syncId) {
|
|
101
|
+
this.syncId = syncId;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
reverseModuleMap(moduleName) {
|
|
105
|
+
return this.constructor.Config.reverseModuleMap[moduleName](this.data);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
static hashJSON(data) {
|
|
109
|
+
let dataString = JSON.stringify(data, null, 2);
|
|
110
|
+
return md5(dataString);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
module.exports = Sync;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# v0.2.0 (Wed Sep 06 2023)
|
|
2
|
+
|
|
3
|
+
#### 🐛 Bug Fix
|
|
4
|
+
|
|
5
|
+
- Bump independent versions \[skip ci\] ([@seanspeaks](https://github.com/seanspeaks))
|
|
6
|
+
|
|
7
|
+
#### Authors: 1
|
|
8
|
+
|
|
9
|
+
- Sean Matthews ([@seanspeaks](https://github.com/seanspeaks))
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
# v0.1.2 (Thu Aug 03 2023)
|
|
14
|
+
|
|
15
|
+
#### 🐛 Bug Fix
|
|
16
|
+
|
|
17
|
+
- Bump independent versions \[skip ci\] ([@seanspeaks](https://github.com/seanspeaks))
|
|
18
|
+
|
|
19
|
+
#### Authors: 1
|
|
20
|
+
|
|
21
|
+
- Sean Matthews ([@seanspeaks](https://github.com/seanspeaks))
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
# v0.1.1 (Wed Jul 05 2023)
|
|
26
|
+
|
|
27
|
+
:tada: This release contains work from new contributors! :tada:
|
|
28
|
+
|
|
29
|
+
Thanks for all your work!
|
|
30
|
+
|
|
31
|
+
:heart: Leonardo Ferreira ([@leofmds](https://github.com/leofmds))
|
|
32
|
+
|
|
33
|
+
:heart: Roberto Oliveros ([@roboli](https://github.com/roboli))
|
|
34
|
+
|
|
35
|
+
:heart: Charaf ([@Fibii](https://github.com/Fibii))
|
|
36
|
+
|
|
37
|
+
#### 🐛 Bug Fix
|
|
38
|
+
|
|
39
|
+
- Add publicshConfig: public and changed license to MIT in types package [#195](https://github.com/friggframework/frigg/pull/195) ([@leofmds](https://github.com/leofmds))
|
|
40
|
+
- Add publicshConfig: public and changed license to MIT in types package ([@leofmds](https://github.com/leofmds))
|
|
41
|
+
- Feature/lef 270 list organizations sites [#192](https://github.com/friggframework/frigg/pull/192) ([@roboli](https://github.com/roboli))
|
|
42
|
+
- add types [#165](https://github.com/friggframework/frigg/pull/165) ([@Fibii](https://github.com/Fibii))
|
|
43
|
+
- add types ([@Fibii](https://github.com/Fibii))
|
|
44
|
+
|
|
45
|
+
#### Authors: 3
|
|
46
|
+
|
|
47
|
+
- Charaf ([@Fibii](https://github.com/Fibii))
|
|
48
|
+
- Leonardo Ferreira ([@leofmds](https://github.com/leofmds))
|
|
49
|
+
- Roberto Oliveros ([@roboli](https://github.com/roboli))
|