swimlanes.js 11 KB

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