swimlanes.js 11 KB

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