swimlanes.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. Swimlanes = new Mongo.Collection('swimlanes');
  2. /**
  3. * A swimlane is an line in the kaban board.
  4. */
  5. Swimlanes.attachSchema(new SimpleSchema({
  6. title: {
  7. /**
  8. * the title of the swimlane
  9. */
  10. type: String,
  11. },
  12. archived: {
  13. /**
  14. * is the swimlane archived?
  15. */
  16. type: Boolean,
  17. autoValue() { // eslint-disable-line consistent-return
  18. if (this.isInsert && !this.isSet) {
  19. return false;
  20. }
  21. },
  22. },
  23. boardId: {
  24. /**
  25. * the ID of the board the swimlane is attached to
  26. */
  27. type: String,
  28. },
  29. createdAt: {
  30. /**
  31. * creation date of the swimlane
  32. */
  33. type: Date,
  34. autoValue() { // eslint-disable-line consistent-return
  35. if (this.isInsert) {
  36. return new Date();
  37. } else {
  38. this.unset();
  39. }
  40. },
  41. },
  42. sort: {
  43. /**
  44. * the sort value of the swimlane
  45. */
  46. type: Number,
  47. decimal: true,
  48. // XXX We should probably provide a default
  49. optional: true,
  50. },
  51. color: {
  52. /**
  53. * the color of the swimlane
  54. */
  55. type: String,
  56. optional: true,
  57. // silver is the default, so it is left out
  58. allowedValues: [
  59. 'white', 'green', 'yellow', 'orange', 'red', 'purple',
  60. 'blue', 'sky', 'lime', 'pink', 'black',
  61. 'peachpuff', 'crimson', 'plum', 'darkgreen',
  62. 'slateblue', 'magenta', 'gold', 'navy', 'gray',
  63. 'saddlebrown', 'paleturquoise', 'mistyrose', 'indigo',
  64. ],
  65. },
  66. updatedAt: {
  67. /**
  68. * when was the swimlane last edited
  69. */
  70. type: Date,
  71. optional: true,
  72. autoValue() { // eslint-disable-line consistent-return
  73. if (this.isUpdate) {
  74. return new Date();
  75. } else {
  76. this.unset();
  77. }
  78. },
  79. },
  80. }));
  81. Swimlanes.allow({
  82. insert(userId, doc) {
  83. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  84. },
  85. update(userId, doc) {
  86. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  87. },
  88. remove(userId, doc) {
  89. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  90. },
  91. fetch: ['boardId'],
  92. });
  93. Swimlanes.helpers({
  94. cards() {
  95. return Cards.find(Filter.mongoSelector({
  96. swimlaneId: this._id,
  97. archived: false,
  98. }), { sort: ['sort'] });
  99. },
  100. allCards() {
  101. return Cards.find({ swimlaneId: this._id });
  102. },
  103. board() {
  104. return Boards.findOne(this.boardId);
  105. },
  106. colorClass() {
  107. if (this.color)
  108. return this.color;
  109. return '';
  110. },
  111. });
  112. Swimlanes.mutations({
  113. rename(title) {
  114. return { $set: { title } };
  115. },
  116. archive() {
  117. return { $set: { archived: true } };
  118. },
  119. restore() {
  120. return { $set: { archived: false } };
  121. },
  122. setColor(newColor) {
  123. if (newColor === 'silver') {
  124. newColor = null;
  125. }
  126. return {
  127. $set: {
  128. color: newColor,
  129. },
  130. };
  131. },
  132. });
  133. Swimlanes.hookOptions.after.update = { fetchPrevious: false };
  134. if (Meteor.isServer) {
  135. Meteor.startup(() => {
  136. Swimlanes._collection._ensureIndex({ boardId: 1 });
  137. });
  138. Swimlanes.after.insert((userId, doc) => {
  139. Activities.insert({
  140. userId,
  141. type: 'swimlane',
  142. activityType: 'createSwimlane',
  143. boardId: doc.boardId,
  144. swimlaneId: doc._id,
  145. });
  146. });
  147. Swimlanes.before.remove((userId, doc) => {
  148. Activities.insert({
  149. userId,
  150. type: 'swimlane',
  151. activityType: 'removeSwimlane',
  152. boardId: doc.boardId,
  153. swimlaneId: doc._id,
  154. title: doc.title,
  155. });
  156. });
  157. Swimlanes.after.update((userId, doc) => {
  158. if (doc.archived) {
  159. Activities.insert({
  160. userId,
  161. type: 'swimlane',
  162. activityType: 'archivedSwimlane',
  163. swimlaneId: doc._id,
  164. boardId: doc.boardId,
  165. });
  166. }
  167. });
  168. }
  169. //SWIMLANE REST API
  170. if (Meteor.isServer) {
  171. /**
  172. * @operation get_all_swimlanes
  173. *
  174. * @summary Get the list of swimlanes attached to a board
  175. *
  176. * @param {string} boardId the ID of the board
  177. * @return_type [{_id: string,
  178. * title: string}]
  179. */
  180. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes', function (req, res) {
  181. try {
  182. const paramBoardId = req.params.boardId;
  183. Authentication.checkBoardAccess( req.userId, paramBoardId);
  184. JsonRoutes.sendResult(res, {
  185. code: 200,
  186. data: Swimlanes.find({ boardId: paramBoardId, archived: false }).map(function (doc) {
  187. return {
  188. _id: doc._id,
  189. title: doc.title,
  190. };
  191. }),
  192. });
  193. }
  194. catch (error) {
  195. JsonRoutes.sendResult(res, {
  196. code: 200,
  197. data: error,
  198. });
  199. }
  200. });
  201. /**
  202. * @operation get_swimlane
  203. *
  204. * @summary Get a swimlane
  205. *
  206. * @param {string} boardId the ID of the board
  207. * @param {string} swimlaneId the ID of the swimlane
  208. * @return_type Swimlanes
  209. */
  210. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId', function (req, res) {
  211. try {
  212. const paramBoardId = req.params.boardId;
  213. const paramSwimlaneId = req.params.swimlaneId;
  214. Authentication.checkBoardAccess( req.userId, paramBoardId);
  215. JsonRoutes.sendResult(res, {
  216. code: 200,
  217. data: Swimlanes.findOne({ _id: paramSwimlaneId, boardId: paramBoardId, archived: false }),
  218. });
  219. }
  220. catch (error) {
  221. JsonRoutes.sendResult(res, {
  222. code: 200,
  223. data: error,
  224. });
  225. }
  226. });
  227. /**
  228. * @operation new_swimlane
  229. *
  230. * @summary Add a swimlane to a board
  231. *
  232. * @param {string} boardId the ID of the board
  233. * @param {string} title the new title of the swimlane
  234. * @return_type {_id: string}
  235. */
  236. JsonRoutes.add('POST', '/api/boards/:boardId/swimlanes', function (req, res) {
  237. try {
  238. Authentication.checkUserId( req.userId);
  239. const paramBoardId = req.params.boardId;
  240. const id = Swimlanes.insert({
  241. title: req.body.title,
  242. boardId: paramBoardId,
  243. });
  244. JsonRoutes.sendResult(res, {
  245. code: 200,
  246. data: {
  247. _id: id,
  248. },
  249. });
  250. }
  251. catch (error) {
  252. JsonRoutes.sendResult(res, {
  253. code: 200,
  254. data: error,
  255. });
  256. }
  257. });
  258. /**
  259. * @operation delete_swimlane
  260. *
  261. * @summary Delete a swimlane
  262. *
  263. * @description The swimlane will be deleted, not moved to the recycle bin
  264. *
  265. * @param {string} boardId the ID of the board
  266. * @param {string} swimlaneId the ID of the swimlane
  267. * @return_type {_id: string}
  268. */
  269. JsonRoutes.add('DELETE', '/api/boards/:boardId/swimlanes/:swimlaneId', function (req, res) {
  270. try {
  271. Authentication.checkUserId( req.userId);
  272. const paramBoardId = req.params.boardId;
  273. const paramSwimlaneId = req.params.swimlaneId;
  274. Swimlanes.remove({ _id: paramSwimlaneId, boardId: paramBoardId });
  275. JsonRoutes.sendResult(res, {
  276. code: 200,
  277. data: {
  278. _id: paramSwimlaneId,
  279. },
  280. });
  281. }
  282. catch (error) {
  283. JsonRoutes.sendResult(res, {
  284. code: 200,
  285. data: error,
  286. });
  287. }
  288. });
  289. }