lists.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. }),
  158. );
  159. Lists.allow({
  160. insert(userId, doc) {
  161. return allowIsBoardMemberCommentOnly(userId, ReactiveCache.getBoard(doc.boardId));
  162. },
  163. update(userId, doc) {
  164. return allowIsBoardMemberCommentOnly(userId, ReactiveCache.getBoard(doc.boardId));
  165. },
  166. remove(userId, doc) {
  167. return allowIsBoardMemberCommentOnly(userId, ReactiveCache.getBoard(doc.boardId));
  168. },
  169. fetch: ['boardId'],
  170. });
  171. Lists.helpers({
  172. copy(boardId, swimlaneId) {
  173. const oldId = this._id;
  174. const oldSwimlaneId = this.swimlaneId || null;
  175. this.boardId = boardId;
  176. this.swimlaneId = swimlaneId;
  177. let _id = null;
  178. const existingListWithSameName = ReactiveCache.getList({
  179. boardId,
  180. title: this.title,
  181. archived: false,
  182. });
  183. if (existingListWithSameName) {
  184. _id = existingListWithSameName._id;
  185. } else {
  186. delete this._id;
  187. delete this.swimlaneId;
  188. _id = Lists.insert(this);
  189. }
  190. // Copy all cards in list
  191. ReactiveCache.getCards({
  192. swimlaneId: oldSwimlaneId,
  193. listId: oldId,
  194. archived: false,
  195. }).forEach(card => {
  196. card.copy(boardId, swimlaneId, _id);
  197. });
  198. },
  199. move(boardId, swimlaneId) {
  200. const boardList = ReactiveCache.getList({
  201. boardId,
  202. title: this.title,
  203. archived: false,
  204. });
  205. let listId;
  206. if (boardList) {
  207. listId = boardList._id;
  208. this.cards().forEach(card => {
  209. card.move(boardId, this._id, boardList._id);
  210. });
  211. } else {
  212. console.log('list.title:', this.title);
  213. console.log('boardList:', boardList);
  214. listId = Lists.insert({
  215. title: this.title,
  216. boardId,
  217. type: this.type,
  218. archived: false,
  219. wipLimit: this.wipLimit,
  220. });
  221. }
  222. this.cards(swimlaneId).forEach(card => {
  223. card.move(boardId, swimlaneId, listId);
  224. });
  225. },
  226. cards(swimlaneId) {
  227. const selector = {
  228. listId: this._id,
  229. archived: false,
  230. };
  231. if (swimlaneId) selector.swimlaneId = swimlaneId;
  232. const ret = ReactiveCache.getCards(Filter.mongoSelector(selector), { sort: ['sort'] });
  233. return ret;
  234. },
  235. cardsUnfiltered(swimlaneId) {
  236. const selector = {
  237. listId: this._id,
  238. archived: false,
  239. };
  240. if (swimlaneId) selector.swimlaneId = swimlaneId;
  241. const ret = ReactiveCache.getCards(selector, { sort: ['sort'] });
  242. return ret;
  243. },
  244. allCards() {
  245. const ret = ReactiveCache.getCards({ listId: this._id });
  246. return ret;
  247. },
  248. board() {
  249. return ReactiveCache.getBoard(this.boardId);
  250. },
  251. getWipLimit(option) {
  252. const list = ReactiveCache.getList(this._id);
  253. if (!list.wipLimit) {
  254. // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
  255. return 0;
  256. } else if (!option) {
  257. return list.wipLimit;
  258. } else {
  259. 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
  260. }
  261. },
  262. colorClass() {
  263. if (this.color) return `list-header-${this.color}`;
  264. return '';
  265. },
  266. isTemplateList() {
  267. return this.type === 'template-list';
  268. },
  269. isStarred() {
  270. return this.starred === true;
  271. },
  272. absoluteUrl() {
  273. const card = ReactiveCache.getCard({ listId: this._id });
  274. return card && card.absoluteUrl();
  275. },
  276. originRelativeUrl() {
  277. const card = ReactiveCache.getCard({ listId: this._id });
  278. return card && card.originRelativeUrl();
  279. },
  280. remove() {
  281. Lists.remove({ _id: this._id });
  282. },
  283. });
  284. Lists.mutations({
  285. rename(title) {
  286. return { $set: { title } };
  287. },
  288. star(enable = true) {
  289. return { $set: { starred: !!enable } };
  290. },
  291. archive() {
  292. if (this.isTemplateList()) {
  293. this.cards().forEach(card => {
  294. return card.archive();
  295. });
  296. }
  297. return { $set: { archived: true, archivedAt: new Date() } };
  298. },
  299. restore() {
  300. if (this.isTemplateList()) {
  301. this.allCards().forEach(card => {
  302. return card.restore();
  303. });
  304. }
  305. return { $set: { archived: false } };
  306. },
  307. toggleSoftLimit(toggle) {
  308. return { $set: { 'wipLimit.soft': toggle } };
  309. },
  310. toggleWipLimit(toggle) {
  311. return { $set: { 'wipLimit.enabled': toggle } };
  312. },
  313. setWipLimit(limit) {
  314. return { $set: { 'wipLimit.value': limit } };
  315. },
  316. setColor(newColor) {
  317. return {
  318. $set: {
  319. color: newColor,
  320. },
  321. };
  322. },
  323. });
  324. Lists.userArchivedLists = userId => {
  325. return ReactiveCache.getLists({
  326. boardId: { $in: Boards.userBoardIds(userId, null) },
  327. archived: true,
  328. })
  329. };
  330. Lists.userArchivedListIds = () => {
  331. return Lists.userArchivedLists().map(list => { return list._id; });
  332. };
  333. Lists.archivedLists = () => {
  334. return ReactiveCache.getLists({ archived: true });
  335. };
  336. Lists.archivedListIds = () => {
  337. return Lists.archivedLists().map(list => {
  338. return list._id;
  339. });
  340. };
  341. Meteor.methods({
  342. applyWipLimit(listId, limit) {
  343. check(listId, String);
  344. check(limit, Number);
  345. if (limit === 0) {
  346. limit = 1;
  347. }
  348. ReactiveCache.getList(listId).setWipLimit(limit);
  349. },
  350. enableWipLimit(listId) {
  351. check(listId, String);
  352. const list = ReactiveCache.getList(listId);
  353. if (list.getWipLimit('value') === 0) {
  354. list.setWipLimit(1);
  355. }
  356. list.toggleWipLimit(!list.getWipLimit('enabled'));
  357. },
  358. enableSoftLimit(listId) {
  359. check(listId, String);
  360. const list = ReactiveCache.getList(listId);
  361. list.toggleSoftLimit(!list.getWipLimit('soft'));
  362. },
  363. myLists() {
  364. // my lists
  365. return _.uniq(
  366. ReactiveCache.getLists(
  367. {
  368. boardId: { $in: Boards.userBoardIds(this.userId) },
  369. archived: false,
  370. },
  371. {
  372. fields: { title: 1 },
  373. },
  374. )
  375. .map(list => {
  376. return list.title;
  377. }),
  378. ).sort();
  379. },
  380. });
  381. Lists.hookOptions.after.update = { fetchPrevious: false };
  382. if (Meteor.isServer) {
  383. Meteor.startup(() => {
  384. Lists._collection.createIndex({ modifiedAt: -1 });
  385. Lists._collection.createIndex({ boardId: 1 });
  386. Lists._collection.createIndex({ archivedAt: -1 });
  387. });
  388. Lists.after.insert((userId, doc) => {
  389. Activities.insert({
  390. userId,
  391. type: 'list',
  392. activityType: 'createList',
  393. boardId: doc.boardId,
  394. listId: doc._id,
  395. // this preserves the name so that the activity can be useful after the
  396. // list is deleted
  397. title: doc.title,
  398. });
  399. });
  400. Lists.before.remove((userId, doc) => {
  401. const cards = ReactiveCache.getCards({ listId: doc._id });
  402. if (cards) {
  403. cards.forEach(card => {
  404. Cards.remove(card._id);
  405. });
  406. }
  407. Activities.insert({
  408. userId,
  409. type: 'list',
  410. activityType: 'removeList',
  411. boardId: doc.boardId,
  412. listId: doc._id,
  413. title: doc.title,
  414. });
  415. });
  416. Lists.after.update((userId, doc, fieldNames) => {
  417. if (fieldNames.includes('title')) {
  418. Activities.insert({
  419. userId,
  420. type: 'list',
  421. activityType: 'changedListTitle',
  422. listId: doc._id,
  423. boardId: doc.boardId,
  424. // this preserves the name so that the activity can be useful after the
  425. // list is deleted
  426. title: doc.title,
  427. });
  428. } else if (doc.archived) {
  429. Activities.insert({
  430. userId,
  431. type: 'list',
  432. activityType: 'archivedList',
  433. listId: doc._id,
  434. boardId: doc.boardId,
  435. // this preserves the name so that the activity can be useful after the
  436. // list is deleted
  437. title: doc.title,
  438. });
  439. } else if (fieldNames.includes('archived')) {
  440. Activities.insert({
  441. userId,
  442. type: 'list',
  443. activityType: 'restoredList',
  444. listId: doc._id,
  445. boardId: doc.boardId,
  446. // this preserves the name so that the activity can be useful after the
  447. // list is deleted
  448. title: doc.title,
  449. });
  450. }
  451. });
  452. }
  453. //LISTS REST API
  454. if (Meteor.isServer) {
  455. /**
  456. * @operation get_all_lists
  457. * @summary Get the list of Lists attached to a board
  458. *
  459. * @param {string} boardId the board ID
  460. * @return_type [{_id: string,
  461. * title: string}]
  462. */
  463. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function(req, res) {
  464. try {
  465. const paramBoardId = req.params.boardId;
  466. Authentication.checkBoardAccess(req.userId, paramBoardId);
  467. JsonRoutes.sendResult(res, {
  468. code: 200,
  469. data: ReactiveCache.getLists({ boardId: paramBoardId, archived: false }).map(
  470. function(doc) {
  471. return {
  472. _id: doc._id,
  473. title: doc.title,
  474. };
  475. },
  476. ),
  477. });
  478. } catch (error) {
  479. JsonRoutes.sendResult(res, {
  480. code: 200,
  481. data: error,
  482. });
  483. }
  484. });
  485. /**
  486. * @operation get_list
  487. * @summary Get a List attached to a board
  488. *
  489. * @param {string} boardId the board ID
  490. * @param {string} listId the List ID
  491. * @return_type Lists
  492. */
  493. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function(
  494. req,
  495. res,
  496. ) {
  497. try {
  498. const paramBoardId = req.params.boardId;
  499. const paramListId = req.params.listId;
  500. Authentication.checkBoardAccess(req.userId, paramBoardId);
  501. JsonRoutes.sendResult(res, {
  502. code: 200,
  503. data: ReactiveCache.getList({
  504. _id: paramListId,
  505. boardId: paramBoardId,
  506. archived: false,
  507. }),
  508. });
  509. } catch (error) {
  510. JsonRoutes.sendResult(res, {
  511. code: 200,
  512. data: error,
  513. });
  514. }
  515. });
  516. /**
  517. * @operation new_list
  518. * @summary Add a List to a board
  519. *
  520. * @param {string} boardId the board ID
  521. * @param {string} title the title of the List
  522. * @return_type {_id: string}
  523. */
  524. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function(req, res) {
  525. try {
  526. const paramBoardId = req.params.boardId;
  527. Authentication.checkBoardAccess(req.userId, paramBoardId);
  528. const board = ReactiveCache.getBoard(paramBoardId);
  529. const id = Lists.insert({
  530. title: req.body.title,
  531. boardId: paramBoardId,
  532. sort: board.lists().length,
  533. });
  534. JsonRoutes.sendResult(res, {
  535. code: 200,
  536. data: {
  537. _id: id,
  538. },
  539. });
  540. } catch (error) {
  541. JsonRoutes.sendResult(res, {
  542. code: 200,
  543. data: error,
  544. });
  545. }
  546. });
  547. /**
  548. * @operation delete_list
  549. * @summary Delete a List
  550. *
  551. * @description This **deletes** a list from a board.
  552. * The list is not put in the recycle bin.
  553. *
  554. * @param {string} boardId the board ID
  555. * @param {string} listId the ID of the list to remove
  556. * @return_type {_id: string}
  557. */
  558. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function(
  559. req,
  560. res,
  561. ) {
  562. try {
  563. const paramBoardId = req.params.boardId;
  564. const paramListId = req.params.listId;
  565. Authentication.checkBoardAccess(req.userId, paramBoardId);
  566. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  567. JsonRoutes.sendResult(res, {
  568. code: 200,
  569. data: {
  570. _id: paramListId,
  571. },
  572. });
  573. } catch (error) {
  574. JsonRoutes.sendResult(res, {
  575. code: 200,
  576. data: error,
  577. });
  578. }
  579. });
  580. }
  581. export default Lists;