integrations.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. Integrations = new Mongo.Collection('integrations');
  2. /**
  3. * Integration with third-party applications
  4. */
  5. Integrations.attachSchema(new SimpleSchema({
  6. enabled: {
  7. /**
  8. * is the integration enabled?
  9. */
  10. type: Boolean,
  11. defaultValue: true,
  12. },
  13. title: {
  14. /**
  15. * name of the integration
  16. */
  17. type: String,
  18. optional: true,
  19. },
  20. type: {
  21. /**
  22. * type of the integratation (Default to 'outgoing-webhooks')
  23. */
  24. type: String,
  25. defaultValue: 'outgoing-webhooks',
  26. },
  27. activities: {
  28. /**
  29. * activities the integration gets triggered (list)
  30. */
  31. type: [String],
  32. defaultValue: ['all'],
  33. },
  34. url: { // URL validation regex (https://mathiasbynens.be/demo/url-regex)
  35. /**
  36. * URL validation regex (https://mathiasbynens.be/demo/url-regex)
  37. */
  38. type: String,
  39. },
  40. token: {
  41. /**
  42. * token of the integration
  43. */
  44. type: String,
  45. optional: true,
  46. },
  47. boardId: {
  48. /**
  49. * Board ID of the integration
  50. */
  51. type: String,
  52. },
  53. createdAt: {
  54. /**
  55. * Creation date of the integration
  56. */
  57. type: Date,
  58. denyUpdate: false,
  59. autoValue() { // eslint-disable-line consistent-return
  60. if (this.isInsert) {
  61. return new Date();
  62. } else {
  63. this.unset();
  64. }
  65. },
  66. },
  67. userId: {
  68. /**
  69. * user ID who created the interation
  70. */
  71. type: String,
  72. },
  73. }));
  74. Integrations.allow({
  75. insert(userId, doc) {
  76. return allowIsBoardAdmin(userId, Boards.findOne(doc.boardId));
  77. },
  78. update(userId, doc) {
  79. return allowIsBoardAdmin(userId, Boards.findOne(doc.boardId));
  80. },
  81. remove(userId, doc) {
  82. return allowIsBoardAdmin(userId, Boards.findOne(doc.boardId));
  83. },
  84. fetch: ['boardId'],
  85. });
  86. //INTEGRATIONS REST API
  87. if (Meteor.isServer) {
  88. Meteor.startup(() => {
  89. Integrations._collection._ensureIndex({ boardId: 1 });
  90. });
  91. /**
  92. * @operation get_all_integrations
  93. * @summary Get all integrations in board
  94. *
  95. * @param {string} boardId the board ID
  96. * @return_type [Integrations]
  97. */
  98. JsonRoutes.add('GET', '/api/boards/:boardId/integrations', function(req, res) {
  99. try {
  100. const paramBoardId = req.params.boardId;
  101. Authentication.checkBoardAccess(req.userId, paramBoardId);
  102. const data = Integrations.find({ boardId: paramBoardId }, { fields: { token: 0 } }).map(function(doc) {
  103. return doc;
  104. });
  105. JsonRoutes.sendResult(res, {code: 200, data});
  106. }
  107. catch (error) {
  108. JsonRoutes.sendResult(res, {
  109. code: 200,
  110. data: error,
  111. });
  112. }
  113. });
  114. /**
  115. * @operation get_integration
  116. * @summary Get a single integration in board
  117. *
  118. * @param {string} boardId the board ID
  119. * @param {string} intId the integration ID
  120. * @return_type Integrations
  121. */
  122. JsonRoutes.add('GET', '/api/boards/:boardId/integrations/:intId', function(req, res) {
  123. try {
  124. const paramBoardId = req.params.boardId;
  125. const paramIntId = req.params.intId;
  126. Authentication.checkBoardAccess(req.userId, paramBoardId);
  127. JsonRoutes.sendResult(res, {
  128. code: 200,
  129. data: Integrations.findOne({ _id: paramIntId, boardId: paramBoardId }, { fields: { token: 0 } }),
  130. });
  131. }
  132. catch (error) {
  133. JsonRoutes.sendResult(res, {
  134. code: 200,
  135. data: error,
  136. });
  137. }
  138. });
  139. /**
  140. * @operation new_integration
  141. * @summary Create a new integration
  142. *
  143. * @param {string} boardId the board ID
  144. * @param {string} url the URL of the integration
  145. * @return_type {_id: string}
  146. */
  147. JsonRoutes.add('POST', '/api/boards/:boardId/integrations', function(req, res) {
  148. try {
  149. const paramBoardId = req.params.boardId;
  150. Authentication.checkBoardAccess(req.userId, paramBoardId);
  151. const id = Integrations.insert({
  152. userId: req.userId,
  153. boardId: paramBoardId,
  154. url: req.body.url,
  155. });
  156. JsonRoutes.sendResult(res, {
  157. code: 200,
  158. data: {
  159. _id: id,
  160. },
  161. });
  162. }
  163. catch (error) {
  164. JsonRoutes.sendResult(res, {
  165. code: 200,
  166. data: error,
  167. });
  168. }
  169. });
  170. /**
  171. * @operation edit_integration
  172. * @summary Edit integration data
  173. *
  174. * @param {string} boardId the board ID
  175. * @param {string} intId the integration ID
  176. * @param {string} [enabled] is the integration enabled?
  177. * @param {string} [title] new name of the integration
  178. * @param {string} [url] new URL of the integration
  179. * @param {string} [token] new token of the integration
  180. * @param {string} [activities] new list of activities of the integration
  181. * @return_type {_id: string}
  182. */
  183. JsonRoutes.add('PUT', '/api/boards/:boardId/integrations/:intId', function (req, res) {
  184. try {
  185. const paramBoardId = req.params.boardId;
  186. const paramIntId = req.params.intId;
  187. Authentication.checkBoardAccess(req.userId, paramBoardId);
  188. if (req.body.hasOwnProperty('enabled')) {
  189. const newEnabled = req.body.enabled;
  190. Integrations.direct.update({_id: paramIntId, boardId: paramBoardId},
  191. {$set: {enabled: newEnabled}});
  192. }
  193. if (req.body.hasOwnProperty('title')) {
  194. const newTitle = req.body.title;
  195. Integrations.direct.update({_id: paramIntId, boardId: paramBoardId},
  196. {$set: {title: newTitle}});
  197. }
  198. if (req.body.hasOwnProperty('url')) {
  199. const newUrl = req.body.url;
  200. Integrations.direct.update({_id: paramIntId, boardId: paramBoardId},
  201. {$set: {url: newUrl}});
  202. }
  203. if (req.body.hasOwnProperty('token')) {
  204. const newToken = req.body.token;
  205. Integrations.direct.update({_id: paramIntId, boardId: paramBoardId},
  206. {$set: {token: newToken}});
  207. }
  208. if (req.body.hasOwnProperty('activities')) {
  209. const newActivities = req.body.activities;
  210. Integrations.direct.update({_id: paramIntId, boardId: paramBoardId},
  211. {$set: {activities: newActivities}});
  212. }
  213. JsonRoutes.sendResult(res, {
  214. code: 200,
  215. data: {
  216. _id: paramIntId,
  217. },
  218. });
  219. }
  220. catch (error) {
  221. JsonRoutes.sendResult(res, {
  222. code: 200,
  223. data: error,
  224. });
  225. }
  226. });
  227. /**
  228. * @operation delete_integration_activities
  229. * @summary Delete subscribed activities
  230. *
  231. * @param {string} boardId the board ID
  232. * @param {string} intId the integration ID
  233. * @param {string} newActivities the activities to remove from the integration
  234. * @return_type Integrations
  235. */
  236. JsonRoutes.add('DELETE', '/api/boards/:boardId/integrations/:intId/activities', function (req, res) {
  237. try {
  238. const paramBoardId = req.params.boardId;
  239. const paramIntId = req.params.intId;
  240. const newActivities = req.body.activities;
  241. Authentication.checkBoardAccess(req.userId, paramBoardId);
  242. Integrations.direct.update({_id: paramIntId, boardId: paramBoardId},
  243. {$pullAll: {activities: newActivities}});
  244. JsonRoutes.sendResult(res, {
  245. code: 200,
  246. data: Integrations.findOne({_id: paramIntId, boardId: paramBoardId}, { fields: {_id: 1, activities: 1}}),
  247. });
  248. }
  249. catch (error) {
  250. JsonRoutes.sendResult(res, {
  251. code: 200,
  252. data: error,
  253. });
  254. }
  255. });
  256. /**
  257. * @operation new_integration_activities
  258. * @summary Add subscribed activities
  259. *
  260. * @param {string} boardId the board ID
  261. * @param {string} intId the integration ID
  262. * @param {string} newActivities the activities to add to the integration
  263. * @return_type Integrations
  264. */
  265. JsonRoutes.add('POST', '/api/boards/:boardId/integrations/:intId/activities', function (req, res) {
  266. try {
  267. const paramBoardId = req.params.boardId;
  268. const paramIntId = req.params.intId;
  269. const newActivities = req.body.activities;
  270. Authentication.checkBoardAccess(req.userId, paramBoardId);
  271. Integrations.direct.update({_id: paramIntId, boardId: paramBoardId},
  272. {$addToSet: {activities: { $each: newActivities}}});
  273. JsonRoutes.sendResult(res, {
  274. code: 200,
  275. data: Integrations.findOne({_id: paramIntId, boardId: paramBoardId}, { fields: {_id: 1, activities: 1}}),
  276. });
  277. }
  278. catch (error) {
  279. JsonRoutes.sendResult(res, {
  280. code: 200,
  281. data: error,
  282. });
  283. }
  284. });
  285. /**
  286. * @operation delete_integration
  287. * @summary Delete integration
  288. *
  289. * @param {string} boardId the board ID
  290. * @param {string} intId the integration ID
  291. * @return_type {_id: string}
  292. */
  293. JsonRoutes.add('DELETE', '/api/boards/:boardId/integrations/:intId', function (req, res) {
  294. try {
  295. const paramBoardId = req.params.boardId;
  296. const paramIntId = req.params.intId;
  297. Authentication.checkBoardAccess(req.userId, paramBoardId);
  298. Integrations.direct.remove({_id: paramIntId, boardId: paramBoardId});
  299. JsonRoutes.sendResult(res, {
  300. code: 200,
  301. data: {
  302. _id: paramIntId,
  303. },
  304. });
  305. }
  306. catch (error) {
  307. JsonRoutes.sendResult(res, {
  308. code: 200,
  309. data: error,
  310. });
  311. }
  312. });
  313. }