forked from cweems/function-frontline-salesforce
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrm.protected.js
195 lines (179 loc) · 5.14 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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(event);
console.log('Frontline user identity: ' + event.Worker);
const sfdcConnectionIdentity = await sfdcAuthenticate(context, event.Worker);
console.log('Connected as SF user:' + sfdcConnectionIdentity);
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) {
console.log("Getcustomer");
response.setBody(
await getCustomersSearch(
event.Worker,
event.Query,
connection,
event.PageSize,
event.NextPageToken
)
);
} else {
response.setBody(
await getCustomersList(
event.Worker,
connection,
event.PageSize,
event.NextPageToken
)
);
}
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("Account")
.find(
{
'Id': contactId
},
{
Id: 1,
Name: 1,
Phone: 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.Phone
},
{
type: 'whatsapp',
value: `whatsapp:${sfdcRecord.Phone}`
},
{
type: 'email',
value: sfdcRecord.Email
}
],
details: {
title: "Information",
content: `${accountName} - ${sfdcRecord.Title}`
}
}
}
}
};
const getCustomersList = async (workerIdentity, connection, pageSize, offset) => {
let sfdcRecords = [];
try {
sfdcRecords = await connection.sobject("Account")
.find(
{
},
{
Id: 1,
Name: 1,
}
)
.sort({ Name: 1 })
.limit(pageSize)
.skip(offset ? offset : 0)
.execute();
console.log("Fetched # SFDC records for customers list: " + sfdcRecords);
} catch (err) {
console.error(err);
}
const list = sfdcRecords.map(contact => ({
display_name: contact.Name,
customer_id: contact.Id
}));
return {
objects:
{
customers: list,
searchable: true,
next_page_token: parseInt(pageSize) + (offset ? parseInt(offset) : 0)
}
};
//var records = [];
// await connection.query("SELECT Id, Name FROM Account", function(err, result) {
// if (err) { return console.error("error account", err); }
// console.log("total : " + result.totalSize);
// console.log("fetched : " + result.records.length);
// console.log("done ? : " + result.done);
// if (!result.done) {
// // you can use the locator to fetch next records set.
// // Connection#queryMore()
// console.log("next records URL : " + result.nextRecordsUrl);
// }
// });
//sfdcRecords = await connection.sobject("Account");
};
const getCustomersSearch = async (workerIdentity, query, connection, pageSize, offset) => {
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}' LIMIT ${pageSize} OFFSET ${offset ? parseInt(offset) : 0})`
);
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,
next_page_token: parseInt(pageSize) + (offset ? parseInt(offset) : 0)
}
};
};