mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-28 14:03:48 +03:00
17feb14e4a
refs #9865 * Added generic messaging for resource not found * Ensured integration model uses transaction for writes * Created POST /integrations endpoint * Created GET /integrations/:id endpoint * Created GET /integrations endpoint * Created PUT /integrations/:id endpoint * Created DELETE /integrations/:id endpoint
83 lines
2.3 KiB
JavaScript
83 lines
2.3 KiB
JavaScript
const ghostBookshelf = require('./base');
|
|
|
|
const Integration = ghostBookshelf.Model.extend({
|
|
tableName: 'integrations',
|
|
|
|
relationships: ['api_keys', 'webhooks'],
|
|
|
|
relationshipBelongsTo: {
|
|
api_keys: 'api_keys',
|
|
webhooks: 'webhooks'
|
|
},
|
|
|
|
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(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});
|
|
});
|
|
}
|
|
},
|
|
|
|
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)
|
|
};
|