wekan/models/checklists.js

299 lines
7.3 KiB
JavaScript
Raw Normal View History

2017-01-20 21:05:48 +08:00
Checklists = new Mongo.Collection('checklists');
/**
* A Checklist
*/
2017-01-20 21:05:48 +08:00
Checklists.attachSchema(new SimpleSchema({
cardId: {
/**
* The ID of the card the checklist is in
*/
2017-01-20 21:05:48 +08:00
type: String,
},
title: {
/**
* the title of the checklist
*/
2017-01-20 21:05:48 +08:00
type: String,
defaultValue: 'Checklist',
2017-01-20 21:05:48 +08:00
},
finishedAt: {
/**
* When was the checklist finished
*/
2017-01-20 21:05:48 +08:00
type: Date,
optional: true,
},
createdAt: {
/**
* Creation date of the checklist
*/
2017-01-20 21:05:48 +08:00
type: Date,
denyUpdate: false,
autoValue() { // eslint-disable-line consistent-return
if (this.isInsert) {
return new Date();
} else {
this.unset();
}
},
2017-01-20 21:05:48 +08:00
},
sort: {
/**
* sorting value of the checklist
*/
type: Number,
decimal: true,
},
2017-01-20 21:05:48 +08:00
}));
Checklists.helpers({
2019-02-23 16:36:29 +01:00
copy(newCardId) {
2019-02-24 00:13:35 +01:00
const oldChecklistId = this._id;
this._id = null;
this.cardId = newCardId;
const newChecklistId = Checklists.insert(this);
ChecklistItems.find({checklistId: oldChecklistId}).forEach((item) => {
item._id = null;
item.checklistId = newChecklistId;
item.cardId = newCardId;
ChecklistItems.insert(item);
});
2019-02-23 16:36:29 +01:00
},
itemCount() {
return ChecklistItems.find({ checklistId: this._id }).count();
2017-01-20 21:05:48 +08:00
},
items() {
2018-04-16 00:16:58 -03:00
return ChecklistItems.find({
checklistId: this._id,
2018-04-16 00:16:58 -03:00
}, { sort: ['sort'] });
},
finishedCount() {
return ChecklistItems.find({
checklistId: this._id,
isFinished: true,
}).count();
2017-01-20 21:05:48 +08:00
},
isFinished() {
2017-01-20 21:05:48 +08:00
return 0 !== this.itemCount() && this.itemCount() === this.finishedCount();
},
2018-08-19 18:53:50 +02:00
checkAllItems(){
const checkItems = ChecklistItems.find({checklistId: this._id});
checkItems.forEach(function(item){
item.check();
});
},
uncheckAllItems(){
const checkItems = ChecklistItems.find({checklistId: this._id});
checkItems.forEach(function(item){
item.uncheck();
});
},
2017-01-20 21:05:48 +08:00
itemIndex(itemId) {
const items = self.findOne({_id : this._id}).items;
return _.pluck(items, '_id').indexOf(itemId);
2017-01-20 21:05:48 +08:00
},
});
Checklists.allow({
insert(userId, doc) {
return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
},
update(userId, doc) {
return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
},
remove(userId, doc) {
return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
},
fetch: ['userId', 'cardId'],
});
Checklists.before.insert((userId, doc) => {
doc.createdAt = new Date();
if (!doc.userId) {
doc.userId = userId;
}
});
Checklists.mutations({
setTitle(title) {
return { $set: { title } };
2017-01-20 21:05:48 +08:00
},
});
if (Meteor.isServer) {
Meteor.startup(() => {
Checklists._collection._ensureIndex({ cardId: 1, createdAt: 1 });
});
2017-01-20 21:05:48 +08:00
Checklists.after.insert((userId, doc) => {
Activities.insert({
userId,
activityType: 'addChecklist',
cardId: doc.cardId,
boardId: Cards.findOne(doc.cardId).boardId,
checklistId: doc._id,
2018-09-16 01:50:36 +03:00
checklistName:doc.title,
listId: doc.listId,
swimlaneId: doc.swimlaneId,
2017-01-20 21:05:48 +08:00
});
});
Checklists.before.remove((userId, doc) => {
const activities = Activities.find({ checklistId: doc._id });
if (activities) {
activities.forEach((activity) => {
Activities.remove(activity._id);
});
2017-01-20 21:05:48 +08:00
}
2018-08-16 21:49:56 +02:00
Activities.insert({
userId,
activityType: 'removeChecklist',
cardId: doc.cardId,
boardId: Cards.findOne(doc.cardId).boardId,
checklistId: doc._id,
2018-09-16 01:50:36 +03:00
checklistName:doc.title,
listId: doc.listId,
swimlaneId: doc.swimlaneId,
2018-08-16 21:49:56 +02:00
});
2017-01-20 21:05:48 +08:00
});
}
if (Meteor.isServer) {
/**
* @operation get_all_checklists
* @summary Get the list of checklists attached to a card
*
* @param {string} boardId the board ID
* @param {string} cardId the card ID
* @return_type [{_id: string,
* title: string}]
*/
2018-02-27 02:57:58 +01:00
JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
2018-04-15 21:34:15 -03:00
Authentication.checkUserId( req.userId);
const paramCardId = req.params.cardId;
const checklists = Checklists.find({ cardId: paramCardId }).map(function (doc) {
return {
_id: doc._id,
title: doc.title,
};
});
if (checklists) {
2017-10-15 07:39:48 +02:00
JsonRoutes.sendResult(res, {
code: 200,
2018-04-15 21:34:15 -03:00
data: checklists,
2017-10-15 07:39:48 +02:00
});
2018-04-15 21:34:15 -03:00
} else {
2017-10-15 07:39:48 +02:00
JsonRoutes.sendResult(res, {
2018-04-15 21:34:15 -03:00
code: 500,
2017-10-15 07:39:48 +02:00
});
}
});
/**
* @operation get_checklist
* @summary Get a checklist
*
* @param {string} boardId the board ID
* @param {string} cardId the card ID
* @param {string} checklistId the ID of the checklist
* @return_type {cardId: string,
* title: string,
* finishedAt: string,
* createdAt: string,
* sort: number,
* items: [{_id: string,
* title: string,
* isFinished: boolean}]}
*/
2018-02-27 02:57:58 +01:00
JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
2018-04-15 21:34:15 -03:00
Authentication.checkUserId( req.userId);
const paramChecklistId = req.params.checklistId;
const paramCardId = req.params.cardId;
const checklist = Checklists.findOne({ _id: paramChecklistId, cardId: paramCardId });
if (checklist) {
checklist.items = ChecklistItems.find({checklistId: checklist._id}).map(function (doc) {
return {
_id: doc._id,
title: doc.title,
isFinished: doc.isFinished,
2018-04-15 21:34:15 -03:00
};
});
2017-10-15 07:39:48 +02:00
JsonRoutes.sendResult(res, {
code: 200,
2018-04-15 21:34:15 -03:00
data: checklist,
2017-10-15 07:39:48 +02:00
});
2018-04-15 21:34:15 -03:00
} else {
2017-10-15 07:39:48 +02:00
JsonRoutes.sendResult(res, {
2018-04-15 21:34:15 -03:00
code: 500,
2017-10-15 07:39:48 +02:00
});
}
2018-04-15 23:06:54 -03:00
});
/**
* @operation new_checklist
* @summary create a new checklist
*
* @param {string} boardId the board ID
* @param {string} cardId the card ID
* @param {string} title the title of the new checklist
* @return_type {_id: string}
*/
2018-02-27 02:57:58 +01:00
JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
2018-04-15 21:34:15 -03:00
Authentication.checkUserId( req.userId);
2018-04-15 21:34:15 -03:00
const paramCardId = req.params.cardId;
const id = Checklists.insert({
title: req.body.title,
cardId: paramCardId,
sort: 0,
});
if (id) {
req.body.items.forEach(function (item, idx) {
ChecklistItems.insert({
cardId: paramCardId,
checklistId: id,
title: item.title,
sort: idx,
});
});
2017-10-15 07:39:48 +02:00
JsonRoutes.sendResult(res, {
code: 200,
data: {
_id: id,
},
});
2018-04-15 21:34:15 -03:00
} else {
2017-10-15 07:39:48 +02:00
JsonRoutes.sendResult(res, {
2018-04-15 21:34:15 -03:00
code: 400,
2017-10-15 07:39:48 +02:00
});
}
});
/**
* @operation delete_checklist
* @summary Delete a checklist
*
* @description The checklist will be removed, not put in the recycle bin.
*
* @param {string} boardId the board ID
* @param {string} cardId the card ID
* @param {string} checklistId the ID of the checklist to remove
* @return_type {_id: string}
*/
2018-02-27 02:57:58 +01:00
JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
2018-04-15 21:34:15 -03:00
Authentication.checkUserId( req.userId);
const paramChecklistId = req.params.checklistId;
Checklists.remove({ _id: paramChecklistId });
JsonRoutes.sendResult(res, {
code: 200,
data: {
_id: paramChecklistId,
},
});
});
}