prevent from save doubled subscriber url (#3811)

Co-authored-by: Adam Garbowski <adamgarbowski90@gmail.com>
Co-authored-by: Hariom Balhara <hariombalhara@gmail.com>
This commit is contained in:
Adam Garbowski 2022-08-31 05:41:23 +02:00 committed by GitHub
parent 0c8847fb87
commit b151ccd4c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 0 deletions

View File

@ -1,3 +1,4 @@
import { Webhook } from "@prisma/client";
import { useEffect, useState } from "react";
import { Controller, useForm } from "react-hook-form";
@ -20,10 +21,12 @@ export default function WebhookDialogForm(props: {
defaultValues?: TWebhook;
app?: string;
handleClose: () => void;
webhooks: Webhook[];
}) {
const { t } = useLocale();
const utils = trpc.useContext();
const appId = props.app;
const webhooks = props.webhooks;
const triggers = !appId
? WEBHOOK_TRIGGER_EVENTS_GROUPED_BY_APP["core"]
@ -45,6 +48,10 @@ export default function WebhookDialogForm(props: {
const hasSecretKey = !!defaultValues.secret;
const currentSecret = defaultValues.secret;
const subscriberUrlReserved = (subscriberUrl: string, id: string): boolean => {
return !!webhooks.find((webhook) => webhook.subscriberUrl === subscriberUrl && webhook.id !== id);
};
const form = useForm({
defaultValues,
});
@ -64,6 +71,10 @@ export default function WebhookDialogForm(props: {
data-testid="WebhookDialogForm"
form={form}
handleSubmit={async (event) => {
if (subscriberUrlReserved(event.subscriberUrl, event.id)) {
showToast(t("webhook_subscriber_url_reserved"), "error");
return;
}
const e = changeSecret
? { ...event, eventTypeId: props.eventTypeId, appId }
: { ...event, secret: currentSecret, eventTypeId: props.eventTypeId, appId };

View File

@ -74,6 +74,7 @@ export default function WebhookListContainer(props: WebhookListContainerType) {
<DialogContent>
<WebhookDialogForm
app={props.appId}
webhooks={data}
eventTypeId={props.eventTypeId}
handleClose={() => setNewWebhookModal(false)}
/>
@ -86,6 +87,7 @@ export default function WebhookListContainer(props: WebhookListContainerType) {
<WebhookDialogForm
app={props.appId}
key={editing.id}
webhooks={data}
eventTypeId={props.eventTypeId || undefined}
handleClose={() => setEditModalOpen(false)}
defaultValues={editing}

View File

@ -1040,6 +1040,7 @@
"company_size": "Company size",
"what_help_needed": "What do you need help with?",
"variable_format": "Variable format",
"webhook_subscriber_url_reserved": "Webhook subscriber url is already defined",
"custom_input_as_variable_info": "Ignore all special characters of the additional input label (use only letters and numbers), use uppercase for all letters and replace whitespaces with underscores.",
"using_additional_inputs_as_variables": "How to use additional inputs as variables?",
"download_desktop_app": "Download desktop app",

View File

@ -0,0 +1,8 @@
/*
Warnings:
- A unique constraint covering the columns `[userId,subscriberUrl]` on the table `Webhook` will be added. If there are existing duplicate values, this will fail.
*/
-- CreateIndex
CREATE UNIQUE INDEX "Webhook_userId_subscriberUrl_key" ON "Webhook"("userId", "subscriberUrl");

View File

@ -407,6 +407,8 @@ model Webhook {
app App? @relation(fields: [appId], references: [slug], onDelete: Cascade)
appId String?
secret String?
@@unique([userId, subscriberUrl], name: "courseIdentifier")
}
model Impersonations {