-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
173 lines (158 loc) · 5.67 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
(function() {
return {
resources: {
DOMAIN_PATTERN: /[a-zA-Z0-9]+\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+/,
DATE_FORMAT: 'MMMM Do YYYY'
},
events: {
'app.activated': 'showCustomer',
'click .action': 'handleClick',
'click .toggle-reveal-hide': 'handleRevealHide'
},
requests: {
fetchCustomerFeedbackByEmail: function (email) {
return {
url: 'https://app.promoter.io/api/feedback/?survey__contact__email='+email.replace(/\+/g, '%2B'),
type:'GET',
headers: {
Authorization: 'Token '+this.settings.api_key
},
dataType: 'json'
};
}
},
buildCustomerFromPromoterCustomer: function (promoterCustomer) {
promoterCustomer.full_name = [promoterCustomer.first_name, promoterCustomer.last_name].join(' ').trim();
promoterCustomer.formatted_unsubscribed = (promoterCustomer.unsubscribed ? 'true' : 'false');
promoterCustomer.formatted_created_date = this.formatDateString(promoterCustomer.created_date);
return promoterCustomer;
},
buildFeedbackFromPromoterFeedback: function (promoterFeedback) {
if (promoterFeedback.contact) {
promoterFeedback.contact = this.buildCustomerFromPromoterCustomer(promoterFeedback.contact);
}
promoterFeedback.formatted_posted_date = this.formatDateString(promoterFeedback.posted_date);
promoterFeedback.formated_comment_updated_date = this.formatDateString(promoterFeedback.comment_updated_date);
switch (promoterFeedback.score_type) {
case 'detractor': promoterFeedback.score_class = 'important'; break;
case 'passive': promoterFeedback.score_class = 'warning'; break;
case 'promoter': promoterFeedback.score_class = 'success'; break;
}
return promoterFeedback;
},
clone: function(obj) {
if (null == obj || "object" != typeof obj) {
return obj;
}
var copy = obj.constructor();
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
}
return copy;
},
formatDateString: function (date_string, format) {
format = format || this.resources.DATE_FORMAT;
return moment(date_string).format(format);
},
getCustomerEmail: function () {
if (this.currentLocation() === 'ticket_sidebar') {
return this.ticket().requester().email();
} else if (this.currentLocation() === 'user_sidebar') {
return this.user().email();
}
return;
},
getDomainFromURL: function(baseURI) {
var regexResult = this.resources.DOMAIN_PATTERN.exec(baseURI);
return regexResult[0];
},
handleClick: function (e, data) {
e.preventDefault();
var link = this.$(e.target);
switch (true) {
case link.is('.show-customer'):
this.showCustomer(e, link.data());
break;
case link.is('.show-search'):
this.showSearch(e, link.data());
break;
case link.is('.show-settings'):
this.showAppSettings(e, link.data());
break;
}
},
handleRevealHide: function (e, data) {
e.preventDefault();
var link = this.$(e.target);
var panel = link.closest('.reveal-hide');
if (panel) {
if (panel.is('.not-visible')) {
panel.find('.panel-body').slideDown(500, function () {
panel.removeClass('not-visible').addClass('visible');
});
} else {
panel.find('.panel-body').slideUp(500, function () {
panel.removeClass('visible').addClass('not-visible');
});
}
}
},
showAppSettings: function(e, data) {
var approvedSettings = this.clone(this.settings);
if (approvedSettings.api_key) {
var original = approvedSettings.api_key;
var masked = original.substring(3, original.length);
approvedSettings.api_key = original.replace(masked, Array(masked.length).join('x'));
}
var pageData = {
title: this.I18n.t('settingsPage.title'),
settings: approvedSettings,
email: this.currentUser().email(),
uri: this.getDomainFromURL(e.currentTarget.baseURI),
installation_id: this.installationId()
};
this.switchTo('settingsPage', pageData);
},
showCustomer: function(e, data) {
if (this.verifyConfiguration(e, data)) {
var app = this;
var pageData = {
title: app.I18n.t('customerPage.title'),
average_score: null,
feedbacks: []
};
var scores = [];
app.switchTo('loading');
app.ajax('fetchCustomerFeedbackByEmail', app.getCustomerEmail())
.done(function (data) {
var results = data.results;
if (results && results.length) {
for (var i = results.length - 1; i >= 0; i--) {
var feedback = app.buildFeedbackFromPromoterFeedback(results[i]);
pageData.feedbacks.push(feedback);
scores.push(feedback.score);
}
} else {
pageData.error_message = 'No feedback available for '+app.getCustomerEmail();
}
if (scores.length) {
var sum = 0;
for ( var j = 0; j < scores.length; j++ ) {
sum += parseInt( scores[j], 10 );
}
var avg = sum/scores.length;
pageData.average_score = avg + '';
}
app.switchTo('customerPage', pageData);
});
}
},
verifyConfiguration: function (e, data) {
if (this.settings.api_key) {
return true;
}
this.showAppSettings(e, data);
return false;
}
};
}());