swimlanes.js 13 KB

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