2018-10-02 19:46:38 +03:00
|
|
|
const ghostBookshelf = require('./base');
|
|
|
|
|
|
|
|
const Integration = ghostBookshelf.Model.extend({
|
|
|
|
tableName: 'integrations',
|
|
|
|
|
2018-10-17 14:17:13 +03:00
|
|
|
relationships: ['api_keys', 'webhooks'],
|
2018-10-12 12:57:46 +03:00
|
|
|
|
|
|
|
relationshipBelongsTo: {
|
2018-10-17 14:17:13 +03:00
|
|
|
api_keys: 'api_keys',
|
|
|
|
webhooks: 'webhooks'
|
2018-10-12 12:57:46 +03:00
|
|
|
},
|
|
|
|
|
2018-10-18 16:03:56 +03:00
|
|
|
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();
|
|
|
|
},
|
|
|
|
|
2018-10-16 08:25:54 +03:00
|
|
|
onSaving(newIntegration, attr, options) {
|
|
|
|
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});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-10-12 12:57:46 +03:00
|
|
|
permittedAttributes(...args) {
|
|
|
|
return ghostBookshelf.Model.prototype.permittedAttributes.apply(this, args).concat(this.relationships);
|
|
|
|
},
|
|
|
|
|
2018-10-02 19:46:38 +03:00
|
|
|
api_keys: function apiKeys() {
|
2018-10-12 12:57:46 +03:00
|
|
|
return this.hasMany('ApiKey', 'integration_id');
|
2018-10-17 14:17:13 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
webhooks: function webhooks() {
|
|
|
|
return this.hasMany('Webhook', 'integration_id');
|
2018-10-02 19:46:38 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const Integrations = ghostBookshelf.Collection.extend({
|
|
|
|
model: Integration
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Integration: ghostBookshelf.model('Integration', Integration),
|
|
|
|
Integrations: ghostBookshelf.collection('Integrations', Integrations)
|
|
|
|
};
|