swimlanes.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { ALLOWED_COLORS } from '/config/const';
  3. Swimlanes = new Mongo.Collection('swimlanes');
  4. /**
  5. * A swimlane is an line in the kaban board.
  6. */
  7. Swimlanes.attachSchema(
  8. new SimpleSchema({
  9. title: {
  10. /**
  11. * the title of the swimlane
  12. */
  13. type: String,
  14. },
  15. archived: {
  16. /**
  17. * is the swimlane archived?
  18. */
  19. type: Boolean,
  20. // eslint-disable-next-line consistent-return
  21. autoValue() {
  22. if (this.isInsert && !this.isSet) {
  23. return false;
  24. }
  25. },
  26. },
  27. archivedAt: {
  28. /**
  29. * latest archiving date of the swimlane
  30. */
  31. type: Date,
  32. optional: true,
  33. },
  34. boardId: {
  35. /**
  36. * the ID of the board the swimlane is attached to
  37. */
  38. type: String,
  39. },
  40. createdAt: {
  41. /**
  42. * creation date of the swimlane
  43. */
  44. type: Date,
  45. // eslint-disable-next-line consistent-return
  46. autoValue() {
  47. if (this.isInsert) {
  48. return new Date();
  49. } else if (this.isUpsert) {
  50. return { $setOnInsert: new Date() };
  51. } else {
  52. this.unset();
  53. }
  54. },
  55. },
  56. sort: {
  57. /**
  58. * the sort value of the swimlane
  59. */
  60. type: Number,
  61. decimal: true,
  62. // XXX We should probably provide a default
  63. optional: true,
  64. },
  65. color: {
  66. /**
  67. * the color of the swimlane
  68. */
  69. type: String,
  70. optional: true,
  71. // silver is the default, so it is left out
  72. allowedValues: ALLOWED_COLORS,
  73. },
  74. updatedAt: {
  75. /**
  76. * when was the swimlane last edited
  77. */
  78. type: Date,
  79. optional: true,
  80. // eslint-disable-next-line consistent-return
  81. autoValue() {
  82. if (this.isUpdate || this.isUpsert || this.isInsert) {
  83. return new Date();
  84. } else {
  85. this.unset();
  86. }
  87. },
  88. },
  89. modifiedAt: {
  90. type: Date,
  91. denyUpdate: false,
  92. // eslint-disable-next-line consistent-return
  93. autoValue() {
  94. if (this.isInsert || this.isUpsert || this.isUpdate) {
  95. return new Date();
  96. } else {
  97. this.unset();
  98. }
  99. },
  100. },
  101. type: {
  102. /**
  103. * The type of swimlane
  104. */
  105. type: String,
  106. defaultValue: 'swimlane',
  107. },
  108. collapsed: {
  109. /**
  110. * is the swimlane collapsed
  111. */
  112. type: Boolean,
  113. defaultValue: false,
  114. },
  115. }),
  116. );
  117. Swimlanes.allow({
  118. insert(userId, doc) {
  119. return allowIsBoardMemberCommentOnly(userId, ReactiveCache.getBoard(doc.boardId));
  120. },
  121. update(userId, doc) {
  122. return allowIsBoardMemberCommentOnly(userId, ReactiveCache.getBoard(doc.boardId));
  123. },
  124. remove(userId, doc) {
  125. return allowIsBoardMemberCommentOnly(userId, ReactiveCache.getBoard(doc.boardId));
  126. },
  127. fetch: ['boardId'],
  128. });
  129. Swimlanes.helpers({
  130. copy(boardId) {
  131. const oldId = this._id;
  132. const oldBoardId = this.boardId;
  133. this.boardId = boardId;
  134. delete this._id;
  135. const _id = Swimlanes.insert(this);
  136. const query = {
  137. swimlaneId: { $in: [oldId, ''] },
  138. archived: false,
  139. };
  140. if (oldBoardId) {
  141. query.boardId = oldBoardId;
  142. }
  143. // Copy all lists in swimlane
  144. ReactiveCache.getLists(query).forEach(list => {
  145. list.type = 'list';
  146. list.swimlaneId = oldId;
  147. list.boardId = boardId;
  148. list.copy(boardId, _id);
  149. });
  150. },
  151. move(toBoardId) {
  152. this.lists().forEach(list => {
  153. const toList = ReactiveCache.getList({
  154. boardId: toBoardId,
  155. title: list.title,
  156. archived: false,
  157. });
  158. let toListId;
  159. if (toList) {
  160. toListId = toList._id;
  161. } else {
  162. toListId = Lists.insert({
  163. title: list.title,
  164. boardId: toBoardId,
  165. type: list.type,
  166. archived: false,
  167. wipLimit: list.wipLimit,
  168. });
  169. }
  170. ReactiveCache.getCards({
  171. listId: list._id,
  172. swimlaneId: this._id,
  173. }).forEach(card => {
  174. card.move(toBoardId, this._id, toListId);
  175. });
  176. });
  177. Swimlanes.update(this._id, {
  178. $set: {
  179. boardId: toBoardId,
  180. },
  181. });
  182. // make sure there is a default swimlane
  183. this.board().getDefaultSwimline();
  184. },
  185. cards() {
  186. const ret = ReactiveCache.getCards(
  187. Filter.mongoSelector({
  188. swimlaneId: this._id,
  189. archived: false,
  190. }),
  191. { sort: ['sort'] },
  192. );
  193. return ret;
  194. },
  195. lists() {
  196. return this.draggableLists();
  197. },
  198. newestLists() {
  199. // sorted lists from newest to the oldest, by its creation date or its cards' last modification date
  200. return ReactiveCache.getLists(
  201. {
  202. boardId: this.boardId,
  203. swimlaneId: { $in: [this._id, ''] },
  204. archived: false,
  205. },
  206. { sort: { modifiedAt: -1 } },
  207. );
  208. },
  209. draggableLists() {
  210. return ReactiveCache.getLists(
  211. {
  212. boardId: this.boardId,
  213. swimlaneId: { $in: [this._id, ''] },
  214. //archived: false,
  215. },
  216. { sort: ['sort'] },
  217. );
  218. },
  219. myLists() {
  220. return ReactiveCache.getLists({ swimlaneId: this._id });
  221. },
  222. allCards() {
  223. const ret = ReactiveCache.getCards({ swimlaneId: this._id });
  224. return ret;
  225. },
  226. isCollapsed() {
  227. return this.collapsed === true;
  228. },
  229. board() {
  230. return ReactiveCache.getBoard(this.boardId);
  231. },
  232. colorClass() {
  233. if (this.color) return `swimlane-${this.color}`;
  234. return '';
  235. },
  236. isTemplateSwimlane() {
  237. return this.type === 'template-swimlane';
  238. },
  239. isTemplateContainer() {
  240. return this.type === 'template-container';
  241. },
  242. isListTemplatesSwimlane() {
  243. const user = ReactiveCache.getCurrentUser();
  244. return (user.profile || {}).listTemplatesSwimlaneId === this._id;
  245. },
  246. isCardTemplatesSwimlane() {
  247. const user = ReactiveCache.getCurrentUser();
  248. return (user.profile || {}).cardTemplatesSwimlaneId === this._id;
  249. },
  250. isBoardTemplatesSwimlane() {
  251. const user = ReactiveCache.getCurrentUser();
  252. return (user.profile || {}).boardTemplatesSwimlaneId === this._id;
  253. },
  254. remove() {
  255. Swimlanes.remove({ _id: this._id });
  256. },
  257. });
  258. Swimlanes.mutations({
  259. rename(title) {
  260. return { $set: { title } };
  261. },
  262. collapse(enable = true) {
  263. return { $set: { collapsed: !!enable } };
  264. },
  265. archive() {
  266. if (this.isTemplateSwimlane()) {
  267. this.myLists().forEach(list => {
  268. return list.archive();
  269. });
  270. }
  271. return { $set: { archived: true, archivedAt: new Date() } };
  272. },
  273. restore() {
  274. if (this.isTemplateSwimlane()) {
  275. this.myLists().forEach(list => {
  276. return list.restore();
  277. });
  278. }
  279. return { $set: { archived: false } };
  280. },
  281. setColor(newColor) {
  282. if (newColor === 'silver') {
  283. newColor = null;
  284. }
  285. return {
  286. $set: {
  287. color: newColor,
  288. },
  289. };
  290. },
  291. });
  292. Swimlanes.userArchivedSwimlanes = userId => {
  293. return ReactiveCache.getSwimlanes({
  294. boardId: { $in: Boards.userBoardIds(userId, null) },
  295. archived: true,
  296. })
  297. };
  298. Swimlanes.userArchivedSwimlaneIds = () => {
  299. return Swimlanes.userArchivedSwimlanes().map(swim => { return swim._id; });
  300. };
  301. Swimlanes.archivedSwimlanes = () => {
  302. return ReactiveCache.getSwimlanes({ archived: true });
  303. };
  304. Swimlanes.archivedSwimlaneIds = () => {
  305. return Swimlanes.archivedSwimlanes().map(swim => {
  306. return swim._id;
  307. });
  308. };
  309. Swimlanes.hookOptions.after.update = { fetchPrevious: false };
  310. if (Meteor.isServer) {
  311. Meteor.startup(() => {
  312. Swimlanes._collection.createIndex({ modifiedAt: -1 });
  313. Swimlanes._collection.createIndex({ boardId: 1 });
  314. });
  315. Swimlanes.after.insert((userId, doc) => {
  316. Activities.insert({
  317. userId,
  318. type: 'swimlane',
  319. activityType: 'createSwimlane',
  320. boardId: doc.boardId,
  321. swimlaneId: doc._id,
  322. });
  323. });
  324. Swimlanes.before.remove(function(userId, doc) {
  325. const lists = ReactiveCache.getLists(
  326. {
  327. boardId: doc.boardId,
  328. swimlaneId: { $in: [doc._id, ''] },
  329. archived: false,
  330. },
  331. { sort: ['sort'] },
  332. );
  333. if (lists.length < 2) {
  334. lists.forEach(list => {
  335. list.remove();
  336. });
  337. } else {
  338. Cards.remove({ swimlaneId: doc._id });
  339. }
  340. Activities.insert({
  341. userId,
  342. type: 'swimlane',
  343. activityType: 'removeSwimlane',
  344. boardId: doc.boardId,
  345. swimlaneId: doc._id,
  346. title: doc.title,
  347. });
  348. });
  349. Swimlanes.after.update((userId, doc, fieldNames) => {
  350. if (fieldNames.includes('title')) {
  351. Activities.insert({
  352. userId,
  353. type: 'swimlane',
  354. activityType: 'changedSwimlaneTitle',
  355. listId: doc._id,
  356. boardId: doc.boardId,
  357. // this preserves the name so that the activity can be useful after the
  358. // list is deleted
  359. title: doc.title,
  360. });
  361. } else if (doc.archived) {
  362. Activities.insert({
  363. userId,
  364. type: 'swimlane',
  365. activityType: 'archivedSwimlane',
  366. listId: doc._id,
  367. boardId: doc.boardId,
  368. // this preserves the name so that the activity can be useful after the
  369. // list is deleted
  370. title: doc.title,
  371. });
  372. } else if (fieldNames.includes('archived')) {
  373. Activities.insert({
  374. userId,
  375. type: 'swimlane',
  376. activityType: 'restoredSwimlane',
  377. listId: doc._id,
  378. boardId: doc.boardId,
  379. // this preserves the name so that the activity can be useful after the
  380. // list is deleted
  381. title: doc.title,
  382. });
  383. }
  384. });
  385. }
  386. //SWIMLANE REST API
  387. if (Meteor.isServer) {
  388. /**
  389. * @operation get_all_swimlanes
  390. *
  391. * @summary Get the list of swimlanes attached to a board
  392. *
  393. * @param {string} boardId the ID of the board
  394. * @return_type [{_id: string,
  395. * title: string}]
  396. */
  397. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes', function(req, res) {
  398. try {
  399. const paramBoardId = req.params.boardId;
  400. Authentication.checkBoardAccess(req.userId, paramBoardId);
  401. JsonRoutes.sendResult(res, {
  402. code: 200,
  403. data: ReactiveCache.getSwimlanes({ boardId: paramBoardId, archived: false }).map(
  404. function(doc) {
  405. return {
  406. _id: doc._id,
  407. title: doc.title,
  408. };
  409. },
  410. ),
  411. });
  412. } catch (error) {
  413. JsonRoutes.sendResult(res, {
  414. code: 200,
  415. data: error,
  416. });
  417. }
  418. });
  419. /**
  420. * @operation get_swimlane
  421. *
  422. * @summary Get a swimlane
  423. *
  424. * @param {string} boardId the ID of the board
  425. * @param {string} swimlaneId the ID of the swimlane
  426. * @return_type Swimlanes
  427. */
  428. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId', function(
  429. req,
  430. res,
  431. ) {
  432. try {
  433. const paramBoardId = req.params.boardId;
  434. const paramSwimlaneId = req.params.swimlaneId;
  435. Authentication.checkBoardAccess(req.userId, paramBoardId);
  436. JsonRoutes.sendResult(res, {
  437. code: 200,
  438. data: ReactiveCache.getSwimlane({
  439. _id: paramSwimlaneId,
  440. boardId: paramBoardId,
  441. archived: false,
  442. }),
  443. });
  444. } catch (error) {
  445. JsonRoutes.sendResult(res, {
  446. code: 200,
  447. data: error,
  448. });
  449. }
  450. });
  451. /**
  452. * @operation new_swimlane
  453. *
  454. * @summary Add a swimlane to a board
  455. *
  456. * @param {string} boardId the ID of the board
  457. * @param {string} title the new title of the swimlane
  458. * @return_type {_id: string}
  459. */
  460. JsonRoutes.add('POST', '/api/boards/:boardId/swimlanes', function(req, res) {
  461. try {
  462. const paramBoardId = req.params.boardId;
  463. Authentication.checkBoardAccess(req.userId, paramBoardId);
  464. const board = ReactiveCache.getBoard(paramBoardId);
  465. const id = Swimlanes.insert({
  466. title: req.body.title,
  467. boardId: paramBoardId,
  468. sort: board.swimlanes().length,
  469. });
  470. JsonRoutes.sendResult(res, {
  471. code: 200,
  472. data: {
  473. _id: id,
  474. },
  475. });
  476. } catch (error) {
  477. JsonRoutes.sendResult(res, {
  478. code: 200,
  479. data: error,
  480. });
  481. }
  482. });
  483. /**
  484. * @operation edit_swimlane
  485. *
  486. * @summary Edit the title of a swimlane
  487. *
  488. * @param {string} boardId the ID of the board
  489. * @param {string} swimlaneId the ID of the swimlane to edit
  490. * @param {string} title the new title of the swimlane
  491. * @return_type {_id: string}
  492. */
  493. JsonRoutes.add('PUT', '/api/boards/:boardId/swimlanes/:swimlaneId', function(req, res) {
  494. try {
  495. const paramBoardId = req.params.boardId;
  496. const paramSwimlaneId = req.params.swimlaneId;
  497. Authentication.checkBoardAccess(req.userId, paramBoardId);
  498. const board = ReactiveCache.getBoard(paramBoardId);
  499. const swimlane = ReactiveCache.getSwimlane({
  500. _id: paramSwimlaneId,
  501. boardId: paramBoardId,
  502. });
  503. if (!swimlane) {
  504. throw new Meteor.Error('not-found', 'Swimlane not found');
  505. }
  506. Swimlanes.direct.update(
  507. { _id: paramSwimlaneId },
  508. { $set: { title: req.body.title } }
  509. );
  510. JsonRoutes.sendResult(res, {
  511. code: 200,
  512. data: {
  513. _id: paramSwimlaneId,
  514. },
  515. });
  516. } catch (error) {
  517. JsonRoutes.sendResult(res, {
  518. code: 200,
  519. data: error,
  520. });
  521. }
  522. });
  523. /**
  524. * @operation delete_swimlane
  525. *
  526. * @summary Delete a swimlane
  527. *
  528. * @description The swimlane will be deleted, not moved to the recycle bin
  529. *
  530. * @param {string} boardId the ID of the board
  531. * @param {string} swimlaneId the ID of the swimlane
  532. * @return_type {_id: string}
  533. */
  534. JsonRoutes.add(
  535. 'DELETE',
  536. '/api/boards/:boardId/swimlanes/:swimlaneId',
  537. function(req, res) {
  538. try {
  539. const paramBoardId = req.params.boardId;
  540. const paramSwimlaneId = req.params.swimlaneId;
  541. Authentication.checkBoardAccess(req.userId, paramBoardId);
  542. Swimlanes.remove({ _id: paramSwimlaneId, boardId: paramBoardId });
  543. JsonRoutes.sendResult(res, {
  544. code: 200,
  545. data: {
  546. _id: paramSwimlaneId,
  547. },
  548. });
  549. } catch (error) {
  550. JsonRoutes.sendResult(res, {
  551. code: 200,
  552. data: error,
  553. });
  554. }
  555. },
  556. );
  557. }
  558. export default Swimlanes;