mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 01:41:46 +03:00
191b313271
refs https://github.com/TryGhost/Team/issues/694 refs https://linear.app/tryghost/issue/CORE-14/tackle-webhooksjs - The controller code is not meant to contain complex business logic.
56 lines
1.8 KiB
JavaScript
56 lines
1.8 KiB
JavaScript
const {ValidationError} = require('@tryghost/errors');
|
|
const tpl = require('@tryghost/tpl');
|
|
|
|
const messages = {
|
|
webhookAlreadyExists: 'Target URL has already been used for this event.',
|
|
nonExistingIntegrationIdProvided: {
|
|
message: `Validation failed for '{key}'.`,
|
|
context: `'integration_id' value does not match any existing integration.`,
|
|
help: `Provide the 'integration_id' of an existing integration.`
|
|
}
|
|
};
|
|
|
|
class WebhooksService {
|
|
constructor({WebhookModel}) {
|
|
this.WebhookModel = WebhookModel;
|
|
}
|
|
|
|
async add(data, options) {
|
|
const webhook = await this.WebhookModel.getByEventAndTarget(
|
|
data.webhooks[0].event,
|
|
data.webhooks[0].target_url,
|
|
options
|
|
);
|
|
|
|
if (webhook) {
|
|
throw new ValidationError({
|
|
message: messages.webhookAlreadyExists
|
|
});
|
|
}
|
|
|
|
try {
|
|
const newWebhook = await this.WebhookModel.add(data.webhooks[0], options);
|
|
return newWebhook;
|
|
} catch (error) {
|
|
if (error.errno === 1452 || (error.code === 'SQLITE_CONSTRAINT' && /SQLITE_CONSTRAINT: FOREIGN KEY constraint failed/.test(error.message))) {
|
|
throw new ValidationError({
|
|
message: tpl(messages.nonExistingIntegrationIdProvided.message, {
|
|
key: 'integration_id'
|
|
}),
|
|
context: messages.nonExistingIntegrationIdProvided.context,
|
|
help: messages.nonExistingIntegrationIdProvided.help
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @returns {WebhooksService} instance of the PostsService
|
|
*/
|
|
const getWebhooksServiceInstance = ({WebhookModel}) => {
|
|
return new WebhooksService({WebhookModel});
|
|
};
|
|
|
|
module.exports = getWebhooksServiceInstance;
|