lists.js 14 KB

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