lists.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. Lists = new Mongo.Collection('lists');
  2. /**
  3. * A list (column) in the Wekan board.
  4. */
  5. Lists.attachSchema(
  6. new SimpleSchema({
  7. title: {
  8. /**
  9. * the title of the list
  10. */
  11. type: String,
  12. },
  13. archived: {
  14. /**
  15. * is the list archived
  16. */
  17. type: Boolean,
  18. // eslint-disable-next-line consistent-return
  19. autoValue() {
  20. if (this.isInsert && !this.isSet) {
  21. return false;
  22. }
  23. },
  24. },
  25. boardId: {
  26. /**
  27. * the board associated to this list
  28. */
  29. type: String,
  30. },
  31. swimlaneId: {
  32. /**
  33. * the swimlane associated to this list. Used for templates
  34. */
  35. type: String,
  36. defaultValue: '',
  37. },
  38. createdAt: {
  39. /**
  40. * creation date
  41. */
  42. type: Date,
  43. // eslint-disable-next-line consistent-return
  44. autoValue() {
  45. if (this.isInsert) {
  46. return new Date();
  47. } else if (this.isUpsert) {
  48. return { $setOnInsert: new Date() };
  49. } else {
  50. this.unset();
  51. }
  52. },
  53. },
  54. sort: {
  55. /**
  56. * is the list sorted
  57. */
  58. type: Number,
  59. decimal: true,
  60. // XXX We should probably provide a default
  61. optional: true,
  62. },
  63. updatedAt: {
  64. /**
  65. * last update of the list
  66. */
  67. type: Date,
  68. optional: true,
  69. // eslint-disable-next-line consistent-return
  70. autoValue() {
  71. if (this.isUpdate || this.isUpsert || this.isInsert) {
  72. return new Date();
  73. } else {
  74. this.unset();
  75. }
  76. },
  77. },
  78. modifiedAt: {
  79. type: Date,
  80. denyUpdate: false,
  81. // eslint-disable-next-line consistent-return
  82. autoValue() {
  83. if (this.isInsert || this.isUpsert || this.isUpdate) {
  84. return new Date();
  85. } else {
  86. this.unset();
  87. }
  88. },
  89. },
  90. wipLimit: {
  91. /**
  92. * WIP object, see below
  93. */
  94. type: Object,
  95. optional: true,
  96. },
  97. 'wipLimit.value': {
  98. /**
  99. * value of the WIP
  100. */
  101. type: Number,
  102. decimal: false,
  103. defaultValue: 1,
  104. },
  105. 'wipLimit.enabled': {
  106. /**
  107. * is the WIP enabled
  108. */
  109. type: Boolean,
  110. defaultValue: false,
  111. },
  112. 'wipLimit.soft': {
  113. /**
  114. * is the WIP a soft or hard requirement
  115. */
  116. type: Boolean,
  117. defaultValue: false,
  118. },
  119. color: {
  120. /**
  121. * the color of the list
  122. */
  123. type: String,
  124. optional: true,
  125. // silver is the default, so it is left out
  126. allowedValues: [
  127. 'white',
  128. 'green',
  129. 'yellow',
  130. 'orange',
  131. 'red',
  132. 'purple',
  133. 'blue',
  134. 'sky',
  135. 'lime',
  136. 'pink',
  137. 'black',
  138. 'peachpuff',
  139. 'crimson',
  140. 'plum',
  141. 'darkgreen',
  142. 'slateblue',
  143. 'magenta',
  144. 'gold',
  145. 'navy',
  146. 'gray',
  147. 'saddlebrown',
  148. 'paleturquoise',
  149. 'mistyrose',
  150. 'indigo',
  151. ],
  152. },
  153. type: {
  154. /**
  155. * The type of list
  156. */
  157. type: String,
  158. defaultValue: 'list',
  159. },
  160. }),
  161. );
  162. Lists.allow({
  163. insert(userId, doc) {
  164. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  165. },
  166. update(userId, doc) {
  167. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  168. },
  169. remove(userId, doc) {
  170. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  171. },
  172. fetch: ['boardId'],
  173. });
  174. Lists.helpers({
  175. copy(boardId, swimlaneId) {
  176. const oldId = this._id;
  177. const oldSwimlaneId = this.swimlaneId || null;
  178. this.boardId = boardId;
  179. this.swimlaneId = swimlaneId;
  180. let _id = null;
  181. existingListWithSameName = Lists.findOne({
  182. boardId,
  183. title: this.title,
  184. archived: false,
  185. });
  186. if (existingListWithSameName) {
  187. _id = existingListWithSameName._id;
  188. } else {
  189. delete this._id;
  190. delete this.swimlaneId;
  191. _id = Lists.insert(this);
  192. }
  193. // Copy all cards in list
  194. Cards.find({
  195. swimlaneId: oldSwimlaneId,
  196. listId: oldId,
  197. archived: false,
  198. }).forEach(card => {
  199. card.copy(boardId, swimlaneId, _id);
  200. });
  201. },
  202. cards(swimlaneId) {
  203. const selector = {
  204. listId: this._id,
  205. archived: false,
  206. };
  207. if (swimlaneId) selector.swimlaneId = swimlaneId;
  208. return Cards.find(Filter.mongoSelector(selector), { sort: ['sort'] });
  209. },
  210. cardsUnfiltered(swimlaneId) {
  211. const selector = {
  212. listId: this._id,
  213. archived: false,
  214. };
  215. if (swimlaneId) selector.swimlaneId = swimlaneId;
  216. return Cards.find(selector, { sort: ['sort'] });
  217. },
  218. allCards() {
  219. return Cards.find({ listId: this._id });
  220. },
  221. board() {
  222. return Boards.findOne(this.boardId);
  223. },
  224. getWipLimit(option) {
  225. const list = Lists.findOne({ _id: this._id });
  226. if (!list.wipLimit) {
  227. // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
  228. return 0;
  229. } else if (!option) {
  230. return list.wipLimit;
  231. } else {
  232. 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
  233. }
  234. },
  235. colorClass() {
  236. if (this.color) return this.color;
  237. return '';
  238. },
  239. isTemplateList() {
  240. return this.type === 'template-list';
  241. },
  242. remove() {
  243. Lists.remove({ _id: this._id });
  244. },
  245. });
  246. Lists.mutations({
  247. rename(title) {
  248. return { $set: { title } };
  249. },
  250. archive() {
  251. if (this.isTemplateList()) {
  252. this.cards().forEach(card => {
  253. return card.archive();
  254. });
  255. }
  256. return { $set: { archived: true } };
  257. },
  258. restore() {
  259. if (this.isTemplateList()) {
  260. this.allCards().forEach(card => {
  261. return card.restore();
  262. });
  263. }
  264. return { $set: { archived: false } };
  265. },
  266. toggleSoftLimit(toggle) {
  267. return { $set: { 'wipLimit.soft': toggle } };
  268. },
  269. toggleWipLimit(toggle) {
  270. return { $set: { 'wipLimit.enabled': toggle } };
  271. },
  272. setWipLimit(limit) {
  273. return { $set: { 'wipLimit.value': limit } };
  274. },
  275. setColor(newColor) {
  276. if (newColor === 'silver') {
  277. newColor = null;
  278. }
  279. return {
  280. $set: {
  281. color: newColor,
  282. },
  283. };
  284. },
  285. });
  286. Meteor.methods({
  287. applyWipLimit(listId, limit) {
  288. check(listId, String);
  289. check(limit, Number);
  290. if (limit === 0) {
  291. limit = 1;
  292. }
  293. Lists.findOne({ _id: listId }).setWipLimit(limit);
  294. },
  295. enableWipLimit(listId) {
  296. check(listId, String);
  297. const list = Lists.findOne({ _id: listId });
  298. if (list.getWipLimit('value') === 0) {
  299. list.setWipLimit(1);
  300. }
  301. list.toggleWipLimit(!list.getWipLimit('enabled'));
  302. },
  303. enableSoftLimit(listId) {
  304. check(listId, String);
  305. const list = Lists.findOne({ _id: listId });
  306. list.toggleSoftLimit(!list.getWipLimit('soft'));
  307. },
  308. });
  309. Lists.hookOptions.after.update = { fetchPrevious: false };
  310. if (Meteor.isServer) {
  311. Meteor.startup(() => {
  312. Lists._collection._ensureIndex({ modifiedAt: -1 });
  313. Lists._collection._ensureIndex({ boardId: 1 });
  314. });
  315. Lists.after.insert((userId, doc) => {
  316. Activities.insert({
  317. userId,
  318. type: 'list',
  319. activityType: 'createList',
  320. boardId: doc.boardId,
  321. listId: doc._id,
  322. });
  323. });
  324. Lists.before.remove((userId, doc) => {
  325. const cards = Cards.find({ listId: doc._id });
  326. if (cards) {
  327. cards.forEach(card => {
  328. Cards.remove(card._id);
  329. });
  330. }
  331. Activities.insert({
  332. userId,
  333. type: 'list',
  334. activityType: 'removeList',
  335. boardId: doc.boardId,
  336. listId: doc._id,
  337. title: doc.title,
  338. });
  339. });
  340. Lists.after.update((userId, doc) => {
  341. if (doc.archived) {
  342. Activities.insert({
  343. userId,
  344. type: 'list',
  345. activityType: 'archivedList',
  346. listId: doc._id,
  347. boardId: doc.boardId,
  348. });
  349. }
  350. });
  351. }
  352. //LISTS REST API
  353. if (Meteor.isServer) {
  354. /**
  355. * @operation get_all_lists
  356. * @summary Get the list of Lists attached to a board
  357. *
  358. * @param {string} boardId the board ID
  359. * @return_type [{_id: string,
  360. * title: string}]
  361. */
  362. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function(req, res) {
  363. try {
  364. const paramBoardId = req.params.boardId;
  365. Authentication.checkBoardAccess(req.userId, paramBoardId);
  366. JsonRoutes.sendResult(res, {
  367. code: 200,
  368. data: Lists.find({ boardId: paramBoardId, archived: false }).map(
  369. function(doc) {
  370. return {
  371. _id: doc._id,
  372. title: doc.title,
  373. };
  374. },
  375. ),
  376. });
  377. } catch (error) {
  378. JsonRoutes.sendResult(res, {
  379. code: 200,
  380. data: error,
  381. });
  382. }
  383. });
  384. /**
  385. * @operation get_list
  386. * @summary Get a List attached to a board
  387. *
  388. * @param {string} boardId the board ID
  389. * @param {string} listId the List ID
  390. * @return_type Lists
  391. */
  392. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function(
  393. req,
  394. res,
  395. ) {
  396. try {
  397. const paramBoardId = req.params.boardId;
  398. const paramListId = req.params.listId;
  399. Authentication.checkBoardAccess(req.userId, paramBoardId);
  400. JsonRoutes.sendResult(res, {
  401. code: 200,
  402. data: Lists.findOne({
  403. _id: paramListId,
  404. boardId: paramBoardId,
  405. archived: false,
  406. }),
  407. });
  408. } catch (error) {
  409. JsonRoutes.sendResult(res, {
  410. code: 200,
  411. data: error,
  412. });
  413. }
  414. });
  415. /**
  416. * @operation new_list
  417. * @summary Add a List to a board
  418. *
  419. * @param {string} boardId the board ID
  420. * @param {string} title the title of the List
  421. * @return_type {_id: string}
  422. */
  423. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function(req, res) {
  424. try {
  425. Authentication.checkUserId(req.userId);
  426. const paramBoardId = req.params.boardId;
  427. const board = Boards.findOne(paramBoardId);
  428. const id = Lists.insert({
  429. title: req.body.title,
  430. boardId: paramBoardId,
  431. sort: board.lists().count(),
  432. });
  433. JsonRoutes.sendResult(res, {
  434. code: 200,
  435. data: {
  436. _id: id,
  437. },
  438. });
  439. } catch (error) {
  440. JsonRoutes.sendResult(res, {
  441. code: 200,
  442. data: error,
  443. });
  444. }
  445. });
  446. /**
  447. * @operation delete_list
  448. * @summary Delete a List
  449. *
  450. * @description This **deletes** a list from a board.
  451. * The list is not put in the recycle bin.
  452. *
  453. * @param {string} boardId the board ID
  454. * @param {string} listId the ID of the list to remove
  455. * @return_type {_id: string}
  456. */
  457. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function(
  458. req,
  459. res,
  460. ) {
  461. try {
  462. Authentication.checkUserId(req.userId);
  463. const paramBoardId = req.params.boardId;
  464. const paramListId = req.params.listId;
  465. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  466. JsonRoutes.sendResult(res, {
  467. code: 200,
  468. data: {
  469. _id: paramListId,
  470. },
  471. });
  472. } catch (error) {
  473. JsonRoutes.sendResult(res, {
  474. code: 200,
  475. data: error,
  476. });
  477. }
  478. });
  479. }
  480. export default Lists;