swimlanes.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. //currentUser = Meteor.user();
  171. //if (currentUser) {
  172. // enabled = Meteor.user().hasSortBy();
  173. //}
  174. //return enabled ? this.newestLists() : this.draggableLists();
  175. return this.draggableLists();
  176. },
  177. newestLists() {
  178. // sorted lists from newest to the oldest, by its creation date or its cards' last modification date
  179. return Lists.find(
  180. {
  181. boardId: this.boardId,
  182. swimlaneId: { $in: [this._id, ''] },
  183. archived: false,
  184. },
  185. { sort: { modifiedAt: -1 } },
  186. );
  187. },
  188. draggableLists() {
  189. return Lists.find(
  190. {
  191. boardId: this.boardId,
  192. swimlaneId: { $in: [this._id, ''] },
  193. archived: false,
  194. },
  195. { sort: ['sort'] },
  196. );
  197. },
  198. myLists() {
  199. return Lists.find({ swimlaneId: this._id });
  200. },
  201. allCards() {
  202. return Cards.find({ swimlaneId: this._id });
  203. },
  204. board() {
  205. return Boards.findOne(this.boardId);
  206. },
  207. colorClass() {
  208. if (this.color) return this.color;
  209. return '';
  210. },
  211. isTemplateSwimlane() {
  212. return this.type === 'template-swimlane';
  213. },
  214. isTemplateContainer() {
  215. return this.type === 'template-container';
  216. },
  217. isListTemplatesSwimlane() {
  218. const user = Users.findOne(Meteor.userId());
  219. return (user.profile || {}).listTemplatesSwimlaneId === this._id;
  220. },
  221. isCardTemplatesSwimlane() {
  222. const user = Users.findOne(Meteor.userId());
  223. return (user.profile || {}).cardTemplatesSwimlaneId === this._id;
  224. },
  225. isBoardTemplatesSwimlane() {
  226. const user = Users.findOne(Meteor.userId());
  227. return (user.profile || {}).boardTemplatesSwimlaneId === this._id;
  228. },
  229. remove() {
  230. Swimlanes.remove({ _id: this._id });
  231. },
  232. });
  233. Swimlanes.mutations({
  234. rename(title) {
  235. return { $set: { title } };
  236. },
  237. archive() {
  238. if (this.isTemplateSwimlane()) {
  239. this.myLists().forEach(list => {
  240. return list.archive();
  241. });
  242. }
  243. return { $set: { archived: true } };
  244. },
  245. restore() {
  246. if (this.isTemplateSwimlane()) {
  247. this.myLists().forEach(list => {
  248. return list.restore();
  249. });
  250. }
  251. return { $set: { archived: false } };
  252. },
  253. setColor(newColor) {
  254. if (newColor === 'silver') {
  255. newColor = null;
  256. }
  257. return {
  258. $set: {
  259. color: newColor,
  260. },
  261. };
  262. },
  263. });
  264. Swimlanes.hookOptions.after.update = { fetchPrevious: false };
  265. if (Meteor.isServer) {
  266. Meteor.startup(() => {
  267. Swimlanes._collection._ensureIndex({ modifiedAt: -1 });
  268. Swimlanes._collection._ensureIndex({ boardId: 1 });
  269. });
  270. Swimlanes.after.insert((userId, doc) => {
  271. Activities.insert({
  272. userId,
  273. type: 'swimlane',
  274. activityType: 'createSwimlane',
  275. boardId: doc.boardId,
  276. swimlaneId: doc._id,
  277. });
  278. });
  279. Swimlanes.before.remove(function(userId, doc) {
  280. const lists = Lists.find(
  281. {
  282. boardId: doc.boardId,
  283. swimlaneId: { $in: [doc._id, ''] },
  284. archived: false,
  285. },
  286. { sort: ['sort'] },
  287. );
  288. if (lists.count() < 2) {
  289. lists.forEach(list => {
  290. list.remove();
  291. });
  292. } else {
  293. Cards.remove({ swimlaneId: doc._id });
  294. }
  295. Activities.insert({
  296. userId,
  297. type: 'swimlane',
  298. activityType: 'removeSwimlane',
  299. boardId: doc.boardId,
  300. swimlaneId: doc._id,
  301. title: doc.title,
  302. });
  303. });
  304. Swimlanes.after.update((userId, doc) => {
  305. if (doc.archived) {
  306. Activities.insert({
  307. userId,
  308. type: 'swimlane',
  309. activityType: 'archivedSwimlane',
  310. swimlaneId: doc._id,
  311. boardId: doc.boardId,
  312. });
  313. }
  314. });
  315. }
  316. //SWIMLANE REST API
  317. if (Meteor.isServer) {
  318. /**
  319. * @operation get_all_swimlanes
  320. *
  321. * @summary Get the list of swimlanes attached to a board
  322. *
  323. * @param {string} boardId the ID of the board
  324. * @return_type [{_id: string,
  325. * title: string}]
  326. */
  327. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes', function(req, res) {
  328. try {
  329. const paramBoardId = req.params.boardId;
  330. Authentication.checkBoardAccess(req.userId, paramBoardId);
  331. JsonRoutes.sendResult(res, {
  332. code: 200,
  333. data: Swimlanes.find({ boardId: paramBoardId, archived: false }).map(
  334. function(doc) {
  335. return {
  336. _id: doc._id,
  337. title: doc.title,
  338. };
  339. },
  340. ),
  341. });
  342. } catch (error) {
  343. JsonRoutes.sendResult(res, {
  344. code: 200,
  345. data: error,
  346. });
  347. }
  348. });
  349. /**
  350. * @operation get_swimlane
  351. *
  352. * @summary Get a swimlane
  353. *
  354. * @param {string} boardId the ID of the board
  355. * @param {string} swimlaneId the ID of the swimlane
  356. * @return_type Swimlanes
  357. */
  358. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId', function(
  359. req,
  360. res,
  361. ) {
  362. try {
  363. const paramBoardId = req.params.boardId;
  364. const paramSwimlaneId = req.params.swimlaneId;
  365. Authentication.checkBoardAccess(req.userId, paramBoardId);
  366. JsonRoutes.sendResult(res, {
  367. code: 200,
  368. data: Swimlanes.findOne({
  369. _id: paramSwimlaneId,
  370. boardId: paramBoardId,
  371. archived: false,
  372. }),
  373. });
  374. } catch (error) {
  375. JsonRoutes.sendResult(res, {
  376. code: 200,
  377. data: error,
  378. });
  379. }
  380. });
  381. /**
  382. * @operation new_swimlane
  383. *
  384. * @summary Add a swimlane to a board
  385. *
  386. * @param {string} boardId the ID of the board
  387. * @param {string} title the new title of the swimlane
  388. * @return_type {_id: string}
  389. */
  390. JsonRoutes.add('POST', '/api/boards/:boardId/swimlanes', function(req, res) {
  391. try {
  392. Authentication.checkUserId(req.userId);
  393. const paramBoardId = req.params.boardId;
  394. const board = Boards.findOne(paramBoardId);
  395. const id = Swimlanes.insert({
  396. title: req.body.title,
  397. boardId: paramBoardId,
  398. sort: board.swimlanes().count(),
  399. });
  400. JsonRoutes.sendResult(res, {
  401. code: 200,
  402. data: {
  403. _id: id,
  404. },
  405. });
  406. } catch (error) {
  407. JsonRoutes.sendResult(res, {
  408. code: 200,
  409. data: error,
  410. });
  411. }
  412. });
  413. /**
  414. * @operation delete_swimlane
  415. *
  416. * @summary Delete a swimlane
  417. *
  418. * @description The swimlane will be deleted, not moved to the recycle bin
  419. *
  420. * @param {string} boardId the ID of the board
  421. * @param {string} swimlaneId the ID of the swimlane
  422. * @return_type {_id: string}
  423. */
  424. JsonRoutes.add(
  425. 'DELETE',
  426. '/api/boards/:boardId/swimlanes/:swimlaneId',
  427. function(req, res) {
  428. try {
  429. Authentication.checkUserId(req.userId);
  430. const paramBoardId = req.params.boardId;
  431. const paramSwimlaneId = req.params.swimlaneId;
  432. Swimlanes.remove({ _id: paramSwimlaneId, boardId: paramBoardId });
  433. JsonRoutes.sendResult(res, {
  434. code: 200,
  435. data: {
  436. _id: paramSwimlaneId,
  437. },
  438. });
  439. } catch (error) {
  440. JsonRoutes.sendResult(res, {
  441. code: 200,
  442. data: error,
  443. });
  444. }
  445. },
  446. );
  447. }
  448. export default Swimlanes;