lists.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. Lists = new Mongo.Collection('lists');
  2. /**
  3. * A list (column) in the Wekan board.
  4. */
  5. Lists.attachSchema(
  6. new SimpleSchema({
  7. title: {
  8. /**
  9. * the title of the list
  10. */
  11. type: String,
  12. },
  13. starred: {
  14. /**
  15. * if a list is stared
  16. * then we put it on the top
  17. */
  18. type: Boolean,
  19. optional: true,
  20. defaultValue: false,
  21. },
  22. archived: {
  23. /**
  24. * is the list archived
  25. */
  26. type: Boolean,
  27. // eslint-disable-next-line consistent-return
  28. autoValue() {
  29. if (this.isInsert && !this.isSet) {
  30. return false;
  31. }
  32. },
  33. },
  34. boardId: {
  35. /**
  36. * the board associated to this list
  37. */
  38. type: String,
  39. },
  40. swimlaneId: {
  41. /**
  42. * the swimlane associated to this list. Used for templates
  43. */
  44. type: String,
  45. defaultValue: '',
  46. },
  47. createdAt: {
  48. /**
  49. * creation date
  50. */
  51. type: Date,
  52. // eslint-disable-next-line consistent-return
  53. autoValue() {
  54. if (this.isInsert) {
  55. return new Date();
  56. } else if (this.isUpsert) {
  57. return { $setOnInsert: new Date() };
  58. } else {
  59. this.unset();
  60. }
  61. },
  62. },
  63. sort: {
  64. /**
  65. * is the list sorted
  66. */
  67. type: Number,
  68. decimal: true,
  69. // XXX We should probably provide a default
  70. optional: true,
  71. },
  72. updatedAt: {
  73. /**
  74. * last update of the list
  75. */
  76. type: Date,
  77. optional: true,
  78. // eslint-disable-next-line consistent-return
  79. autoValue() {
  80. if (this.isUpdate || this.isUpsert || this.isInsert) {
  81. return new Date();
  82. } else {
  83. this.unset();
  84. }
  85. },
  86. },
  87. modifiedAt: {
  88. type: Date,
  89. denyUpdate: false,
  90. // eslint-disable-next-line consistent-return
  91. autoValue() {
  92. // this is redundant with updatedAt
  93. /*if (this.isInsert || this.isUpsert || this.isUpdate) {
  94. return new Date();
  95. } else {
  96. this.unset();
  97. }*/
  98. if (!this.isSet) {
  99. return new Date();
  100. }
  101. },
  102. },
  103. wipLimit: {
  104. /**
  105. * WIP object, see below
  106. */
  107. type: Object,
  108. optional: true,
  109. },
  110. 'wipLimit.value': {
  111. /**
  112. * value of the WIP
  113. */
  114. type: Number,
  115. decimal: false,
  116. defaultValue: 1,
  117. },
  118. 'wipLimit.enabled': {
  119. /**
  120. * is the WIP enabled
  121. */
  122. type: Boolean,
  123. defaultValue: false,
  124. },
  125. 'wipLimit.soft': {
  126. /**
  127. * is the WIP a soft or hard requirement
  128. */
  129. type: Boolean,
  130. defaultValue: false,
  131. },
  132. color: {
  133. /**
  134. * the color of the list
  135. */
  136. type: String,
  137. optional: true,
  138. // silver is the default, so it is left out
  139. allowedValues: [
  140. 'white',
  141. 'green',
  142. 'yellow',
  143. 'orange',
  144. 'red',
  145. 'purple',
  146. 'blue',
  147. 'sky',
  148. 'lime',
  149. 'pink',
  150. 'black',
  151. 'peachpuff',
  152. 'crimson',
  153. 'plum',
  154. 'darkgreen',
  155. 'slateblue',
  156. 'magenta',
  157. 'gold',
  158. 'navy',
  159. 'gray',
  160. 'saddlebrown',
  161. 'paleturquoise',
  162. 'mistyrose',
  163. 'indigo',
  164. ],
  165. },
  166. type: {
  167. /**
  168. * The type of list
  169. */
  170. type: String,
  171. defaultValue: 'list',
  172. },
  173. }),
  174. );
  175. Lists.allow({
  176. insert(userId, doc) {
  177. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  178. },
  179. update(userId, doc) {
  180. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  181. },
  182. remove(userId, doc) {
  183. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  184. },
  185. fetch: ['boardId'],
  186. });
  187. Lists.helpers({
  188. copy(boardId, swimlaneId) {
  189. const oldId = this._id;
  190. const oldSwimlaneId = this.swimlaneId || null;
  191. this.boardId = boardId;
  192. this.swimlaneId = swimlaneId;
  193. let _id = null;
  194. existingListWithSameName = Lists.findOne({
  195. boardId,
  196. title: this.title,
  197. archived: false,
  198. });
  199. if (existingListWithSameName) {
  200. _id = existingListWithSameName._id;
  201. } else {
  202. delete this._id;
  203. delete this.swimlaneId;
  204. _id = Lists.insert(this);
  205. }
  206. // Copy all cards in list
  207. Cards.find({
  208. swimlaneId: oldSwimlaneId,
  209. listId: oldId,
  210. archived: false,
  211. }).forEach(card => {
  212. card.copy(boardId, swimlaneId, _id);
  213. });
  214. },
  215. cards(swimlaneId) {
  216. const selector = {
  217. listId: this._id,
  218. archived: false,
  219. };
  220. if (swimlaneId) selector.swimlaneId = swimlaneId;
  221. return Cards.find(Filter.mongoSelector(selector), { sort: ['sort'] });
  222. },
  223. cardsUnfiltered(swimlaneId) {
  224. const selector = {
  225. listId: this._id,
  226. archived: false,
  227. };
  228. if (swimlaneId) selector.swimlaneId = swimlaneId;
  229. return Cards.find(selector, { sort: ['sort'] });
  230. },
  231. allCards() {
  232. return Cards.find({ listId: this._id });
  233. },
  234. board() {
  235. return Boards.findOne(this.boardId);
  236. },
  237. getWipLimit(option) {
  238. const list = Lists.findOne({ _id: this._id });
  239. if (!list.wipLimit) {
  240. // Necessary check to avoid exceptions for the case where the doc doesn't have the wipLimit field yet set
  241. return 0;
  242. } else if (!option) {
  243. return list.wipLimit;
  244. } else {
  245. 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
  246. }
  247. },
  248. colorClass() {
  249. if (this.color) return this.color;
  250. return '';
  251. },
  252. isTemplateList() {
  253. return this.type === 'template-list';
  254. },
  255. isStarred() {
  256. return this.starred === true;
  257. },
  258. remove() {
  259. Lists.remove({ _id: this._id });
  260. },
  261. });
  262. Lists.mutations({
  263. rename(title) {
  264. return { $set: { title } };
  265. },
  266. star(enable = true) {
  267. return { $set: { starred: !!enable } };
  268. },
  269. archive() {
  270. if (this.isTemplateList()) {
  271. this.cards().forEach(card => {
  272. return card.archive();
  273. });
  274. }
  275. return { $set: { archived: true } };
  276. },
  277. restore() {
  278. if (this.isTemplateList()) {
  279. this.allCards().forEach(card => {
  280. return card.restore();
  281. });
  282. }
  283. return { $set: { archived: false } };
  284. },
  285. toggleSoftLimit(toggle) {
  286. return { $set: { 'wipLimit.soft': toggle } };
  287. },
  288. toggleWipLimit(toggle) {
  289. return { $set: { 'wipLimit.enabled': toggle } };
  290. },
  291. setWipLimit(limit) {
  292. return { $set: { 'wipLimit.value': limit } };
  293. },
  294. setColor(newColor) {
  295. if (newColor === 'silver') {
  296. newColor = null;
  297. }
  298. return {
  299. $set: {
  300. color: newColor,
  301. },
  302. };
  303. },
  304. });
  305. Meteor.methods({
  306. applyWipLimit(listId, limit) {
  307. check(listId, String);
  308. check(limit, Number);
  309. if (limit === 0) {
  310. limit = 1;
  311. }
  312. Lists.findOne({ _id: listId }).setWipLimit(limit);
  313. },
  314. enableWipLimit(listId) {
  315. check(listId, String);
  316. const list = Lists.findOne({ _id: listId });
  317. if (list.getWipLimit('value') === 0) {
  318. list.setWipLimit(1);
  319. }
  320. list.toggleWipLimit(!list.getWipLimit('enabled'));
  321. },
  322. enableSoftLimit(listId) {
  323. check(listId, String);
  324. const list = Lists.findOne({ _id: listId });
  325. list.toggleSoftLimit(!list.getWipLimit('soft'));
  326. },
  327. });
  328. Lists.hookOptions.after.update = { fetchPrevious: false };
  329. if (Meteor.isServer) {
  330. Meteor.startup(() => {
  331. Lists._collection._ensureIndex({ modifiedAt: -1 });
  332. Lists._collection._ensureIndex({ boardId: 1 });
  333. });
  334. Lists.after.insert((userId, doc) => {
  335. Activities.insert({
  336. userId,
  337. type: 'list',
  338. activityType: 'createList',
  339. boardId: doc.boardId,
  340. listId: doc._id,
  341. });
  342. });
  343. Lists.before.remove((userId, doc) => {
  344. const cards = Cards.find({ listId: doc._id });
  345. if (cards) {
  346. cards.forEach(card => {
  347. Cards.remove(card._id);
  348. });
  349. }
  350. Activities.insert({
  351. userId,
  352. type: 'list',
  353. activityType: 'removeList',
  354. boardId: doc.boardId,
  355. listId: doc._id,
  356. title: doc.title,
  357. });
  358. });
  359. Lists.after.update((userId, doc) => {
  360. if (doc.archived) {
  361. Activities.insert({
  362. userId,
  363. type: 'list',
  364. activityType: 'archivedList',
  365. listId: doc._id,
  366. boardId: doc.boardId,
  367. });
  368. }
  369. });
  370. }
  371. //LISTS REST API
  372. if (Meteor.isServer) {
  373. /**
  374. * @operation get_all_lists
  375. * @summary Get the list of Lists attached to a board
  376. *
  377. * @param {string} boardId the board ID
  378. * @return_type [{_id: string,
  379. * title: string}]
  380. */
  381. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function(req, res) {
  382. try {
  383. const paramBoardId = req.params.boardId;
  384. Authentication.checkBoardAccess(req.userId, paramBoardId);
  385. JsonRoutes.sendResult(res, {
  386. code: 200,
  387. data: Lists.find({ boardId: paramBoardId, archived: false }).map(
  388. function(doc) {
  389. return {
  390. _id: doc._id,
  391. title: doc.title,
  392. };
  393. },
  394. ),
  395. });
  396. } catch (error) {
  397. JsonRoutes.sendResult(res, {
  398. code: 200,
  399. data: error,
  400. });
  401. }
  402. });
  403. /**
  404. * @operation get_list
  405. * @summary Get a List attached to a board
  406. *
  407. * @param {string} boardId the board ID
  408. * @param {string} listId the List ID
  409. * @return_type Lists
  410. */
  411. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function(
  412. req,
  413. res,
  414. ) {
  415. try {
  416. const paramBoardId = req.params.boardId;
  417. const paramListId = req.params.listId;
  418. Authentication.checkBoardAccess(req.userId, paramBoardId);
  419. JsonRoutes.sendResult(res, {
  420. code: 200,
  421. data: Lists.findOne({
  422. _id: paramListId,
  423. boardId: paramBoardId,
  424. archived: false,
  425. }),
  426. });
  427. } catch (error) {
  428. JsonRoutes.sendResult(res, {
  429. code: 200,
  430. data: error,
  431. });
  432. }
  433. });
  434. /**
  435. * @operation new_list
  436. * @summary Add a List to a board
  437. *
  438. * @param {string} boardId the board ID
  439. * @param {string} title the title of the List
  440. * @return_type {_id: string}
  441. */
  442. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function(req, res) {
  443. try {
  444. Authentication.checkUserId(req.userId);
  445. const paramBoardId = req.params.boardId;
  446. const board = Boards.findOne(paramBoardId);
  447. const id = Lists.insert({
  448. title: req.body.title,
  449. boardId: paramBoardId,
  450. sort: board.lists().count(),
  451. });
  452. JsonRoutes.sendResult(res, {
  453. code: 200,
  454. data: {
  455. _id: id,
  456. },
  457. });
  458. } catch (error) {
  459. JsonRoutes.sendResult(res, {
  460. code: 200,
  461. data: error,
  462. });
  463. }
  464. });
  465. /**
  466. * @operation delete_list
  467. * @summary Delete a List
  468. *
  469. * @description This **deletes** a list from a board.
  470. * The list is not put in the recycle bin.
  471. *
  472. * @param {string} boardId the board ID
  473. * @param {string} listId the ID of the list to remove
  474. * @return_type {_id: string}
  475. */
  476. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function(
  477. req,
  478. res,
  479. ) {
  480. try {
  481. Authentication.checkUserId(req.userId);
  482. const paramBoardId = req.params.boardId;
  483. const paramListId = req.params.listId;
  484. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  485. JsonRoutes.sendResult(res, {
  486. code: 200,
  487. data: {
  488. _id: paramListId,
  489. },
  490. });
  491. } catch (error) {
  492. JsonRoutes.sendResult(res, {
  493. code: 200,
  494. data: error,
  495. });
  496. }
  497. });
  498. }
  499. export default Lists;