-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.js
More file actions
47 lines (37 loc) · 1.46 KB
/
upload.js
File metadata and controls
47 lines (37 loc) · 1.46 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
const {fs, path, SyncStatus} = require('./lib/pathsAndFS');
const { hubspotClient } = require('./lib/hubspotClient');
const syncStatuses = {};
async function upload(typePlural, foreachItem) {
const items = require('../data-hubspot/source/' + typePlural + '.json');
const syncStatus = new SyncStatus(typePlural);
let r = items.length;
for (const item of items) {
r--;
if (syncStatus.get(item.copperid) != null) continue;
if (foreachItem != null) foreachItem(item);
delete item._transitional;
try {
const res = await hubspotClient.crm[typePlural].basicApi.create({properties: item});
await syncStatus.add(item.copperid, res.id);
console.log(typePlural, r, item.name || res.id);
} catch (e) {
console.log('On item', item);
console.log('Error', e.body );
try { syncStatus.close(); } catch (e) { }
process.exit(1);
}
}
syncStatus.close();
syncStatuses[typePlural] = syncStatus.data;
}
async function flow() {
await upload('companies');
await upload('contacts', function(item) {
// add 'associatedcompanyid': to contacts and remove copper_company_id
if (item._transitional.copperCompanyId == null) return;
const hubspotCompanyId = syncStatuses.companies[item._transitional.copperCompanyId];
if (hubspotCompanyId == null) throw new Error('Cannot find synchronized copperCompanyId ' + item._transitional.copperCompanyId);
item.associatedcompanyid = hubspotCompanyId;
});
}
flow();