diff options
Diffstat (limited to 'models/integrations.js')
-rw-r--r-- | models/integrations.js | 336 |
1 files changed, 201 insertions, 135 deletions
diff --git a/models/integrations.js b/models/integrations.js index 65a7af63..bb36d6e8 100644 --- a/models/integrations.js +++ b/models/integrations.js @@ -3,75 +3,96 @@ Integrations = new Mongo.Collection('integrations'); /** * Integration with third-party applications */ -Integrations.attachSchema(new SimpleSchema({ - enabled: { - /** - * is the integration enabled? - */ - type: Boolean, - defaultValue: true, - }, - title: { - /** - * name of the integration - */ - type: String, - optional: true, - }, - type: { - /** - * type of the integratation (Default to 'outgoing-webhooks') - */ - type: String, - defaultValue: 'outgoing-webhooks', - }, - activities: { - /** - * activities the integration gets triggered (list) - */ - type: [String], - defaultValue: ['all'], - }, - url: { // URL validation regex (https://mathiasbynens.be/demo/url-regex) - /** - * URL validation regex (https://mathiasbynens.be/demo/url-regex) - */ - type: String, - }, - token: { - /** - * token of the integration - */ - type: String, - optional: true, - }, - boardId: { - /** - * Board ID of the integration - */ - type: String, - }, - createdAt: { - /** - * Creation date of the integration - */ - type: Date, - denyUpdate: false, - autoValue() { // eslint-disable-line consistent-return - if (this.isInsert) { - return new Date(); - } else { - this.unset(); - } +Integrations.attachSchema( + new SimpleSchema({ + enabled: { + /** + * is the integration enabled? + */ + type: Boolean, + defaultValue: true, }, - }, - userId: { - /** - * user ID who created the interation - */ - type: String, - }, -})); + title: { + /** + * name of the integration + */ + type: String, + optional: true, + }, + type: { + /** + * type of the integratation (Default to 'outgoing-webhooks') + */ + type: String, + defaultValue: 'outgoing-webhooks', + }, + activities: { + /** + * activities the integration gets triggered (list) + */ + type: [String], + defaultValue: ['all'], + }, + url: { + // URL validation regex (https://mathiasbynens.be/demo/url-regex) + /** + * URL validation regex (https://mathiasbynens.be/demo/url-regex) + */ + type: String, + }, + token: { + /** + * token of the integration + */ + type: String, + optional: true, + }, + boardId: { + /** + * Board ID of the integration + */ + type: String, + }, + createdAt: { + /** + * Creation date of the integration + */ + type: Date, + denyUpdate: false, + // eslint-disable-next-line consistent-return + autoValue() { + if (this.isInsert) { + return new Date(); + } else { + this.unset(); + } + }, + }, + modifiedAt: { + type: Date, + denyUpdate: false, + // eslint-disable-next-line consistent-return + autoValue() { + if (this.isInsert || this.isUpsert || this.isUpdate) { + return new Date(); + } else { + this.unset(); + } + }, + }, + userId: { + /** + * user ID who created the interation + */ + type: String, + }, + }) +); + +Integrations.before.update((userId, doc, fieldNames, modifier, options) => { + modifier.$set = modifier.$set || {}; + modifier.$set.modifiedAt = Date.now(); +}); Integrations.allow({ insert(userId, doc) { @@ -89,6 +110,7 @@ Integrations.allow({ //INTEGRATIONS REST API if (Meteor.isServer) { Meteor.startup(() => { + Integrations._collection._ensureIndex({ modifiedAt: -1 }); Integrations._collection._ensureIndex({ boardId: 1 }); }); @@ -99,18 +121,23 @@ if (Meteor.isServer) { * @param {string} boardId the board ID * @return_type [Integrations] */ - JsonRoutes.add('GET', '/api/boards/:boardId/integrations', function(req, res) { + JsonRoutes.add('GET', '/api/boards/:boardId/integrations', function( + req, + res + ) { try { const paramBoardId = req.params.boardId; Authentication.checkBoardAccess(req.userId, paramBoardId); - const data = Integrations.find({ boardId: paramBoardId }, { fields: { token: 0 } }).map(function(doc) { + const data = Integrations.find( + { boardId: paramBoardId }, + { fields: { token: 0 } } + ).map(function(doc) { return doc; }); - JsonRoutes.sendResult(res, {code: 200, data}); - } - catch (error) { + JsonRoutes.sendResult(res, { code: 200, data }); + } catch (error) { JsonRoutes.sendResult(res, { code: 200, data: error, @@ -126,7 +153,10 @@ if (Meteor.isServer) { * @param {string} intId the integration ID * @return_type Integrations */ - JsonRoutes.add('GET', '/api/boards/:boardId/integrations/:intId', function(req, res) { + JsonRoutes.add('GET', '/api/boards/:boardId/integrations/:intId', function( + req, + res + ) { try { const paramBoardId = req.params.boardId; const paramIntId = req.params.intId; @@ -134,10 +164,12 @@ if (Meteor.isServer) { JsonRoutes.sendResult(res, { code: 200, - data: Integrations.findOne({ _id: paramIntId, boardId: paramBoardId }, { fields: { token: 0 } }), + data: Integrations.findOne( + { _id: paramIntId, boardId: paramBoardId }, + { fields: { token: 0 } } + ), }); - } - catch (error) { + } catch (error) { JsonRoutes.sendResult(res, { code: 200, data: error, @@ -153,7 +185,10 @@ if (Meteor.isServer) { * @param {string} url the URL of the integration * @return_type {_id: string} */ - JsonRoutes.add('POST', '/api/boards/:boardId/integrations', function(req, res) { + JsonRoutes.add('POST', '/api/boards/:boardId/integrations', function( + req, + res + ) { try { const paramBoardId = req.params.boardId; Authentication.checkBoardAccess(req.userId, paramBoardId); @@ -170,8 +205,7 @@ if (Meteor.isServer) { _id: id, }, }); - } - catch (error) { + } catch (error) { JsonRoutes.sendResult(res, { code: 200, data: error, @@ -192,7 +226,10 @@ if (Meteor.isServer) { * @param {string} [activities] new list of activities of the integration * @return_type {_id: string} */ - JsonRoutes.add('PUT', '/api/boards/:boardId/integrations/:intId', function (req, res) { + JsonRoutes.add('PUT', '/api/boards/:boardId/integrations/:intId', function( + req, + res + ) { try { const paramBoardId = req.params.boardId; const paramIntId = req.params.intId; @@ -200,28 +237,38 @@ if (Meteor.isServer) { if (req.body.hasOwnProperty('enabled')) { const newEnabled = req.body.enabled; - Integrations.direct.update({_id: paramIntId, boardId: paramBoardId}, - {$set: {enabled: newEnabled}}); + Integrations.direct.update( + { _id: paramIntId, boardId: paramBoardId }, + { $set: { enabled: newEnabled } } + ); } if (req.body.hasOwnProperty('title')) { const newTitle = req.body.title; - Integrations.direct.update({_id: paramIntId, boardId: paramBoardId}, - {$set: {title: newTitle}}); + Integrations.direct.update( + { _id: paramIntId, boardId: paramBoardId }, + { $set: { title: newTitle } } + ); } if (req.body.hasOwnProperty('url')) { const newUrl = req.body.url; - Integrations.direct.update({_id: paramIntId, boardId: paramBoardId}, - {$set: {url: newUrl}}); + Integrations.direct.update( + { _id: paramIntId, boardId: paramBoardId }, + { $set: { url: newUrl } } + ); } if (req.body.hasOwnProperty('token')) { const newToken = req.body.token; - Integrations.direct.update({_id: paramIntId, boardId: paramBoardId}, - {$set: {token: newToken}}); + Integrations.direct.update( + { _id: paramIntId, boardId: paramBoardId }, + { $set: { token: newToken } } + ); } if (req.body.hasOwnProperty('activities')) { const newActivities = req.body.activities; - Integrations.direct.update({_id: paramIntId, boardId: paramBoardId}, - {$set: {activities: newActivities}}); + Integrations.direct.update( + { _id: paramIntId, boardId: paramBoardId }, + { $set: { activities: newActivities } } + ); } JsonRoutes.sendResult(res, { @@ -230,8 +277,7 @@ if (Meteor.isServer) { _id: paramIntId, }, }); - } - catch (error) { + } catch (error) { JsonRoutes.sendResult(res, { code: 200, data: error, @@ -248,28 +294,36 @@ if (Meteor.isServer) { * @param {string} newActivities the activities to remove from the integration * @return_type Integrations */ - JsonRoutes.add('DELETE', '/api/boards/:boardId/integrations/:intId/activities', function (req, res) { - try { - const paramBoardId = req.params.boardId; - const paramIntId = req.params.intId; - const newActivities = req.body.activities; - Authentication.checkBoardAccess(req.userId, paramBoardId); + JsonRoutes.add( + 'DELETE', + '/api/boards/:boardId/integrations/:intId/activities', + function(req, res) { + try { + const paramBoardId = req.params.boardId; + const paramIntId = req.params.intId; + const newActivities = req.body.activities; + Authentication.checkBoardAccess(req.userId, paramBoardId); - Integrations.direct.update({_id: paramIntId, boardId: paramBoardId}, - {$pullAll: {activities: newActivities}}); + Integrations.direct.update( + { _id: paramIntId, boardId: paramBoardId }, + { $pullAll: { activities: newActivities } } + ); - JsonRoutes.sendResult(res, { - code: 200, - data: Integrations.findOne({_id: paramIntId, boardId: paramBoardId}, { fields: {_id: 1, activities: 1}}), - }); - } - catch (error) { - JsonRoutes.sendResult(res, { - code: 200, - data: error, - }); + JsonRoutes.sendResult(res, { + code: 200, + data: Integrations.findOne( + { _id: paramIntId, boardId: paramBoardId }, + { fields: { _id: 1, activities: 1 } } + ), + }); + } catch (error) { + JsonRoutes.sendResult(res, { + code: 200, + data: error, + }); + } } - }); + ); /** * @operation new_integration_activities @@ -280,28 +334,36 @@ if (Meteor.isServer) { * @param {string} newActivities the activities to add to the integration * @return_type Integrations */ - JsonRoutes.add('POST', '/api/boards/:boardId/integrations/:intId/activities', function (req, res) { - try { - const paramBoardId = req.params.boardId; - const paramIntId = req.params.intId; - const newActivities = req.body.activities; - Authentication.checkBoardAccess(req.userId, paramBoardId); + JsonRoutes.add( + 'POST', + '/api/boards/:boardId/integrations/:intId/activities', + function(req, res) { + try { + const paramBoardId = req.params.boardId; + const paramIntId = req.params.intId; + const newActivities = req.body.activities; + Authentication.checkBoardAccess(req.userId, paramBoardId); - Integrations.direct.update({_id: paramIntId, boardId: paramBoardId}, - {$addToSet: {activities: { $each: newActivities}}}); + Integrations.direct.update( + { _id: paramIntId, boardId: paramBoardId }, + { $addToSet: { activities: { $each: newActivities } } } + ); - JsonRoutes.sendResult(res, { - code: 200, - data: Integrations.findOne({_id: paramIntId, boardId: paramBoardId}, { fields: {_id: 1, activities: 1}}), - }); - } - catch (error) { - JsonRoutes.sendResult(res, { - code: 200, - data: error, - }); + JsonRoutes.sendResult(res, { + code: 200, + data: Integrations.findOne( + { _id: paramIntId, boardId: paramBoardId }, + { fields: { _id: 1, activities: 1 } } + ), + }); + } catch (error) { + JsonRoutes.sendResult(res, { + code: 200, + data: error, + }); + } } - }); + ); /** * @operation delete_integration @@ -311,21 +373,23 @@ if (Meteor.isServer) { * @param {string} intId the integration ID * @return_type {_id: string} */ - JsonRoutes.add('DELETE', '/api/boards/:boardId/integrations/:intId', function (req, res) { + JsonRoutes.add('DELETE', '/api/boards/:boardId/integrations/:intId', function( + req, + res + ) { try { const paramBoardId = req.params.boardId; const paramIntId = req.params.intId; Authentication.checkBoardAccess(req.userId, paramBoardId); - Integrations.direct.remove({_id: paramIntId, boardId: paramBoardId}); + Integrations.direct.remove({ _id: paramIntId, boardId: paramBoardId }); JsonRoutes.sendResult(res, { code: 200, data: { _id: paramIntId, }, }); - } - catch (error) { + } catch (error) { JsonRoutes.sendResult(res, { code: 200, data: error, @@ -333,3 +397,5 @@ if (Meteor.isServer) { } }); } + +export default Integrations; |