Ghost/core/server/models/integration.js
Kevin Ansfield 8e9ade6357
Added migration to insert builtin Zapier integration (#10500)
no issue

A new Zapier app will be released that uses the v2 Admin API which means it will require an ApiKey that is linked to an Integration.

- adds a `type` column to the `integrations` table with the following types allowed:
  - `custom` (default) used by custom integrations added by users
  - `builtin` used by built-in integrations that have their own UI and won't show up in the "Custom Integrations" list
  - `internal` used by "internal" integrations such as the scheduler
- adds a `zapier` "builtin" integration to the fixtures
2019-02-15 14:48:21 +00:00

91 lines
2.5 KiB
JavaScript

const ghostBookshelf = require('./base');
const Integration = ghostBookshelf.Model.extend({
tableName: 'integrations',
relationships: ['api_keys', 'webhooks'],
relationshipBelongsTo: {
api_keys: 'api_keys',
webhooks: 'webhooks'
},
defaults() {
return {
type: 'custom'
};
},
add(data, options) {
const addIntegration = () => {
return ghostBookshelf.Model.add.call(this, data, options)
.then(({id}) => {
return this.findOne({id}, options);
});
};
if (!options.transacting) {
return ghostBookshelf.transaction((transacting) => {
options.transacting = transacting;
return addIntegration();
});
}
return addIntegration();
},
edit(data, options) {
const editIntegration = () => {
return ghostBookshelf.Model.edit.call(this, data, options)
.then(({id}) => {
return this.findOne({id}, options);
});
};
if (!options.transacting) {
return ghostBookshelf.transaction((transacting) => {
options.transacting = transacting;
return editIntegration();
});
}
return editIntegration();
},
onSaving(integration, attrs, options) {
ghostBookshelf.Model.prototype.onSaving.apply(this, arguments);
if (this.hasChanged('slug') || !this.get('slug')) {
// Pass the new slug through the generator to strip illegal characters, detect duplicates
return ghostBookshelf.Model.generateSlug(Integration, this.get('slug') || this.get('name'),
{transacting: options.transacting})
.then((slug) => {
this.set({slug});
});
}
},
permittedAttributes(...args) {
return ghostBookshelf.Model.prototype.permittedAttributes.apply(this, args).concat(this.relationships);
},
api_keys: function apiKeys() {
return this.hasMany('ApiKey', 'integration_id');
},
webhooks: function webhooks() {
return this.hasMany('Webhook', 'integration_id');
}
});
const Integrations = ghostBookshelf.Collection.extend({
model: Integration
});
module.exports = {
Integration: ghostBookshelf.model('Integration', Integration),
Integrations: ghostBookshelf.collection('Integrations', Integrations)
};