This repository was archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrm.protected.js
174 lines (164 loc) · 4.43 KB
/
crm.protected.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const sfdcAuthenticatePath = Runtime.getFunctions()['auth/sfdc-authenticate'].path;
const { sfdcAuthenticate } = require(sfdcAuthenticatePath);
exports.handler = async function (context, event, callback) {
let response = new Twilio.Response();
response.appendHeader('Content-Type', 'application/json');
try {
console.log('Frontline user identity: ' + event.Worker);
if (event.Anchor) { // workaround to avoid pagination
response.setBody([]);
return callback(null, response);
} else {
const sfdcConnectionIdentity = await sfdcAuthenticate(context, event.Worker);
const { connection, identityInfo } = sfdcConnectionIdentity;
console.log('Connected as SF user:' + identityInfo.username);
switch (event.Location) {
case 'GetCustomerDetailsByCustomerId': {
response.setBody(
await getCustomerDetailsByCustomerIdCallback(
event.CustomerId,
connection)
);
break;
}
case 'GetCustomersList': {
if (event.Query && event.Query.length > 1) {
response.setBody(
await getCustomersSearch(
event.Worker,
event.Query,
connection,
)
);
} else {
response.setBody(
await getCustomersList(
//event.PageSize, // not currently handling pagination
event.Worker,
connection)
);
}
break;
}
default: {
console.log('Unknown Location: ', event.Location);
res.setStatusCode(422);
}
}
return callback(null, response);
}
} catch (e) {
console.error(e);
response.setStatusCode(500);
return callback(null, response);
}
};
const getCustomerDetailsByCustomerIdCallback = async (contactId, connection) => {
console.log('Getting Customer details: ', contactId);
let sfdcRecords = [];
try {
sfdcRecords = await connection.sobject("Contact")
.find(
{
'Id': contactId
},
{
Id: 1,
Name: 1,
Title: 1,
MobilePhone: 1,
'Account.Name': 1,
}
)
.limit(1)
.execute();
console.log("Fetched # SFDC records for customer details by ID: " + sfdcRecords.length);
} catch (err) {
console.error(err);
}
const sfdcRecord = sfdcRecords[0];
const accountName = (
sfdcRecord.Account ? sfdcRecord.Account.Name : 'Unknown Company'
);
return {
objects: {
customer: {
customer_id: sfdcRecord.Id,
display_name: sfdcRecord.Name,
channels: [
{
type: 'sms',
value: sfdcRecord.MobilePhone
},
{
type: 'whatsapp',
value: `whatsapp:${sfdcRecord.MobilePhone}`
},
{
type: 'email',
value: sfdcRecord.Email
}
],
details: {
title: "Information",
content: `${accountName} - ${sfdcRecord.Title}`
}
}
}
}
};
const getCustomersList = async (workerIdentity, connection) => {
let sfdcRecords = [];
try {
sfdcRecords = await connection.sobject("Contact")
.find(
{
'Owner.Username': workerIdentity
},
{
Id: 1,
Name: 1,
}
)
.sort({ Name: 1 })
.limit(2000)
.execute();
console.log("Fetched # SFDC records for customers list: " + sfdcRecords.length);
} catch (err) {
console.error(err);
}
const list = sfdcRecords.map(contact => ({
display_name: contact.Name,
customer_id: contact.Id
}));
return {
objects:
{
customers: list,
searchable: true
}
};
};
const getCustomersSearch = async (workerIdentity, query, connection) => {
console.log('A search query was sent:', JSON.stringify(query));
let sfdcRecords = [];
try {
sfdcRecords = await connection.search(
`FIND {${query}*} IN NAME FIELDS RETURNING Contact(Id, Name WHERE Owner.Username = '${workerIdentity}')`
);
console.log("Fetched # SFDC records for customers search: " + sfdcRecords.searchRecords.length);
} catch (err) {
console.error(err);
}
const list = sfdcRecords.searchRecords.map(contact => ({
display_name: contact.Name,
customer_id: contact.Id
}));
return {
objects:
{
customers: list,
searchable: true
}
};
};