-
Notifications
You must be signed in to change notification settings - Fork 0
/
getAssociations.js
107 lines (97 loc) · 3.87 KB
/
getAssociations.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const { hubspotClient, hubspotApi } = require('./lib/hubspotClient');
const { fs, path, dataCurrentPath } = require('./lib/pathsAndFS');
const { pluralMap } = require('./lib/typeMaps');
const { getHubspotAssociationKey, getHubspotAssociationKeyFor } = require('./lib/getHubspotAssociationKey');
async function getAssociations(forceFecth) {
const filePath = path.resolve(dataCurrentPath, 'associations.json');
if (fs.existsSync(filePath) && ! forceFecth) {
return require(filePath);
}
const associationObjects = Object.keys(pluralMap);
associationObjects.push(...['note', 'call', 'meeting', 'email', 'task', 'communication']);
const associations = {};
for (const fromType of associationObjects) {
for (const toType of associationObjects) {
const body = await hubspotApi.get('v4/associations/' + fromType + '/' + toType + '/labels');
await new Promise((r) => { setTimeout(r, 200) });
for (const item of body.results) {
const entry = {
fromType,
toType,
label: item.label,
category: item.category,
typeId: item.typeId,
}
const hubspotAssociationKey = getHubspotAssociationKeyFor(entry);
associations[hubspotAssociationKey] = entry;
}
}
};
generateReverse(associations);
fs.writeFileSync(filePath, JSON.stringify(associations, null, 2));
return associations;
}
function generateReverse(associations) {
for (const [associationKey, association] of Object.entries(associations)) {
if (association.reverse != null) {
continue;
}
const candidates = [];
for (const [aKey, a] of Object.entries(associations)) {
if (aKey != associationKey &&
a.toType === association.fromType &&
a.fromType === association.toType &&
association.reverse == null) {
candidates.push(a);
}
};
function setReverse(candidate) {
association.reverse = getHubspotAssociationKeyFor(candidate);
candidate.reverse = associationKey;
}
if (candidates.length === 1) {
setReverse(candidates[0]);
continue;
}
const correspondingKeyCandidates = candidates.filter((a) => getHubspotAssociationKey(a.toType, a.fromType, a.label) == associationKey);
if (correspondingKeyCandidates.length > 1) throw new Error('Found more than one canidate with same label ' + JSON.stringify(association)+ ' Candidates ' + JSON.stringify(sameLabelCandidates));
if (correspondingKeyCandidates.length === 1) {
setReverse(correspondingKeyCandidates[0]);
continue;
}
if (association.label != null && association.label.toLowerCase().includes('primary')) {
const nullLabelCandidates = candidates.filter((a) => a.label != null && a.label.toLowerCase().includes('primary'));
if (nullLabelCandidates.length === 1) {
setReverse(nullLabelCandidates[0]);
continue;
}
}
}
for (const [associationKey, association] of Object.entries(associations)) {
if (getHubspotAssociationKeyFor(association) != associationKey) {
console.log('Wrong key for', association, 'got: ' + associationKey + ' expected :' + getHubspotAssociationKeyFor(association));
continue;
}
if (association.reverse == null) {
console.log('Missing association for', associationKey, association);
continue;
}
if (associations[association.reverse] == null) {
console.log('Non existent reverse for', association);
continue;
}
const reverseAssociation = associations[association.reverse];
const reverseKey = getHubspotAssociationKeyFor(reverseAssociation);
if (reverseAssociation.reverse != associationKey) {
console.log('Wrong reverse For ',associationKey, association, ' ===> ',reverseKey, reverseAssociation);
}
}
}
module.exports = {
getAssociations
};
if (!global.SKIP_AUTO_GET_ASSOCIATIONS) {
(async () => {
await getAssociations(true);
})();
}