123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- Lists = new Mongo.Collection('lists');
- Lists.attachSchema(new SimpleSchema({
- title: {
- type: String,
- },
- archived: {
- type: Boolean,
- autoValue() { // eslint-disable-line consistent-return
- if (this.isInsert && !this.isSet) {
- return false;
- }
- },
- },
- boardId: {
- type: String,
- },
- createdAt: {
- type: Date,
- autoValue() { // eslint-disable-line consistent-return
- if (this.isInsert) {
- return new Date();
- } else {
- this.unset();
- }
- },
- },
- sort: {
- type: Number,
- decimal: true,
- // XXX We should probably provide a default
- optional: true,
- },
- updatedAt: {
- type: Date,
- optional: true,
- autoValue() { // eslint-disable-line consistent-return
- if (this.isUpdate) {
- return new Date();
- } else {
- this.unset();
- }
- },
- },
- wipLimit: {
- type: Object,
- optional: true,
- },
- 'wipLimit.value': {
- type: Number,
- decimal: false,
- defaultValue: 1,
- },
- 'wipLimit.enabled': {
- type: Boolean,
- defaultValue: false,
- },
- 'wipLimit.soft': {
- type: Boolean,
- defaultValue: false,
- },
- }));
- Lists.allow({
- insert(userId, doc) {
- return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
- },
- update(userId, doc) {
- return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
- },
- remove(userId, doc) {
- return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
- },
- fetch: ['boardId'],
- });
- Lists.helpers({
- cards(swimlaneId) {
- const selector = {
- listId: this._id,
- archived: false,
- };
- if (swimlaneId)
- selector.swimlaneId = swimlaneId;
- return Cards.find(Filter.mongoSelector(selector),
- { sort: ['sort'] });
- },
- cardsUnfiltered(swimlaneId) {
- const selector = {
- listId: this._id,
- archived: false,
- };
- if (swimlaneId)
- selector.swimlaneId = swimlaneId;
- return Cards.find(selector,
- { sort: ['sort'] });
- },
- allCards() {
- return Cards.find({ listId: this._id });
- },
- board() {
- return Boards.findOne(this.boardId);
- },
- getWipLimit(option){
- const list = Lists.findOne({ _id: this._id });
- if(!list.wipLimit) { // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
- return 0;
- } else if(!option) {
- return list.wipLimit;
- } else {
- return list.wipLimit[option] ? list.wipLimit[option] : 0; // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
- }
- },
- });
- Lists.mutations({
- rename(title) {
- return { $set: { title } };
- },
- archive() {
- return { $set: { archived: true } };
- },
- restore() {
- return { $set: { archived: false } };
- },
- toggleSoftLimit(toggle) {
- return { $set: { 'wipLimit.soft': toggle } };
- },
- toggleWipLimit(toggle) {
- return { $set: { 'wipLimit.enabled': toggle } };
- },
- setWipLimit(limit) {
- return { $set: { 'wipLimit.value': limit } };
- },
- });
- Meteor.methods({
- applyWipLimit(listId, limit){
- check(listId, String);
- check(limit, Number);
- if(limit === 0){
- limit = 1;
- }
- Lists.findOne({ _id: listId }).setWipLimit(limit);
- },
- enableWipLimit(listId) {
- check(listId, String);
- const list = Lists.findOne({ _id: listId });
- if(list.getWipLimit('value') === 0){
- list.setWipLimit(1);
- }
- list.toggleWipLimit(!list.getWipLimit('enabled'));
- },
- enableSoftLimit(listId) {
- check(listId, String);
- const list = Lists.findOne({ _id: listId });
- list.toggleSoftLimit(!list.getWipLimit('soft'));
- },
- });
- Lists.hookOptions.after.update = { fetchPrevious: false };
- if (Meteor.isServer) {
- Meteor.startup(() => {
- Lists._collection._ensureIndex({ boardId: 1 });
- });
- Lists.after.insert((userId, doc) => {
- Activities.insert({
- userId,
- type: 'list',
- activityType: 'createList',
- boardId: doc.boardId,
- listId: doc._id,
- });
- });
- Lists.before.remove((userId, doc) => {
- Activities.insert({
- userId,
- type: 'list',
- activityType: 'removeList',
- boardId: doc.boardId,
- listId: doc._id,
- title: doc.title,
- });
- });
- Lists.after.update((userId, doc) => {
- if (doc.archived) {
- Activities.insert({
- userId,
- type: 'list',
- activityType: 'archivedList',
- listId: doc._id,
- boardId: doc.boardId,
- });
- }
- });
- }
- //LISTS REST API
- if (Meteor.isServer) {
- JsonRoutes.add('GET', '/api/boards/:boardId/lists', function (req, res) {
- try {
- const paramBoardId = req.params.boardId;
- Authentication.checkBoardAccess( req.userId, paramBoardId);
- JsonRoutes.sendResult(res, {
- code: 200,
- data: Lists.find({ boardId: paramBoardId, archived: false }).map(function (doc) {
- return {
- _id: doc._id,
- title: doc.title,
- };
- }),
- });
- }
- catch (error) {
- JsonRoutes.sendResult(res, {
- code: 200,
- data: error,
- });
- }
- });
- JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function (req, res) {
- try {
- const paramBoardId = req.params.boardId;
- const paramListId = req.params.listId;
- Authentication.checkBoardAccess( req.userId, paramBoardId);
- JsonRoutes.sendResult(res, {
- code: 200,
- data: Lists.findOne({ _id: paramListId, boardId: paramBoardId, archived: false }),
- });
- }
- catch (error) {
- JsonRoutes.sendResult(res, {
- code: 200,
- data: error,
- });
- }
- });
- JsonRoutes.add('POST', '/api/boards/:boardId/lists', function (req, res) {
- try {
- Authentication.checkUserId( req.userId);
- const paramBoardId = req.params.boardId;
- const id = Lists.insert({
- title: req.body.title,
- boardId: paramBoardId,
- });
- JsonRoutes.sendResult(res, {
- code: 200,
- data: {
- _id: id,
- },
- });
- }
- catch (error) {
- JsonRoutes.sendResult(res, {
- code: 200,
- data: error,
- });
- }
- });
- JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function (req, res) {
- try {
- Authentication.checkUserId( req.userId);
- const paramBoardId = req.params.boardId;
- const paramListId = req.params.listId;
- Lists.remove({ _id: paramListId, boardId: paramBoardId });
- JsonRoutes.sendResult(res, {
- code: 200,
- data: {
- _id: paramListId,
- },
- });
- }
- catch (error) {
- JsonRoutes.sendResult(res, {
- code: 200,
- data: error,
- });
- }
- });
- }
|