-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateMissingAssociations.js
More file actions
72 lines (63 loc) · 2.71 KB
/
createMissingAssociations.js
File metadata and controls
72 lines (63 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
global.SKIP_AUTO_GET_ASSOCIATIONS = true; // prevent getAssociations to run automatically
const { hubspotApi } = require('./lib/hubspotClient');
const { getAssociations } = require('./getAssociations');
const { SyncStatus } = require('./lib/pathsAndFS');
const { pluralMap } = require('./lib/typeMaps');
const customAssociations = require('../data-hubspot/conf/custom_connections.json');
let currentAssociationsMap = null;
const customMap = {};
const syncStatus = new SyncStatus('associationDefinitions');
(async () => {
currentAssociationsMap = await getAssociations();
initCopperCustoms();
const autos = checkCustomsAndGetAuto();
if (await createMissingAssociations(autos) > 0) {
await getAssociations(true); // refresh associations
}
})();
async function createMissingAssociations(autos) {
let created = 0;
for (const auto of autos) {
if (syncStatus.get(auto.fromCopperId) != null) continue;
const hubspotAssociationKey = await hubspotApi.createAssociationLabel(auto.fromType, auto.toType, auto.name);
await syncStatus.add(auto.fromCopperId, hubspotAssociationKey);
console.log('Created', auto, 'With association key: ' + hubspotAssociationKey);
created++;
}
return created;
}
function checkCustomsAndGetAuto() {
const ignored = [];
const auto = [];
for (const [fromCopperId, fromCopperEntry] of Object.entries(customMap)) {
const toCopperId = fromCopperEntry.toCopperId;
fromCopperEntry.fromCopperId = fromCopperId;
if (fromCopperEntry.hubspotAssociationKey !== null && customMap[toCopperId].hubspotAssociationKey !== null) {
throw new Error('Ony one direction of associations defintion should be mapped in file data-hubspot/conf/custom_connections.json please check; \n' +
JSON.stringify(fromCopperEntry) + '\n and \n' + JSON.stringify(customMap[toCopperId]));
}
const hubspotAssociationKey = fromCopperEntry.hubspotAssociationKey;
if (hubspotAssociationKey === null && customMap[toCopperId].hubspotAssociationKey === null) {
ignored.push(fromCopperEntry);
continue;
}
if (hubspotAssociationKey === null) continue;
if (hubspotAssociationKey === "auto") {
auto.push(fromCopperEntry);
continue;
}
if (currentAssociationsMap[hubspotAssociationKey] == null) {
throw new Error('Cannot find hubspot corresponding entry for ' + JSON.stringify(fromCopperEntry));
}
}
console.log('Custom checked Ignored: ' + ignored.length + ' autos: ' + auto.length);
return auto;
}
function initCopperCustoms() {
for (const [fromType, typeEntries] of Object.entries(customAssociations)) {
for (const [copperId, entry] of Object.entries(typeEntries)) {
entry.fromType = fromType;
customMap[copperId] = entry;
}
}
}