swimlanes.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 `swimlane-${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.archivedSwimlanes = () => {
  265. return Swimlanes.find({ archived: true });
  266. };
  267. Swimlanes.archivedSwimlaneIds = () => {
  268. return Swimlanes.archivedSwimlanes().map(swim => {
  269. return swim._id;
  270. });
  271. };
  272. Swimlanes.hookOptions.after.update = { fetchPrevious: false };
  273. if (Meteor.isServer) {
  274. Meteor.startup(() => {
  275. Swimlanes._collection._ensureIndex({ modifiedAt: -1 });
  276. Swimlanes._collection._ensureIndex({ boardId: 1 });
  277. });
  278. Swimlanes.after.insert((userId, doc) => {
  279. Activities.insert({
  280. userId,
  281. type: 'swimlane',
  282. activityType: 'createSwimlane',
  283. boardId: doc.boardId,
  284. swimlaneId: doc._id,
  285. });
  286. });
  287. Swimlanes.before.remove(function(userId, doc) {
  288. const lists = Lists.find(
  289. {
  290. boardId: doc.boardId,
  291. swimlaneId: { $in: [doc._id, ''] },
  292. archived: false,
  293. },
  294. { sort: ['sort'] },
  295. );
  296. if (lists.count() < 2) {
  297. lists.forEach(list => {
  298. list.remove();
  299. });
  300. } else {
  301. Cards.remove({ swimlaneId: doc._id });
  302. }
  303. Activities.insert({
  304. userId,
  305. type: 'swimlane',
  306. activityType: 'removeSwimlane',
  307. boardId: doc.boardId,
  308. swimlaneId: doc._id,
  309. title: doc.title,
  310. });
  311. });
  312. Swimlanes.after.update((userId, doc) => {
  313. if (doc.archived) {
  314. Activities.insert({
  315. userId,
  316. type: 'swimlane',
  317. activityType: 'archivedSwimlane',
  318. swimlaneId: doc._id,
  319. boardId: doc.boardId,
  320. });
  321. }
  322. });
  323. }
  324. //SWIMLANE REST API
  325. if (Meteor.isServer) {
  326. /**
  327. * @operation get_all_swimlanes
  328. *
  329. * @summary Get the list of swimlanes attached to a board
  330. *
  331. * @param {string} boardId the ID of the board
  332. * @return_type [{_id: string,
  333. * title: string}]
  334. */
  335. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes', function(req, res) {
  336. try {
  337. const paramBoardId = req.params.boardId;
  338. Authentication.checkBoardAccess(req.userId, paramBoardId);
  339. JsonRoutes.sendResult(res, {
  340. code: 200,
  341. data: Swimlanes.find({ boardId: paramBoardId, archived: false }).map(
  342. function(doc) {
  343. return {
  344. _id: doc._id,
  345. title: doc.title,
  346. };
  347. },
  348. ),
  349. });
  350. } catch (error) {
  351. JsonRoutes.sendResult(res, {
  352. code: 200,
  353. data: error,
  354. });
  355. }
  356. });
  357. /**
  358. * @operation get_swimlane
  359. *
  360. * @summary Get a swimlane
  361. *
  362. * @param {string} boardId the ID of the board
  363. * @param {string} swimlaneId the ID of the swimlane
  364. * @return_type Swimlanes
  365. */
  366. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId', function(
  367. req,
  368. res,
  369. ) {
  370. try {
  371. const paramBoardId = req.params.boardId;
  372. const paramSwimlaneId = req.params.swimlaneId;
  373. Authentication.checkBoardAccess(req.userId, paramBoardId);
  374. JsonRoutes.sendResult(res, {
  375. code: 200,
  376. data: Swimlanes.findOne({
  377. _id: paramSwimlaneId,
  378. boardId: paramBoardId,
  379. archived: false,
  380. }),
  381. });
  382. } catch (error) {
  383. JsonRoutes.sendResult(res, {
  384. code: 200,
  385. data: error,
  386. });
  387. }
  388. });
  389. /**
  390. * @operation new_swimlane
  391. *
  392. * @summary Add a swimlane to a board
  393. *
  394. * @param {string} boardId the ID of the board
  395. * @param {string} title the new title of the swimlane
  396. * @return_type {_id: string}
  397. */
  398. JsonRoutes.add('POST', '/api/boards/:boardId/swimlanes', function(req, res) {
  399. try {
  400. Authentication.checkUserId(req.userId);
  401. const paramBoardId = req.params.boardId;
  402. const board = Boards.findOne(paramBoardId);
  403. const id = Swimlanes.insert({
  404. title: req.body.title,
  405. boardId: paramBoardId,
  406. sort: board.swimlanes().count(),
  407. });
  408. JsonRoutes.sendResult(res, {
  409. code: 200,
  410. data: {
  411. _id: id,
  412. },
  413. });
  414. } catch (error) {
  415. JsonRoutes.sendResult(res, {
  416. code: 200,
  417. data: error,
  418. });
  419. }
  420. });
  421. /**
  422. * @operation delete_swimlane
  423. *
  424. * @summary Delete a swimlane
  425. *
  426. * @description The swimlane will be deleted, not moved to the recycle bin
  427. *
  428. * @param {string} boardId the ID of the board
  429. * @param {string} swimlaneId the ID of the swimlane
  430. * @return_type {_id: string}
  431. */
  432. JsonRoutes.add(
  433. 'DELETE',
  434. '/api/boards/:boardId/swimlanes/:swimlaneId',
  435. function(req, res) {
  436. try {
  437. Authentication.checkUserId(req.userId);
  438. const paramBoardId = req.params.boardId;
  439. const paramSwimlaneId = req.params.swimlaneId;
  440. Swimlanes.remove({ _id: paramSwimlaneId, boardId: paramBoardId });
  441. JsonRoutes.sendResult(res, {
  442. code: 200,
  443. data: {
  444. _id: paramSwimlaneId,
  445. },
  446. });
  447. } catch (error) {
  448. JsonRoutes.sendResult(res, {
  449. code: 200,
  450. data: error,
  451. });
  452. }
  453. },
  454. );
  455. }
  456. export default Swimlanes;