integrations.js 9.9 KB

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