swimlanes.js 10.0 KB

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