-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonScheduleNewV1_BU
291 lines (291 loc) · 9.17 KB
/
onScheduleNewV1_BU
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// global variables
crmContactOwnerEmail = zoho.loginuserid;
plugin = "acuity_scheduling";
function_new = "onScheduleNew";
// end variables
resp = crmAPIRequest.toMap();
respBody = resp.get("body");
// debugging email
debugMail = "ciddarth@xtracut.com";
debugVal = zoho.crm.getOrgVariable("acuityscheduling1__debug");
if(debugVal == "no")
{
debug = false;
}
else if(debugVal == "yes")
{
debug = true;
}
// send response mail
if(debug)
{
// Define the URL endpoint
// Create the payload as a map (dictionary in Deluge)
payload = Map();
payload.put("log_msg","Incoming Request from WebHook: \n" + resp);
payload.put("StatusCode","201");
payload.put("user_mailid",crmContactOwnerEmail);
payload.put("Plugin",plugin);
payload.put("function",function_new);
// Send the request using postUrl() in Deluge
response = invokeurl
[
url :"https://xtracut-log-management-system.vercel.app/api/create/"
type :POST
parameters:payload
content-type:"application/x-www-form-urlencoded"
];
}
// Getting appointment ID and Action type
paramsPairs = respBody.toList("&");
id = "";
actionType = "";
for each param in paramsPairs
{
fieldsName = param.toList("=");
if(fieldsName.get(0) == "action")
{
actionType = fieldsName.get(1);
}
if(fieldsName.get(0) == "id")
{
id = fieldsName.get(1);
}
}
// Getting Module and Acitivity
moduleType = zoho.crm.getOrgVariable("acuityscheduling1__module");
// Leads or Contacts
activityType = zoho.crm.getOrgVariable("acuityscheduling1__activities");
// Mobile or Phone
contactType = zoho.crm.getOrgVariable("acuityscheduling1__phonemobile");
// Meetings or Tasks
// Get all details about appoinment
requestObj = Map();
requestObj.put("id",id);
// Invoke Connector to get single appoinment details
singleAppointmentResponse = zoho.crm.invokeConnector("acuityscheduling1.acuityscheduling.getsingleappointment",requestObj).toMap();
if(debug)
{
// Define the URL endpoint
// Create the payload as a map (dictionary in Deluge)
payload = Map();
payload.put("log_msg","Single Appoinment Response: \n" + singleAppointmentResponse.get("response"));
payload.put("StatusCode","201");
payload.put("user_mailid",crmContactOwnerEmail);
payload.put("Plugin",plugin);
payload.put("function",function_new);
// Send the request using postUrl() in Deluge
response = invokeurl
[
url :"https://xtracut-log-management-system.vercel.app/api/create/"
type :POST
parameters:payload
content-type:"application/x-www-form-urlencoded"
];
}
singleAppoinmentData = singleAppointmentResponse.get("response");
email = singleAppoinmentData.get("email");
firstName = singleAppoinmentData.get("firstName");
lastName = singleAppoinmentData.get("lastName");
phoneNumber = singleAppoinmentData.get("phone");
appoinmentType = singleAppoinmentData.get("type");
formsText = singleAppoinmentData.get("formsText");
meetingDateTime = singleAppoinmentData.get("datetime");
confirmationPage = singleAppoinmentData.get("confirmationPage");
agentPage = "https://secure.acuityscheduling.com/appointments/view/" + id;
appoinmentPrice = singleAppoinmentData.get("price");
calendarName = singleAppoinmentData.get("calendar");
// Check if the user is present in the given module
userPresentFlag = false;
getAllUsersResp = zoho.crm.getRecords(moduleType).toJsonList();
for each user in getAllUsersResp
{
tempEmail = user.getJson("Email");
if(tempEmail == email)
{
userPresentFlag = true;
userId = user.getJson("id");
break;
}
}
// If not present create new user
if(!userPresentFlag)
{
// create user with first name, last name and email
newUser = Map();
newUser.put("First_Name",firstName);
newUser.put("Last_Name",lastName);
newUser.put("Email",email);
newUser.put(contactType,phoneNumber);
createEntity = zoho.crm.createRecord(moduleType,newUser,{"trigger":{"workflow"}});
// fetch id of newly created user
allUserResp = zoho.crm.getRecords(moduleType).toJsonList();
for each user in allUserResp
{
tempEmail = user.get("Email");
if(email == tempEmail)
{
userId = user.getJson("id");
break;
}
}
// Define the URL endpoint
// Create the payload as a map (dictionary in Deluge)
payload = Map();
payload.put("log_msg","New user created: " + firstName + ", " + lastName + ", " + email);
payload.put("StatusCode","201");
payload.put("user_mailid",crmContactOwnerEmail);
payload.put("Plugin",plugin);
payload.put("function",function_new);
// Send the request using postUrl() in Deluge
response = invokeurl
[
url :"https://xtracut-log-management-system.vercel.app/api/create/"
type :POST
parameters:payload
content-type:"application/x-www-form-urlencoded"
];
}
if(debug)
{
payload = Map();
payload.put("log_msg","User ID: " + userId);
payload.put("StatusCode","201");
payload.put("user_mailid",crmContactOwnerEmail);
payload.put("Plugin",plugin);
payload.put("function",function_new);
// Send the request using postUrl() in Deluge
response = invokeurl
[
url :"https://xtracut-log-management-system.vercel.app/api/create/"
type :POST
parameters:payload
content-type:"application/x-www-form-urlencoded"
];
}
// Add Activity to the user
newActivityRecord = Map();
// Creating Title Text
titleText = appoinmentType + " to " + firstName + " " + lastName;
// Creating new activity - Tasks or Meeting
if(activityType == "Tasks")
{
newActivityRecord.put("Subject",titleText);
newActivityRecord.put("Description",formsText);
newActivityRecord.put("Due_Date",meetingDateTime.subString(0,10));
newActivityRecord.put("acuityscheduling1__AcuityID",id);
// appointment id
newActivityRecord.put("acuityscheduling1__Customer_URL",confirmationPage);
// appointment page
newActivityRecord.put("acuityscheduling1__Agent_URL",agentPage);
newActivityRecord.put("acuityscheduling1__Task_Price",appoinmentPrice);
// appointment price
newActivityRecord.put("acuityscheduling1__Calendar",calendarName);
// appoinment calendar name
}
else
{
newActivityRecord.put("Event_Title",titleText);
newActivityRecord.put("Description",formsText);
newActivityRecord.put("acuityscheduling1__AcuityID",id);
// appointment id
newActivityRecord.put("acuityscheduling1__ConfirmationPage",confirmationPage);
// appointment page
newActivityRecord.put("acuityscheduling1__AcuityURL",agentPage);
newActivityRecord.put("acuityscheduling1__Calendar_Name",calendarName);
// appoinment calendar name
newActivityRecord.put("acuityscheduling1__Event_Price",appoinmentPrice);
// appointment price
timeZone = singleAppoinmentData.get("timezone");
// timezone : redundent
duration = singleAppoinmentData.get("duration");
// duration
// Calculating Meeting start and end time
startTime = singleAppoinmentData.get("datetime").subString(0,19);
requestParams = Map();
requestParams.put("time",startTime);
requestParams.put("minutes",duration);
// Digital Ocean Function that adds the given duration to the time
endTime = invokeurl
[
url :"https://faas-blr1-8177d592.doserverless.co/api/v1/web/fn-0e938d59-984f-4e43-b133-7cc920485b22/default/add_minutes_to_time"
type :POST
parameters:requestParams
content-type:"application/x-www-form-urlencoded"
];
if(debug)
{
// Define the URL endpoint
// Create the payload as a map (dictionary in Deluge)
payload = Map();
payload.put("log_msg","End Time Response: \n");
payload.put("StatusCode","201");
payload.put("user_mailid",crmContactOwnerEmail);
payload.put("Plugin",plugin);
payload.put("function",function_new);
// Send the request using postUrl() in Deluge
response = invokeurl
[
url :"https://xtracut-log-management-system.vercel.app/api/create/"
type :POST
parameters:payload
content-type:"application/x-www-form-urlencoded"
];
}
newActivityRecord.put("Start_DateTime",startTime);
newActivityRecord.put("End_DateTime",endTime);
}
// Setting id based on moduleType - Leads or Contacts
if(moduleType == "Leads")
{
newActivityRecord.put("What_Id",userId);
newActivityRecord.put("$se_module",moduleType);
}
else
{
newActivityRecord.put("Who_Id",userId);
}
// Creating Activity
createRecordResp = zoho.crm.createRecord(activityType,newActivityRecord,{"trigger":{"workflow"}});
if(debug)
{
payload = Map();
payload.put("log_msg","New Record Creation Response: \n" + createRecordResp);
payload.put("StatusCode","201");
payload.put("user_mailid",crmContactOwnerEmail);
payload.put("Plugin",plugin);
payload.put("function",function_new);
// Send the request using postUrl() in Deluge
response = invokeurl
[
url :"https://xtracut-log-management-system.vercel.app/api/create/"
type :POST
parameters:payload
content-type:"application/x-www-form-urlencoded"
];
}
// raising signal
signalMap = Map();
signalMap.put("signal_namespace","acuityscheduling1.acuity");
signalMap.put("email",email);
signalMap.put("subject",titleText);
signalMap.put("message",formsText);
signalResult = zoho.crm.raiseSignal(signalMap);
if(debug)
{
payload = Map();
payload.put("log_msg","Signal Result: " + signalResult);
payload.put("StatusCode","201");
payload.put("user_mailid",crmContactOwnerEmail);
payload.put("Plugin",plugin);
payload.put("function",function_new);
// Send the request using postUrl() in Deluge
response = invokeurl
[
url :"https://xtracut-log-management-system.vercel.app/api/create/"
type :POST
parameters:payload
content-type:"application/x-www-form-urlencoded"
];
}
return "Hello world";