lists.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. allCards() {
  86. return Cards.find({ listId: this._id });
  87. },
  88. board() {
  89. return Boards.findOne(this.boardId);
  90. },
  91. getWipLimit(option){
  92. const list = Lists.findOne({ _id: this._id });
  93. if(!list.wipLimit) { // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
  94. return 0;
  95. } else if(!option) {
  96. return list.wipLimit;
  97. } else {
  98. 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
  99. }
  100. },
  101. });
  102. Lists.mutations({
  103. rename(title) {
  104. return { $set: { title } };
  105. },
  106. archive() {
  107. return { $set: { archived: true } };
  108. },
  109. restore() {
  110. return { $set: { archived: false } };
  111. },
  112. toggleSoftLimit(toggle) {
  113. return { $set: { 'wipLimit.soft': toggle } };
  114. },
  115. toggleWipLimit(toggle) {
  116. return { $set: { 'wipLimit.enabled': toggle } };
  117. },
  118. setWipLimit(limit) {
  119. return { $set: { 'wipLimit.value': limit } };
  120. },
  121. });
  122. Meteor.methods({
  123. applyWipLimit(listId, limit){
  124. check(listId, String);
  125. check(limit, Number);
  126. if(limit === 0){
  127. limit = 1;
  128. }
  129. Lists.findOne({ _id: listId }).setWipLimit(limit);
  130. },
  131. enableWipLimit(listId) {
  132. check(listId, String);
  133. const list = Lists.findOne({ _id: listId });
  134. if(list.getWipLimit('value') === 0){
  135. list.setWipLimit(1);
  136. }
  137. list.toggleWipLimit(!list.getWipLimit('enabled'));
  138. },
  139. enableSoftLimit(listId) {
  140. check(listId, String);
  141. const list = Lists.findOne({ _id: listId });
  142. list.toggleSoftLimit(!list.getWipLimit('soft'));
  143. },
  144. });
  145. Lists.hookOptions.after.update = { fetchPrevious: false };
  146. if (Meteor.isServer) {
  147. Meteor.startup(() => {
  148. Lists._collection._ensureIndex({ boardId: 1 });
  149. });
  150. Lists.after.insert((userId, doc) => {
  151. Activities.insert({
  152. userId,
  153. type: 'list',
  154. activityType: 'createList',
  155. boardId: doc.boardId,
  156. listId: doc._id,
  157. });
  158. });
  159. Lists.before.remove((userId, doc) => {
  160. Activities.insert({
  161. userId,
  162. type: 'list',
  163. activityType: 'removeList',
  164. boardId: doc.boardId,
  165. listId: doc._id,
  166. title: doc.title,
  167. });
  168. });
  169. Lists.after.update((userId, doc) => {
  170. if (doc.archived) {
  171. Activities.insert({
  172. userId,
  173. type: 'list',
  174. activityType: 'archivedList',
  175. listId: doc._id,
  176. boardId: doc.boardId,
  177. });
  178. }
  179. });
  180. }
  181. //LISTS REST API
  182. if (Meteor.isServer) {
  183. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function (req, res) {
  184. try {
  185. const paramBoardId = req.params.boardId;
  186. Authentication.checkBoardAccess( req.userId, paramBoardId);
  187. JsonRoutes.sendResult(res, {
  188. code: 200,
  189. data: Lists.find({ boardId: paramBoardId, archived: false }).map(function (doc) {
  190. return {
  191. _id: doc._id,
  192. title: doc.title,
  193. };
  194. }),
  195. });
  196. }
  197. catch (error) {
  198. JsonRoutes.sendResult(res, {
  199. code: 200,
  200. data: error,
  201. });
  202. }
  203. });
  204. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function (req, res) {
  205. try {
  206. const paramBoardId = req.params.boardId;
  207. const paramListId = req.params.listId;
  208. Authentication.checkBoardAccess( req.userId, paramBoardId);
  209. JsonRoutes.sendResult(res, {
  210. code: 200,
  211. data: Lists.findOne({ _id: paramListId, boardId: paramBoardId, archived: false }),
  212. });
  213. }
  214. catch (error) {
  215. JsonRoutes.sendResult(res, {
  216. code: 200,
  217. data: error,
  218. });
  219. }
  220. });
  221. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function (req, res) {
  222. try {
  223. Authentication.checkUserId( req.userId);
  224. const paramBoardId = req.params.boardId;
  225. const id = Lists.insert({
  226. title: req.body.title,
  227. boardId: paramBoardId,
  228. });
  229. JsonRoutes.sendResult(res, {
  230. code: 200,
  231. data: {
  232. _id: id,
  233. },
  234. });
  235. }
  236. catch (error) {
  237. JsonRoutes.sendResult(res, {
  238. code: 200,
  239. data: error,
  240. });
  241. }
  242. });
  243. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function (req, res) {
  244. try {
  245. Authentication.checkUserId( req.userId);
  246. const paramBoardId = req.params.boardId;
  247. const paramListId = req.params.listId;
  248. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  249. JsonRoutes.sendResult(res, {
  250. code: 200,
  251. data: {
  252. _id: paramListId,
  253. },
  254. });
  255. }
  256. catch (error) {
  257. JsonRoutes.sendResult(res, {
  258. code: 200,
  259. data: error,
  260. });
  261. }
  262. });
  263. }