swimlanes.js 11 KB

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