-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsubmit.js
148 lines (134 loc) · 3.33 KB
/
submit.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
const axios = require("axios");
const { APIKEY } = process.env;
exports.main = ({ accountId, contact, body, headers }, sendResponse) => {
const { email, firstName, lastName, formId, propertyId } = body;
const FORMS_API = `https://api.hsforms.com/submissions/v3/integration/submit/${accountId}/${formId}`;
const CONTACT_API = `https://api.hubapi.com/contacts/v1/contact/email/${email}/profile`;
const ASSOCIATIONS_API = `https://api.hubapi.com/crm/v3/associations/contact/p${accountId}_propertylisting/batch/create`;
if (!APIKEY) {
sendResponse({
statusCode: 403,
body: { message: "API key not present" },
});
}
const getUtk = () => {
if (headers.hasOwnProperty("Cookie")) {
return headers.Cookie.split("; ")
.find((row) => row.startsWith("hubspotutk"))
.split("=")[1];
}
};
const submitFormData = async () => {
try {
await axios({
method: "post",
url: FORMS_API,
data: {
fields: [
{
name: "firstname",
value: firstName,
},
{
name: "lastname",
value: lastName,
},
{
name: "email",
value: email,
},
],
context: {
hutk: getUtk(),
},
},
});
} catch (e) {
sendResponse({
statusCode: 500,
body: { message: e },
});
}
};
const getContactByEmail = async () => {
const CONTACT_API_POLLING_DELAY = 500;
const delay = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
const fetchContact = async () => {
return axios({
method: "get",
url: CONTACT_API,
params: {
portalId: accountId,
hapikey: APIKEY,
},
validateStatus: function (status) {
return status < 500;
},
});
};
try {
let contactData = null;
while (!contactData) {
const { data, status } = await fetchContact();
if (status == 200) {
contactData = data.vid;
} else {
await delay(CONTACT_API_POLLING_DELAY);
}
}
return contactData;
} catch (e) {
sendResponse({
statusCode: 500,
body: { message: e },
});
}
};
const associateContactWithProperty = async (
visitorId,
accountId,
propertyId
) => {
try {
await axios({
method: "post",
url: ASSOCIATIONS_API,
params: {
portalId: accountId,
hapikey: APIKEY,
},
data: {
inputs: [
{
from: {
id: visitorId,
},
to: {
id: propertyId,
},
type: "property_listing_to_contact",
},
],
},
});
} catch (e) {
sendResponse({
statusCode: 500,
body: { message: e },
});
}
};
(async () => {
await submitFormData();
let visitorId;
if (contact && contact.hasOwnProperty("vid")) {
visitorId = contact.vid;
} else {
visitorId = await getContactByEmail(email);
}
await associateContactWithProperty(visitorId, accountId, propertyId);
sendResponse({ body: { statusCode: 200 } });
})();
};