swimlanes.js 10 KB

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