lists.js 14 KB

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