swimlanes.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. updatedAt: {
  52. /**
  53. * when was the swimlane last edited
  54. */
  55. type: Date,
  56. optional: true,
  57. autoValue() { // eslint-disable-line consistent-return
  58. if (this.isUpdate) {
  59. return new Date();
  60. } else {
  61. this.unset();
  62. }
  63. },
  64. },
  65. }));
  66. Swimlanes.allow({
  67. insert(userId, doc) {
  68. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  69. },
  70. update(userId, doc) {
  71. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  72. },
  73. remove(userId, doc) {
  74. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  75. },
  76. fetch: ['boardId'],
  77. });
  78. Swimlanes.helpers({
  79. cards() {
  80. return Cards.find(Filter.mongoSelector({
  81. swimlaneId: this._id,
  82. archived: false,
  83. }), { sort: ['sort'] });
  84. },
  85. allCards() {
  86. return Cards.find({ swimlaneId: this._id });
  87. },
  88. board() {
  89. return Boards.findOne(this.boardId);
  90. },
  91. });
  92. Swimlanes.mutations({
  93. rename(title) {
  94. return { $set: { title } };
  95. },
  96. archive() {
  97. return { $set: { archived: true } };
  98. },
  99. restore() {
  100. return { $set: { archived: false } };
  101. },
  102. });
  103. Swimlanes.hookOptions.after.update = { fetchPrevious: false };
  104. if (Meteor.isServer) {
  105. Meteor.startup(() => {
  106. Swimlanes._collection._ensureIndex({ boardId: 1 });
  107. });
  108. Swimlanes.after.insert((userId, doc) => {
  109. Activities.insert({
  110. userId,
  111. type: 'swimlane',
  112. activityType: 'createSwimlane',
  113. boardId: doc.boardId,
  114. swimlaneId: doc._id,
  115. });
  116. });
  117. Swimlanes.before.remove((userId, doc) => {
  118. Activities.insert({
  119. userId,
  120. type: 'swimlane',
  121. activityType: 'removeSwimlane',
  122. boardId: doc.boardId,
  123. swimlaneId: doc._id,
  124. title: doc.title,
  125. });
  126. });
  127. Swimlanes.after.update((userId, doc) => {
  128. if (doc.archived) {
  129. Activities.insert({
  130. userId,
  131. type: 'swimlane',
  132. activityType: 'archivedSwimlane',
  133. swimlaneId: doc._id,
  134. boardId: doc.boardId,
  135. });
  136. }
  137. });
  138. }
  139. //SWIMLANE REST API
  140. if (Meteor.isServer) {
  141. /**
  142. * @operation get_all_swimlanes
  143. *
  144. * @summary Get the list of swimlanes attached to a board
  145. *
  146. * @param {string} boardId the ID of the board
  147. * @return_type [{_id: string,
  148. * title: string}]
  149. */
  150. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes', function (req, res) {
  151. try {
  152. const paramBoardId = req.params.boardId;
  153. Authentication.checkBoardAccess( req.userId, paramBoardId);
  154. JsonRoutes.sendResult(res, {
  155. code: 200,
  156. data: Swimlanes.find({ boardId: paramBoardId, archived: false }).map(function (doc) {
  157. return {
  158. _id: doc._id,
  159. title: doc.title,
  160. };
  161. }),
  162. });
  163. }
  164. catch (error) {
  165. JsonRoutes.sendResult(res, {
  166. code: 200,
  167. data: error,
  168. });
  169. }
  170. });
  171. /**
  172. * @operation get_swimlane
  173. *
  174. * @summary Get a swimlane
  175. *
  176. * @param {string} boardId the ID of the board
  177. * @param {string} swimlaneId the ID of the swimlane
  178. * @return_type Swimlanes
  179. */
  180. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId', function (req, res) {
  181. try {
  182. const paramBoardId = req.params.boardId;
  183. const paramSwimlaneId = req.params.swimlaneId;
  184. Authentication.checkBoardAccess( req.userId, paramBoardId);
  185. JsonRoutes.sendResult(res, {
  186. code: 200,
  187. data: Swimlanes.findOne({ _id: paramSwimlaneId, boardId: paramBoardId, archived: false }),
  188. });
  189. }
  190. catch (error) {
  191. JsonRoutes.sendResult(res, {
  192. code: 200,
  193. data: error,
  194. });
  195. }
  196. });
  197. /**
  198. * @operation new_swimlane
  199. *
  200. * @summary Add a swimlane to a board
  201. *
  202. * @param {string} boardId the ID of the board
  203. * @param {string} title the new title of the swimlane
  204. * @return_type {_id: string}
  205. */
  206. JsonRoutes.add('POST', '/api/boards/:boardId/swimlanes', function (req, res) {
  207. try {
  208. Authentication.checkUserId( req.userId);
  209. const paramBoardId = req.params.boardId;
  210. const id = Swimlanes.insert({
  211. title: req.body.title,
  212. boardId: paramBoardId,
  213. });
  214. JsonRoutes.sendResult(res, {
  215. code: 200,
  216. data: {
  217. _id: id,
  218. },
  219. });
  220. }
  221. catch (error) {
  222. JsonRoutes.sendResult(res, {
  223. code: 200,
  224. data: error,
  225. });
  226. }
  227. });
  228. /**
  229. * @operation delete_swimlane
  230. *
  231. * @summary Delete a swimlane
  232. *
  233. * @description The swimlane will be deleted, not moved to the recycle bin
  234. *
  235. * @param {string} boardId the ID of the board
  236. * @param {string} swimlaneId the ID of the swimlane
  237. * @return_type {_id: string}
  238. */
  239. JsonRoutes.add('DELETE', '/api/boards/:boardId/swimlanes/:swimlaneId', function (req, res) {
  240. try {
  241. Authentication.checkUserId( req.userId);
  242. const paramBoardId = req.params.boardId;
  243. const paramSwimlaneId = req.params.swimlaneId;
  244. Swimlanes.remove({ _id: paramSwimlaneId, boardId: paramBoardId });
  245. JsonRoutes.sendResult(res, {
  246. code: 200,
  247. data: {
  248. _id: paramSwimlaneId,
  249. },
  250. });
  251. }
  252. catch (error) {
  253. JsonRoutes.sendResult(res, {
  254. code: 200,
  255. data: error,
  256. });
  257. }
  258. });
  259. }