lists.js 11 KB

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