-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbpmn-tasklist.js
210 lines (173 loc) · 5.41 KB
/
bpmn-tasklist.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
/* global Bpmn:true */
import { check, Match } from 'meteor/check'
import { Mongo } from 'meteor/mongo'
import { Meteor } from 'meteor/meteor'
const {EventEmitter} = require('events')
const _name = 'extensions:tasklist'
const tasklist = {}
// //////////////////////////////////////////////////////////////////////////////////////
//
// Define Collection
//
// //////////////////////////////////////////////////////////////////////////////////////
const BpmnTasklistCollectionSchema = {
instanceId: String,
id: String,
type: String,
name: String,
formKey: {
type: String,
optional: true,
},
assignee: {
type: String,
optional: true,
},
candidateGroups: {
type: String,
optional: true,
},
candidateUsers: {
type: String,
optional: true,
},
dueDate: {
type: Date,
optional: true,
},
followupDate: {
type: Date,
optional: true,
},
priority: {
type: String,
optional: true,
},
documentation: {
type: String,
optional: true,
}
}
const collectionName = 'BpmnTasklistCollection'
const BpmnTaskListCollection = new Mongo.Collection(collectionName)
BpmnTaskListCollection.name = collectionName
BpmnTaskListCollection.schema = BpmnTasklistCollectionSchema
tasklist.collection = BpmnTaskListCollection
tasklist.methods = {}
Meteor.startup(() => {
if (Meteor.isServer) {
const userTask = BpmnTaskListCollection.findOne()
BpmnTaskListCollection.remove({})
}
})
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// clear
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const nonEmptyString = Match.Where(s => typeof s === 'string' && s.length > 0)
const isUserTask = Match.Where(t => {
if (!t || !t.id || !t.name) return false
let is = true
Object.keys(t).forEach(key => {
is = is && !!BpmnTasklistCollectionSchema[key]
})
return is
})
tasklist.remove = function remove (query) {
check(query, {
instanceId: String,
id: Match.Maybe(String),
})
return BpmnTaskListCollection.remove(query)
}
tasklist.add = function add (instanceId, userTask) {
check(instanceId, String)
check(userTask, isUserTask)
if (BpmnTaskListCollection.findOne({instanceId, id: userTask.id})) {
throw new Error('try to add a task that already exist')
}
const insertTarget = Object.assign({}, userTask)
// transform date fields into Date object fields
// to support mongo native sort by ISO Date
if (typeof insertTarget.dueDate !== 'undefined') {
insertTarget.dueDate = new Date(insertTarget.dueDate)
}
if (typeof insertTarget.followupDate !== 'undefined') {
insertTarget.followupDate = new Date(insertTarget.followupDate)
}
const insertTask = Object.assign({}, userTask, {instanceId})
return BpmnTaskListCollection.insert(insertTask)
}
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Hooks
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const tasklistHooks = {}
// EXECUTE
tasklistHooks.onExecuteBefore = function (engineFct, options) {
const engine = engineFct()
engine.on('end', Meteor.bindEnvironment(() => {
const innserEngine = engineFct()
const {instanceId} = innserEngine
tasklist.remove({instanceId})
}))
const tasklistListener = new EventEmitter()
tasklistListener.on('wait', Meteor.bindEnvironment((userTask) => {
const innserEngine = engineFct()
const {instanceId} = innserEngine
tasklist.add(instanceId, clean(userTask.activity))
}))
tasklistListener.on('end-userTask', Meteor.bindEnvironment((userTask) => {
const innerEngine = engineFct()
const {instanceId} = innerEngine
tasklist.remove({instanceId, id: userTask.activity.id})
}))
options.listener = Bpmn.mergeListeners({
source: options.listener,
target: tasklistListener,
})
}
// make mongo compatible
function clean (userTask) {
const activity = userTask
activity.type = activity.$type
delete activity.$type
return activity
}
// RESUME
tasklistHooks.onResumeBefore = function (engineFct, options) {
const tasklistListener = new EventEmitter()
tasklistListener.on('wait', Meteor.bindEnvironment((userTask) => {
const engine = engineFct()
const {instanceId} = engine
tasklist.add(instanceId, clean(userTask.activity))
}))
tasklistListener.on('end-userTask', Meteor.bindEnvironment((userTask) => {
const engine = engineFct()
const {instanceId} = engine
tasklist.remove({instanceId, id: userTask.activity.id})
}))
options.listener = Bpmn.mergeListeners({
source: options.listener,
target: tasklistListener,
})
}
tasklistHooks.onResumeAfter = Meteor.bindEnvironment(function (engineFct) {
const engine = engineFct()
engine.on('end', Meteor.bindEnvironment(() => {
const innerEngine = engineFct()
const {instanceId} = innerEngine
tasklist.remove({instanceId})
}))
})
tasklist.hooks = tasklistHooks
tasklist.on = function on () {
Bpmn.hooks.add(_name, tasklistHooks)
}
tasklist.off = function off () {
Bpmn.hooks.remove(_name)
}
// //////////////////////////////////////////////////////////////////////////////////////
//
// ASSIGN EXTENSION
//
// //////////////////////////////////////////////////////////////////////////////////////
Bpmn.tasklist = tasklist