lists.js 14 KB

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