This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsalesforce.js
142 lines (132 loc) · 4.14 KB
/
salesforce.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
/* eslint-disable max-params */
const jsforce = require('jsforce');
const env = require('./config.js').env;
const config = require('./config.js')[env];
const bdk = require('@salesforce/refocus-bdk')(config);
const { chatterLoginDetails, chatterOptionsArray, httpProxy } = config;
const conn = [];
const START_OF_ARRAY = 0;
const NOT_FOUND = -1;
chatterLoginDetails.map((option, index) => {
const loginUrl = option.loginurl;
if (httpProxy) {
conn.push(new jsforce.Connection({ loginUrl, httpProxy }));
} else {
conn.push(new jsforce.Connection({ loginUrl }));
}
conn[index].login(option.username, option.password, (loginErr) => {
if (loginErr) {
bdk.log.error(loginUrl, loginErr);
}
});
});
/**
* @param {string} caseNumber - caseNumber from room
* @param {object} connection - connection to salesforce org
* @returns {Promise} - promise resolving an object containing the case
*/
function getCaseByCaseNumber(caseNumber, connection) {
return new Promise((resolve, reject) => {
const q = `SELECT id FROM Case WHERE CaseNumber = '${caseNumber}'`;
connection.query(q).execute((err, resp) => {
if (err) {
bdk.log.error('ERROR', err, resp);
reject(err);
}
if (resp.records[START_OF_ARRAY]) {
resolve(resp);
}
});
});
}
/**
* @param {object} attachmentObject - object with the details of the file
* @param {object} connection - connection to salesforce org
* @returns {Promise} - resolves the response from salesforce to the attachment
*/
function createAttachmentOnCase(attachmentObject, connection) {
return new Promise((resolve, reject) => {
connection
.sobject('Attachment')
.create(attachmentObject, (postErr, ret) => {
if (postErr) {
bdk.log.error('ERROR', postErr, ret);
reject(postErr);
}
resolve(ret);
});
});
}
/**
* Creates a new attachment in Salesforce
*
* @param {Object} timelineBotAttachment - attachment Details
* @param {String} userName - Username of person posting file
* @param {String} caseNumber - Case number used to get case id
* @param {String} id - Action ID
* @param {string} selectedChatter - the chatter to send to
*/
async function postAttachment(
timelineBotAttachment,
userName,
caseNumber,
id,
selectedChatter
) {
const chatterIndex = chatterOptionsArray.indexOf(selectedChatter);
// eslint-disable-next-line
if (!timelineBotAttachment) return;
if (chatterIndex === NOT_FOUND) {
bdk.log.error(
'Invalid Selected Org Setting in post attachment',
selectedChatter
);
} else {
try {
const caseResponse = await getCaseByCaseNumber(
caseNumber,
conn[chatterIndex]
);
const attachmentObject = {
ParentId: caseResponse.records[START_OF_ARRAY].Id,
Name: timelineBotAttachment.name,
Body: timelineBotAttachment.file,
ContentType: timelineBotAttachment.type,
};
const postResponse = await createAttachmentOnCase(
attachmentObject,
conn[chatterIndex]
);
const eventLog = {
log: userName + ' has uploaded' + timelineBotAttachment.name,
context: {
type: 'Event',
attachment:
chatterLoginDetails[chatterIndex].loginurl +
'/servlet/servlet.FileDownload?file=' +
postResponse.id,
fileType: timelineBotAttachment.type,
fileName: timelineBotAttachment.name,
userName,
},
};
postResponse.name = timelineBotAttachment.name;
postResponse.type = timelineBotAttachment.type;
postResponse.imageUrl =
chatterLoginDetails[chatterIndex].loginurl +
'/servlet/servlet.FileDownload?file=' +
postResponse.id;
// needs to be same parameters from package.json
const parametersOverride = [
{ value: postResponse.imageUrl, name: 'timelineBotAttachment' },
];
bdk.log.info(eventLog);
bdk.respondBotAction(id, postResponse, eventLog, parametersOverride);
} catch (err) {
bdk.log.error(`Failed to attach upload to case #${caseNumber}. ${err}`);
}
}
}
module.exports = {
postAttachment,
};