2
0

swimlanes.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. Swimlanes = new Mongo.Collection('swimlanes');
  2. Swimlanes.attachSchema(new SimpleSchema({
  3. title: {
  4. type: String,
  5. },
  6. archived: {
  7. type: Boolean,
  8. autoValue() { // eslint-disable-line consistent-return
  9. if (this.isInsert && !this.isSet) {
  10. return false;
  11. }
  12. },
  13. },
  14. boardId: {
  15. type: String,
  16. },
  17. createdAt: {
  18. type: Date,
  19. autoValue() { // eslint-disable-line consistent-return
  20. if (this.isInsert) {
  21. return new Date();
  22. } else {
  23. this.unset();
  24. }
  25. },
  26. },
  27. sort: {
  28. type: Number,
  29. decimal: true,
  30. // XXX We should probably provide a default
  31. optional: true,
  32. },
  33. updatedAt: {
  34. type: Date,
  35. optional: true,
  36. autoValue() { // eslint-disable-line consistent-return
  37. if (this.isUpdate) {
  38. return new Date();
  39. } else {
  40. this.unset();
  41. }
  42. },
  43. },
  44. }));
  45. Swimlanes.allow({
  46. insert(userId, doc) {
  47. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  48. },
  49. update(userId, doc) {
  50. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  51. },
  52. remove(userId, doc) {
  53. return allowIsBoardMemberCommentOnly(userId, Boards.findOne(doc.boardId));
  54. },
  55. fetch: ['boardId'],
  56. });
  57. Swimlanes.helpers({
  58. cards() {
  59. return Cards.find(Filter.mongoSelector({
  60. swimlaneId: this._id,
  61. archived: false,
  62. }), { sort: ['sort'] });
  63. },
  64. allCards() {
  65. return Cards.find({ swimlaneId: this._id });
  66. },
  67. board() {
  68. return Boards.findOne(this.boardId);
  69. },
  70. });
  71. Swimlanes.mutations({
  72. rename(title) {
  73. return { $set: { title } };
  74. },
  75. archive() {
  76. return { $set: { archived: true } };
  77. },
  78. restore() {
  79. return { $set: { archived: false } };
  80. },
  81. });
  82. Swimlanes.hookOptions.after.update = { fetchPrevious: false };
  83. if (Meteor.isServer) {
  84. Meteor.startup(() => {
  85. Swimlanes._collection._ensureIndex({ boardId: 1 });
  86. });
  87. Swimlanes.after.insert((userId, doc) => {
  88. Activities.insert({
  89. userId,
  90. type: 'swimlane',
  91. activityType: 'createSwimlane',
  92. boardId: doc.boardId,
  93. swimlaneId: doc._id,
  94. });
  95. });
  96. Swimlanes.before.remove((userId, doc) => {
  97. Activities.insert({
  98. userId,
  99. type: 'swimlane',
  100. activityType: 'removeSwimlane',
  101. boardId: doc.boardId,
  102. swimlaneId: doc._id,
  103. title: doc.title,
  104. });
  105. });
  106. Swimlanes.after.update((userId, doc) => {
  107. if (doc.archived) {
  108. Activities.insert({
  109. userId,
  110. type: 'swimlane',
  111. activityType: 'archivedSwimlane',
  112. swimlaneId: doc._id,
  113. boardId: doc.boardId,
  114. });
  115. }
  116. });
  117. }
  118. //SWIMLANE REST API
  119. if (Meteor.isServer) {
  120. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes', function (req, res) {
  121. try {
  122. const paramBoardId = req.params.boardId;
  123. Authentication.checkBoardAccess( req.userId, paramBoardId);
  124. JsonRoutes.sendResult(res, {
  125. code: 200,
  126. data: Swimlanes.find({ boardId: paramBoardId, archived: false }).map(function (doc) {
  127. return {
  128. _id: doc._id,
  129. title: doc.title,
  130. };
  131. }),
  132. });
  133. }
  134. catch (error) {
  135. JsonRoutes.sendResult(res, {
  136. code: 200,
  137. data: error,
  138. });
  139. }
  140. });
  141. JsonRoutes.add('GET', '/api/boards/:boardId/swimlanes/:swimlaneId', function (req, res) {
  142. try {
  143. const paramBoardId = req.params.boardId;
  144. const paramSwimlaneId = req.params.swimlaneId;
  145. Authentication.checkBoardAccess( req.userId, paramBoardId);
  146. JsonRoutes.sendResult(res, {
  147. code: 200,
  148. data: Swimlanes.findOne({ _id: paramSwimlaneId, boardId: paramBoardId, archived: false }),
  149. });
  150. }
  151. catch (error) {
  152. JsonRoutes.sendResult(res, {
  153. code: 200,
  154. data: error,
  155. });
  156. }
  157. });
  158. JsonRoutes.add('POST', '/api/boards/:boardId/swimlanes', function (req, res) {
  159. try {
  160. Authentication.checkUserId( req.userId);
  161. const paramBoardId = req.params.boardId;
  162. const id = Swimlanes.insert({
  163. title: req.body.title,
  164. boardId: paramBoardId,
  165. });
  166. JsonRoutes.sendResult(res, {
  167. code: 200,
  168. data: {
  169. _id: id,
  170. },
  171. });
  172. }
  173. catch (error) {
  174. JsonRoutes.sendResult(res, {
  175. code: 200,
  176. data: error,
  177. });
  178. }
  179. });
  180. JsonRoutes.add('DELETE', '/api/boards/:boardId/swimlanes/:swimlaneId', function (req, res) {
  181. try {
  182. Authentication.checkUserId( req.userId);
  183. const paramBoardId = req.params.boardId;
  184. const paramSwimlaneId = req.params.swimlaneId;
  185. Swimlanes.remove({ _id: paramSwimlaneId, boardId: paramBoardId });
  186. JsonRoutes.sendResult(res, {
  187. code: 200,
  188. data: {
  189. _id: paramSwimlaneId,
  190. },
  191. });
  192. }
  193. catch (error) {
  194. JsonRoutes.sendResult(res, {
  195. code: 200,
  196. data: error,
  197. });
  198. }
  199. });
  200. }