lists.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. Lists = new Mongo.Collection('lists');
  2. Lists.attachSchema(new SimpleSchema({
  3. title: {
  4. type: String,
  5. },
  6. archived: {
  7. type: Boolean,
  8. autoValue() { // eslint-disable-line consistent-return
  9. if (this.isInsert && !this.isSet) {
  10. return false;
  11. }
  12. },
  13. },
  14. boardId: {
  15. type: String,
  16. },
  17. createdAt: {
  18. type: Date,
  19. autoValue() { // eslint-disable-line consistent-return
  20. if (this.isInsert) {
  21. return new Date();
  22. } else {
  23. this.unset();
  24. }
  25. },
  26. },
  27. sort: {
  28. type: Number,
  29. decimal: true,
  30. // XXX We should probably provide a default
  31. optional: true,
  32. },
  33. updatedAt: {
  34. type: Date,
  35. optional: true,
  36. autoValue() { // eslint-disable-line consistent-return
  37. if (this.isUpdate) {
  38. return new Date();
  39. } else {
  40. this.unset();
  41. }
  42. },
  43. },
  44. wipLimit: {
  45. type: Object,
  46. optional: true,
  47. },
  48. 'wipLimit.value': {
  49. type: Number,
  50. decimal: false,
  51. defaultValue: 1,
  52. },
  53. 'wipLimit.enabled': {
  54. type: Boolean,
  55. defaultValue: false,
  56. },
  57. 'wipLimit.soft': {
  58. type: Boolean,
  59. defaultValue: false,
  60. },
  61. }));
  62. Lists.allow({
  63. insert(userId, doc) {
  64. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  65. },
  66. update(userId, doc) {
  67. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  68. },
  69. remove(userId, doc) {
  70. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  71. },
  72. fetch: ['boardId'],
  73. });
  74. Lists.helpers({
  75. cards(swimlaneId) {
  76. const selector = {
  77. listId: this._id,
  78. archived: false,
  79. };
  80. if (swimlaneId)
  81. selector.swimlaneId = swimlaneId;
  82. return Cards.find(Filter.mongoSelector(selector),
  83. { sort: ['sort'] });
  84. },
  85. cardsUnfiltered(swimlaneId) {
  86. const selector = {
  87. listId: this._id,
  88. archived: false,
  89. };
  90. if (swimlaneId)
  91. selector.swimlaneId = swimlaneId;
  92. return Cards.find(selector,
  93. { sort: ['sort'] });
  94. },
  95. allCards() {
  96. return Cards.find({ listId: this._id });
  97. },
  98. board() {
  99. return Boards.findOne(this.boardId);
  100. },
  101. getWipLimit(option){
  102. const list = Lists.findOne({ _id: this._id });
  103. if(!list.wipLimit) { // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
  104. return 0;
  105. } else if(!option) {
  106. return list.wipLimit;
  107. } else {
  108. 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
  109. }
  110. },
  111. });
  112. Lists.mutations({
  113. rename(title) {
  114. return { $set: { title } };
  115. },
  116. archive() {
  117. return { $set: { archived: true } };
  118. },
  119. restore() {
  120. return { $set: { archived: false } };
  121. },
  122. toggleSoftLimit(toggle) {
  123. return { $set: { 'wipLimit.soft': toggle } };
  124. },
  125. toggleWipLimit(toggle) {
  126. return { $set: { 'wipLimit.enabled': toggle } };
  127. },
  128. setWipLimit(limit) {
  129. return { $set: { 'wipLimit.value': limit } };
  130. },
  131. });
  132. Meteor.methods({
  133. applyWipLimit(listId, limit){
  134. check(listId, String);
  135. check(limit, Number);
  136. if(limit === 0){
  137. limit = 1;
  138. }
  139. Lists.findOne({ _id: listId }).setWipLimit(limit);
  140. },
  141. enableWipLimit(listId) {
  142. check(listId, String);
  143. const list = Lists.findOne({ _id: listId });
  144. if(list.getWipLimit('value') === 0){
  145. list.setWipLimit(1);
  146. }
  147. list.toggleWipLimit(!list.getWipLimit('enabled'));
  148. },
  149. enableSoftLimit(listId) {
  150. check(listId, String);
  151. const list = Lists.findOne({ _id: listId });
  152. list.toggleSoftLimit(!list.getWipLimit('soft'));
  153. },
  154. });
  155. Lists.hookOptions.after.update = { fetchPrevious: false };
  156. if (Meteor.isServer) {
  157. Meteor.startup(() => {
  158. Lists._collection._ensureIndex({ boardId: 1 });
  159. });
  160. Lists.after.insert((userId, doc) => {
  161. Activities.insert({
  162. userId,
  163. type: 'list',
  164. activityType: 'createList',
  165. boardId: doc.boardId,
  166. listId: doc._id,
  167. });
  168. });
  169. Lists.before.remove((userId, doc) => {
  170. Activities.insert({
  171. userId,
  172. type: 'list',
  173. activityType: 'removeList',
  174. boardId: doc.boardId,
  175. listId: doc._id,
  176. title: doc.title,
  177. });
  178. });
  179. Lists.after.update((userId, doc) => {
  180. if (doc.archived) {
  181. Activities.insert({
  182. userId,
  183. type: 'list',
  184. activityType: 'archivedList',
  185. listId: doc._id,
  186. boardId: doc.boardId,
  187. });
  188. }
  189. });
  190. }
  191. //LISTS REST API
  192. if (Meteor.isServer) {
  193. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function (req, res) {
  194. try {
  195. const paramBoardId = req.params.boardId;
  196. Authentication.checkBoardAccess( req.userId, paramBoardId);
  197. JsonRoutes.sendResult(res, {
  198. code: 200,
  199. data: Lists.find({ boardId: paramBoardId, archived: false }).map(function (doc) {
  200. return {
  201. _id: doc._id,
  202. title: doc.title,
  203. };
  204. }),
  205. });
  206. }
  207. catch (error) {
  208. JsonRoutes.sendResult(res, {
  209. code: 200,
  210. data: error,
  211. });
  212. }
  213. });
  214. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function (req, res) {
  215. try {
  216. const paramBoardId = req.params.boardId;
  217. const paramListId = req.params.listId;
  218. Authentication.checkBoardAccess( req.userId, paramBoardId);
  219. JsonRoutes.sendResult(res, {
  220. code: 200,
  221. data: Lists.findOne({ _id: paramListId, boardId: paramBoardId, archived: false }),
  222. });
  223. }
  224. catch (error) {
  225. JsonRoutes.sendResult(res, {
  226. code: 200,
  227. data: error,
  228. });
  229. }
  230. });
  231. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function (req, res) {
  232. try {
  233. Authentication.checkUserId( req.userId);
  234. const paramBoardId = req.params.boardId;
  235. const id = Lists.insert({
  236. title: req.body.title,
  237. boardId: paramBoardId,
  238. });
  239. JsonRoutes.sendResult(res, {
  240. code: 200,
  241. data: {
  242. _id: id,
  243. },
  244. });
  245. }
  246. catch (error) {
  247. JsonRoutes.sendResult(res, {
  248. code: 200,
  249. data: error,
  250. });
  251. }
  252. });
  253. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function (req, res) {
  254. try {
  255. Authentication.checkUserId( req.userId);
  256. const paramBoardId = req.params.boardId;
  257. const paramListId = req.params.listId;
  258. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  259. JsonRoutes.sendResult(res, {
  260. code: 200,
  261. data: {
  262. _id: paramListId,
  263. },
  264. });
  265. }
  266. catch (error) {
  267. JsonRoutes.sendResult(res, {
  268. code: 200,
  269. data: error,
  270. });
  271. }
  272. });
  273. }