This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
sugarcrm.coffee
87 lines (74 loc) · 2.94 KB
/
sugarcrm.coffee
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
# Can output total amount in your sales pipeline, as specified in a report from SugarCRM
#
# pipeline me - Gives the total amount in sales pipeline
module.exports = (robot) ->
robot.respond /pipeline( me)?/i, (msg) ->
url = process.env.HUBOT_SUGARCRM_URL
username = process.env.HUBOT_SUGARCRM_USERNAME
password = process.env.HUBOT_SUGARCRM_PASSWORD
reportID = process.env.HUBOT_SUGARCRM_REPORT_ID
reportField = process.env.HUBOT_SUGARCRM_REPORT_FIELD
unless url
msg.send "SugarCRM URL isn't set."
msg.send "Please set the HUBOT_SUGARCRM_URL environment variable without prefixed HTTP or trailing slash"
return
unless username
msg.send "SugarCRM username isn't set."
msg.send "Please set the HUBOT_SUGARCRM_USERNAME environment variable"
return
unless password
msg.send "SugarCRM password isn't set."
msg.send "Please set the HUBOT_SUGARCRM_PASSWORD environment variable"
return
unless reportID
msg.send "SugarCRM report ID is not set."
msg.send "Please set the HUBOT_SUGARCRM_REPORT_ID to the report ID of your pipeline report"
return
unless reportField
msg.send "SugarCRM report field is not set."
msg.send "Please set the HUBOT_SUGARCRM_REPORT_FIELD to the field of the report that should be totaled"
return
sugarCRMLogin msg, url, username, password, (session) ->
data = {
session: session,
ids: [reportID]
}
sugarCRMCall msg, url, 'get_report_entries', data, (err, res, body) ->
json = JSON.parse(body)
entries = json.entry_list[0]
fields = json.field_list[0]
fieldID = -1
pipelineTotal = 0
for i in [0...fields.length]
fieldID = i if fields[i].name == reportField
if fieldID == -1
msg.send "Could not find " + reportField + " in the report fields."
msg.send "Please double check HUBOT_SUGARCRM_REPORT_FIELD."
return
for entry in entries
if entry.id
amount = (entry.name_value_list.filter (field) -> field.name == fieldID)[0]
pipelineTotal += parseInt(amount.value.replace(',', ''))
msg.send "Total: $" + pipelineTotal
sugarCRMLogin = (msg, url, user_name, password, callback) ->
crypto = require('crypto')
hashedPassword = crypto.createHash('md5').update(password).digest("hex")
data = {
user_auth: {
user_name: user_name,
password: hashedPassword
}
}
sugarCRMCall msg, url, 'login', data, (err, res, body) ->
sessionID = JSON.parse(body).id
callback(sessionID)
sugarCRMCall = (msg, url, method, data, callback) ->
msg.http('https://' + url + '/service/v4/rest.php')
.header('Content-Length', 0)
.query
method: method,
input_type: 'JSON',
response_type: 'JSON',
rest_data: JSON.stringify data
.post() (err, res, body) ->
callback(err, res, body)