forked from leancloud/ticket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicket.js
More file actions
231 lines (217 loc) · 6.96 KB
/
Ticket.js
File metadata and controls
231 lines (217 loc) · 6.96 KB
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
const _ = require('lodash')
const AV = require('leanengine')
const {getTinyUserInfo, htmlify, isCustomerService, getTinyReplyInfo}= require('./common')
const oauth = require('./oauth')
const notify = require('./notify')
const {TICKET_STATUS, ticketClosedStatuses} = require('../lib/common')
const errorHandler = require('./errorHandler')
AV.Cloud.beforeSave('Ticket', (req, res) => {
if (!req.currentUser._sessionToken) {
return res.error('noLogin')
}
return oauth.hasPermission(req.currentUser)
.then((hasPremission) => {
if (!hasPremission) {
return res.error('您的账号不具备提交工单的条件。')
}
const ticket = req.object
if (!ticket.get('title') || ticket.get('title').trim().length === 0) {
throw new AV.Cloud.Error('title 不能为空')
}
if (!ticket.get('category') || !ticket.get('category').objectId) {
throw new AV.Cloud.Error('category 不能为空')
}
ticket.set('status', TICKET_STATUS.NEW)
ticket.set('content_HTML', htmlify(ticket.get('content')))
return getTicketAcl(ticket, req.currentUser).then((acl) => {
ticket.setACL(acl)
ticket.set('author', req.currentUser)
return selectAssignee(ticket)
}).then((assignee) => {
ticket.set('assignee', assignee)
res.success()
})
}).catch((err) => {
errorHandler.captureException(err)
res.error(err)
})
})
const getTicketAcl = (ticket, author) => {
const acl = new AV.ACL()
acl.setWriteAccess(author, true)
acl.setReadAccess(author, true)
acl.setRoleWriteAccess(new AV.Role('customerService'), true)
acl.setRoleReadAccess(new AV.Role('customerService'), true)
return Promise.resolve(acl)
}
AV.Cloud.afterSave('Ticket', (req) => {
req.object.get('assignee').fetch()
.then((assignee) => {
return getTinyUserInfo(assignee)
.then((assigneeInfo) => {
return new AV.Object('OpsLog').save({
ticket: req.object,
action: 'selectAssignee',
data: {assignee: assigneeInfo},
}, {useMasterKey: true})
})
.then(() => {
return notify.newTicket(req.object, req.currentUser, assignee)
})
}).catch(errorHandler.captureException)
})
AV.Cloud.beforeUpdate('Ticket', (req, res) => {
if (req.object.updatedKeys.indexOf('assignee') != -1) {
getVacationers()
.then(vacationers => {
const finded = _.find(vacationers, {id: req.object.get('assignee').id})
if (finded) {
return res.error('抱歉,该客服正在休假。')
}
return res.success()
})
} else {
return res.success()
}
})
AV.Cloud.afterUpdate('Ticket', (req) => {
const ticket = req.object
return getTinyUserInfo(req.currentUser).then((user) => {
if (ticket.updatedKeys.includes('category')) {
new AV.Object('OpsLog').save({
ticket,
action: 'changeCategory',
data: {category: ticket.get('category'), operator: user},
}, {useMasterKey: true})
}
if (ticket.updatedKeys.includes('assignee')) {
getTinyUserInfo(ticket.get('assignee'))
.then((assignee) => {
return new AV.Object('OpsLog').save({
ticket,
action: 'changeAssignee',
data: {assignee: assignee, operator: user},
}, {useMasterKey: true})
})
.then(() => {
return notify.changeAssignee(ticket, req.currentUser, ticket.get('assignee'))
})
}
if (ticket.updatedKeys.includes('status')
&& ticketClosedStatuses().includes(ticket.get('status'))) {
AV.Cloud.run('statsTicket', {ticketId: ticket.id})
.catch(errorHandler.captureException)
}
if (ticket.updatedKeys.includes('evaluation')) {
ticket.fetch({include: 'assignee'}, {user: req.currentUser})
.then((ticket) => {
return notify.ticketEvaluation(ticket, req.currentUser, ticket.get('assignee'))
})
}
})
})
AV.Cloud.define('operateTicket', (req) => {
const {ticketId, action} = req.params
return Promise.all([
new AV.Query('Ticket')
.include('files')
.include('author')
.get(ticketId),
getTinyUserInfo(req.currentUser),
isCustomerService(req.currentUser),
])
.then(([ticket, operator, isCustomerService]) => {
if (isCustomerService) {
ticket.addUnique('joinedCustomerServices', operator)
}
ticket.set('status', getTargetStatus(action, isCustomerService))
return ticket.save(null, {user: req.currentUser})
.then(() => {
return new AV.Object('OpsLog').save({
ticket,
action,
data: {operator}
}, {useMasterKey: true})
})
.then(() => {
return ticket.toFullJSON()
})
})
.catch(errorHandler.captureException)
})
const getTargetStatus = (action, isCustomerService) => {
switch (action) {
case 'replyWithNoContent':
return TICKET_STATUS.WAITING_CUSTOMER
case 'replySoon':
return TICKET_STATUS.WAITING_CUSTOMER_SERVICE
case 'resolve':
return isCustomerService ? TICKET_STATUS.PRE_FULFILLED : TICKET_STATUS.FULFILLED
case 'reject':
return TICKET_STATUS.REJECTED
case 'reopen':
return TICKET_STATUS.WAITING_CUSTOMER
default:
throw new Error('unsupport action: ' + action)
}
}
exports.replyTicket = (ticket, reply, replyAuthor) => {
Promise.all([
ticket.fetch({include: 'author,assignee'}, {user: replyAuthor}),
getTinyReplyInfo(reply),
getTinyUserInfo(reply.get('author'))
]).then(([ticket, tinyReply, tinyReplyAuthor]) => {
ticket.set('latestReply', tinyReply)
.increment('replyCount', 1)
if (reply.get('isCustomerService')) {
ticket.addUnique('joinedCustomerServices', tinyReplyAuthor)
ticket.set('status', TICKET_STATUS.WAITING_CUSTOMER)
} else {
ticket.set('status', TICKET_STATUS.WAITING_CUSTOMER_SERVICE)
}
return ticket.save(null, {user: replyAuthor})
}).then((ticket) => {
return notify.replyTicket(ticket, reply, replyAuthor)
}).then(() => {
return ticket
}).catch(errorHandler.captureException)
}
const selectAssignee = (ticket) => {
return Promise.all([
new AV.Query(AV.Role)
.equalTo('name', 'customerService')
.first(),
getVacationers(),
])
.then(([role, vacationers]) => {
let query = role.getUsers().query()
const category = ticket.get('category')
if (!_.isEmpty(category)) {
query.equalTo('categories.objectId', category.objectId)
}
if (vacationers.length > 0) {
query.notContainedIn('objectId', vacationers.map(v => v.id))
}
return query.find({useMasterKey: true})
.then((users) => {
if (users.length != 0) {
return _.sample(users)
}
query = role.getUsers().query()
if (vacationers.length > 0) {
query.notContainedIn('objectId', vacationers.map(v => v.id))
}
return query.find({useMasterKey: true}).then(_.sample)
})
})
}
const getVacationers = () => {
const now = new Date()
return new AV.Query('Vacation')
.lessThan('startDate', now)
.greaterThan('endDate', now)
.find({useMasterKey: true})
.then(vacations => {
return vacations.map(v => v.get('vacationer'))
})
}