lists.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. Required for per-swimlane list titles
  52. */
  53. type: String,
  54. // Remove defaultValue to make it required
  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. this.swimlaneId = swimlaneId; // Set the target swimlane for the copied list
  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. swimlaneId: swimlaneId, // Set the target swimlane for the moved list
  228. });
  229. }
  230. this.cards(swimlaneId).forEach(card => {
  231. card.move(boardId, swimlaneId, listId);
  232. });
  233. },
  234. cards(swimlaneId) {
  235. const selector = {
  236. listId: this._id,
  237. archived: false,
  238. };
  239. if (swimlaneId) selector.swimlaneId = swimlaneId;
  240. const ret = ReactiveCache.getCards(Filter.mongoSelector(selector), { sort: ['sort'] });
  241. return ret;
  242. },
  243. cardsUnfiltered(swimlaneId) {
  244. const selector = {
  245. listId: this._id,
  246. archived: false,
  247. };
  248. if (swimlaneId) selector.swimlaneId = swimlaneId;
  249. const ret = ReactiveCache.getCards(selector, { sort: ['sort'] });
  250. return ret;
  251. },
  252. allCards() {
  253. const ret = ReactiveCache.getCards({ listId: this._id });
  254. return ret;
  255. },
  256. board() {
  257. return ReactiveCache.getBoard(this.boardId);
  258. },
  259. getWipLimit(option) {
  260. const list = ReactiveCache.getList(this._id);
  261. if (!list.wipLimit) {
  262. // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
  263. return 0;
  264. } else if (!option) {
  265. return list.wipLimit;
  266. } else {
  267. 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
  268. }
  269. },
  270. colorClass() {
  271. if (this.color) return `list-header-${this.color}`;
  272. return '';
  273. },
  274. isTemplateList() {
  275. return this.type === 'template-list';
  276. },
  277. isStarred() {
  278. return this.starred === true;
  279. },
  280. isCollapsed() {
  281. return this.collapsed === true;
  282. },
  283. absoluteUrl() {
  284. const card = ReactiveCache.getCard({ listId: this._id });
  285. return card && card.absoluteUrl();
  286. },
  287. originRelativeUrl() {
  288. const card = ReactiveCache.getCard({ listId: this._id });
  289. return card && card.originRelativeUrl();
  290. },
  291. remove() {
  292. Lists.remove({ _id: this._id });
  293. },
  294. });
  295. Lists.mutations({
  296. rename(title) {
  297. return { $set: { title } };
  298. },
  299. star(enable = true) {
  300. return { $set: { starred: !!enable } };
  301. },
  302. collapse(enable = true) {
  303. return { $set: { collapsed: !!enable } };
  304. },
  305. archive() {
  306. if (this.isTemplateList()) {
  307. this.cards().forEach(card => {
  308. return card.archive();
  309. });
  310. }
  311. return { $set: { archived: true, archivedAt: new Date() } };
  312. },
  313. restore() {
  314. if (this.isTemplateList()) {
  315. this.allCards().forEach(card => {
  316. return card.restore();
  317. });
  318. }
  319. return { $set: { archived: false } };
  320. },
  321. toggleSoftLimit(toggle) {
  322. return { $set: { 'wipLimit.soft': toggle } };
  323. },
  324. toggleWipLimit(toggle) {
  325. return { $set: { 'wipLimit.enabled': toggle } };
  326. },
  327. setWipLimit(limit) {
  328. return { $set: { 'wipLimit.value': limit } };
  329. },
  330. setColor(newColor) {
  331. return {
  332. $set: {
  333. color: newColor,
  334. },
  335. };
  336. },
  337. });
  338. Lists.userArchivedLists = userId => {
  339. return ReactiveCache.getLists({
  340. boardId: { $in: Boards.userBoardIds(userId, null) },
  341. archived: true,
  342. })
  343. };
  344. Lists.userArchivedListIds = () => {
  345. return Lists.userArchivedLists().map(list => { return list._id; });
  346. };
  347. Lists.archivedLists = () => {
  348. return ReactiveCache.getLists({ archived: true });
  349. };
  350. Lists.archivedListIds = () => {
  351. return Lists.archivedLists().map(list => {
  352. return list._id;
  353. });
  354. };
  355. Meteor.methods({
  356. applyWipLimit(listId, limit) {
  357. check(listId, String);
  358. check(limit, Number);
  359. if (limit === 0) {
  360. limit = 1;
  361. }
  362. ReactiveCache.getList(listId).setWipLimit(limit);
  363. },
  364. enableWipLimit(listId) {
  365. check(listId, String);
  366. const list = ReactiveCache.getList(listId);
  367. if (list.getWipLimit('value') === 0) {
  368. list.setWipLimit(1);
  369. }
  370. list.toggleWipLimit(!list.getWipLimit('enabled'));
  371. },
  372. enableSoftLimit(listId) {
  373. check(listId, String);
  374. const list = ReactiveCache.getList(listId);
  375. list.toggleSoftLimit(!list.getWipLimit('soft'));
  376. },
  377. myLists() {
  378. // my lists
  379. return _.uniq(
  380. ReactiveCache.getLists(
  381. {
  382. boardId: { $in: Boards.userBoardIds(this.userId) },
  383. archived: false,
  384. },
  385. {
  386. fields: { title: 1 },
  387. },
  388. )
  389. .map(list => {
  390. return list.title;
  391. }),
  392. ).sort();
  393. },
  394. });
  395. Lists.hookOptions.after.update = { fetchPrevious: false };
  396. if (Meteor.isServer) {
  397. Meteor.startup(() => {
  398. Lists._collection.createIndex({ modifiedAt: -1 });
  399. Lists._collection.createIndex({ boardId: 1 });
  400. Lists._collection.createIndex({ archivedAt: -1 });
  401. });
  402. Lists.after.insert((userId, doc) => {
  403. Activities.insert({
  404. userId,
  405. type: 'list',
  406. activityType: 'createList',
  407. boardId: doc.boardId,
  408. listId: doc._id,
  409. // this preserves the name so that the activity can be useful after the
  410. // list is deleted
  411. title: doc.title,
  412. });
  413. });
  414. Lists.before.remove((userId, doc) => {
  415. const cards = ReactiveCache.getCards({ listId: doc._id });
  416. if (cards) {
  417. cards.forEach(card => {
  418. Cards.remove(card._id);
  419. });
  420. }
  421. Activities.insert({
  422. userId,
  423. type: 'list',
  424. activityType: 'removeList',
  425. boardId: doc.boardId,
  426. listId: doc._id,
  427. title: doc.title,
  428. });
  429. });
  430. Lists.after.update((userId, doc, fieldNames) => {
  431. if (fieldNames.includes('title')) {
  432. Activities.insert({
  433. userId,
  434. type: 'list',
  435. activityType: 'changedListTitle',
  436. listId: doc._id,
  437. boardId: doc.boardId,
  438. // this preserves the name so that the activity can be useful after the
  439. // list is deleted
  440. title: doc.title,
  441. });
  442. } else if (doc.archived) {
  443. Activities.insert({
  444. userId,
  445. type: 'list',
  446. activityType: 'archivedList',
  447. listId: doc._id,
  448. boardId: doc.boardId,
  449. // this preserves the name so that the activity can be useful after the
  450. // list is deleted
  451. title: doc.title,
  452. });
  453. } else if (fieldNames.includes('archived')) {
  454. Activities.insert({
  455. userId,
  456. type: 'list',
  457. activityType: 'restoredList',
  458. listId: doc._id,
  459. boardId: doc.boardId,
  460. // this preserves the name so that the activity can be useful after the
  461. // list is deleted
  462. title: doc.title,
  463. });
  464. }
  465. });
  466. }
  467. //LISTS REST API
  468. if (Meteor.isServer) {
  469. /**
  470. * @operation get_all_lists
  471. * @summary Get the list of Lists attached to a board
  472. *
  473. * @param {string} boardId the board ID
  474. * @return_type [{_id: string,
  475. * title: string}]
  476. */
  477. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function(req, res) {
  478. try {
  479. const paramBoardId = req.params.boardId;
  480. Authentication.checkBoardAccess(req.userId, paramBoardId);
  481. JsonRoutes.sendResult(res, {
  482. code: 200,
  483. data: ReactiveCache.getLists({ boardId: paramBoardId, archived: false }).map(
  484. function(doc) {
  485. return {
  486. _id: doc._id,
  487. title: doc.title,
  488. };
  489. },
  490. ),
  491. });
  492. } catch (error) {
  493. JsonRoutes.sendResult(res, {
  494. code: 200,
  495. data: error,
  496. });
  497. }
  498. });
  499. /**
  500. * @operation get_list
  501. * @summary Get a List attached to a board
  502. *
  503. * @param {string} boardId the board ID
  504. * @param {string} listId the List ID
  505. * @return_type Lists
  506. */
  507. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function(
  508. req,
  509. res,
  510. ) {
  511. try {
  512. const paramBoardId = req.params.boardId;
  513. const paramListId = req.params.listId;
  514. Authentication.checkBoardAccess(req.userId, paramBoardId);
  515. JsonRoutes.sendResult(res, {
  516. code: 200,
  517. data: ReactiveCache.getList({
  518. _id: paramListId,
  519. boardId: paramBoardId,
  520. archived: false,
  521. }),
  522. });
  523. } catch (error) {
  524. JsonRoutes.sendResult(res, {
  525. code: 200,
  526. data: error,
  527. });
  528. }
  529. });
  530. /**
  531. * @operation new_list
  532. * @summary Add a List to a board
  533. *
  534. * @param {string} boardId the board ID
  535. * @param {string} title the title of the List
  536. * @return_type {_id: string}
  537. */
  538. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function(req, res) {
  539. try {
  540. const paramBoardId = req.params.boardId;
  541. Authentication.checkBoardAccess(req.userId, paramBoardId);
  542. const board = ReactiveCache.getBoard(paramBoardId);
  543. const id = Lists.insert({
  544. title: req.body.title,
  545. boardId: paramBoardId,
  546. sort: board.lists().length,
  547. swimlaneId: req.body.swimlaneId || board.getDefaultSwimline()._id, // Use provided swimlaneId or default
  548. });
  549. JsonRoutes.sendResult(res, {
  550. code: 200,
  551. data: {
  552. _id: id,
  553. },
  554. });
  555. } catch (error) {
  556. JsonRoutes.sendResult(res, {
  557. code: 200,
  558. data: error,
  559. });
  560. }
  561. });
  562. /**
  563. * @operation edit_list
  564. * @summary Edit a List
  565. *
  566. * @description This updates a list on a board.
  567. * You can update the title, color, wipLimit, starred, and collapsed properties.
  568. *
  569. * @param {string} boardId the board ID
  570. * @param {string} listId the ID of the list to update
  571. * @param {string} [title] the new title of the list
  572. * @param {string} [color] the new color of the list
  573. * @param {Object} [wipLimit] the WIP limit configuration
  574. * @param {boolean} [starred] whether the list is starred
  575. * @param {boolean} [collapsed] whether the list is collapsed
  576. * @return_type {_id: string}
  577. */
  578. JsonRoutes.add('PUT', '/api/boards/:boardId/lists/:listId', function(
  579. req,
  580. res,
  581. ) {
  582. try {
  583. const paramBoardId = req.params.boardId;
  584. const paramListId = req.params.listId;
  585. let updated = false;
  586. Authentication.checkBoardAccess(req.userId, paramBoardId);
  587. const list = ReactiveCache.getList({
  588. _id: paramListId,
  589. boardId: paramBoardId,
  590. archived: false,
  591. });
  592. if (!list) {
  593. JsonRoutes.sendResult(res, {
  594. code: 404,
  595. data: { error: 'List not found' },
  596. });
  597. return;
  598. }
  599. // Update title if provided
  600. if (req.body.title) {
  601. const newTitle = req.body.title;
  602. Lists.direct.update(
  603. {
  604. _id: paramListId,
  605. boardId: paramBoardId,
  606. archived: false,
  607. },
  608. {
  609. $set: {
  610. title: newTitle,
  611. },
  612. },
  613. );
  614. updated = true;
  615. }
  616. // Update color if provided
  617. if (req.body.color) {
  618. const newColor = req.body.color;
  619. Lists.direct.update(
  620. {
  621. _id: paramListId,
  622. boardId: paramBoardId,
  623. archived: false,
  624. },
  625. {
  626. $set: {
  627. color: newColor,
  628. },
  629. },
  630. );
  631. updated = true;
  632. }
  633. // Update starred status if provided
  634. if (req.body.hasOwnProperty('starred')) {
  635. const newStarred = req.body.starred;
  636. Lists.direct.update(
  637. {
  638. _id: paramListId,
  639. boardId: paramBoardId,
  640. archived: false,
  641. },
  642. {
  643. $set: {
  644. starred: newStarred,
  645. },
  646. },
  647. );
  648. updated = true;
  649. }
  650. // Update collapsed status if provided
  651. if (req.body.hasOwnProperty('collapsed')) {
  652. const newCollapsed = req.body.collapsed;
  653. Lists.direct.update(
  654. {
  655. _id: paramListId,
  656. boardId: paramBoardId,
  657. archived: false,
  658. },
  659. {
  660. $set: {
  661. collapsed: newCollapsed,
  662. },
  663. },
  664. );
  665. updated = true;
  666. }
  667. // Update wipLimit if provided
  668. if (req.body.wipLimit) {
  669. const newWipLimit = req.body.wipLimit;
  670. Lists.direct.update(
  671. {
  672. _id: paramListId,
  673. boardId: paramBoardId,
  674. archived: false,
  675. },
  676. {
  677. $set: {
  678. wipLimit: newWipLimit,
  679. },
  680. },
  681. );
  682. updated = true;
  683. }
  684. // Check if update is true or false
  685. if (!updated) {
  686. JsonRoutes.sendResult(res, {
  687. code: 404,
  688. data: {
  689. message: 'Error',
  690. },
  691. });
  692. return;
  693. }
  694. JsonRoutes.sendResult(res, {
  695. code: 200,
  696. data: {
  697. _id: paramListId,
  698. },
  699. });
  700. } catch (error) {
  701. JsonRoutes.sendResult(res, {
  702. code: 200,
  703. data: error,
  704. });
  705. }
  706. });
  707. /**
  708. * @operation delete_list
  709. * @summary Delete a List
  710. *
  711. * @description This **deletes** a list from a board.
  712. * The list is not put in the recycle bin.
  713. *
  714. * @param {string} boardId the board ID
  715. * @param {string} listId the ID of the list to remove
  716. * @return_type {_id: string}
  717. */
  718. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function(
  719. req,
  720. res,
  721. ) {
  722. try {
  723. const paramBoardId = req.params.boardId;
  724. const paramListId = req.params.listId;
  725. Authentication.checkBoardAccess(req.userId, paramBoardId);
  726. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  727. JsonRoutes.sendResult(res, {
  728. code: 200,
  729. data: {
  730. _id: paramListId,
  731. },
  732. });
  733. } catch (error) {
  734. JsonRoutes.sendResult(res, {
  735. code: 200,
  736. data: error,
  737. });
  738. }
  739. });
  740. }
  741. export default Lists;