swimlanes.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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. // sorted lists from newest to the oldest, by its creation date or its cards' last modification date
  202. // Include lists without swimlaneId for backward compatibility (they belong to default swimlane)
  203. return ReactiveCache.getLists(
  204. {
  205. boardId: this.boardId,
  206. $or: [
  207. { swimlaneId: this._id },
  208. { swimlaneId: { $exists: false } },
  209. { swimlaneId: '' },
  210. { swimlaneId: null }
  211. ],
  212. archived: false,
  213. },
  214. { sort: { modifiedAt: -1 } },
  215. );
  216. },
  217. draggableLists() {
  218. // Include lists without swimlaneId for backward compatibility (they belong to default swimlane)
  219. return ReactiveCache.getLists(
  220. {
  221. boardId: this.boardId,
  222. $or: [
  223. { swimlaneId: this._id },
  224. { swimlaneId: { $exists: false } },
  225. { swimlaneId: '' },
  226. { swimlaneId: null }
  227. ],
  228. //archived: false,
  229. },
  230. { sort: ['sort'] },
  231. );
  232. },
  233. myLists() {
  234. return ReactiveCache.getLists({ swimlaneId: this._id });
  235. },
  236. allCards() {
  237. const ret = ReactiveCache.getCards({ swimlaneId: this._id });
  238. return ret;
  239. },
  240. isCollapsed() {
  241. return this.collapsed === true;
  242. },
  243. board() {
  244. return ReactiveCache.getBoard(this.boardId);
  245. },
  246. colorClass() {
  247. if (this.color) return `swimlane-${this.color}`;
  248. return '';
  249. },
  250. isTemplateSwimlane() {
  251. return this.type === 'template-swimlane';
  252. },
  253. isTemplateContainer() {
  254. return this.type === 'template-container';
  255. },
  256. isListTemplatesSwimlane() {
  257. const user = ReactiveCache.getCurrentUser();
  258. return (user.profile || {}).listTemplatesSwimlaneId === this._id;
  259. },
  260. isCardTemplatesSwimlane() {
  261. const user = ReactiveCache.getCurrentUser();
  262. return (user.profile || {}).cardTemplatesSwimlaneId === this._id;
  263. },
  264. isBoardTemplatesSwimlane() {
  265. const user = ReactiveCache.getCurrentUser();
  266. return (user.profile || {}).boardTemplatesSwimlaneId === this._id;
  267. },
  268. remove() {
  269. Swimlanes.remove({ _id: this._id });
  270. },
  271. });
  272. Swimlanes.mutations({
  273. rename(title) {
  274. return { $set: { title } };
  275. },
  276. collapse(enable = true) {
  277. return { $set: { collapsed: !!enable } };
  278. },
  279. archive() {
  280. if (this.isTemplateSwimlane()) {
  281. this.myLists().forEach(list => {
  282. return list.archive();
  283. });
  284. }
  285. return { $set: { archived: true, archivedAt: new Date() } };
  286. },
  287. restore() {
  288. if (this.isTemplateSwimlane()) {
  289. this.myLists().forEach(list => {
  290. return list.restore();
  291. });
  292. }
  293. return { $set: { archived: false } };
  294. },
  295. setColor(newColor) {
  296. return {
  297. $set: {
  298. color: newColor,
  299. },
  300. };
  301. },
  302. });
  303. Swimlanes.userArchivedSwimlanes = userId => {
  304. return ReactiveCache.getSwimlanes({
  305. boardId: { $in: Boards.userBoardIds(userId, null) },
  306. archived: true,
  307. })
  308. };
  309. Swimlanes.userArchivedSwimlaneIds = () => {
  310. return Swimlanes.userArchivedSwimlanes().map(swim => { return swim._id; });
  311. };
  312. Swimlanes.archivedSwimlanes = () => {
  313. return ReactiveCache.getSwimlanes({ archived: true });
  314. };
  315. Swimlanes.archivedSwimlaneIds = () => {
  316. return Swimlanes.archivedSwimlanes().map(swim => {
  317. return swim._id;
  318. });
  319. };
  320. Swimlanes.hookOptions.after.update = { fetchPrevious: false };
  321. if (Meteor.isServer) {
  322. Meteor.startup(() => {
  323. Swimlanes._collection.createIndex({ modifiedAt: -1 });
  324. Swimlanes._collection.createIndex({ boardId: 1 });
  325. });
  326. Swimlanes.after.insert((userId, doc) => {
  327. Activities.insert({
  328. userId,
  329. type: 'swimlane',
  330. activityType: 'createSwimlane',
  331. boardId: doc.boardId,
  332. swimlaneId: doc._id,
  333. });
  334. // Track original position for new swimlanes
  335. Meteor.setTimeout(() => {
  336. const swimlane = Swimlanes.findOne(doc._id);
  337. if (swimlane) {
  338. swimlane.trackOriginalPosition();
  339. }
  340. }, 100);
  341. });
  342. Swimlanes.before.remove(function(userId, doc) {
  343. const lists = ReactiveCache.getLists(
  344. {
  345. boardId: doc.boardId,
  346. swimlaneId: { $in: [doc._id, ''] },
  347. archived: false,
  348. },
  349. { sort: ['sort'] },
  350. );
  351. if (lists.length < 2) {
  352. lists.forEach(list => {
  353. list.remove();
  354. });
  355. } else {
  356. Cards.remove({ swimlaneId: doc._id });
  357. }
  358. Activities.insert({
  359. userId,
  360. type: 'swimlane',
  361. activityType: 'removeSwimlane',
  362. boardId: doc.boardId,
  363. swimlaneId: doc._id,
  364. title: doc.title,
  365. });
  366. });
  367. Swimlanes.after.update((userId, doc, fieldNames) => {
  368. if (fieldNames.includes('title')) {
  369. Activities.insert({
  370. userId,
  371. type: 'swimlane',
  372. activityType: 'changedSwimlaneTitle',
  373. listId: doc._id,
  374. boardId: doc.boardId,
  375. // this preserves the name so that the activity can be useful after the
  376. // list is deleted
  377. title: doc.title,
  378. });
  379. } else if (doc.archived) {
  380. Activities.insert({
  381. userId,
  382. type: 'swimlane',
  383. activityType: 'archivedSwimlane',
  384. listId: doc._id,
  385. boardId: doc.boardId,
  386. // this preserves the name so that the activity can be useful after the
  387. // list is deleted
  388. title: doc.title,
  389. });
  390. } else if (fieldNames.includes('archived')) {
  391. Activities.insert({
  392. userId,
  393. type: 'swimlane',
  394. activityType: 'restoredSwimlane',
  395. listId: doc._id,
  396. boardId: doc.boardId,
  397. // this preserves the name so that the activity can be useful after the
  398. // list is deleted
  399. title: doc.title,
  400. });
  401. }
  402. });
  403. }
  404. //SWIMLANE REST API
  405. if (Meteor.isServer) {
  406. /**
  407. * @operation get_all_swimlanes
  408. *
  409. * @summary Get the list of swimlanes attached to a board
  410. *
  411. * @param {string} boardId the ID of the board
  412. * @return_type [{_id: string,
  413. * title: string}]
  414. */
  415. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes', function(req, res) {
  416. try {
  417. const paramBoardId = req.params.boardId;
  418. Authentication.checkBoardAccess(req.userId, paramBoardId);
  419. JsonRoutes.sendResult(res, {
  420. code: 200,
  421. data: ReactiveCache.getSwimlanes({ boardId: paramBoardId, archived: false }).map(
  422. function(doc) {
  423. return {
  424. _id: doc._id,
  425. title: doc.title,
  426. };
  427. },
  428. ),
  429. });
  430. } catch (error) {
  431. JsonRoutes.sendResult(res, {
  432. code: 200,
  433. data: error,
  434. });
  435. }
  436. });
  437. /**
  438. * @operation get_swimlane
  439. *
  440. * @summary Get a swimlane
  441. *
  442. * @param {string} boardId the ID of the board
  443. * @param {string} swimlaneId the ID of the swimlane
  444. * @return_type Swimlanes
  445. */
  446. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId', function(
  447. req,
  448. res,
  449. ) {
  450. try {
  451. const paramBoardId = req.params.boardId;
  452. const paramSwimlaneId = req.params.swimlaneId;
  453. Authentication.checkBoardAccess(req.userId, paramBoardId);
  454. JsonRoutes.sendResult(res, {
  455. code: 200,
  456. data: ReactiveCache.getSwimlane({
  457. _id: paramSwimlaneId,
  458. boardId: paramBoardId,
  459. archived: false,
  460. }),
  461. });
  462. } catch (error) {
  463. JsonRoutes.sendResult(res, {
  464. code: 200,
  465. data: error,
  466. });
  467. }
  468. });
  469. /**
  470. * @operation new_swimlane
  471. *
  472. * @summary Add a swimlane to a board
  473. *
  474. * @param {string} boardId the ID of the board
  475. * @param {string} title the new title of the swimlane
  476. * @return_type {_id: string}
  477. */
  478. JsonRoutes.add('POST', '/api/boards/:boardId/swimlanes', function(req, res) {
  479. try {
  480. const paramBoardId = req.params.boardId;
  481. Authentication.checkBoardAccess(req.userId, paramBoardId);
  482. const board = ReactiveCache.getBoard(paramBoardId);
  483. const id = Swimlanes.insert({
  484. title: req.body.title,
  485. boardId: paramBoardId,
  486. sort: board.swimlanes().length,
  487. });
  488. JsonRoutes.sendResult(res, {
  489. code: 200,
  490. data: {
  491. _id: id,
  492. },
  493. });
  494. } catch (error) {
  495. JsonRoutes.sendResult(res, {
  496. code: 200,
  497. data: error,
  498. });
  499. }
  500. });
  501. /**
  502. * @operation edit_swimlane
  503. *
  504. * @summary Edit the title of a swimlane
  505. *
  506. * @param {string} boardId the ID of the board
  507. * @param {string} swimlaneId the ID of the swimlane to edit
  508. * @param {string} title the new title of the swimlane
  509. * @return_type {_id: string}
  510. */
  511. JsonRoutes.add('PUT', '/api/boards/:boardId/swimlanes/:swimlaneId', function(req, res) {
  512. try {
  513. const paramBoardId = req.params.boardId;
  514. const paramSwimlaneId = req.params.swimlaneId;
  515. Authentication.checkBoardAccess(req.userId, paramBoardId);
  516. const board = ReactiveCache.getBoard(paramBoardId);
  517. const swimlane = ReactiveCache.getSwimlane({
  518. _id: paramSwimlaneId,
  519. boardId: paramBoardId,
  520. });
  521. if (!swimlane) {
  522. throw new Meteor.Error('not-found', 'Swimlane not found');
  523. }
  524. Swimlanes.direct.update(
  525. { _id: paramSwimlaneId },
  526. { $set: { title: req.body.title } }
  527. );
  528. JsonRoutes.sendResult(res, {
  529. code: 200,
  530. data: {
  531. _id: paramSwimlaneId,
  532. },
  533. });
  534. } catch (error) {
  535. JsonRoutes.sendResult(res, {
  536. code: 200,
  537. data: error,
  538. });
  539. }
  540. });
  541. /**
  542. * @operation delete_swimlane
  543. *
  544. * @summary Delete a swimlane
  545. *
  546. * @description The swimlane will be deleted, not moved to the recycle bin
  547. *
  548. * @param {string} boardId the ID of the board
  549. * @param {string} swimlaneId the ID of the swimlane
  550. * @return_type {_id: string}
  551. */
  552. JsonRoutes.add(
  553. 'DELETE',
  554. '/api/boards/:boardId/swimlanes/:swimlaneId',
  555. function(req, res) {
  556. try {
  557. const paramBoardId = req.params.boardId;
  558. const paramSwimlaneId = req.params.swimlaneId;
  559. Authentication.checkBoardAccess(req.userId, paramBoardId);
  560. Swimlanes.remove({ _id: paramSwimlaneId, boardId: paramBoardId });
  561. JsonRoutes.sendResult(res, {
  562. code: 200,
  563. data: {
  564. _id: paramSwimlaneId,
  565. },
  566. });
  567. } catch (error) {
  568. JsonRoutes.sendResult(res, {
  569. code: 200,
  570. data: error,
  571. });
  572. }
  573. },
  574. );
  575. }
  576. // Position history tracking methods
  577. Swimlanes.helpers({
  578. /**
  579. * Track the original position of this swimlane
  580. */
  581. trackOriginalPosition() {
  582. const existingHistory = PositionHistory.findOne({
  583. boardId: this.boardId,
  584. entityType: 'swimlane',
  585. entityId: this._id,
  586. });
  587. if (!existingHistory) {
  588. PositionHistory.insert({
  589. boardId: this.boardId,
  590. entityType: 'swimlane',
  591. entityId: this._id,
  592. originalPosition: {
  593. sort: this.sort,
  594. title: this.title,
  595. },
  596. originalTitle: this.title,
  597. createdAt: new Date(),
  598. updatedAt: new Date(),
  599. });
  600. }
  601. },
  602. /**
  603. * Get the original position history for this swimlane
  604. */
  605. getOriginalPosition() {
  606. return PositionHistory.findOne({
  607. boardId: this.boardId,
  608. entityType: 'swimlane',
  609. entityId: this._id,
  610. });
  611. },
  612. /**
  613. * Check if this swimlane has moved from its original position
  614. */
  615. hasMovedFromOriginalPosition() {
  616. const history = this.getOriginalPosition();
  617. if (!history) return false;
  618. return history.originalPosition.sort !== this.sort;
  619. },
  620. /**
  621. * Get a description of the original position
  622. */
  623. getOriginalPositionDescription() {
  624. const history = this.getOriginalPosition();
  625. if (!history) return 'No original position data';
  626. return `Original position: ${history.originalPosition.sort || 0}`;
  627. },
  628. });
  629. export default Swimlanes;