lists.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. Lists = new Mongo.Collection('lists');
  2. /**
  3. * A list (column) in the Wekan board.
  4. */
  5. Lists.attachSchema(new SimpleSchema({
  6. title: {
  7. /**
  8. * the title of the list
  9. */
  10. type: String,
  11. },
  12. archived: {
  13. /**
  14. * is the list archived
  15. */
  16. type: Boolean,
  17. autoValue() { // eslint-disable-line consistent-return
  18. if (this.isInsert && !this.isSet) {
  19. return false;
  20. }
  21. },
  22. },
  23. boardId: {
  24. /**
  25. * the board associated to this list
  26. */
  27. type: String,
  28. },
  29. createdAt: {
  30. /**
  31. * creation date
  32. */
  33. type: Date,
  34. autoValue() { // eslint-disable-line consistent-return
  35. if (this.isInsert) {
  36. return new Date();
  37. } else {
  38. this.unset();
  39. }
  40. },
  41. },
  42. sort: {
  43. /**
  44. * is the list sorted
  45. */
  46. type: Number,
  47. decimal: true,
  48. // XXX We should probably provide a default
  49. optional: true,
  50. },
  51. updatedAt: {
  52. /**
  53. * last update of the list
  54. */
  55. type: Date,
  56. optional: true,
  57. autoValue() { // eslint-disable-line consistent-return
  58. if (this.isUpdate) {
  59. return new Date();
  60. } else {
  61. this.unset();
  62. }
  63. },
  64. },
  65. wipLimit: {
  66. /**
  67. * WIP object, see below
  68. */
  69. type: Object,
  70. optional: true,
  71. },
  72. 'wipLimit.value': {
  73. /**
  74. * value of the WIP
  75. */
  76. type: Number,
  77. decimal: false,
  78. defaultValue: 1,
  79. },
  80. 'wipLimit.enabled': {
  81. /**
  82. * is the WIP enabled
  83. */
  84. type: Boolean,
  85. defaultValue: false,
  86. },
  87. 'wipLimit.soft': {
  88. /**
  89. * is the WIP a soft or hard requirement
  90. */
  91. type: Boolean,
  92. defaultValue: false,
  93. },
  94. }));
  95. Lists.allow({
  96. insert(userId, doc) {
  97. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  98. },
  99. update(userId, doc) {
  100. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  101. },
  102. remove(userId, doc) {
  103. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  104. },
  105. fetch: ['boardId'],
  106. });
  107. Lists.helpers({
  108. cards(swimlaneId) {
  109. const selector = {
  110. listId: this._id,
  111. archived: false,
  112. };
  113. if (swimlaneId)
  114. selector.swimlaneId = swimlaneId;
  115. return Cards.find(Filter.mongoSelector(selector),
  116. { sort: ['sort'] });
  117. },
  118. cardsUnfiltered(swimlaneId) {
  119. const selector = {
  120. listId: this._id,
  121. archived: false,
  122. };
  123. if (swimlaneId)
  124. selector.swimlaneId = swimlaneId;
  125. return Cards.find(selector,
  126. { sort: ['sort'] });
  127. },
  128. allCards() {
  129. return Cards.find({ listId: this._id });
  130. },
  131. board() {
  132. return Boards.findOne(this.boardId);
  133. },
  134. getWipLimit(option){
  135. const list = Lists.findOne({ _id: this._id });
  136. if(!list.wipLimit) { // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
  137. return 0;
  138. } else if(!option) {
  139. return list.wipLimit;
  140. } else {
  141. 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
  142. }
  143. },
  144. });
  145. Lists.mutations({
  146. rename(title) {
  147. return { $set: { title } };
  148. },
  149. archive() {
  150. return { $set: { archived: true } };
  151. },
  152. restore() {
  153. return { $set: { archived: false } };
  154. },
  155. toggleSoftLimit(toggle) {
  156. return { $set: { 'wipLimit.soft': toggle } };
  157. },
  158. toggleWipLimit(toggle) {
  159. return { $set: { 'wipLimit.enabled': toggle } };
  160. },
  161. setWipLimit(limit) {
  162. return { $set: { 'wipLimit.value': limit } };
  163. },
  164. });
  165. Meteor.methods({
  166. applyWipLimit(listId, limit){
  167. check(listId, String);
  168. check(limit, Number);
  169. if(limit === 0){
  170. limit = 1;
  171. }
  172. Lists.findOne({ _id: listId }).setWipLimit(limit);
  173. },
  174. enableWipLimit(listId) {
  175. check(listId, String);
  176. const list = Lists.findOne({ _id: listId });
  177. if(list.getWipLimit('value') === 0){
  178. list.setWipLimit(1);
  179. }
  180. list.toggleWipLimit(!list.getWipLimit('enabled'));
  181. },
  182. enableSoftLimit(listId) {
  183. check(listId, String);
  184. const list = Lists.findOne({ _id: listId });
  185. list.toggleSoftLimit(!list.getWipLimit('soft'));
  186. },
  187. });
  188. Lists.hookOptions.after.update = { fetchPrevious: false };
  189. if (Meteor.isServer) {
  190. Meteor.startup(() => {
  191. Lists._collection._ensureIndex({ boardId: 1 });
  192. });
  193. Lists.after.insert((userId, doc) => {
  194. Activities.insert({
  195. userId,
  196. type: 'list',
  197. activityType: 'createList',
  198. boardId: doc.boardId,
  199. listId: doc._id,
  200. });
  201. });
  202. Lists.before.remove((userId, doc) => {
  203. Activities.insert({
  204. userId,
  205. type: 'list',
  206. activityType: 'removeList',
  207. boardId: doc.boardId,
  208. listId: doc._id,
  209. title: doc.title,
  210. });
  211. });
  212. Lists.after.update((userId, doc) => {
  213. if (doc.archived) {
  214. Activities.insert({
  215. userId,
  216. type: 'list',
  217. activityType: 'archivedList',
  218. listId: doc._id,
  219. boardId: doc.boardId,
  220. });
  221. }
  222. });
  223. }
  224. //LISTS REST API
  225. if (Meteor.isServer) {
  226. /**
  227. * @operation get_all_lists
  228. * @summary Get the list of Lists attached to a board
  229. *
  230. * @param {string} boardId the board ID
  231. * @return_type [{_id: string,
  232. * title: string}]
  233. */
  234. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function (req, res) {
  235. try {
  236. const paramBoardId = req.params.boardId;
  237. Authentication.checkBoardAccess( req.userId, paramBoardId);
  238. JsonRoutes.sendResult(res, {
  239. code: 200,
  240. data: Lists.find({ boardId: paramBoardId, archived: false }).map(function (doc) {
  241. return {
  242. _id: doc._id,
  243. title: doc.title,
  244. };
  245. }),
  246. });
  247. }
  248. catch (error) {
  249. JsonRoutes.sendResult(res, {
  250. code: 200,
  251. data: error,
  252. });
  253. }
  254. });
  255. /**
  256. * @operation get_list
  257. * @summary Get a List attached to a board
  258. *
  259. * @param {string} boardId the board ID
  260. * @param {string} listId the List ID
  261. * @return_type Lists
  262. */
  263. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function (req, res) {
  264. try {
  265. const paramBoardId = req.params.boardId;
  266. const paramListId = req.params.listId;
  267. Authentication.checkBoardAccess( req.userId, paramBoardId);
  268. JsonRoutes.sendResult(res, {
  269. code: 200,
  270. data: Lists.findOne({ _id: paramListId, boardId: paramBoardId, archived: false }),
  271. });
  272. }
  273. catch (error) {
  274. JsonRoutes.sendResult(res, {
  275. code: 200,
  276. data: error,
  277. });
  278. }
  279. });
  280. /**
  281. * @operation new_list
  282. * @summary Add a List to a board
  283. *
  284. * @param {string} boardId the board ID
  285. * @param {string} title the title of the List
  286. * @return_type {_id: string}
  287. */
  288. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function (req, res) {
  289. try {
  290. Authentication.checkUserId( req.userId);
  291. const paramBoardId = req.params.boardId;
  292. const board = Boards.findOne(paramBoardId);
  293. const id = Lists.insert({
  294. title: req.body.title,
  295. boardId: paramBoardId,
  296. sort: board.lists().count(),
  297. });
  298. JsonRoutes.sendResult(res, {
  299. code: 200,
  300. data: {
  301. _id: id,
  302. },
  303. });
  304. }
  305. catch (error) {
  306. JsonRoutes.sendResult(res, {
  307. code: 200,
  308. data: error,
  309. });
  310. }
  311. });
  312. /**
  313. * @operation delete_list
  314. * @summary Delete a List
  315. *
  316. * @description This **deletes** a list from a board.
  317. * The list is not put in the recycle bin.
  318. *
  319. * @param {string} boardId the board ID
  320. * @param {string} listId the ID of the list to remove
  321. * @return_type {_id: string}
  322. */
  323. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function (req, res) {
  324. try {
  325. Authentication.checkUserId( req.userId);
  326. const paramBoardId = req.params.boardId;
  327. const paramListId = req.params.listId;
  328. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  329. JsonRoutes.sendResult(res, {
  330. code: 200,
  331. data: {
  332. _id: paramListId,
  333. },
  334. });
  335. }
  336. catch (error) {
  337. JsonRoutes.sendResult(res, {
  338. code: 200,
  339. data: error,
  340. });
  341. }
  342. });
  343. }