-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathticket.js
249 lines (224 loc) · 5.8 KB
/
ticket.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
'use strict'
function TicketError(message) {
this.name = 'TicketError';
this.message = message || 'Ticketing error';
this.stack = (new Error()).stack;
}
TicketError.prototype = Object.create(Error.prototype);
TicketError.prototype.constructor = TicketError;
const Sample = {
assignee: null,
closed: null,
comments: [],
content: null,
created: null,
id: null,
requester: null,
roomName: null,
status: 'open',
}
function createTicket() {
return JSON.parse(JSON.stringify(Sample))
}
function findTicket(tickets, id) {
if(!id) {
throw new TicketError('Invalid id')
}
if(!tickets[id]) {
throw new TicketError(`Not found: #${id}`)
}
return tickets[id]
}
function closeTicket(tickets, id) {
const ticket = findTicket(tickets, id)
ticket.status = 'closed'
ticket.closed = Date.now()
return ticket
}
function openTicket(tickets, message, content) {
const newTicket = createTicket()
tickets.lastId += 1
newTicket.id = `${tickets.lastId}`
newTicket.created = new Date()
newTicket.content = content
newTicket.roomName = message.roomName
newTicket.requester = message.userName
tickets[newTicket.id] = newTicket
return tickets[newTicket.id]
}
function formatNull(value) {
return value ? value : '??'
}
function showTicket(ticket) {
var main = `#${ticket.id}: ${formatNull(ticket.requester)} >> ${formatNull(ticket.assignee)}`
if(ticket.roomName) {
main += ` (${ticket.roomName})`
}
if(ticket.content) {
main += `\n${ticket.content}`
}
if(ticket.comments && ticket.comments.length) {
main += ' -- '
main += ticket.comments.join('; ')
}
return main
}
function compareUserName(userA, userB) {
if(!userA || !userB) {
return false
}
return userA.toLowerCase().startsWith(userB.toLowerCase())
}
function showTickets(tickets) {
if(tickets.length == 0) {
return 'nothing TODO!'
}
var output = '\n'
for(var ticket of tickets) {
output += `${showTicket(ticket)}\n\n`
}
return output
}
function userNameFilter(tickets, username) {
return filterTicketsOr(tickets, {
'assignee': compareUserName.bind(null, username),
'requester': compareUserName.bind(null, username),
})
}
function testCondition(field, expectedValue, value) {
if(expectedValue === undefined) {
return true
}
if(expectedValue instanceof Function) {
return expectedValue(value[field])
}
if(expectedValue === value[field]) {
return true
}
return false
}
function ensureArray(tickets) {
if(tickets instanceof Array) {
return tickets
}
if(tickets instanceof Object) {
return Object.values(tickets)
}
throw new TicketError('only accept Object and Array')
}
function filterTicketsAnd(tickets, conditions) {
const ticketsArray = ensureArray(tickets)
return ticketsArray
.filter((entry) => {
const foundMismatch = Object
.entries(conditions)
.find((condition) => !testCondition(
condition[0], condition[1], entry))
return foundMismatch == undefined
})
}
function filterTicketsOr(tickets, conditions) {
const ticketsArray = ensureArray(tickets)
return ticketsArray
.filter((entry) => {
const foundMatch = Object
.entries(conditions)
.find((condition) => testCondition(
condition[0], condition[1], entry))
return foundMatch != undefined
})
}
function forget(tickets) {
if(!tickets.lastId) {
return 'nothing to remove!'
}
const lastId = tickets.lastId
for(var i = 1; i <= tickets.lastId; i++) {
delete tickets[i]
}
tickets.lastId = 0
return lastId
}
function forgetTicket(tickets, id) {
findTicket(tickets, id)
delete tickets[id]
return id
}
function assign(tickets, id, assignee) {
const ticket = findTicket(tickets, id)
ticket.assignee = assignee
return ticket
}
function addComment(ticket, comment) {
if(!(ticket.comments instanceof Array)) {
ticket.comments = []
}
ticket.comments.push(comment)
return showTicket(ticket)
}
function createTickets() {
return {
lastId: 0
}
}
function isValidTickets(tickets) {
if(!tickets) {
return false
}
if(!(tickets instanceof Object)) {
return false
}
if(!Number.isInteger(tickets.lastId)) {
return false
}
return true
}
function store(fs, storePath, tickets) {
if(!isValidTickets(tickets)) {
throw new TicketError(`refusing to write invalid data: ${JSON.stringify(tickets)}`)
}
/* eslint-disable no-sync */
fs.writeFileSync(storePath, JSON.stringify(tickets, null, 4))
}
function load(fs, storePath) {
/* eslint-disable no-sync */
if(fs.existsSync(storePath)) {
/* eslint-disable no-sync */
const datastore = fs.readFileSync(storePath).toString()
try {
const tickets = JSON.parse(datastore)
if(!isValidTickets(tickets)) {
throw new TicketError(`does not contain valid data: ${storePath}`)
}
return tickets
} catch(e) {
if(e instanceof SyntaxError) {
throw new TicketError(`invalid content of file: ${storePath}`)
} else {
throw e
}
}
}
return createTickets()
}
module.exports = {
TicketError,
addComment,
assign,
closeTicket,
compareUserName,
createTicket,
createTickets,
filterTicketsAnd,
filterTicketsOr,
findTicket,
forget,
forgetTicket,
isValidTickets,
load,
openTicket,
showTicket,
showTickets,
store,
userNameFilter,
}