swimlanes.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. }),
  109. );
  110. Swimlanes.allow({
  111. insert(userId, doc) {
  112. return allowIsBoardMemberCommentOnly(userId, ReactiveCache.getBoard(doc.boardId));
  113. },
  114. update(userId, doc) {
  115. return allowIsBoardMemberCommentOnly(userId, ReactiveCache.getBoard(doc.boardId));
  116. },
  117. remove(userId, doc) {
  118. return allowIsBoardMemberCommentOnly(userId, ReactiveCache.getBoard(doc.boardId));
  119. },
  120. fetch: ['boardId'],
  121. });
  122. Swimlanes.helpers({
  123. copy(boardId) {
  124. const oldId = this._id;
  125. const oldBoardId = this.boardId;
  126. this.boardId = boardId;
  127. delete this._id;
  128. const _id = Swimlanes.insert(this);
  129. const query = {
  130. swimlaneId: { $in: [oldId, ''] },
  131. archived: false,
  132. };
  133. if (oldBoardId) {
  134. query.boardId = oldBoardId;
  135. }
  136. // Copy all lists in swimlane
  137. Lists.find(query).forEach(list => {
  138. list.type = 'list';
  139. list.swimlaneId = oldId;
  140. list.boardId = boardId;
  141. list.copy(boardId, _id);
  142. });
  143. },
  144. move(toBoardId) {
  145. this.lists().forEach(list => {
  146. const toList = Lists.findOne({
  147. boardId: toBoardId,
  148. title: list.title,
  149. archived: false,
  150. });
  151. let toListId;
  152. if (toList) {
  153. toListId = toList._id;
  154. } else {
  155. toListId = Lists.insert({
  156. title: list.title,
  157. boardId: toBoardId,
  158. type: list.type,
  159. archived: false,
  160. wipLimit: list.wipLimit,
  161. });
  162. }
  163. Cards.find({
  164. listId: list._id,
  165. swimlaneId: this._id,
  166. }).forEach(card => {
  167. card.move(toBoardId, this._id, toListId);
  168. });
  169. });
  170. Swimlanes.update(this._id, {
  171. $set: {
  172. boardId: toBoardId,
  173. },
  174. });
  175. // make sure there is a default swimlane
  176. this.board().getDefaultSwimline();
  177. },
  178. cards() {
  179. return Cards.find(
  180. Filter.mongoSelector({
  181. swimlaneId: this._id,
  182. archived: false,
  183. }),
  184. { sort: ['sort'] },
  185. );
  186. },
  187. lists() {
  188. return this.draggableLists();
  189. },
  190. newestLists() {
  191. // sorted lists from newest to the oldest, by its creation date or its cards' last modification date
  192. return Lists.find(
  193. {
  194. boardId: this.boardId,
  195. swimlaneId: { $in: [this._id, ''] },
  196. archived: false,
  197. },
  198. { sort: { modifiedAt: -1 } },
  199. );
  200. },
  201. draggableLists() {
  202. return Lists.find(
  203. {
  204. boardId: this.boardId,
  205. swimlaneId: { $in: [this._id, ''] },
  206. //archived: false,
  207. },
  208. { sort: ['sort'] },
  209. );
  210. },
  211. myLists() {
  212. return Lists.find({ swimlaneId: this._id });
  213. },
  214. allCards() {
  215. return Cards.find({ swimlaneId: this._id });
  216. },
  217. board() {
  218. return ReactiveCache.getBoard(this.boardId);
  219. },
  220. colorClass() {
  221. if (this.color) return `swimlane-${this.color}`;
  222. return '';
  223. },
  224. isTemplateSwimlane() {
  225. return this.type === 'template-swimlane';
  226. },
  227. isTemplateContainer() {
  228. return this.type === 'template-container';
  229. },
  230. isListTemplatesSwimlane() {
  231. const user = ReactiveCache.getCurrentUser();
  232. return (user.profile || {}).listTemplatesSwimlaneId === this._id;
  233. },
  234. isCardTemplatesSwimlane() {
  235. const user = ReactiveCache.getCurrentUser();
  236. return (user.profile || {}).cardTemplatesSwimlaneId === this._id;
  237. },
  238. isBoardTemplatesSwimlane() {
  239. const user = ReactiveCache.getCurrentUser();
  240. return (user.profile || {}).boardTemplatesSwimlaneId === this._id;
  241. },
  242. remove() {
  243. Swimlanes.remove({ _id: this._id });
  244. },
  245. });
  246. Swimlanes.mutations({
  247. rename(title) {
  248. return { $set: { title } };
  249. },
  250. archive() {
  251. if (this.isTemplateSwimlane()) {
  252. this.myLists().forEach(list => {
  253. return list.archive();
  254. });
  255. }
  256. return { $set: { archived: true, archivedAt: new Date() } };
  257. },
  258. restore() {
  259. if (this.isTemplateSwimlane()) {
  260. this.myLists().forEach(list => {
  261. return list.restore();
  262. });
  263. }
  264. return { $set: { archived: false } };
  265. },
  266. setColor(newColor) {
  267. if (newColor === 'silver') {
  268. newColor = null;
  269. }
  270. return {
  271. $set: {
  272. color: newColor,
  273. },
  274. };
  275. },
  276. });
  277. Swimlanes.userArchivedSwimlanes = userId => {
  278. return Swimlanes.find({
  279. boardId: { $in: Boards.userBoardIds(userId, null) },
  280. archived: true,
  281. })
  282. };
  283. Swimlanes.userArchivedSwimlaneIds = () => {
  284. return Swimlanes.userArchivedSwimlanes().map(swim => { return swim._id; });
  285. };
  286. Swimlanes.archivedSwimlanes = () => {
  287. return Swimlanes.find({ archived: true });
  288. };
  289. Swimlanes.archivedSwimlaneIds = () => {
  290. return Swimlanes.archivedSwimlanes().map(swim => {
  291. return swim._id;
  292. });
  293. };
  294. Swimlanes.hookOptions.after.update = { fetchPrevious: false };
  295. if (Meteor.isServer) {
  296. Meteor.startup(() => {
  297. Swimlanes._collection.createIndex({ modifiedAt: -1 });
  298. Swimlanes._collection.createIndex({ boardId: 1 });
  299. });
  300. Swimlanes.after.insert((userId, doc) => {
  301. Activities.insert({
  302. userId,
  303. type: 'swimlane',
  304. activityType: 'createSwimlane',
  305. boardId: doc.boardId,
  306. swimlaneId: doc._id,
  307. });
  308. });
  309. Swimlanes.before.remove(function(userId, doc) {
  310. const lists = Lists.find(
  311. {
  312. boardId: doc.boardId,
  313. swimlaneId: { $in: [doc._id, ''] },
  314. archived: false,
  315. },
  316. { sort: ['sort'] },
  317. );
  318. if (lists.count() < 2) {
  319. lists.forEach(list => {
  320. list.remove();
  321. });
  322. } else {
  323. Cards.remove({ swimlaneId: doc._id });
  324. }
  325. Activities.insert({
  326. userId,
  327. type: 'swimlane',
  328. activityType: 'removeSwimlane',
  329. boardId: doc.boardId,
  330. swimlaneId: doc._id,
  331. title: doc.title,
  332. });
  333. });
  334. Swimlanes.after.update((userId, doc) => {
  335. if (doc.archived) {
  336. Activities.insert({
  337. userId,
  338. type: 'swimlane',
  339. activityType: 'archivedSwimlane',
  340. swimlaneId: doc._id,
  341. boardId: doc.boardId,
  342. });
  343. }
  344. });
  345. }
  346. //SWIMLANE REST API
  347. if (Meteor.isServer) {
  348. /**
  349. * @operation get_all_swimlanes
  350. *
  351. * @summary Get the list of swimlanes attached to a board
  352. *
  353. * @param {string} boardId the ID of the board
  354. * @return_type [{_id: string,
  355. * title: string}]
  356. */
  357. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes', function(req, res) {
  358. try {
  359. const paramBoardId = req.params.boardId;
  360. Authentication.checkBoardAccess(req.userId, paramBoardId);
  361. JsonRoutes.sendResult(res, {
  362. code: 200,
  363. data: Swimlanes.find({ boardId: paramBoardId, archived: false }).map(
  364. function(doc) {
  365. return {
  366. _id: doc._id,
  367. title: doc.title,
  368. };
  369. },
  370. ),
  371. });
  372. } catch (error) {
  373. JsonRoutes.sendResult(res, {
  374. code: 200,
  375. data: error,
  376. });
  377. }
  378. });
  379. /**
  380. * @operation get_swimlane
  381. *
  382. * @summary Get a swimlane
  383. *
  384. * @param {string} boardId the ID of the board
  385. * @param {string} swimlaneId the ID of the swimlane
  386. * @return_type Swimlanes
  387. */
  388. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId', function(
  389. req,
  390. res,
  391. ) {
  392. try {
  393. const paramBoardId = req.params.boardId;
  394. const paramSwimlaneId = req.params.swimlaneId;
  395. Authentication.checkBoardAccess(req.userId, paramBoardId);
  396. JsonRoutes.sendResult(res, {
  397. code: 200,
  398. data: Swimlanes.findOne({
  399. _id: paramSwimlaneId,
  400. boardId: paramBoardId,
  401. archived: false,
  402. }),
  403. });
  404. } catch (error) {
  405. JsonRoutes.sendResult(res, {
  406. code: 200,
  407. data: error,
  408. });
  409. }
  410. });
  411. /**
  412. * @operation new_swimlane
  413. *
  414. * @summary Add a swimlane to a board
  415. *
  416. * @param {string} boardId the ID of the board
  417. * @param {string} title the new title of the swimlane
  418. * @return_type {_id: string}
  419. */
  420. JsonRoutes.add('POST', '/api/boards/:boardId/swimlanes', function(req, res) {
  421. try {
  422. const paramBoardId = req.params.boardId;
  423. Authentication.checkBoardAccess(req.userId, paramBoardId);
  424. const board = ReactiveCache.getBoard(paramBoardId);
  425. const id = Swimlanes.insert({
  426. title: req.body.title,
  427. boardId: paramBoardId,
  428. sort: board.swimlanes().count(),
  429. });
  430. JsonRoutes.sendResult(res, {
  431. code: 200,
  432. data: {
  433. _id: id,
  434. },
  435. });
  436. } catch (error) {
  437. JsonRoutes.sendResult(res, {
  438. code: 200,
  439. data: error,
  440. });
  441. }
  442. });
  443. /**
  444. * @operation edit_swimlane
  445. *
  446. * @summary Edit the title of a swimlane
  447. *
  448. * @param {string} boardId the ID of the board
  449. * @param {string} swimlaneId the ID of the swimlane to edit
  450. * @param {string} title the new title of the swimlane
  451. * @return_type {_id: string}
  452. */
  453. JsonRoutes.add('PUT', '/api/boards/:boardId/swimlanes/:swimlaneId', function(req, res) {
  454. try {
  455. const paramBoardId = req.params.boardId;
  456. const paramSwimlaneId = req.params.swimlaneId;
  457. Authentication.checkBoardAccess(req.userId, paramBoardId);
  458. const board = ReactiveCache.getBoard(paramBoardId);
  459. const swimlane = Swimlanes.findOne({
  460. _id: paramSwimlaneId,
  461. boardId: paramBoardId,
  462. });
  463. if (!swimlane) {
  464. throw new Meteor.Error('not-found', 'Swimlane not found');
  465. }
  466. Swimlanes.update(
  467. { _id: paramSwimlaneId },
  468. { $set: { title: req.body.title } }
  469. );
  470. JsonRoutes.sendResult(res, {
  471. code: 200,
  472. data: {
  473. _id: paramSwimlaneId,
  474. },
  475. });
  476. } catch (error) {
  477. JsonRoutes.sendResult(res, {
  478. code: 200,
  479. data: error,
  480. });
  481. }
  482. });
  483. /**
  484. * @operation delete_swimlane
  485. *
  486. * @summary Delete a swimlane
  487. *
  488. * @description The swimlane will be deleted, not moved to the recycle bin
  489. *
  490. * @param {string} boardId the ID of the board
  491. * @param {string} swimlaneId the ID of the swimlane
  492. * @return_type {_id: string}
  493. */
  494. JsonRoutes.add(
  495. 'DELETE',
  496. '/api/boards/:boardId/swimlanes/:swimlaneId',
  497. function(req, res) {
  498. try {
  499. const paramBoardId = req.params.boardId;
  500. const paramSwimlaneId = req.params.swimlaneId;
  501. Authentication.checkBoardAccess(req.userId, paramBoardId);
  502. Swimlanes.remove({ _id: paramSwimlaneId, boardId: paramBoardId });
  503. JsonRoutes.sendResult(res, {
  504. code: 200,
  505. data: {
  506. _id: paramSwimlaneId,
  507. },
  508. });
  509. } catch (error) {
  510. JsonRoutes.sendResult(res, {
  511. code: 200,
  512. data: error,
  513. });
  514. }
  515. },
  516. );
  517. }
  518. export default Swimlanes;