This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
328 lines (266 loc) · 10.5 KB
/
app.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
(function() {
String.prototype.toTitleCase = function() {
var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;
return this.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title) {
if (index > 0 && index + match.length !== title.length &&
match.search(smallWords) > -1 && title.charAt(index - 2) !== ':' &&
(title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
title.charAt(index - 1).search(/[^\s-]/) < 0) {
return match.toLowerCase();
}
if (match.substr(1).search(/[A-Z]|\../) > -1) return match;
return match.charAt(0).toUpperCase() + match.substr(1);
});
};
function IntercomApp(zd) {
this.zd = zd;
this.user = {};
// Endpoints
this.apiRoot = 'https://{{setting.intercomAppID}}:{{setting.intercomAPIKey}}@api.intercom.io';
this.linkRoot = 'https://app.intercom.io/apps/' + zd.setting('intercomAppID') + '/users';
}
IntercomApp.prototype.filterMetadata = function(data) {
// Intercom metadata fields to display
var fields = [
'phone',
'marketer',
'current LP solution',
'pages',
'domains',
'clients',
'api keys',
'NPS - CS - 1month',
'NPS - CS - 6month',
'NPS - CS - Support',
'NPS - Product - 1month',
'NPS - Product - 6month',
'NPS - Product - Support'
];
// Store those fields in array
var metadata = [];
_.each(fields, function(field) {
var value = data[field];
if ( value || value === 0 ) {
// Custom field displays
if ( field === 'pages' ) value = value + ' (' + data['published pages'] + ' published)';
if ( field === 'marketer' ) value = value.replace(/_/g, ' ').toTitleCase();
if ( field.indexOf('NPS') > -1 ) value = value + ' / 10';
metadata.push({
field: field.replace(/_/g, ' ').toTitleCase(),
value: value
});
}
});
return metadata;
};
IntercomApp.prototype.findTag = function(search, attr) {
// Search for global tags by ID or name
var result = _.find(this.tags, function(tag) {
if ( search.id ) return parseInt(tag.id, 10) === parseInt(search.id, 10);
if ( search.name ) return tag.name.toLowerCase() === search.name.toLowerCase();
});
if ( result && result[attr] ) return result[attr];
else return result;
};
IntercomApp.prototype.addTag = function(newTagID) {
// Check if the user already has this tag
if ( this.userHasTag(newTagID) ) {
this.addTagCleanup();
// Highlight the pre-existing tag
this.zd.$('#tags dd#tag-' + newTagID).css('background-color', '#F5F5D3');
var self = this;
setTimeout(function () {
self.zd.$('#tags dd#tag-' + newTagID).css('background-color', 'white');
}, 5000);
return services.notify(this.user.name + ' already has the tag <b>' +
this.findTag({id: newTagID}, 'name') + '</b>.',
'error');
}
this.user.newTagID = newTagID;
this.zd.ajax('addTagRequest');
};
IntercomApp.prototype.userHasTag = function(searchID) {
return typeof _.find(this.user.tags, function(tag) {
return parseInt(tag.id, 10) === parseInt(searchID, 10);
}) !== 'undefined';
};
IntercomApp.prototype.addTagCleanup = function() {
this.zd.$('#new-tag').val('none');
delete this.user.newTagID;
};
IntercomApp.prototype.ajaxParamsGET = function(url) {
return {
url: this.apiRoot + url,
type: 'GET',
dataType: 'json',
secure: true
};
};
IntercomApp.prototype.ajaxParamsPOST = function(url, data) {
return {
url: this.apiRoot + url,
type: 'POST',
dataType: 'json',
contentType: 'application/json',
secure: true,
data: JSON.stringify(data)
};
};
return {
requests: { // API call parameters
getUserRequest: function() {
return this.app.ajaxParamsGET('/users/?email=' + this.app.user.email);
},
findTagsRequest: function() {
return this.app.ajaxParamsGET('/tags');
},
getSegmentsRequest: function() {
return this.app.ajaxParamsGET('/segments');
},
addTagRequest: function() {
return this.app.ajaxParamsPOST('/tags', {
id: this.app.user.newTagID,
users: [ { user_id: this.app.user.userID } ]
});
}
},
events: {
'app.activated': function() {
// Runs on load. Instantiate our object and show our default view
this.app = new IntercomApp(this);
this.trigger('init');
},
'init': function() {
var user;
if ( typeof this.ticket === 'function' ) user = this.ticket().requester();
else if ( typeof this.user === 'function' ) user = this.user();
if ( user && user.email() ) {
this.app.user.email = user.email();
if ( user.name() ) this.app.user.name = user.name();
this.switchTo('account', { app: this.app });
// Make the API calls asynchronously
this.ajax('getUserRequest');
this.ajax('findTagsRequest');
this.ajax('getSegmentsRequest');
} else {
this.switchTo('no-account', { app: this.app } );
}
},
'ticket.requester.email.changed': function() {
this.trigger('init');
},
'ticket.requester.name.changed': function() {
this.trigger('init');
},
'getUserRequest.done': function(user) {
this.app.user = {
receivedFromIntercom: true,
userID: user.user_id,
name: user.name ? user.name.toTitleCase() : this.app.user.name,
email: user.email,
tags: user.tags.tags,
segments: user.segments.segments,
metadata: this.app.filterMetadata(user.custom_attributes)
};
// Add location information
if ( this.app.user.metadata && user.location_data && user.location_data.city_name && user.location_data.region_name ) {
this.app.user.metadata.unshift({
field: 'Location',
value: user.location_data.city_name + ', ' + user.location_data.region_name
});
}
this.trigger('requestDone');
},
'findTagsRequest.done': function(tags) {
this.app.tags = tags.tags || [];
this.trigger('requestDone');
},
'getSegmentsRequest.done': function(segments) {
this.app.segments = segments.segments || [];
this.trigger('requestDone');
},
'requestDone': function() {
if ( !this.app.user.receivedFromIntercom || !this.app.tags ||
!this.app.segments ) return false;
// Callback when *all* Ajax requests are complete
var self = this;
// Add tag names to user tags array
_.each(this.app.user.tags, function(userTag) {
userTag.name = self.app.findTag({id: userTag.id}, 'name');
});
// Filter out tags that weren't present on the global tag list
this.app.user.tags = _.filter(this.app.user.tags, function(tag) {
return typeof tag.name === 'string';
});
// Add segment names to user segments array
_.each(this.app.user.segments, function(userSegment, key) {
var globalSegment = _.find(self.app.segments, function(segment) {
return segment.id === userSegment.id;
});
if ( globalSegment ) userSegment.name = globalSegment.name;
});
// Filter out segments that weren't present on the global segment list
this.app.user.segments = _.filter(this.app.user.segments, function(segment) {
return typeof segment.name !== 'undefined';
});
console.log( this.app );
this.switchTo('account', { app: this.app });
},
'getUserRequest.fail': function() {
// Show the 'no account' message and search box
this.switchTo('no-account', { app: this.app });
},
// This will auto-tag the user with a Feature Interest tag whenever a
// feature is chosen from the 'Feature Interest' custom field dropdown
// e.g. selecting 'feature_interest_responsive'
// adds tag 'Product - Feature Request - Responsive'
'ticket.custom_field_21359544.changed': function(data) {
// Only tag if the feature interest has actually changed
if ( !data[0] || !this.ticket().customField('custom_field_21359544') )
return false;
var newTagName = 'Product - Feature Interest - ' +
this.ticket().customField('custom_field_21359544')
.replace(/feature_request_/, '').replace(/_/g, ' ');
// Don't tag if there isn't an associated Intercom user
if ( !this.app.user.userID )
return services.notify('User will not be tagged with <b>' +
newTagName.toTitleCase() + '</b>, because \'' + this.app.user.name +
'\' was not found on Intercom.<br /><br /><a href="' +
this.app.linkRoot + '/?search=' + encodeURI(this.app.user.name) + '">' +
'Click here to search for them.</a>', 'alert');
// Check whether a corresponding Intercom tag exists
var newTagID = this.app.findTag({name: newTagName}, 'id');
if ( !newTagID )
return services.notify('Intercom user will not be tagged because ' +
'there is no tag <b>' + newTagName.toTitleCase() + '</b>.' +
'<br /><br />To add a new tag, <a href="' + this.app.linkRoot +
'/show?email=' + this.app.user.email + '">go to Intercom</a>.',
'alert');
// Tag the user
this.app.addTag(newTagID);
},
'change #new-tag': function() {
var newTagID = this.$('#new-tag').val();
this.app.addTag(newTagID);
},
'addTagRequest.done': function(data) {
console.log('Adding tag', data);
this.app.addTagCleanup();
// Fail
if ( data.errors || typeof data !== 'object' )
return this.trigger('addTagRequest.fail');
// Success
services.notify('Added tag <b>' + data.name + '</b> to ' +
this.app.user.name + '.');
this.app.user.tags.push( data );
this.switchTo('account', { app: this.app }); // Refresh the view
},
'addTagRequest.fail': function() {
var tagName = this.app.findTag({id: this.app.user.newTagID}) || '';
services.notify('Failed to add tag <b>' + tagName + '</b> to ' +
this.app.user.name + '</b>.', 'error');
this.app.addTagCleanup();
}
}
};
}());