swimlanes.js 11 KB

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