swimlanes.js 12 KB

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