integrations.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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.Const = {
  91. GLOBAL_WEBHOOK_ID: '_global',
  92. ONEWAY: 'outgoing-webhooks',
  93. TWOWAY: 'bidirectional-webhooks',
  94. get WEBHOOK_TYPES() {
  95. return [this.ONEWAY, this.TWOWAY];
  96. },
  97. };
  98. const permissionHelper = {
  99. allow(userId, doc) {
  100. const user = Users.findOne(userId);
  101. const isAdmin = user && Meteor.user().isAdmin;
  102. return isAdmin || allowIsBoardAdmin(userId, Boards.findOne(doc.boardId));
  103. },
  104. };
  105. Integrations.allow({
  106. insert(userId, doc) {
  107. return permissionHelper.allow(userId, doc);
  108. },
  109. update(userId, doc) {
  110. return permissionHelper.allow(userId, doc);
  111. },
  112. remove(userId, doc) {
  113. return permissionHelper.allow(userId, doc);
  114. },
  115. fetch: ['boardId'],
  116. });
  117. //INTEGRATIONS REST API
  118. if (Meteor.isServer) {
  119. Meteor.startup(() => {
  120. Integrations._collection._ensureIndex({ modifiedAt: -1 });
  121. Integrations._collection._ensureIndex({ boardId: 1 });
  122. });
  123. /**
  124. * @operation get_all_integrations
  125. * @summary Get all integrations in board
  126. *
  127. * @param {string} boardId the board ID
  128. * @return_type [Integrations]
  129. */
  130. JsonRoutes.add('GET', '/api/boards/:boardId/integrations', function(
  131. req,
  132. res,
  133. ) {
  134. try {
  135. const paramBoardId = req.params.boardId;
  136. Authentication.checkBoardAccess(req.userId, paramBoardId);
  137. const data = Integrations.find(
  138. { boardId: paramBoardId },
  139. { fields: { token: 0 } },
  140. ).map(function(doc) {
  141. return doc;
  142. });
  143. JsonRoutes.sendResult(res, { code: 200, data });
  144. } catch (error) {
  145. JsonRoutes.sendResult(res, {
  146. code: 200,
  147. data: error,
  148. });
  149. }
  150. });
  151. /**
  152. * @operation get_integration
  153. * @summary Get a single integration in board
  154. *
  155. * @param {string} boardId the board ID
  156. * @param {string} intId the integration ID
  157. * @return_type Integrations
  158. */
  159. JsonRoutes.add('GET', '/api/boards/:boardId/integrations/:intId', function(
  160. req,
  161. res,
  162. ) {
  163. try {
  164. const paramBoardId = req.params.boardId;
  165. const paramIntId = req.params.intId;
  166. Authentication.checkBoardAccess(req.userId, paramBoardId);
  167. JsonRoutes.sendResult(res, {
  168. code: 200,
  169. data: Integrations.findOne(
  170. { _id: paramIntId, boardId: paramBoardId },
  171. { fields: { token: 0 } },
  172. ),
  173. });
  174. } catch (error) {
  175. JsonRoutes.sendResult(res, {
  176. code: 200,
  177. data: error,
  178. });
  179. }
  180. });
  181. /**
  182. * @operation new_integration
  183. * @summary Create a new integration
  184. *
  185. * @param {string} boardId the board ID
  186. * @param {string} url the URL of the integration
  187. * @return_type {_id: string}
  188. */
  189. JsonRoutes.add('POST', '/api/boards/:boardId/integrations', function(
  190. req,
  191. res,
  192. ) {
  193. try {
  194. const paramBoardId = req.params.boardId;
  195. Authentication.checkBoardAccess(req.userId, paramBoardId);
  196. const id = Integrations.insert({
  197. userId: req.userId,
  198. boardId: paramBoardId,
  199. url: req.body.url,
  200. });
  201. JsonRoutes.sendResult(res, {
  202. code: 200,
  203. data: {
  204. _id: id,
  205. },
  206. });
  207. } catch (error) {
  208. JsonRoutes.sendResult(res, {
  209. code: 200,
  210. data: error,
  211. });
  212. }
  213. });
  214. /**
  215. * @operation edit_integration
  216. * @summary Edit integration data
  217. *
  218. * @param {string} boardId the board ID
  219. * @param {string} intId the integration ID
  220. * @param {string} [enabled] is the integration enabled?
  221. * @param {string} [title] new name of the integration
  222. * @param {string} [url] new URL of the integration
  223. * @param {string} [token] new token of the integration
  224. * @param {string} [activities] new list of activities of the integration
  225. * @return_type {_id: string}
  226. */
  227. JsonRoutes.add('PUT', '/api/boards/:boardId/integrations/:intId', function(
  228. req,
  229. res,
  230. ) {
  231. try {
  232. const paramBoardId = req.params.boardId;
  233. const paramIntId = req.params.intId;
  234. Authentication.checkBoardAccess(req.userId, paramBoardId);
  235. if (req.body.hasOwnProperty('enabled')) {
  236. const newEnabled = req.body.enabled;
  237. Integrations.direct.update(
  238. { _id: paramIntId, boardId: paramBoardId },
  239. { $set: { enabled: newEnabled } },
  240. );
  241. }
  242. if (req.body.hasOwnProperty('title')) {
  243. const newTitle = req.body.title;
  244. Integrations.direct.update(
  245. { _id: paramIntId, boardId: paramBoardId },
  246. { $set: { title: newTitle } },
  247. );
  248. }
  249. if (req.body.hasOwnProperty('url')) {
  250. const newUrl = req.body.url;
  251. Integrations.direct.update(
  252. { _id: paramIntId, boardId: paramBoardId },
  253. { $set: { url: newUrl } },
  254. );
  255. }
  256. if (req.body.hasOwnProperty('token')) {
  257. const newToken = req.body.token;
  258. Integrations.direct.update(
  259. { _id: paramIntId, boardId: paramBoardId },
  260. { $set: { token: newToken } },
  261. );
  262. }
  263. if (req.body.hasOwnProperty('activities')) {
  264. const newActivities = req.body.activities;
  265. Integrations.direct.update(
  266. { _id: paramIntId, boardId: paramBoardId },
  267. { $set: { activities: newActivities } },
  268. );
  269. }
  270. JsonRoutes.sendResult(res, {
  271. code: 200,
  272. data: {
  273. _id: paramIntId,
  274. },
  275. });
  276. } catch (error) {
  277. JsonRoutes.sendResult(res, {
  278. code: 200,
  279. data: error,
  280. });
  281. }
  282. });
  283. /**
  284. * @operation delete_integration_activities
  285. * @summary Delete subscribed activities
  286. *
  287. * @param {string} boardId the board ID
  288. * @param {string} intId the integration ID
  289. * @param {string} newActivities the activities to remove from the integration
  290. * @return_type Integrations
  291. */
  292. JsonRoutes.add(
  293. 'DELETE',
  294. '/api/boards/:boardId/integrations/:intId/activities',
  295. function(req, res) {
  296. try {
  297. const paramBoardId = req.params.boardId;
  298. const paramIntId = req.params.intId;
  299. const newActivities = req.body.activities;
  300. Authentication.checkBoardAccess(req.userId, paramBoardId);
  301. Integrations.direct.update(
  302. { _id: paramIntId, boardId: paramBoardId },
  303. { $pullAll: { activities: newActivities } },
  304. );
  305. JsonRoutes.sendResult(res, {
  306. code: 200,
  307. data: Integrations.findOne(
  308. { _id: paramIntId, boardId: paramBoardId },
  309. { fields: { _id: 1, activities: 1 } },
  310. ),
  311. });
  312. } catch (error) {
  313. JsonRoutes.sendResult(res, {
  314. code: 200,
  315. data: error,
  316. });
  317. }
  318. },
  319. );
  320. /**
  321. * @operation new_integration_activities
  322. * @summary Add subscribed activities
  323. *
  324. * @param {string} boardId the board ID
  325. * @param {string} intId the integration ID
  326. * @param {string} newActivities the activities to add to the integration
  327. * @return_type Integrations
  328. */
  329. JsonRoutes.add(
  330. 'POST',
  331. '/api/boards/:boardId/integrations/:intId/activities',
  332. function(req, res) {
  333. try {
  334. const paramBoardId = req.params.boardId;
  335. const paramIntId = req.params.intId;
  336. const newActivities = req.body.activities;
  337. Authentication.checkBoardAccess(req.userId, paramBoardId);
  338. Integrations.direct.update(
  339. { _id: paramIntId, boardId: paramBoardId },
  340. { $addToSet: { activities: { $each: newActivities } } },
  341. );
  342. JsonRoutes.sendResult(res, {
  343. code: 200,
  344. data: Integrations.findOne(
  345. { _id: paramIntId, boardId: paramBoardId },
  346. { fields: { _id: 1, activities: 1 } },
  347. ),
  348. });
  349. } catch (error) {
  350. JsonRoutes.sendResult(res, {
  351. code: 200,
  352. data: error,
  353. });
  354. }
  355. },
  356. );
  357. /**
  358. * @operation delete_integration
  359. * @summary Delete integration
  360. *
  361. * @param {string} boardId the board ID
  362. * @param {string} intId the integration ID
  363. * @return_type {_id: string}
  364. */
  365. JsonRoutes.add('DELETE', '/api/boards/:boardId/integrations/:intId', function(
  366. req,
  367. res,
  368. ) {
  369. try {
  370. const paramBoardId = req.params.boardId;
  371. const paramIntId = req.params.intId;
  372. Authentication.checkBoardAccess(req.userId, paramBoardId);
  373. Integrations.direct.remove({ _id: paramIntId, boardId: paramBoardId });
  374. JsonRoutes.sendResult(res, {
  375. code: 200,
  376. data: {
  377. _id: paramIntId,
  378. },
  379. });
  380. } catch (error) {
  381. JsonRoutes.sendResult(res, {
  382. code: 200,
  383. data: error,
  384. });
  385. }
  386. });
  387. }
  388. export default Integrations;