/
ticket.coffee
257 lines (224 loc) · 12.5 KB
/
ticket.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
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
class App.Ticket extends App.Model
@configure 'Ticket', 'number', 'title', 'group_id', 'owner_id', 'customer_id', 'state_id', 'priority_id', 'article', 'tags', 'links', 'updated_at', 'preferences'
@extend Spine.Model.Ajax
@url: @apiPath + '/tickets'
@configure_attributes = [
{ name: 'number', display: '#', tag: 'input', type: 'text', limit: 100, null: true, readonly: 1, width: '68px' },
{ name: 'title', display: 'Title', tag: 'input', type: 'text', limit: 100, null: false },
{ name: 'customer_id', display: 'Customer', tag: 'input', type: 'text', limit: 100, null: false, autocapitalize: false, relation: 'User' },
{ name: 'organization_id', display: 'Organization', tag: 'select', relation: 'Organization', readonly: 1 },
{ name: 'group_id', display: 'Group', tag: 'select', multiple: false, limit: 100, null: false, relation: 'Group', width: '10%', edit: true },
{ name: 'owner_id', display: 'Owner', tag: 'select', multiple: false, limit: 100, null: true, relation: 'User', width: '12%', edit: true },
{ name: 'state_id', display: 'State', tag: 'select', multiple: false, null: false, relation: 'TicketState', default: 'new', width: '12%', edit: true, customer: true },
{ name: 'pending_time', display: 'Pending till', tag: 'datetime', null: true, width: '130px' },
{ name: 'priority_id', display: 'Priority', tag: 'select', multiple: false, null: false, relation: 'TicketPriority', default: '2 normal', width: '12%', edit: true, customer: true },
{ name: 'article_count', display: 'Article#', readonly: 1, width: '12%' },
{ name: 'escalation_at', display: 'Escalation', tag: 'datetime', null: true, readonly: 1, width: '110px', class: 'escalation' },
{ name: 'last_contact_at', display: 'Last contact', tag: 'datetime', null: true, readonly: 1, width: '110px' },
{ name: 'last_contact_agent_at', display: 'Last contact (agent)', tag: 'datetime', null: true, readonly: 1, width: '110px' },
{ name: 'last_contact_customer_at', display: 'Last contact (customer)', tag: 'datetime', null: true, readonly: 1, width: '110px' },
{ name: 'first_response_at', display: 'First response', tag: 'datetime', null: true, readonly: 1, width: '110px' },
{ name: 'close_at', display: 'Closing time', tag: 'datetime', null: true, readonly: 1, width: '110px' },
{ name: 'created_by_id', display: 'Created by', relation: 'User', readonly: 1 },
{ name: 'created_at', display: 'Created at', tag: 'datetime', width: '110px', align: 'right', readonly: 1 },
{ name: 'updated_by_id', display: 'Updated by', relation: 'User', readonly: 1 },
{ name: 'updated_at', display: 'Updated at', tag: 'datetime', width: '110px', align: 'right', readonly: 1 },
]
uiUrl: ->
"#ticket/zoom/#{@id}"
getState: ->
type = App.TicketState.find(@state_id)
stateType = App.TicketStateType.find(type.state_type_id)
state = 'closed'
if stateType.name is 'new' || stateType.name is 'open'
state = 'open'
# if ticket is escalated, overwrite state
if @escalation_at && new Date( Date.parse(@escalation_at) ) < new Date
state = 'escalating'
else if stateType.name is 'pending reminder'
state = 'pending'
# if ticket pending_time is reached, overwrite state
if @pending_time && new Date( Date.parse(@pending_time) ) < new Date
state = 'open'
else if stateType.name is 'pending action'
state = 'pending'
state
icon: ->
'task-state'
iconClass: ->
@getState()
iconTitle: ->
type = App.TicketState.find(@state_id)
stateType = App.TicketStateType.find(type.state_type_id)
if stateType.name is 'pending reminder' && @pending_time && new Date( Date.parse(@pending_time) ) < new Date
return "#{App.i18n.translateInline(type.displayName())} - #{App.i18n.translateInline('reached')}"
if @escalation_at && new Date( Date.parse(@escalation_at) ) < new Date
return "#{App.i18n.translateInline(type.displayName())} - #{App.i18n.translateInline('escalated')}"
App.i18n.translateInline(type.displayName())
iconTextClass: ->
"task-state-#{ @getState() }-color"
iconActivity: (user) ->
return if !user
if @owner_id == user.id
return 'important'
''
searchResultAttributes: ->
display: "##{@number} - #{@title}"
id: @id
class: "task-state-#{ @getState() } ticket-popover"
url: @uiUrl()
icon: 'task-state'
iconClass: @getState()
activityMessage: (item) ->
return if !item
if item.type is 'create'
return App.i18n.translateContent('%s created Ticket |%s|', item.created_by.displayName(), item.title)
else if item.type is 'update'
return App.i18n.translateContent('%s updated Ticket |%s|', item.created_by.displayName(), item.title)
else if item.type is 'reminder_reached'
return App.i18n.translateContent('Pending reminder reached for Ticket |%s|', item.title)
else if item.type is 'escalation'
return App.i18n.translateContent('Ticket |%s| is escalated!', item.title)
else if item.type is 'escalation_warning'
return App.i18n.translateContent('Ticket |%s| will escalate soon!', item.title)
return "Unknow action for (#{@objectDisplayName()}/#{item.type}), extend activityMessage() of model."
# apply macro
@macro: (params) ->
for key, content of params.macro
attributes = key.split('.')
if attributes[0] is 'ticket'
# apply tag changes
if attributes[1] is 'tags'
tags = content.value.split(',')
for tag in tags
if content.operator is 'remove'
if params.callback && params.callback.tagRemove
params.callback.tagRemove(tag)
else
@tagRemove(params.ticket.id, tag)
else
if params.callback && params.callback.tagAdd
params.callback.tagAdd(tag)
else
@tagAdd(params.ticket.id, tag)
# apply user changes
else if attributes[1] is 'owner_id'
if content.pre_condition is 'current_user.id'
params.ticket[attributes[1]] = App.Session.get('id')
else
params.ticket[attributes[1]] = content.value
# apply direct value changes
else
params.ticket[attributes[1]] = content.value
# check if selector is matching
@selector: (ticket, selector) ->
return true if _.isEmpty(selector)
for objectAttribute, condition of selector
[objectName, attributeName] = objectAttribute.split('.')
# there is no article.subject so we take the title instead
if objectAttribute == 'article.subject' && !ticket['article']['subject']
objectName = 'ticket'
attributeName = 'title'
# for new articles there is no created_by_id so we set the current user
# if no id is given
if objectAttribute == 'article.created_by_id' && !ticket['article']['created_by_id']
ticket['article']['created_by_id'] = App.Session.get('id')
if objectName == 'ticket'
object = ticket
else
object = ticket[objectName] || {}
return false if !@_selectorMatch(object, objectName, attributeName, condition)
return true
@_selectorConditionDate: (condition, operator) ->
return if operator != '+' && operator != '-'
conditionValue = new Date()
if condition['range'] == 'minute'
conditionValue.setTime( eval( conditionValue.getTime() + operator + 60 * 1000 ) )
else if condition['range'] == 'hour'
conditionValue.setTime( eval( conditionValue.getTime() + operator + 60 * 60 * 1000 ) )
else if condition['range'] == 'day'
conditionValue.setTime( eval( conditionValue.getTime() + operator + 60 * 60 * 24 * 1000 ) )
else if condition['range'] == 'month'
conditionValue.setTime( eval( conditionValue.getTime() + operator + 60 * 60 * 30 * 1000 ) )
else if condition['range'] == 'year'
conditionValue.setTime( eval( conditionValue.getTime() + operator + 60 * 60 * 365 * 1000 ) )
conditionValue
@_selectorMatch: (object, objectName, attributeName, condition) ->
conditionValue = condition.value
conditionValue = '' if conditionValue == undefined
objectValue = object[attributeName]
objectValue = '' if objectValue == undefined
# take care about pre conditions
if condition['pre_condition']
[conditionType, conditionKey] = condition['pre_condition'].split('.')
if conditionType == 'current_user'
conditionValue = App.Session.get(conditionKey)
else if condition['pre_condition'] == 'not_set'
conditionValue = ''
# prepare regex for contains conditions
contains_regex = new RegExp(App.Utils.escapeRegExp(conditionValue.toString()), 'i')
# move value to array if it is not already
if !_.isArray(objectValue)
objectValue = [objectValue]
# move value to array if it is not already
if !_.isArray(conditionValue)
conditionValue = [conditionValue]
result = false
for loopObjectKey, loopObjectValue of objectValue
for loopConditionKey, loopConditionValue of conditionValue
if condition.operator == 'contains'
result = true if objectValue.toString().match(contains_regex)
else if condition.operator == 'contains not'
result = true if !objectValue.toString().match(contains_regex)
else if condition.operator == 'contains all'
result = true
for loopConditionValue in conditionValue
if !_.contains(objectValue, loopConditionValue)
result = false
else if condition.operator == 'contains one'
result = false
for loopConditionValue in conditionValue
if _.contains(objectValue, loopConditionValue)
result = true
else if condition.operator == 'contains all not'
result = true
for loopObjectValue in objectValue
if _.contains(conditionValue, loopObjectValue)
result = false
else if condition.operator == 'contains one not'
result = false
for loopObjectValue in objectValue
if !_.contains(conditionValue, loopObjectValue)
result = true
else if condition.operator == 'is'
result = true if objectValue.toString().trim().toLowerCase() is loopConditionValue.toString().trim().toLowerCase()
else if condition.operator == 'is not'
result = true if objectValue.toString().trim().toLowerCase() isnt loopConditionValue.toString().trim().toLowerCase()
else if condition.operator == 'after (absolute)'
result = true if new Date(objectValue.toString()) > new Date(loopConditionValue.toString())
else if condition.operator == 'before (absolute)'
result = true if new Date(objectValue.toString()) < new Date(loopConditionValue.toString())
else if condition.operator == 'before (relative)'
loopConditionValue = @_selectorConditionDate(condition, '-')
result = true if new Date(objectValue.toString()) < new Date(loopConditionValue.toString())
else if condition.operator == 'within last (relative)'
loopConditionValue = @_selectorConditionDate(condition, '-')
result = true if new Date(objectValue.toString()) < new Date() && new Date(objectValue.toString()) > new Date(loopConditionValue.toString())
else if condition.operator == 'after (relative)'
loopConditionValue = @_selectorConditionDate(condition, '+')
result = true if new Date(objectValue.toString()) > new Date(loopConditionValue.toString())
else if condition.operator == 'within next (relative)'
loopConditionValue = @_selectorConditionDate(condition, '+')
result = true if new Date(objectValue.toString()) > new Date() && new Date(objectValue.toString()) < new Date(loopConditionValue.toString())
else
throw "Unknown operator: #{condition.operator}"
result
editable: (permission = 'change') ->
user_id = App.Session.get('id')
return true if user_id is @customer_id
return false if !App.User.exists(user_id)
group_ids = App.User.find(user_id).allGroupIds(permission)
for local_group_id in group_ids
if local_group_id.toString() is @group_id.toString()
return true
false