-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
156 lines (131 loc) · 3.41 KB
/
index.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
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
require('dotenv').config()
const util = require('util')
const csv = require('csv-parser')
const fs = require('fs')
const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({ "accessToken": process.env.PROD_ACCESS_TOKEN });
const objectType = 'deal'
const fileName = 'INSERT_FILE_NAME.csv'
let inputs = []
const addProperty = (row) => {
if (row.fieldType == "number") return addNumber(row)
if (row.fieldType == "select") return addSelect(row)
if (row.fieldType == "checkbox") return addCheckbox(row)
if (row.fieldType == "string") return addString(row)
if (row.fieldType == "multi") return addMulti(row)
if (row.fieldType == "owner") return addOwner(row)
if (row.fieldType == "date") return addDate(row)
console.log(`Unsupported Field Type: "${row.fieldType}"`)
}
const addNumber = (row) => {
console.log(`Adding Number "${row.label}"`)
inputs.push(
{
"name": row.name,
"label": row.label,
"description": row.description,
"type": "number",
"fieldType": "number",
"groupName": row.groupName
}
)
}
const addSelect = (row) => {
console.log(`Adding Select "${row.label}"`)
const fixedOptions = row.options.replace(/FALSE/g,'false')
inputs.push(
{
"name": row.name,
"label": row.label,
"description": row.description,
"type": "enumeration",
"fieldType": "select",
"groupName": row.groupName,
"options": JSON.parse(fixedOptions)
}
)
}
const addCheckbox = (row) => {
console.log(`Adding Checkbox "${row.label}"`)
const fixedOptions = row.options.replace(/FALSE/g,'false')
inputs.push(
{
"name": row.name,
"label": row.label,
"description": row.description,
"type": "enumeration",
"fieldType": "checkbox",
"groupName": row.groupName,
"options": JSON.parse(fixedOptions)
}
)
}
const addString = (row) => {
console.log(`Adding String "${row.label}"`)
inputs.push(
{
"name": row.name,
"label": row.label,
"description": row.description,
"type": "string",
"fieldType": "text",
"groupName": row.groupName
}
)
}
const addMulti = (row) => {
console.log(`Adding Multi "${row.label}"`)
inputs.push(
{
"name": row.name,
"label": row.label,
"description": row.description,
"type": "string",
"fieldType": "textarea",
"groupName": row.groupName
}
)
}
const addDate = (row) => {
console.log(`Adding Date "${row.label}"`)
inputs.push(
{
"name": row.name,
"label": row.label,
"description": row.description,
"type": "date",
"fieldType": "date",
"groupName": row.groupName
}
)
}
const addOwner = (row) => {
console.log(`Adding Owner "${row.label}"`)
inputs.push(
{
"name": row.name,
"label": row.label,
"description": row.description,
"type": "enumeration",
"fieldType": "select",
"groupName": row.groupName,
"externalOptions": true,
"referencedObjectType": "OWNER"
}
)
}
fs.createReadStream(fileName)
.pipe(csv())
.on('data', (row) => addProperty(row))
.on('end', () => {
const BatchInputPropertyCreate = {
inputs
}
hubspotClient.crm.properties.batchApi.create(objectType, BatchInputPropertyCreate).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
}).finally(() => {
console.log('Finished')
})
})