@friggframework/core 0.1.0 → 0.2.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/{Delegate.js → delegate.js} +1 -1
- package/index.js +4 -0
- package/jest.config.js +5 -0
- package/package.json +31 -30
- package/{Worker.js → worker.js} +2 -6
- package/Requester.js +0 -153
- package/auth/ApiKeyBase.js +0 -30
- package/auth/OAuth2Base.js +0 -203
- package/managers/IntegrationConfigManager.js +0 -22
- package/managers/IntegrationManager.js +0 -330
- package/managers/Migrator.js +0 -190
- package/managers/ModuleManager.js +0 -178
- package/managers/SyncManager.js +0 -501
- package/objects/association/Association.js +0 -81
- package/objects/integration/Options.js +0 -70
- package/objects/migration/Options.js +0 -34
- package/objects/sync/Sync.js +0 -119
package/managers/SyncManager.js
DELETED
|
@@ -1,501 +0,0 @@
|
|
|
1
|
-
const moment = require('moment');
|
|
2
|
-
const _ = require('lodash');
|
|
3
|
-
const mongoose = require('mongoose');
|
|
4
|
-
const Delegate = require('../Delegate');
|
|
5
|
-
const SyncObject = require('../objects/sync/Sync');
|
|
6
|
-
const ModuleManager = require('./ModuleManager');
|
|
7
|
-
const Sync = require('@friggframework/models/Sync');
|
|
8
|
-
const Integration = require('@friggframework/models/Integration');
|
|
9
|
-
const { debug } = require('@friggframework/logs');
|
|
10
|
-
const { get } = require('@friggframework/assertions');
|
|
11
|
-
|
|
12
|
-
class SyncManager extends Delegate {
|
|
13
|
-
constructor(params) {
|
|
14
|
-
super(params);
|
|
15
|
-
this.primaryModule = getAndVerifyType(params, 'primary', ModuleManager);
|
|
16
|
-
this.secondaryModule = getAndVerifyType(
|
|
17
|
-
params,
|
|
18
|
-
'secondary',
|
|
19
|
-
ModuleManager
|
|
20
|
-
);
|
|
21
|
-
this.SyncObjectClass = getAndVerifyType(
|
|
22
|
-
params,
|
|
23
|
-
'syncObjectClass',
|
|
24
|
-
SyncObject
|
|
25
|
-
);
|
|
26
|
-
this.ignoreEmptyMatchValues = get(
|
|
27
|
-
params,
|
|
28
|
-
'ignoreEmptyMatchValues',
|
|
29
|
-
true
|
|
30
|
-
);
|
|
31
|
-
this.isUnidirectionalSync = get(params, 'isUnidirectionalSync', false);
|
|
32
|
-
this.useFirstMatchingDuplicate = get(
|
|
33
|
-
params,
|
|
34
|
-
'useFirstMatchingDuplicate',
|
|
35
|
-
true
|
|
36
|
-
);
|
|
37
|
-
this.omitEmptyStringsFromData = get(
|
|
38
|
-
params,
|
|
39
|
-
'omitEmptyStringsFromData',
|
|
40
|
-
true
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
this.integration = get(params, 'integration', null); // TODO Change to type validation
|
|
44
|
-
|
|
45
|
-
this.syncMO = new Sync();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// calls getAllSyncObjects() on the modules and then finds the difference between each. The Primary Module
|
|
49
|
-
// takes precedence unless the field is an empty string or null
|
|
50
|
-
async initialSync() {
|
|
51
|
-
const time0 = parseInt(moment().format('x'));
|
|
52
|
-
const primaryEntityId = await this.primaryModule.entity.id;
|
|
53
|
-
const secondaryEntityId = await this.secondaryModule.entity.id;
|
|
54
|
-
|
|
55
|
-
// get array of sync objects
|
|
56
|
-
let primaryArr = await this.primaryModule.getAllSyncObjects(
|
|
57
|
-
this.SyncObjectClass
|
|
58
|
-
);
|
|
59
|
-
const primaryArrayInitialCount = primaryArr.length;
|
|
60
|
-
const time1 = parseInt(moment().format('x'));
|
|
61
|
-
debug(
|
|
62
|
-
`${primaryArr.length} number of ${
|
|
63
|
-
this.SyncObjectClass.name
|
|
64
|
-
} retrieved from ${this.primaryModule.constructor.getName()} in ${
|
|
65
|
-
time1 - time0
|
|
66
|
-
} ms`
|
|
67
|
-
);
|
|
68
|
-
let secondaryArr = await this.secondaryModule.getAllSyncObjects(
|
|
69
|
-
this.SyncObjectClass
|
|
70
|
-
);
|
|
71
|
-
const secondaryArrayInitialCount = secondaryArr.length;
|
|
72
|
-
const time2 = parseInt(moment().format('x'));
|
|
73
|
-
debug(
|
|
74
|
-
`${secondaryArr.length} number of ${
|
|
75
|
-
this.SyncObjectClass.name
|
|
76
|
-
} retrieved from ${this.secondaryModule.constructor.getName()} in ${
|
|
77
|
-
time2 - time1
|
|
78
|
-
} ms`
|
|
79
|
-
);
|
|
80
|
-
|
|
81
|
-
// ignore the empty match values
|
|
82
|
-
if (this.ignoreEmptyMatchValues) {
|
|
83
|
-
const primaryCountBefore = primaryArr.length;
|
|
84
|
-
primaryArr = primaryArr.filter((obj) => !obj.missingMatchData);
|
|
85
|
-
const primaryCountAfter = primaryArr.length;
|
|
86
|
-
const secondaryCountBefore = secondaryArr.length;
|
|
87
|
-
secondaryArr = secondaryArr.filter((obj) => !obj.missingMatchData);
|
|
88
|
-
const secondaryCountAfter = secondaryArr.length;
|
|
89
|
-
debug(
|
|
90
|
-
`Ignoring ${primaryCountBefore - primaryCountAfter} ${
|
|
91
|
-
this.SyncObjectClass.name
|
|
92
|
-
} objects from ${this.primaryModule.constructor.getName()}`
|
|
93
|
-
);
|
|
94
|
-
debug(
|
|
95
|
-
`Ignoring ${secondaryCountBefore - secondaryCountAfter} ${
|
|
96
|
-
this.SyncObjectClass.name
|
|
97
|
-
} objects from ${this.secondaryModule.constructor.getName()}`
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
if (this.useFirstMatchingDuplicate) {
|
|
101
|
-
primaryArr = _.uniqBy(primaryArr, 'matchHash');
|
|
102
|
-
debug(
|
|
103
|
-
`${primaryArr.length} Objects remaining after removing duplicates from Primary Array`
|
|
104
|
-
);
|
|
105
|
-
secondaryArr = _.uniqBy(secondaryArr, 'matchHash');
|
|
106
|
-
debug(
|
|
107
|
-
`${secondaryArr.length} Objects remaining after removing duplicates from Secondary Array`
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
const primaryUpdate = [];
|
|
111
|
-
const secondaryUpdate = [];
|
|
112
|
-
// PrimaryIntersection is an array where at least one matching object was found inside
|
|
113
|
-
// SecondaryArray that matched the inspected object from Primary.
|
|
114
|
-
// The only catch is, there will definitely be duplicates unless self filtered
|
|
115
|
-
const primaryIntersection = primaryArr.filter((e1) =>
|
|
116
|
-
secondaryArr.some((e2) => e1.equals(e2))
|
|
117
|
-
);
|
|
118
|
-
// SecondaryIntersection is an array where at least one matching object was found inside
|
|
119
|
-
// primaryIntersection that matched the inspected object from secondaryArray.
|
|
120
|
-
// The only catch is, there will definitely be duplicates unless self filtered
|
|
121
|
-
const secondaryIntersection = secondaryArr.filter((e1) =>
|
|
122
|
-
primaryIntersection.some((e2) => e1.equals(e2))
|
|
123
|
-
);
|
|
124
|
-
const secondaryCreate = primaryArr.filter(
|
|
125
|
-
(e1) => !secondaryArr.some((e2) => e1.equals(e2))
|
|
126
|
-
);
|
|
127
|
-
const primaryCreate = secondaryArr.filter(
|
|
128
|
-
(e1) => !primaryArr.some((e2) => e1.equals(e2))
|
|
129
|
-
);
|
|
130
|
-
|
|
131
|
-
// process the intersections and see which ones need to be updated.
|
|
132
|
-
for (const primaryObj of primaryIntersection) {
|
|
133
|
-
const secondaryObj = secondaryIntersection.find((e1) =>
|
|
134
|
-
e1.equals(primaryObj)
|
|
135
|
-
);
|
|
136
|
-
|
|
137
|
-
let primaryUpdated = false;
|
|
138
|
-
let secondaryUpdated = false;
|
|
139
|
-
|
|
140
|
-
for (const key in primaryObj.data) {
|
|
141
|
-
let valuesAreNotEquivalent = true; // Default to this just to be safe
|
|
142
|
-
// Make sure we're not comparing a number 0 to a empty string/null/undefined.
|
|
143
|
-
if (_.isEqual(primaryObj.data[key], secondaryObj.data[key])) {
|
|
144
|
-
// This should basically tell us if both values are falsy, in which case we're good
|
|
145
|
-
valuesAreNotEquivalent = false;
|
|
146
|
-
} else if (
|
|
147
|
-
typeof primaryObj.data[key] === 'number' ||
|
|
148
|
-
typeof secondaryObj.data[key] === 'number'
|
|
149
|
-
) {
|
|
150
|
-
// This should try comparing if at least one of the two are numbers
|
|
151
|
-
valuesAreNotEquivalent =
|
|
152
|
-
primaryObj.data[key] !== secondaryObj.data[key];
|
|
153
|
-
} else if (!primaryObj.data[key] && !secondaryObj.data[key]) {
|
|
154
|
-
valuesAreNotEquivalent = false;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
if (valuesAreNotEquivalent) {
|
|
158
|
-
if (
|
|
159
|
-
primaryObj.dataKeyIsReplaceable(key) &&
|
|
160
|
-
!secondaryObj.dataKeyIsReplaceable(key) &&
|
|
161
|
-
!this.isUnidirectionalSync
|
|
162
|
-
) {
|
|
163
|
-
primaryObj.data[key] = secondaryObj.data[key];
|
|
164
|
-
primaryUpdated = true;
|
|
165
|
-
} else if (!primaryObj.dataKeyIsReplaceable(key)) {
|
|
166
|
-
secondaryObj.data[key] = primaryObj.data[key];
|
|
167
|
-
secondaryUpdated = true;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
if (primaryUpdated && !this.isUnidirectionalSync) {
|
|
172
|
-
primaryUpdate.push(primaryObj);
|
|
173
|
-
}
|
|
174
|
-
if (secondaryUpdated) {
|
|
175
|
-
secondaryUpdate.push(secondaryObj);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const createdObj = await this.createSyncDBObject(
|
|
179
|
-
[primaryObj, secondaryObj],
|
|
180
|
-
[primaryEntityId, secondaryEntityId]
|
|
181
|
-
);
|
|
182
|
-
|
|
183
|
-
primaryObj.setSyncId(createdObj.id);
|
|
184
|
-
secondaryObj.setSyncId(createdObj.id);
|
|
185
|
-
}
|
|
186
|
-
debug(
|
|
187
|
-
`Found ${
|
|
188
|
-
primaryUpdate.length
|
|
189
|
-
} for updating in ${this.primaryModule.constructor.getName()}`
|
|
190
|
-
);
|
|
191
|
-
debug(
|
|
192
|
-
`Found ${
|
|
193
|
-
primaryCreate.length
|
|
194
|
-
} for creating in ${this.primaryModule.constructor.getName()}`
|
|
195
|
-
);
|
|
196
|
-
debug(
|
|
197
|
-
`Found ${
|
|
198
|
-
secondaryUpdate.length
|
|
199
|
-
} for updating in ${this.secondaryModule.constructor.getName()}`
|
|
200
|
-
);
|
|
201
|
-
debug(
|
|
202
|
-
`Found ${
|
|
203
|
-
secondaryCreate.length
|
|
204
|
-
} for creating in ${this.secondaryModule.constructor.getName()}`
|
|
205
|
-
);
|
|
206
|
-
|
|
207
|
-
const time3 = parseInt(moment().format('x'));
|
|
208
|
-
debug(`Sorting complete in ${time3 - time2} ms`);
|
|
209
|
-
|
|
210
|
-
// create the database entries for the
|
|
211
|
-
if (!this.isUnidirectionalSync) {
|
|
212
|
-
for (const secondaryObj of primaryCreate) {
|
|
213
|
-
const createdObj = await this.createSyncDBObject(
|
|
214
|
-
[secondaryObj],
|
|
215
|
-
[secondaryEntityId, primaryEntityId]
|
|
216
|
-
);
|
|
217
|
-
|
|
218
|
-
secondaryObj.setSyncId(createdObj.id);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
for (const primaryObj of secondaryCreate) {
|
|
223
|
-
const createdObj = await this.createSyncDBObject(
|
|
224
|
-
[primaryObj],
|
|
225
|
-
[primaryEntityId, secondaryEntityId]
|
|
226
|
-
);
|
|
227
|
-
primaryObj.setSyncId(createdObj.id);
|
|
228
|
-
}
|
|
229
|
-
const time4 = parseInt(moment().format('x'));
|
|
230
|
-
debug(`Sync objects create in DB in ${time4 - time3} ms`);
|
|
231
|
-
|
|
232
|
-
// call the batch update/creates
|
|
233
|
-
let time5 = parseInt(moment().format('x'));
|
|
234
|
-
let time6 = parseInt(moment().format('x'));
|
|
235
|
-
if (!this.isUnidirectionalSync) {
|
|
236
|
-
await this.primaryModule.batchUpdateSyncObjects(
|
|
237
|
-
primaryUpdate,
|
|
238
|
-
this
|
|
239
|
-
);
|
|
240
|
-
time5 = parseInt(moment().format('x'));
|
|
241
|
-
debug(
|
|
242
|
-
`Updated ${primaryUpdate.length} ${
|
|
243
|
-
this.SyncObjectClass.name
|
|
244
|
-
}s in ${this.primaryModule.constructor.getName()} in ${
|
|
245
|
-
time5 - time4
|
|
246
|
-
} ms`
|
|
247
|
-
);
|
|
248
|
-
await this.primaryModule.batchCreateSyncObjects(
|
|
249
|
-
primaryCreate,
|
|
250
|
-
this
|
|
251
|
-
);
|
|
252
|
-
time6 = parseInt(moment().format('x'));
|
|
253
|
-
debug(
|
|
254
|
-
`Created ${primaryCreate.length} ${
|
|
255
|
-
this.SyncObjectClass.name
|
|
256
|
-
}s in ${this.primaryModule.constructor.getName()} in ${
|
|
257
|
-
time6 - time5
|
|
258
|
-
} ms`
|
|
259
|
-
);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
await this.secondaryModule.batchUpdateSyncObjects(
|
|
263
|
-
secondaryUpdate,
|
|
264
|
-
this
|
|
265
|
-
);
|
|
266
|
-
const time7 = parseInt(moment().format('x'));
|
|
267
|
-
debug(
|
|
268
|
-
`Updated ${secondaryUpdate.length} ${
|
|
269
|
-
this.SyncObjectClass.name
|
|
270
|
-
}s in ${this.secondaryModule.constructor.getName()} in ${
|
|
271
|
-
time7 - time6
|
|
272
|
-
} ms`
|
|
273
|
-
);
|
|
274
|
-
|
|
275
|
-
await this.secondaryModule.batchCreateSyncObjects(
|
|
276
|
-
secondaryCreate,
|
|
277
|
-
this
|
|
278
|
-
);
|
|
279
|
-
const time8 = parseInt(moment().format('x'));
|
|
280
|
-
debug(
|
|
281
|
-
`${primaryArrayInitialCount} number of ${
|
|
282
|
-
this.SyncObjectClass.name
|
|
283
|
-
} objects retrieved from ${this.primaryModule.constructor.getName()} in ${
|
|
284
|
-
time1 - time0
|
|
285
|
-
} ms`
|
|
286
|
-
);
|
|
287
|
-
debug(
|
|
288
|
-
`${secondaryArrayInitialCount} number of ${
|
|
289
|
-
this.SyncObjectClass.name
|
|
290
|
-
} objects retrieved from ${this.secondaryModule.constructor.getName()} in ${
|
|
291
|
-
time2 - time1
|
|
292
|
-
} ms`
|
|
293
|
-
);
|
|
294
|
-
debug(`Sorting complete in ${time3 - time2} ms`);
|
|
295
|
-
debug(`Sync objects create in DB in ${time4 - time3} ms`);
|
|
296
|
-
debug(
|
|
297
|
-
`Updated ${primaryUpdate.length} ${
|
|
298
|
-
this.SyncObjectClass.name
|
|
299
|
-
}s in ${this.primaryModule.constructor.getName()} in ${
|
|
300
|
-
time5 - time4
|
|
301
|
-
} ms`
|
|
302
|
-
);
|
|
303
|
-
debug(
|
|
304
|
-
`Created ${primaryCreate.length} ${
|
|
305
|
-
this.SyncObjectClass.name
|
|
306
|
-
}s in ${this.primaryModule.constructor.getName()} in ${
|
|
307
|
-
time6 - time5
|
|
308
|
-
} ms`
|
|
309
|
-
);
|
|
310
|
-
debug(
|
|
311
|
-
`Updated ${secondaryUpdate.length} ${
|
|
312
|
-
this.SyncObjectClass.name
|
|
313
|
-
}s in ${this.secondaryModule.constructor.getName()} in ${
|
|
314
|
-
time7 - time6
|
|
315
|
-
} ms`
|
|
316
|
-
);
|
|
317
|
-
debug(
|
|
318
|
-
`Created ${secondaryCreate.length} ${
|
|
319
|
-
this.SyncObjectClass.name
|
|
320
|
-
}s in ${this.secondaryModule.constructor.getName()} in ${
|
|
321
|
-
time8 - time7
|
|
322
|
-
} ms`
|
|
323
|
-
);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
async createSyncDBObject(objArr, entities) {
|
|
327
|
-
const entityIds = entities.map(
|
|
328
|
-
(ent) => ({ $elemMatch: { $eq: mongoose.Types.ObjectId(ent) } })
|
|
329
|
-
// return {"$elemMatch": {"$eq": ent}};
|
|
330
|
-
);
|
|
331
|
-
const dataIdentifiers = [];
|
|
332
|
-
for (const index in objArr) {
|
|
333
|
-
dataIdentifiers.push({
|
|
334
|
-
entity: entities[index],
|
|
335
|
-
id: objArr[index].dataIdentifier,
|
|
336
|
-
hash: objArr[index].dataIdentifierHash,
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
const primaryObj = objArr[0];
|
|
340
|
-
|
|
341
|
-
const createSyncObj = {
|
|
342
|
-
name: primaryObj.getName(),
|
|
343
|
-
entities,
|
|
344
|
-
hash: primaryObj.getHashData({
|
|
345
|
-
omitEmptyStringsFromData: this.omitEmptyStringsFromData,
|
|
346
|
-
}),
|
|
347
|
-
dataIdentifiers,
|
|
348
|
-
};
|
|
349
|
-
const filter = {
|
|
350
|
-
name: primaryObj.getName(),
|
|
351
|
-
dataIdentifiers: {
|
|
352
|
-
$elemMatch: {
|
|
353
|
-
id: primaryObj.dataIdentifier,
|
|
354
|
-
entity: entities[0],
|
|
355
|
-
},
|
|
356
|
-
},
|
|
357
|
-
entities: { $all: entityIds },
|
|
358
|
-
// entities
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
return await this.syncMO.upsert(filter, createSyncObj);
|
|
362
|
-
// return await this.syncMO.create(createSyncObj);
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
// Automatically syncs the objects with the secondary module if the object was updated
|
|
366
|
-
async sync(syncObjects) {
|
|
367
|
-
const batchUpdates = [];
|
|
368
|
-
const batchCreates = [];
|
|
369
|
-
const noChange = [];
|
|
370
|
-
const primaryEntityId = await this.primaryModule.entity.id;
|
|
371
|
-
const secondaryEntityId = await this.secondaryModule.entity.id;
|
|
372
|
-
|
|
373
|
-
const secondaryModuleName = this.secondaryModule.constructor.getName();
|
|
374
|
-
for (const primaryObj of syncObjects) {
|
|
375
|
-
const dataHash = primaryObj.getHashData({
|
|
376
|
-
omitEmptyStringsFromData: this.omitEmptyStringsFromData,
|
|
377
|
-
});
|
|
378
|
-
|
|
379
|
-
// get the sync object in the database if it exists
|
|
380
|
-
let syncObj = await this.syncMO.getSyncObject(
|
|
381
|
-
primaryObj.getName(),
|
|
382
|
-
primaryObj.dataIdentifier,
|
|
383
|
-
primaryEntityId
|
|
384
|
-
);
|
|
385
|
-
|
|
386
|
-
if (syncObj) {
|
|
387
|
-
debug('Sync object found, evaluating...');
|
|
388
|
-
const hashMatch = syncObj.hash === dataHash;
|
|
389
|
-
const dataIdentifierLength = syncObj.dataIdentifiers.length;
|
|
390
|
-
|
|
391
|
-
if (!hashMatch && dataIdentifierLength > 1) {
|
|
392
|
-
debug(
|
|
393
|
-
"Previously successful sync, but hashes don't match. Updating."
|
|
394
|
-
);
|
|
395
|
-
const secondaryObj = new this.SyncObjectClass({
|
|
396
|
-
data: primaryObj.data,
|
|
397
|
-
dataIdentifier:
|
|
398
|
-
Sync.getEntityObjIdForEntityIdFromObject(
|
|
399
|
-
syncObj,
|
|
400
|
-
secondaryEntityId
|
|
401
|
-
),
|
|
402
|
-
moduleName: secondaryModuleName,
|
|
403
|
-
useMapping: false,
|
|
404
|
-
});
|
|
405
|
-
secondaryObj.setSyncId(syncObj.id);
|
|
406
|
-
batchUpdates.push(secondaryObj);
|
|
407
|
-
} else if (hashMatch && dataIdentifierLength > 1) {
|
|
408
|
-
debug(
|
|
409
|
-
'Data hashes match, no updates or creates needed for this one.'
|
|
410
|
-
);
|
|
411
|
-
noChange.push(syncObj);
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
if (dataIdentifierLength === 1) {
|
|
415
|
-
debug(
|
|
416
|
-
"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."
|
|
417
|
-
);
|
|
418
|
-
primaryObj.setSyncId(syncObj.id);
|
|
419
|
-
batchCreates.push(primaryObj);
|
|
420
|
-
}
|
|
421
|
-
} else {
|
|
422
|
-
debug(
|
|
423
|
-
"No sync object, so we'll try creating, first creating an object"
|
|
424
|
-
);
|
|
425
|
-
syncObj = await this.createSyncDBObject(
|
|
426
|
-
[primaryObj],
|
|
427
|
-
[primaryEntityId, secondaryEntityId]
|
|
428
|
-
);
|
|
429
|
-
primaryObj.setSyncId(syncObj.id);
|
|
430
|
-
batchCreates.push(primaryObj);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
const updateRes =
|
|
434
|
-
batchUpdates.length > 0
|
|
435
|
-
? await this.secondaryModule.batchUpdateSyncObjects(
|
|
436
|
-
batchUpdates,
|
|
437
|
-
this
|
|
438
|
-
)
|
|
439
|
-
: [];
|
|
440
|
-
const createRes =
|
|
441
|
-
batchCreates.length > 0
|
|
442
|
-
? await this.secondaryModule.batchCreateSyncObjects(
|
|
443
|
-
batchCreates,
|
|
444
|
-
this
|
|
445
|
-
)
|
|
446
|
-
: [];
|
|
447
|
-
return updateRes.concat(createRes).concat(noChange);
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
// takes in:
|
|
451
|
-
// 1. the Sync Id of an object in our database
|
|
452
|
-
// 2. the object Id in the form of a json object for example:
|
|
453
|
-
// {
|
|
454
|
-
// companyId: 12,
|
|
455
|
-
// saleId:524
|
|
456
|
-
// }
|
|
457
|
-
// 3. the module manager calling the function
|
|
458
|
-
async confirmCreate(syncObj, createdId, moduleManager) {
|
|
459
|
-
const dataIdentifier = {
|
|
460
|
-
entity: await moduleManager.entity.id,
|
|
461
|
-
id: createdId,
|
|
462
|
-
hash: this.SyncObjectClass.hashJSON(createdId),
|
|
463
|
-
};
|
|
464
|
-
// No matter what, save the hash because why not?
|
|
465
|
-
// TODO this is suboptimal because it does 2 DB requests where only 1 is needed
|
|
466
|
-
// TODO If you want to get even more optimized, batch any/all updates together.
|
|
467
|
-
// Also this is only needed because of the case where an "update" becomes a "create" when we find only
|
|
468
|
-
// 1 data identifier. So, during `sync()`, if we see that the hashes don't match, we check for DataIDs and
|
|
469
|
-
// decide to create in the "target" or "secondary" because we know it failed for some reason. We also want
|
|
470
|
-
// to hold off on updating the hash in case the create fails for some reason again.
|
|
471
|
-
|
|
472
|
-
await this.syncMO.update(syncObj.syncId, {
|
|
473
|
-
hash: syncObj.getHashData({
|
|
474
|
-
omitEmptyStringsFromData: this.omitEmptyStringsFromData,
|
|
475
|
-
}),
|
|
476
|
-
});
|
|
477
|
-
|
|
478
|
-
const result = await this.syncMO.addDataIdentifier(
|
|
479
|
-
syncObj.syncId,
|
|
480
|
-
dataIdentifier
|
|
481
|
-
);
|
|
482
|
-
|
|
483
|
-
return result;
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
async confirmUpdate(syncObj) {
|
|
487
|
-
debug(
|
|
488
|
-
'Successfully updated secondaryObject. Updating the hash in the DB'
|
|
489
|
-
);
|
|
490
|
-
const result = await this.syncMO.update(syncObj.syncId, {
|
|
491
|
-
hash: syncObj.getHashData({
|
|
492
|
-
omitEmptyStringsFromData: this.omitEmptyStringsFromData,
|
|
493
|
-
}),
|
|
494
|
-
});
|
|
495
|
-
debug('Success');
|
|
496
|
-
|
|
497
|
-
return result;
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
module.exports = SyncManager;
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
const md5 = require('md5');
|
|
2
|
-
const { get } = require('@friggframework/assertions');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @file This file is meant to be the thing that enforces proper use of
|
|
6
|
-
* the Association model.
|
|
7
|
-
* For now, we're going to use the model directly and worry about proper use
|
|
8
|
-
* later...
|
|
9
|
-
*/
|
|
10
|
-
class Association {
|
|
11
|
-
static Config = {
|
|
12
|
-
name: 'Association',
|
|
13
|
-
|
|
14
|
-
reverseModuleMap: {},
|
|
15
|
-
};
|
|
16
|
-
constructor(params) {
|
|
17
|
-
super(params);
|
|
18
|
-
this.data = {};
|
|
19
|
-
|
|
20
|
-
let data = get(params, 'data');
|
|
21
|
-
this.moduleName = get(params, 'moduleName');
|
|
22
|
-
this.dataIdentifier = get(params, 'dataIdentifier');
|
|
23
|
-
|
|
24
|
-
this.dataIdentifierHash = this.constructor.hashJSON(
|
|
25
|
-
this.dataIdentifier
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
for (let key of this.constructor.Config.keys) {
|
|
29
|
-
this.data[key] =
|
|
30
|
-
this.constructor.Config.moduleMap[this.moduleName][key](data);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// matchHash is used to find matches between two sync objects
|
|
34
|
-
let matchHashData = [];
|
|
35
|
-
for (let key of this.constructor.Config.matchOn) {
|
|
36
|
-
matchHashData.push(this.data[key]);
|
|
37
|
-
}
|
|
38
|
-
this.matchHash = this.constructor.hashJSON(matchHashData);
|
|
39
|
-
|
|
40
|
-
this.syncId = null;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
equals(syncObj) {
|
|
44
|
-
return this.matchHash === syncObj.matchHash;
|
|
45
|
-
}
|
|
46
|
-
dataKeyIsReplaceable(key) {
|
|
47
|
-
return this.data[key] === null || this.data[key] === '';
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
isModuleInMap(moduleName) {
|
|
51
|
-
return this.constructor.Config.moduleMap[name];
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
getName() {
|
|
55
|
-
return this.name;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
getHashData() {
|
|
59
|
-
let orderedData = [];
|
|
60
|
-
for (let key of this.constructor.Config.keys) {
|
|
61
|
-
orderedData.push(this.data[key]);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return this.constructor.hashJSON(orderedData);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
setSyncId(syncId) {
|
|
68
|
-
this.syncId = syncId;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
reverseModuleMap(moduleName) {
|
|
72
|
-
return this.constructor.Config.reverseModuleMap[moduleName](this.data);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
static hashJSON(data) {
|
|
76
|
-
let dataString = JSON.stringify(data, null, 2);
|
|
77
|
-
return md5(dataString);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
module.exports = Association;
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
const ModuleManager = require('../../managers/ModuleManager');
|
|
2
|
-
const {
|
|
3
|
-
RequiredPropertyError,
|
|
4
|
-
} = require('@friggframework/errors/ValidationErrors');
|
|
5
|
-
const { get, getAndVerifyType } = require('@friggframework/assertions');
|
|
6
|
-
|
|
7
|
-
class Options {
|
|
8
|
-
constructor(params) {
|
|
9
|
-
super(params);
|
|
10
|
-
|
|
11
|
-
this.module = getAndVerifyType(params, 'module', ModuleManager);
|
|
12
|
-
this.integrations = getAndVerifyType(
|
|
13
|
-
params,
|
|
14
|
-
'integrations',
|
|
15
|
-
ModuleManager
|
|
16
|
-
);
|
|
17
|
-
this.isMany = Boolean(get(params, 'isMany', false));
|
|
18
|
-
this.hasUserConfig = Boolean(get(params, 'hasUserConfig', false));
|
|
19
|
-
this.requiresNewEntity = Boolean(
|
|
20
|
-
get(params, 'requiresNewEntity', false)
|
|
21
|
-
);
|
|
22
|
-
if (!params.display) {
|
|
23
|
-
throw new RequiredPropertyError({
|
|
24
|
-
parent: this,
|
|
25
|
-
key: 'display',
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
this.display = {};
|
|
30
|
-
this.display.name = get(params.display, 'name');
|
|
31
|
-
this.display.description = get(params.display, 'description');
|
|
32
|
-
this.display.detailsUrl = get(params.display, 'detailsUrl');
|
|
33
|
-
this.display.icon = get(params.display, 'icon');
|
|
34
|
-
this.keys = get(params, 'keys', []);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
get() {
|
|
38
|
-
return {
|
|
39
|
-
type: this.module.getName(),
|
|
40
|
-
|
|
41
|
-
// list of entities the module can connect to
|
|
42
|
-
integrations: this.integrations.map((val) => val.getName()),
|
|
43
|
-
|
|
44
|
-
// list of special data required to make an entity i.e. a shop id. This information should be sent back
|
|
45
|
-
keys: this.keys,
|
|
46
|
-
|
|
47
|
-
// Flag for if the User can configure any settings
|
|
48
|
-
hasUserConfig: this.hasUserConfig,
|
|
49
|
-
|
|
50
|
-
// if this integration can be used multiple times with the same integration pair. For example I want to
|
|
51
|
-
// connect two different Etsy shops to the same Freshbooks account.
|
|
52
|
-
isMany: this.isMany,
|
|
53
|
-
|
|
54
|
-
// if this is true it means we need to create a new entity for every integration pair and not use an
|
|
55
|
-
// existing one. This would be true for scenarios where the client wishes to have individual control over
|
|
56
|
-
// the integerations it has connected to its app. They would want this to let their users only delete
|
|
57
|
-
// single integrations without notifying our server.
|
|
58
|
-
requiresNewEntity: this.requiresNewEntity,
|
|
59
|
-
|
|
60
|
-
// this is information required for the display side of things on the front end
|
|
61
|
-
display: this.display,
|
|
62
|
-
|
|
63
|
-
// this is information for post-authentication config, using jsonSchema and uiSchema for display on the frontend
|
|
64
|
-
// Maybe include but probably not, I like making someone make a follow-on request
|
|
65
|
-
// configOptions: this.configOptions,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
module.exports = Options;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
const IntegrationManager = require('../../managers/IntegrationManager');
|
|
2
|
-
const { get, getAndVerifyType } = require('@friggframework/assertions');
|
|
3
|
-
|
|
4
|
-
class Options {
|
|
5
|
-
constructor(params) {
|
|
6
|
-
super(params);
|
|
7
|
-
|
|
8
|
-
this.integrationManager = getAndVerifyType(
|
|
9
|
-
params,
|
|
10
|
-
'integrationManager',
|
|
11
|
-
IntegrationManager
|
|
12
|
-
);
|
|
13
|
-
this.fromVersion = get(params, 'fromVersion');
|
|
14
|
-
this.toVersion = get(params, 'toVersion');
|
|
15
|
-
this.generalFunctions = get(params, 'generalFunctions', []);
|
|
16
|
-
this.perIntegrationFunctions = get(
|
|
17
|
-
params,
|
|
18
|
-
'perIntegrationFunctions',
|
|
19
|
-
[]
|
|
20
|
-
);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
get() {
|
|
24
|
-
return {
|
|
25
|
-
type: this.integrationManager.getName(),
|
|
26
|
-
fromVersion: this.fromVersion,
|
|
27
|
-
toVersion: this.toVersion,
|
|
28
|
-
generalFunctions: this.generalFunctions,
|
|
29
|
-
perIntegrationFunctions: this.perIntegrationFunctions,
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
module.exports = Options;
|