2019-08-09 17:11:24 +03:00
|
|
|
const models = require('../../models');
|
2020-05-22 21:22:20 +03:00
|
|
|
const {i18n} = require('../../lib/common');
|
|
|
|
const errors = require('@tryghost/errors');
|
2019-08-09 17:11:24 +03:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
docName: 'webhooks',
|
|
|
|
|
|
|
|
add: {
|
|
|
|
statusCode: 201,
|
|
|
|
headers: {},
|
|
|
|
options: [],
|
|
|
|
data: [],
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
return models.Webhook.getByEventAndTarget(
|
|
|
|
frame.data.webhooks[0].event,
|
|
|
|
frame.data.webhooks[0].target_url,
|
|
|
|
frame.options
|
|
|
|
).then((webhook) => {
|
|
|
|
if (webhook) {
|
|
|
|
return Promise.reject(
|
2020-05-22 21:22:20 +03:00
|
|
|
new errors.ValidationError({message: i18n.t('errors.api.webhooks.webhookAlreadyExists')})
|
2019-08-09 17:11:24 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return models.Webhook.add(frame.data.webhooks[0], frame.options);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
edit: {
|
2020-07-08 07:54:31 +03:00
|
|
|
permissions: {
|
|
|
|
before: (frame) => {
|
|
|
|
if (frame.options.context && frame.options.context.api_key && frame.options.context.api_key.id) {
|
|
|
|
return models.Webhook.findOne({id: frame.options.id})
|
|
|
|
.then((webhook) => {
|
|
|
|
if (webhook.get('integration_id') !== frame.options.context.api_key.id) {
|
|
|
|
throw new errors.NoPermissionError({
|
|
|
|
message: i18n.t('errors.api.webhooks.noPermissionToEdit.message', {
|
|
|
|
method: 'edit'
|
|
|
|
}),
|
|
|
|
context: i18n.t('errors.api.webhooks.noPermissionToEdit.context', {
|
|
|
|
method: 'edit'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-08-09 17:11:24 +03:00
|
|
|
data: [
|
|
|
|
'name',
|
|
|
|
'event',
|
|
|
|
'target_url',
|
|
|
|
'secret',
|
|
|
|
'api_version'
|
|
|
|
],
|
|
|
|
options: [
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
query({data, options}) {
|
|
|
|
return models.Webhook.edit(data.webhooks[0], Object.assign(options, {require: true}))
|
|
|
|
.catch(models.Webhook.NotFoundError, () => {
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.resource.resourceNotFound', {
|
2019-08-09 17:11:24 +03:00
|
|
|
resource: 'Webhook'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: {
|
|
|
|
statusCode: 204,
|
|
|
|
headers: {},
|
|
|
|
options: [
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-07-08 07:54:31 +03:00
|
|
|
permissions: {
|
|
|
|
before: (frame) => {
|
|
|
|
if (frame.options.context && frame.options.context.api_key && frame.options.context.api_key.id) {
|
|
|
|
return models.Webhook.findOne({id: frame.options.id})
|
|
|
|
.then((webhook) => {
|
|
|
|
if (webhook.get('integration_id') !== frame.options.context.api_key.id) {
|
|
|
|
throw new errors.NoPermissionError({
|
|
|
|
message: i18n.t('errors.api.webhooks.noPermissionToEdit.message', {
|
|
|
|
method: 'destory'
|
|
|
|
}),
|
|
|
|
context: i18n.t('errors.api.webhooks.noPermissionToEdit.context', {
|
|
|
|
method: 'destroy'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-08-09 17:11:24 +03:00
|
|
|
query(frame) {
|
|
|
|
frame.options.require = true;
|
2020-04-13 13:21:47 +03:00
|
|
|
|
|
|
|
return models.Webhook.destroy(frame.options)
|
|
|
|
.then(() => null)
|
|
|
|
.catch(models.Webhook.NotFoundError, () => {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.resource.resourceNotFound', {
|
2020-04-13 13:21:47 +03:00
|
|
|
resource: 'Webhook'
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
});
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|