lists.js 13 KB

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