lists.js 6.3 KB

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