lists.js 6.3 KB

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