integrations.js 10 KB

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