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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
|
Cards = new Mongo.Collection('cards');
// XXX To improve pub/sub performances a card document should include a
// de-normalized number of comments so we don't have to publish the whole list
// of comments just to display the number of them in the board view.
Cards.attachSchema(new SimpleSchema({
title: {
type: String,
},
archived: {
type: Boolean,
autoValue() { // eslint-disable-line consistent-return
if (this.isInsert && !this.isSet) {
return false;
}
},
},
parentId: {
type: String,
optional: true,
defaultValue: '',
},
listId: {
type: String,
},
swimlaneId: {
type: String,
},
// The system could work without this `boardId` information (we could deduce
// the board identifier from the card), but it would make the system more
// difficult to manage and less efficient.
boardId: {
type: String,
},
coverId: {
type: String,
optional: true,
},
createdAt: {
type: Date,
autoValue() { // eslint-disable-line consistent-return
if (this.isInsert) {
return new Date();
} else {
this.unset();
}
},
},
customFields: {
type: [Object],
optional: true,
},
'customFields.$': {
type: new SimpleSchema({
_id: {
type: String,
},
value: {
type: Match.OneOf(String, Number, Boolean, Date),
optional: true,
},
}),
},
dateLastActivity: {
type: Date,
autoValue() {
return new Date();
},
},
description: {
type: String,
optional: true,
},
requestedBy: {
type: String,
optional: true,
},
assignedBy: {
type: String,
optional: true,
},
labelIds: {
type: [String],
optional: true,
},
members: {
type: [String],
optional: true,
},
receivedAt: {
type: Date,
optional: true,
},
startAt: {
type: Date,
optional: true,
},
dueAt: {
type: Date,
optional: true,
},
endAt: {
type: Date,
optional: true,
},
spentTime: {
type: Number,
decimal: true,
optional: true,
},
isOvertime: {
type: Boolean,
defaultValue: false,
optional: true,
},
// XXX Should probably be called `authorId`. Is it even needed since we have
// the `members` field?
userId: {
type: String,
autoValue() { // eslint-disable-line consistent-return
if (this.isInsert && !this.isSet) {
return this.userId;
}
},
},
sort: {
type: Number,
decimal: true,
},
subtaskSort: {
type: Number,
decimal: true,
defaultValue: -1,
optional: true,
},
type: {
type: String,
},
importedId: {
type: String,
optional: true,
},
}));
Cards.allow({
insert(userId, doc) {
return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
},
update(userId, doc) {
return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
},
remove(userId, doc) {
return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
},
fetch: ['boardId'],
});
Cards.helpers({
list() {
return Lists.findOne(this.listId);
},
board() {
return Boards.findOne(this.boardId);
},
labels() {
const boardLabels = this.board().labels;
const cardLabels = _.filter(boardLabels, (label) => {
return _.contains(this.labelIds, label._id);
});
return cardLabels;
},
hasLabel(labelId) {
return _.contains(this.labelIds, labelId);
},
user() {
return Users.findOne(this.userId);
},
isAssigned(memberId) {
return _.contains(this.members, memberId);
},
activities() {
return Activities.find({cardId: this._id}, {sort: {createdAt: -1}});
},
comments() {
return CardComments.find({cardId: this._id}, {sort: {createdAt: -1}});
},
attachments() {
return Attachments.find({cardId: this._id}, {sort: {uploadedAt: -1}});
},
cover() {
const cover = Attachments.findOne(this.coverId);
// if we return a cover before it is fully stored, we will get errors when we try to display it
// todo XXX we could return a default "upload pending" image in the meantime?
return cover && cover.url() && cover;
},
checklists() {
return Checklists.find({cardId: this._id}, {sort: { sort: 1 } });
},
checklistItemCount() {
const checklists = this.checklists().fetch();
return checklists.map((checklist) => {
return checklist.itemCount();
}).reduce((prev, next) => {
return prev + next;
}, 0);
},
checklistFinishedCount() {
const checklists = this.checklists().fetch();
return checklists.map((checklist) => {
return checklist.finishedCount();
}).reduce((prev, next) => {
return prev + next;
}, 0);
},
checklistFinished() {
return this.hasChecklist() && this.checklistItemCount() === this.checklistFinishedCount();
},
hasChecklist() {
return this.checklistItemCount() !== 0;
},
subtasks() {
return Cards.find({
parentId: this._id,
archived: false,
}, {sort: { sort: 1 } });
},
allSubtasks() {
return Cards.find({
parentId: this._id,
archived: false,
}, {sort: { sort: 1 } });
},
subtasksCount() {
return Cards.find({
parentId: this._id,
archived: false,
}).count();
},
subtasksFinishedCount() {
return Cards.find({
parentId: this._id,
archived: true}).count();
},
subtasksFinished() {
const finishCount = this.subtasksFinishedCount();
return finishCount > 0 && this.subtasksCount() === finishCount;
},
allowsSubtasks() {
return this.subtasksCount() !== 0;
},
customFieldIndex(customFieldId) {
return _.pluck(this.customFields, '_id').indexOf(customFieldId);
},
// customFields with definitions
customFieldsWD() {
// get all definitions
const definitions = CustomFields.find({
boardId: this.boardId,
}).fetch();
// match right definition to each field
if (!this.customFields) return [];
return this.customFields.map((customField) => {
const definition = definitions.find((definition) => {
return definition._id === customField._id;
});
//search for "True Value" which is for DropDowns other then the Value (which is the id)
let trueValue = customField.value;
if (definition.settings.dropdownItems && definition.settings.dropdownItems.length > 0)
{
for (let i = 0; i < definition.settings.dropdownItems.length; i++)
{
if (definition.settings.dropdownItems[i]._id === customField.value)
{
trueValue = definition.settings.dropdownItems[i].name;
}
}
}
return {
_id: customField._id,
value: customField.value,
trueValue,
definition,
};
});
},
absoluteUrl() {
const board = this.board();
return FlowRouter.url('card', {
boardId: board._id,
slug: board.slug,
cardId: this._id,
});
},
canBeRestored() {
const list = Lists.findOne({_id: this.listId});
if(!list.getWipLimit('soft') && list.getWipLimit('enabled') && list.getWipLimit('value') === list.cards().count()){
return false;
}
return true;
},
parentCard() {
if (this.parentId === '') {
return null;
}
return Cards.findOne(this.parentId);
},
parentCardName() {
let result = '';
if (this.parentId !== '') {
const card = Cards.findOne(this.parentId);
if (card) {
result = card.title;
}
}
return result;
},
parentListId() {
const result = [];
let crtParentId = this.parentId;
while (crtParentId !== '') {
const crt = Cards.findOne(crtParentId);
if ((crt === null) || (crt === undefined)) {
// maybe it has been deleted
break;
}
if (crtParentId in result) {
// circular reference
break;
}
result.unshift(crtParentId);
crtParentId = crt.parentId;
}
return result;
},
parentList() {
const resultId = [];
const result = [];
let crtParentId = this.parentId;
while (crtParentId !== '') {
const crt = Cards.findOne(crtParentId);
if ((crt === null) || (crt === undefined)) {
// maybe it has been deleted
break;
}
if (crtParentId in resultId) {
// circular reference
break;
}
resultId.unshift(crtParentId);
result.unshift(crt);
crtParentId = crt.parentId;
}
return result;
},
parentString(sep) {
return this.parentList().map(function(elem){
return elem.title;
}).join(sep);
},
isTopLevel() {
return this.parentId === '';
},
isImportedCard() {
return this.type === 'cardType-importedCard';
},
isImportedBoard() {
return this.type === 'cardType-importedBoard';
},
isImported() {
return this.isImportedCard() || this.isImportedBoard();
},
getDescription() {
if (this.isImportedCard()) {
const card = Cards.findOne({_id: this.importedId});
if (card && card.description)
return card.description;
else
return null;
} else if (this.isImportedBoard()) {
const board = Boards.findOne({_id: this.importedId});
if (board && board.description)
return board.description;
else
return null;
} else {
if (this.description)
return this.description;
else
return null;
}
},
});
Cards.mutations({
applyToChildren(funct) {
Cards.find({ parentId: this._id }).forEach((card) => {
funct(card);
});
},
archive() {
this.applyToChildren((card) => { return card.archive(); });
return {$set: {archived: true}};
},
restore() {
this.applyToChildren((card) => { return card.restore(); });
return {$set: {archived: false}};
},
setTitle(title) {
return {$set: {title}};
},
setDescription(description) {
return {$set: {description}};
},
setRequestedBy(requestedBy) {
return {$set: {requestedBy}};
},
setAssignedBy(assignedBy) {
return {$set: {assignedBy}};
},
move(swimlaneId, listId, sortIndex) {
const list = Lists.findOne(listId);
const mutatedFields = {
swimlaneId,
listId,
boardId: list.boardId,
sort: sortIndex,
};
return {$set: mutatedFields};
},
addLabel(labelId) {
return {$addToSet: {labelIds: labelId}};
},
removeLabel(labelId) {
return {$pull: {labelIds: labelId}};
},
toggleLabel(labelId) {
if (this.labelIds && this.labelIds.indexOf(labelId) > -1) {
return this.removeLabel(labelId);
} else {
return this.addLabel(labelId);
}
},
assignMember(memberId) {
return {$addToSet: {members: memberId}};
},
unassignMember(memberId) {
return {$pull: {members: memberId}};
},
toggleMember(memberId) {
if (this.members && this.members.indexOf(memberId) > -1) {
return this.unassignMember(memberId);
} else {
return this.assignMember(memberId);
}
},
assignCustomField(customFieldId) {
return {$addToSet: {customFields: {_id: customFieldId, value: null}}};
},
unassignCustomField(customFieldId) {
return {$pull: {customFields: {_id: customFieldId}}};
},
toggleCustomField(customFieldId) {
if (this.customFields && this.customFieldIndex(customFieldId) > -1) {
return this.unassignCustomField(customFieldId);
} else {
return this.assignCustomField(customFieldId);
}
},
setCustomField(customFieldId, value) {
// todo
const index = this.customFieldIndex(customFieldId);
if (index > -1) {
const update = {$set: {}};
update.$set[`customFields.${index}.value`] = value;
return update;
}
// TODO
// Ignatz 18.05.2018: Return null to silence ESLint. No Idea if that is correct
return null;
},
setCover(coverId) {
return {$set: {coverId}};
},
unsetCover() {
return {$unset: {coverId: ''}};
},
setReceived(receivedAt) {
return {$set: {receivedAt}};
},
unsetReceived() {
return {$unset: {receivedAt: ''}};
},
setStart(startAt) {
return {$set: {startAt}};
},
unsetStart() {
return {$unset: {startAt: ''}};
},
setDue(dueAt) {
return {$set: {dueAt}};
},
unsetDue() {
return {$unset: {dueAt: ''}};
},
setEnd(endAt) {
return {$set: {endAt}};
},
unsetEnd() {
return {$unset: {endAt: ''}};
},
setOvertime(isOvertime) {
return {$set: {isOvertime}};
},
setSpentTime(spentTime) {
return {$set: {spentTime}};
},
unsetSpentTime() {
return {$unset: {spentTime: '', isOvertime: false}};
},
setParentId(parentId) {
return {$set: {parentId}};
},
});
//FUNCTIONS FOR creation of Activities
function cardMove(userId, doc, fieldNames, oldListId) {
if (_.contains(fieldNames, 'listId') && doc.listId !== oldListId) {
Activities.insert({
userId,
oldListId,
activityType: 'moveCard',
listId: doc.listId,
boardId: doc.boardId,
cardId: doc._id,
});
}
}
function cardState(userId, doc, fieldNames) {
if (_.contains(fieldNames, 'archived')) {
if (doc.archived) {
Activities.insert({
userId,
activityType: 'archivedCard',
boardId: doc.boardId,
listId: doc.listId,
cardId: doc._id,
});
} else {
Activities.insert({
userId,
activityType: 'restoredCard',
boardId: doc.boardId,
listId: doc.listId,
cardId: doc._id,
});
}
}
}
function cardMembers(userId, doc, fieldNames, modifier) {
if (!_.contains(fieldNames, 'members'))
return;
let memberId;
// Say hello to the new member
if (modifier.$addToSet && modifier.$addToSet.members) {
memberId = modifier.$addToSet.members;
if (!_.contains(doc.members, memberId)) {
Activities.insert({
userId,
memberId,
activityType: 'joinMember',
boardId: doc.boardId,
cardId: doc._id,
});
}
}
// Say goodbye to the former member
if (modifier.$pull && modifier.$pull.members) {
memberId = modifier.$pull.members;
// Check that the former member is member of the card
if (_.contains(doc.members, memberId)) {
Activities.insert({
userId,
memberId,
activityType: 'unjoinMember',
boardId: doc.boardId,
cardId: doc._id,
});
}
}
}
function cardCreation(userId, doc) {
Activities.insert({
userId,
activityType: 'createCard',
boardId: doc.boardId,
listId: doc.listId,
cardId: doc._id,
});
}
function cardRemover(userId, doc) {
Activities.remove({
cardId: doc._id,
});
Checklists.remove({
cardId: doc._id,
});
Subtasks.remove({
cardId: doc._id,
});
CardComments.remove({
cardId: doc._id,
});
Attachments.remove({
cardId: doc._id,
});
}
if (Meteor.isServer) {
// Cards are often fetched within a board, so we create an index to make these
// queries more efficient.
Meteor.startup(() => {
Cards._collection._ensureIndex({boardId: 1, createdAt: -1});
});
Cards.after.insert((userId, doc) => {
cardCreation(userId, doc);
});
// New activity for card (un)archivage
Cards.after.update((userId, doc, fieldNames) => {
cardState(userId, doc, fieldNames);
});
//New activity for card moves
Cards.after.update(function (userId, doc, fieldNames) {
const oldListId = this.previous.listId;
cardMove(userId, doc, fieldNames, oldListId);
});
// Add a new activity if we add or remove a member to the card
Cards.before.update((userId, doc, fieldNames, modifier) => {
cardMembers(userId, doc, fieldNames, modifier);
});
// Remove all activities associated with a card if we remove the card
// Remove also card_comments / checklists / attachments
Cards.after.remove((userId, doc) => {
cardRemover(userId, doc);
});
}
//LISTS REST API
if (Meteor.isServer) {
JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards', function (req, res) {
const paramBoardId = req.params.boardId;
const paramListId = req.params.listId;
Authentication.checkBoardAccess(req.userId, paramBoardId);
JsonRoutes.sendResult(res, {
code: 200,
data: Cards.find({boardId: paramBoardId, listId: paramListId, archived: false}).map(function (doc) {
return {
_id: doc._id,
title: doc.title,
description: doc.description,
};
}),
});
});
JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
const paramBoardId = req.params.boardId;
const paramListId = req.params.listId;
const paramCardId = req.params.cardId;
Authentication.checkBoardAccess(req.userId, paramBoardId);
JsonRoutes.sendResult(res, {
code: 200,
data: Cards.findOne({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false}),
});
});
JsonRoutes.add('POST', '/api/boards/:boardId/lists/:listId/cards', function (req, res) {
Authentication.checkUserId(req.userId);
const paramBoardId = req.params.boardId;
const paramListId = req.params.listId;
const check = Users.findOne({_id: req.body.authorId});
const members = req.body.members || [req.body.authorId];
if (typeof check !== 'undefined') {
const id = Cards.direct.insert({
title: req.body.title,
boardId: paramBoardId,
listId: paramListId,
description: req.body.description,
userId: req.body.authorId,
swimlaneId: req.body.swimlaneId,
sort: 0,
members,
});
JsonRoutes.sendResult(res, {
code: 200,
data: {
_id: id,
},
});
const card = Cards.findOne({_id:id});
cardCreation(req.body.authorId, card);
} else {
JsonRoutes.sendResult(res, {
code: 401,
});
}
});
JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
Authentication.checkUserId(req.userId);
const paramBoardId = req.params.boardId;
const paramCardId = req.params.cardId;
const paramListId = req.params.listId;
if (req.body.hasOwnProperty('title')) {
const newTitle = req.body.title;
Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
{$set: {title: newTitle}});
}
if (req.body.hasOwnProperty('listId')) {
const newParamListId = req.body.listId;
Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
{$set: {listId: newParamListId}});
const card = Cards.findOne({_id: paramCardId} );
cardMove(req.body.authorId, card, {fieldName: 'listId'}, paramListId);
}
if (req.body.hasOwnProperty('description')) {
const newDescription = req.body.description;
Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
{$set: {description: newDescription}});
}
if (req.body.hasOwnProperty('labelIds')) {
const newlabelIds = req.body.labelIds;
Cards.direct.update({_id: paramCardId, listId: paramListId, boardId: paramBoardId, archived: false},
{$set: {labelIds: newlabelIds}});
}
JsonRoutes.sendResult(res, {
code: 200,
data: {
_id: paramCardId,
},
});
});
JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId/cards/:cardId', function (req, res) {
Authentication.checkUserId(req.userId);
const paramBoardId = req.params.boardId;
const paramListId = req.params.listId;
const paramCardId = req.params.cardId;
Cards.direct.remove({_id: paramCardId, listId: paramListId, boardId: paramBoardId});
const card = Cards.find({_id: paramCardId} );
cardRemover(req.body.authorId, card);
JsonRoutes.sendResult(res, {
code: 200,
data: {
_id: paramCardId,
},
});
});
}
|