lists.js 13 KB

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