-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateMissingProperties.js
More file actions
165 lines (144 loc) · 4.7 KB
/
createMissingProperties.js
File metadata and controls
165 lines (144 loc) · 4.7 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
global.SKIP_AUTO_GET_PROPERTIES = true; // prevent getProperties to run automatically
const { hubspotClient } = require('./lib/hubspotClient');
const getCurrentProperties = require('./getProperties');
const { pluralMap } = require('./lib/typeMaps');
const customDefs = require('../data-hubspot/conf/custom_def.json');
const fieldDefs = require('../data-hubspot/conf/fields_def.json');
async function createProp(type, propertyObj) {
try {
const response = await hubspotClient.crm.properties.coreApi.create(type, propertyObj);
// console.log(response);
console.log(type + ' created ✅' + propertyObj.name);
} catch (e) {
if (e.body?.category === 'OBJECT_ALREADY_EXISTS') {
console.log(type + ' found ❓' + propertyObj.name);
} else {
console.log(e.body || e);
}
}
};
async function updateProp(type, udpateObjSource) {
const updateObj = structuredClone(udpateObjSource);
const name = updateObj.name;
delete updateObj.name;
try {
const response = await hubspotClient.crm.properties.coreApi.update(type, name, updateObj);
// console.log(response);
console.log(type + ' updated ✅' + name);
} catch (e) {
console.log(e.body || e);
}
};
function getProperties(type, currentProps) {
const todo = {
create: [{
groupName: type + 'information',
name: 'copperid',
label: 'CopperId',
hasUniqueValue: true,
type: 'string',
fieldType: 'text'
}, {
groupName: type + 'information',
name: 'description',
label: 'Description',
hasUniqueValue: false,
type: 'string',
fieldType: 'textarea'
}, {
groupName: type + 'information',
name: 'tags',
label: 'Tags',
hasUniqueValue: false,
type: 'string',
fieldType: 'text'
}
],
update: []
};
// check properties from custom_def
checkExistingPropsVsDefinition(customDefs, type, currentProps, todo);
// check properties from custom_def
checkExistingPropsVsDefinition(fieldDefs, type, currentProps, todo);
return todo;
}
(async () => {
for (const type of Object.keys(pluralMap)) {
console.log('**** ' + type);
const propsCurrent = await getCurrentProperties(pluralMap[type]);
const propRequired = getProperties(type, propsCurrent);
let createdOne = false;
for (const prop of propRequired.create) {
if (propsCurrent.find((i) => i.name == prop.name)) {
// check if property match
console.log(type + ' skip ⏭️ ' + prop.name);
} else {
await createProp(type, prop);
createdOne = true;
}
}
let updatedOne = false;
for (const prop of propRequired.update) {
await updateProp(type, prop);
createdOne = true;
}
if (createdOne || updatedOne) {
await getCurrentProperties(pluralMap[type], true);
}
}
})();
function checkExistingPropsVsDefinition(definitions, type, currentProps, todo) {
const typeDefs = definitions[type];
if (!typeDefs) return;
for (const k of Object.keys(typeDefs)) {
const def = typeDefs[k];
if (def.dest.startsWith('extra')) continue;
const matchingProp = currentProps.find((p) => p.name === def.dest);
if (matchingProp) { // Found;
let action = null;
// check if OK
if (def.handle === 'MapItemSELECT') {
def.fieldType = def.fieldType || 'select';
if (matchingProp.type !== 'enumeration' || matchingProp.fieldType !== def.fieldType) {
console.log('>>' + type + ' Wrong type for def', def, 'Required: ' + matchingProp.fieldType);
throw new Error('Cannot update');
}
// hide all existing option
const options = structuredClone(matchingProp.options).map((o) => {
o.hidden = true;
return o;
});
for (const oKey of Object.keys(def.conf)) {
const oVal = def.conf[oKey];
const matchedOption = matchingProp.options.find((o) => o.value === oVal.value);
if (!matchedOption) {
console.log('>>' + type + '>' + def.name + ' missing option: ' + oVal.value);
options.push(oVal);
action = 'update';
} else {
matchedOption.hidden = false;
}
}
if (action != null) {
const propObj = {
name: def.dest,
options: options
};
todo.update.push(propObj);
}
}
} else {
const propObj = {
name: def.dest,
label: def.name,
groupName: def.groupName || type + 'information',
propertyUpdate: 'options',
hasUniqueValue: false,
type: 'enumeration',
fieldType: def.fieldType || 'select',
options: Object.values(def.conf)
}
todo.create.push(propObj);
}
}
}