-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.js
155 lines (137 loc) · 3.67 KB
/
template.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
const sendHttpRequest = require('sendHttpRequest');
const JSON = require('JSON');
const getRequestHeader = require('getRequestHeader');
const encodeUriComponent = require('encodeUriComponent');
const logToConsole = require('logToConsole');
const getContainerVersion = require('getContainerVersion');
const makeString = require('makeString');
const makeTableMap = require('makeTableMap');
const containerVersion = getContainerVersion();
const isDebug = containerVersion.debugMode;
const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = getRequestHeader('trace-id');
if (data.type === 'lead') {
createLead(createPerson());
} else {
createPerson();
}
function createPerson() {
const requestUrl = 'https://api.pipedrive.com/v1/persons?api_token=' + enc(data.apiToken);
const postBody = makeTableMap(data.person || [], 'field', 'value') || {};
if (data.name) postBody.name = data.name;
if (data.email) postBody.email = [data.email];
if (data.phone) postBody.phone = [data.phone];
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'PipeDrive',
Type: 'Request',
TraceId: traceId,
EventName: 'Person',
RequestMethod: 'POST',
RequestUrl: requestUrl,
RequestBody: postBody
})
);
}
return sendHttpRequest(
requestUrl,
(statusCode, headers, body) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'PipeDrive',
Type: 'Response',
TraceId: traceId,
EventName: 'Person',
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body
})
);
}
if (statusCode >= 200 && statusCode < 303) {
if (data.type === 'lead') {
return JSON.parse(body).data.id;
} else {
data.gtmOnSuccess();
}
} else {
data.gtmOnFailure();
}
},
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: 'POST'
},
JSON.stringify(postBody)
);
}
function createLead() {
const requestUrl = 'https://api.pipedrive.com/v1/leads?api_token=' + enc(data.apiToken);
const postBody = makeTableMap(data.lead || [], 'field', 'value') || {};
if (data.leadName) postBody.name = data.leadName;
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'PipeDrive',
Type: 'Request',
TraceId: traceId,
EventName: 'Lead',
RequestMethod: 'POST',
RequestUrl: requestUrl,
RequestBody: postBody
})
);
}
return sendHttpRequest(
requestUrl,
(statusCode, headers, body) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'PipeDrive',
Type: 'Response',
TraceId: traceId,
EventName: 'Lead',
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body
})
);
}
if (statusCode >= 200 && statusCode < 303) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
},
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: 'POST'
},
JSON.stringify(postBody)
);
}
function enc(data) {
data = data || '';
return encodeUriComponent(makeString(data));
}
function determinateIsLoggingEnabled() {
if (!data.logType) {
return isDebug;
}
if (data.logType === 'no') {
return false;
}
if (data.logType === 'debug') {
return isDebug;
}
return data.logType === 'always';
}