swimlanes.js 16 KB

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