forked from bvarent/zcc_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
143 lines (121 loc) · 5.07 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
(function () {
var self = this;
return {
events: {
'app.activated': 'initialize',
'click .saveEmails': 'saveEmails',
'ticket.submit.done': 'cleanUp'
},
requests: {
createTarget: function (payload) {
return {
url: '/api/v2/targets.json',
type: 'POST',
contentType: 'application/json',
data: payload
};
},
createTrigger: function (payload) {
return {
url: '/api/v2/triggers.json',
type: 'POST',
contentType: 'application/json',
data: payload
};
},
deleteTarget: function (targetID) {
return {
url: '/api/v2/targets/' + targetID + '.json',
type: 'DELETE',
contentType: 'application/json'
};
},
deleteTrigger: function (triggerID) {
return {
url: '/api/v2/triggers/' + triggerID + '.json',
type: 'DELETE',
contentType: 'application/json'
};
}
},
initialize: function () {
this.switchTo("main");
self.ticketID = this.ticket().id();
},
tagTicket: function () {
this.ticket().tags().add("zcc_" + self.ticketID);
},
getEmailList: function () {
var emails = this.$("textarea[name=emailList]")[0].value;
var emailList = emails.split(",");
var validateEmail = function (email) {
var re = /^([\w-]+(?:\.[\w-]+)*(?:\+[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
return re.test(email);
}
;
try {
for (var i = 0; i < emailList.length; i++) {
emailList[i] = emailList[i].trim();
if (!validateEmail(emailList[i])) {
throw this.I18n.t('notifications.invalid') + emailList[i];
}
}
return emailList;
} catch (err) {
services.notify(err, 'error');
}
},
saveEmails: function () {
self.targetID = [];
var emailList = this.getEmailList();
var length = emailList.length;
self.commentsType = '{{' + this.$('input[name="ticketComments"]:checked').val() + '}}';
this.tagTicket();
// Build Targets
_.each(emailList, function (email) {
var targetPayload = '{"target": {"type": "email_target", "title": "' + this.I18n.t('content.target-title') + email + '", "email": "' + email + '", "subject": "[{{ticket.account}}] ' + this.I18n.t('content.email-subject') + ' {{ticket.title}}"}';
this.ajax('createTarget', targetPayload).done(function (targetData) {
self.targetID.push(targetData.target.id);
if (self.targetID.length === emailList.length) {
this.buildTrigger();
services.notify(this.I18n.t('fetch.done'));
}
}).fail(function (data) {
services.notify(this.I18n.t('fetch.fail'));
});
}, this);
},
buildTrigger: function () {
self.actions = '"actions": [';
_.each(self.targetID, function (targetID) {
self.actions = self.actions + '{"field": "notification_target", "value": [' + targetID + ', "' + this.I18n.t('content.email-notification') + ' </br> ' + self.commentsType + '"]},';
}, this);
self.actions = self.actions.substring(0, self.actions.length - 1);
var triggerPayload = '{"trigger": {"title": "' + this.I18n.t('content.trigger-title') + self.ticketID + '", "all": [{ "field": "current_tags", "operator": "includes", "value": "zcc_' + self.ticketID + '"}], ' + self.actions + ']}}';
this.ajax('createTrigger', triggerPayload).done(function (triggerData) {
self.triggerID = triggerData.trigger.id;
});
},
getUserChoice: function () {
var selectedRadio;
this.$("input[name=TicketComments]").each(function (index, target) {
if (target.checked) {
selectedRadio = target.value;
}
});
return selectedRadio;
},
cleanUp: function () {
var that = this;
setTimeout(function () {
that.ajax('deleteTrigger', self.triggerID);
}, 3000);
_.each(self.targetID, function (targetID) {
setTimeout(function () {
that.ajax('deleteTarget', targetID).done(function (data) {
});
}, 3000);
}, this);
}
};
}());