fix(api): Prevent webhooks from having duplicate ids

This commit is contained in:
Blade Barringer
2016-10-03 08:13:33 -05:00
parent 04fd907a45
commit 8b6052a3ca
3 changed files with 21 additions and 1 deletions

View File

@@ -1,7 +1,7 @@
import { authWithHeaders } from '../../middlewares/auth';
import { model as Webhook } from '../../models/webhook';
import { removeFromArray } from '../../libs/collectionManipulators';
import { NotFound } from '../../libs/errors';
import { NotFound, BadRequest } from '../../libs/errors';
let api = {};
@@ -53,6 +53,7 @@ let api = {};
* @apiSuccess {Object} data.options The options for the webhook (See examples)
*
* @apiError InvalidUUID The `id` was not a valid `UUID`
* @apiError IdTaken The `id` is already being used by another webhook
* @apiError InvalidEnable The `enable` param was not a `Boolean` value
* @apiError InvalidUrl The `url` param was not valid url
* @apiError InvalidWebhookType The `type` param was not a supported Webhook type
@@ -67,6 +68,14 @@ api.addWebhook = {
let user = res.locals.user;
let webhook = new Webhook(req.body);
let existingWebhook = user.webhooks.find((wh) => {
return wh.id === webhook.id;
});
if (existingWebhook) {
throw new BadRequest(res.t('webhookIdAlreadyTaken', { id: webhook.id }));
}
webhook.formatOptions(res);
user.webhooks.push(webhook);