lists.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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.archivedLists = () => {
  324. return Lists.find({ archived: true });
  325. };
  326. Lists.archivedListIds = () => {
  327. return Lists.archivedLists().map(list => {
  328. return list._id;
  329. });
  330. };
  331. Meteor.methods({
  332. applyWipLimit(listId, limit) {
  333. check(listId, String);
  334. check(limit, Number);
  335. if (limit === 0) {
  336. limit = 1;
  337. }
  338. Lists.findOne({ _id: listId }).setWipLimit(limit);
  339. },
  340. enableWipLimit(listId) {
  341. check(listId, String);
  342. const list = Lists.findOne({ _id: listId });
  343. if (list.getWipLimit('value') === 0) {
  344. list.setWipLimit(1);
  345. }
  346. list.toggleWipLimit(!list.getWipLimit('enabled'));
  347. },
  348. enableSoftLimit(listId) {
  349. check(listId, String);
  350. const list = Lists.findOne({ _id: listId });
  351. list.toggleSoftLimit(!list.getWipLimit('soft'));
  352. },
  353. myLists() {
  354. // my lists
  355. return _.uniq(
  356. Lists.find(
  357. {
  358. boardId: { $in: Boards.userBoardIds(this.userId) },
  359. archived: false,
  360. },
  361. {
  362. fields: { title: 1 },
  363. },
  364. )
  365. .fetch()
  366. .map(list => {
  367. return list.title;
  368. }),
  369. ).sort();
  370. },
  371. });
  372. Lists.hookOptions.after.update = { fetchPrevious: false };
  373. if (Meteor.isServer) {
  374. Meteor.startup(() => {
  375. Lists._collection._ensureIndex({ modifiedAt: -1 });
  376. Lists._collection._ensureIndex({ boardId: 1 });
  377. Lists._collection._ensureIndex({ archivedAt: -1 });
  378. });
  379. Lists.after.insert((userId, doc) => {
  380. Activities.insert({
  381. userId,
  382. type: 'list',
  383. activityType: 'createList',
  384. boardId: doc.boardId,
  385. listId: doc._id,
  386. // this preserves the name so that the activity can be useful after the
  387. // list is deleted
  388. title: doc.title,
  389. });
  390. });
  391. Lists.before.remove((userId, doc) => {
  392. const cards = Cards.find({ listId: doc._id });
  393. if (cards) {
  394. cards.forEach(card => {
  395. Cards.remove(card._id);
  396. });
  397. }
  398. Activities.insert({
  399. userId,
  400. type: 'list',
  401. activityType: 'removeList',
  402. boardId: doc.boardId,
  403. listId: doc._id,
  404. title: doc.title,
  405. });
  406. });
  407. Lists.after.update((userId, doc) => {
  408. if (doc.archived) {
  409. Activities.insert({
  410. userId,
  411. type: 'list',
  412. activityType: 'archivedList',
  413. listId: doc._id,
  414. boardId: doc.boardId,
  415. // this preserves the name so that the activity can be useful after the
  416. // list is deleted
  417. title: doc.title,
  418. });
  419. }
  420. });
  421. }
  422. //LISTS REST API
  423. if (Meteor.isServer) {
  424. /**
  425. * @operation get_all_lists
  426. * @summary Get the list of Lists attached to a board
  427. *
  428. * @param {string} boardId the board ID
  429. * @return_type [{_id: string,
  430. * title: string}]
  431. */
  432. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function(req, res) {
  433. try {
  434. const paramBoardId = req.params.boardId;
  435. Authentication.checkBoardAccess(req.userId, paramBoardId);
  436. JsonRoutes.sendResult(res, {
  437. code: 200,
  438. data: Lists.find({ boardId: paramBoardId, archived: false }).map(
  439. function(doc) {
  440. return {
  441. _id: doc._id,
  442. title: doc.title,
  443. };
  444. },
  445. ),
  446. });
  447. } catch (error) {
  448. JsonRoutes.sendResult(res, {
  449. code: 200,
  450. data: error,
  451. });
  452. }
  453. });
  454. /**
  455. * @operation get_list
  456. * @summary Get a List attached to a board
  457. *
  458. * @param {string} boardId the board ID
  459. * @param {string} listId the List ID
  460. * @return_type Lists
  461. */
  462. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function(
  463. req,
  464. res,
  465. ) {
  466. try {
  467. const paramBoardId = req.params.boardId;
  468. const paramListId = req.params.listId;
  469. Authentication.checkBoardAccess(req.userId, paramBoardId);
  470. JsonRoutes.sendResult(res, {
  471. code: 200,
  472. data: Lists.findOne({
  473. _id: paramListId,
  474. boardId: paramBoardId,
  475. archived: false,
  476. }),
  477. });
  478. } catch (error) {
  479. JsonRoutes.sendResult(res, {
  480. code: 200,
  481. data: error,
  482. });
  483. }
  484. });
  485. /**
  486. * @operation new_list
  487. * @summary Add a List to a board
  488. *
  489. * @param {string} boardId the board ID
  490. * @param {string} title the title of the List
  491. * @return_type {_id: string}
  492. */
  493. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function(req, res) {
  494. try {
  495. Authentication.checkUserId(req.userId);
  496. const paramBoardId = req.params.boardId;
  497. const board = Boards.findOne(paramBoardId);
  498. const id = Lists.insert({
  499. title: req.body.title,
  500. boardId: paramBoardId,
  501. sort: board.lists().count(),
  502. });
  503. JsonRoutes.sendResult(res, {
  504. code: 200,
  505. data: {
  506. _id: id,
  507. },
  508. });
  509. } catch (error) {
  510. JsonRoutes.sendResult(res, {
  511. code: 200,
  512. data: error,
  513. });
  514. }
  515. });
  516. /**
  517. * @operation delete_list
  518. * @summary Delete a List
  519. *
  520. * @description This **deletes** a list from a board.
  521. * The list is not put in the recycle bin.
  522. *
  523. * @param {string} boardId the board ID
  524. * @param {string} listId the ID of the list to remove
  525. * @return_type {_id: string}
  526. */
  527. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function(
  528. req,
  529. res,
  530. ) {
  531. try {
  532. Authentication.checkUserId(req.userId);
  533. const paramBoardId = req.params.boardId;
  534. const paramListId = req.params.listId;
  535. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  536. JsonRoutes.sendResult(res, {
  537. code: 200,
  538. data: {
  539. _id: paramListId,
  540. },
  541. });
  542. } catch (error) {
  543. JsonRoutes.sendResult(res, {
  544. code: 200,
  545. data: error,
  546. });
  547. }
  548. });
  549. }
  550. export default Lists;