-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepareFieldsMapping.js
More file actions
166 lines (147 loc) · 4.88 KB
/
prepareFieldsMapping.js
File metadata and controls
166 lines (147 loc) · 4.88 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
166
/**
* Prepare configuration for custom data mapping
* Will not "touch" existing mappings in confs files just add the new ones.
* Make sure to erase the configuration files if you have modifications such as new dropdown items (or complee manually)
*/
const slug = require('slug');
const {fs, path, dataConfPath} = require('./lib/pathsAndFS');
const { mapType } = require('./lib/typeMaps');
const customFields = require('../data/custom_field_definitions.json');
const connectionFile = path.resolve(dataConfPath, 'custom_connections.json');
const customDefFiles = path.resolve(dataConfPath, 'custom_def.json');
const fieldsDefFiles = path.resolve(dataConfPath, 'fields_def.json');
const fieldsDef = fs.existsSync(fieldsDefFiles) ? require(fieldsDefFiles) : {};
const customDef = fs.existsSync(customDefFiles) ? require(customDefFiles) : {};
const connects = fs.existsSync(connectionFile) ? require(connectionFile) : {};;
// ----- custom fields --------- //
// extract Connect fields
const connectMap = {};
for (const cf of customFields.filter((c) => c.data_type === 'Connect')) {
if (cf.available_on.length > 1) throw new Error('Cannot handle multiple available on value for custom connected fields', JSON.stringify(cf));
cf.available_on = cf.fromType = cf.available_on[0];
delete cf.available_on;
connectMap[cf.id + ''] = cf;
}
for (const cf of customFields.filter((c) => c.data_type === 'Connect')) {
const fromType = mapType[cf.fromType];
const toCF = connectMap[cf.connected_id + ''];
if (toCF == null) throw new Error('Cannot find "to" connect field for' + JSON.stringify(cf));
const toType = mapType[toCF.fromType];
if (connects[fromType] == null) connects[fromType] = {};
if (connects[fromType][cf.id + '']) continue;
connects[fromType][cf.id + ''] = {
toType: toType,
name: cf.name,
toCopperId: cf.connected_id + '',
hubspotAssociationKey: null
}
}
for (const cf of customFields) {
if (cf.data_type === 'Connect') continue; // ignore connections
for (const type of cf.available_on) {
if (customDef[mapType[type]] == null) customDef[mapType[type]] = {};
if (customDef[mapType[type]][cf.id + '']) continue; // skip existing types
const def = {
name: cf.name,
dest: 'extras.others'
}
switch (cf.data_type) {
case 'String':
def.handle = 'Copy';
break;
case 'Date':
def.handle = 'DateToString';
break;
case 'Dropdown':
def.handle = 'MapItem';
def.conf = {};
for (const o of cf.options) {
def.conf[o.id + ''] = o.name;
}
break;
case 'MultiSelect':
def.handle = 'MapMultiString';
def.conf = {};
for (const o of cf.options) {
def.conf[o.id + ''] = o.name;
}
break;
case 'Checkbox':
def.handle = 'Bool';
break;
case 'Float':
def.handle = 'Num';
break;
case 'URL':
def.handle = 'Copy';
def.dest = 'extra.websites';
break;
default:
console.log('Unkown type: ', cf)
}
customDef[mapType[type]][cf.id + ''] = def;
}
}
// ------ contacts ------------ //
if (! fieldsDef.contact) fieldsDef.contact = {};
if (! fieldsDef.contact?.contact_type_id) {
const copperContactTypes = require('../data/contact_types.json');
const conf = {};
for (const cct of copperContactTypes) {
conf[cct.id + ''] = {
label: cct.name,
value: slug(cct.name, '_').toUpperCase()
}
}
fieldsDef.contact.contact_type_id = {
name: 'Contact Type',
dest: 'copper_contact_type',
handle: 'MapItemSELECT',
fieldType: 'select',
conf
};
}
if (! fieldsDef.contact?.status_id) {
const copperLeadStatuses = require('../data/lead_statuses.json');
const hubspotDefaultMap = {
'1000860': 'NEW',
'1000861': 'OPEN', // Qualified
'1000861': 'IN_PROGRESS',
'1000862': 'UNQUALIFIED'
}
const conf = {};
for (const cls of copperLeadStatuses) {
conf[cls.id + ''] = {
label: cls.name,
value: hubspotDefaultMap[cls.id + ''] || slug(cls.name, '_').toUpperCase()
}
}
fieldsDef.contact.status_id = {
name: 'Lead Status',
dest: 'hs_lead_status',
groupName: 'sales_properties',
fieldType: 'radio',
handle: 'MapItemSELECT',
conf
};
}
if (! fieldsDef.contact?.customer_source_id) {
const conf = {};
for (const cs of require('../data/customer_sources.json')) {
conf[cs.id + ''] = {
label: cs.name,
value: slug(cs.name, '_').toUpperCase()
}
}
fieldsDef.contact.customer_source_id = {
name: 'Customer Source',
dest: 'customer_source',
groupName: 'sales_properties',
fieldType: 'select',
handle: 'MapItemSELECT',
conf
};
}
fs.writeFileSync(connectionFile, JSON.stringify(connects, null, 2));
fs.writeFileSync(customDefFiles, JSON.stringify(customDef, null, 2));
fs.writeFileSync(fieldsDefFiles, JSON.stringify(fieldsDef, null, 2));