feat: Platform OAuthClient Webhooks (#16134)

* wip

* fixup! Merge branch 'main' into platform-oauth-client-webhooks

* wip

* fixup! Merge branch 'main' into platform-oauth-client-webhooks

* fixup! fixup! Merge branch 'main' into platform-oauth-client-webhooks

* fixup! fixup! fixup! Merge branch 'main' into platform-oauth-client-webhooks

* fixup! fixup! fixup! fixup! Merge branch 'main' into platform-oauth-client-webhooks

* fixup! fixup! fixup! fixup! fixup! Merge branch 'main' into platform-oauth-client-webhooks

* fixup! fixup! fixup! fixup! fixup! fixup! Merge branch 'main' into platform-oauth-client-webhooks

* fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge branch 'main' into platform-oauth-client-webhooks

* fixup! Merge branch 'platform-oauth-client-webhooks' of github.com:calcom/cal.com into platform-oauth-client-webhooks

* fixup! fixup! Merge branch 'platform-oauth-client-webhooks' of github.com:calcom/cal.com into platform-oauth-client-webhooks

---------

Co-authored-by: Peer Richelsen <[email protected]>
This commit is contained in:
MorganandPeer Richelsen authored and GitHub committed 2024-08-09 17:02:39 +02:00
1 parent 4def31e98f
commit dda4b17a7c
47 files changed
+1199 -68

No files matched your search

@@ -158,6 +158,13 @@ export const OAuthClientCard = ({
</div>
</div>
<div className="flex items-start gap-4">
<Button
className="bg-subtle hover:bg-emphasis text-white"
loading={isLoading}
disabled={isLoading}
onClick={() => router.push(`/settings/platform/oauth-clients/${id}/edit/webhooks`)}>
Webhooks
</Button>
<Button
className="bg-subtle hover:bg-emphasis text-white"
loading={isLoading}
@@ -0,0 +1,84 @@
import { useMutation, useQuery } from "@tanstack/react-query";
import type { ApiSuccessResponse } from "@calcom/platform-types";
import type { WebhookTriggerEvents } from "@calcom/prisma/enums";
type Input = {
active: boolean;
subscriberUrl: string;
triggers: WebhookTriggerEvents[];
secret?: string;
payloadTemplate?: string;
};
type Output = {
id: string;
OAuthClientId: string;
active: boolean;
subscriberUrl: string;
triggers: WebhookTriggerEvents[];
secret?: string;
payloadTemplate: string | undefined | null;
};
export const useOAuthClientWebhooks = (clientId: string) => {
const query = useQuery<ApiSuccessResponse<Output[]>>({
queryKey: ["oauth-clients-webhooks", "findMany", clientId],
queryFn: () => {
return fetch(`/api/v2/oauth-clients/${clientId}/webhooks`, {
method: "get",
headers: { "Content-type": "application/json" },
}).then((res) => res.json());
},
enabled: !!clientId,
});
return { ...query, data: query.data?.data ?? [], status: query.data?.status };
};
export const useOAuthClientWebhook = (clientId: string, webhookId: string) => {
const query = useQuery<ApiSuccessResponse<Output>>({
queryKey: ["oauth-clients-webhooks", "findOne", clientId, webhookId],
queryFn: () => {
return fetch(`/api/v2/oauth-clients/${clientId}/webhooks/${webhookId}`, {
method: "get",
headers: { "Content-type": "application/json" },
}).then((res) => res.json());
},
enabled: !!clientId,
});
return { ...query, data: query.data?.data ?? [], status: query.data?.status };
};
export const useUpdateOAuthClientWebhook = (clientId: string) => {
const mutation = useMutation<
ApiSuccessResponse<Output>,
unknown,
{ webhookId: string; body: Partial<Input> }
>({
mutationFn: ({ webhookId, body }) => {
return fetch(`/api/v2/oauth-clients/${clientId}/webhooks/${webhookId}`, {
method: "PATCH",
headers: { "Content-type": "application/json" },
body: JSON.stringify(body),
}).then((res) => res.json());
},
});
return mutation;
};
export const useCreateOAuthClientWebhook = (clientId: string) => {
const mutation = useMutation<ApiSuccessResponse<Output>, unknown, Input>({
mutationFn: (body: Input) => {
return fetch(`/api/v2/oauth-clients/${clientId}/webhooks`, {
method: "POST",
headers: { "Content-type": "application/json" },
body: JSON.stringify(body),
}).then((res) => res.json());
},
});
return mutation;
};
@@ -0,0 +1,123 @@
import { useParams } from "next/navigation";
import { useRouter } from "next/navigation";
import Shell from "@calcom/features/shell/Shell";
import { WebhookForm } from "@calcom/features/webhooks/components";
import { WebhookTriggerEvents } from "@calcom/prisma/enums";
import { showToast } from "@calcom/ui";
import {
useOAuthClientWebhooks,
useCreateOAuthClientWebhook,
useUpdateOAuthClientWebhook,
} from "@lib/hooks/settings/platform/oauth-clients/useOAuthClientWebhooks";
import PageWrapper from "@components/PageWrapper";
import NoPlatformPlan from "@components/settings/platform/dashboard/NoPlatformPlan";
import { useGetUserAttributes } from "@components/settings/platform/hooks/useGetUserAttributes";
export default function EditOAuthClientWebhooks() {
const router = useRouter();
const params = useParams<{ clientId: string }>();
const clientId = params?.clientId || "";
const { isUserLoading, isPlatformUser, isPaidUser } = useGetUserAttributes();
const {
data: webhooks,
status: webhooksStatus,
isFetched: isWebhooksFetched,
refetch: refetchWebhooks,
} = useOAuthClientWebhooks(clientId);
const webhookId = webhooks?.[0]?.id ?? "";
const webhook = webhooks?.[0];
const { mutateAsync: createWebhook } = useCreateOAuthClientWebhook(clientId);
const { mutateAsync: updateWebhook } = useUpdateOAuthClientWebhook(clientId);
if (isUserLoading) return <div className="m-5">Loading...</div>;
if (isPlatformUser && isPaidUser) {
return (
<div>
<Shell title="OAuth client updation form" isPlatformUser={true}>
<div className="m-2 md:mx-5">
<div className="border-subtle mx-auto block justify-between rounded-t-lg border px-4 py-6 sm:flex sm:px-6">
<div className="flex w-full flex-col">
<h1 className="font-cal text-emphasis mb-1 text-xl font-semibold leading-5 tracking-wide">
Webhook update form
</h1>
<p className="text-default text-sm ltr:mr-4 rtl:ml-4">Add a webhook to your OAuthClient.</p>
</div>
</div>
{webhooksStatus !== "success" && <p>Error while trying to access webhooks.</p>}
{isWebhooksFetched && webhooksStatus === "success" && (
<WebhookForm
overrideTriggerOptions={[
{ value: WebhookTriggerEvents.BOOKING_CANCELLED, label: "booking_cancelled" },
{ value: WebhookTriggerEvents.BOOKING_CREATED, label: "booking_created" },
{ value: WebhookTriggerEvents.BOOKING_REJECTED, label: "booking_rejected" },
{ value: WebhookTriggerEvents.BOOKING_REQUESTED, label: "booking_requested" },
{ value: WebhookTriggerEvents.BOOKING_RESCHEDULED, label: "booking_rescheduled" },
{ value: WebhookTriggerEvents.BOOKING_NO_SHOW_UPDATED, label: "booking_no_show_updated" },
{ value: WebhookTriggerEvents.MEETING_ENDED, label: "meeting_ended" },
{ value: WebhookTriggerEvents.MEETING_STARTED, label: "meeting_started" },
{ value: WebhookTriggerEvents.RECORDING_READY, label: "recording_ready" },
{
value: WebhookTriggerEvents.RECORDING_TRANSCRIPTION_GENERATED,
label: "recording_transcription_generated",
},
]}
onSubmit={async (data) => {
try {
const body = {
active: data.active,
payloadTemplate: data.payloadTemplate ?? undefined,
subscriberUrl: data.subscriberUrl,
triggers: data.eventTriggers,
secret: data.secret ?? undefined,
};
if (webhook) {
await updateWebhook({
webhookId,
body,
});
showToast("Webhook updated successfully.", "success");
} else {
await createWebhook(body);
showToast("Webhook created successfully.", "success");
}
await refetchWebhooks();
router.push("/settings/platform/");
} catch (err) {
showToast(`Failed to ${webhookId ? "update" : "create"} webhook.`, "error");
}
}}
onCancel={() => {
router.push("/settings/platform/");
}}
noRoutingFormTriggers={true}
webhook={
webhook
? { ...webhook, eventTriggers: webhook.triggers, secret: webhook.secret ?? null }
: undefined
}
/>
)}
</div>
</Shell>
</div>
);
}
return (
<div>
<Shell isPlatformUser={true} hideHeadingOnMobile withoutMain={false} SidebarContainer={<></>}>
<NoPlatformPlan />
</Shell>
</div>
);
}
EditOAuthClientWebhooks.PageWrapper = PageWrapper;
@@ -62,7 +62,7 @@ export default function CreateOAuthClient() {
return (
<div>
<Shell title={`OAuth client ${!!clientId ? "updation" : "creation"} form`} isPlatformUser={true}>
<div className="m-2 md:mx-14 md:mx-5">
<div className="m-2 md:mx-5">
<div className="border-subtle mx-auto block justify-between rounded-t-lg border px-4 py-6 sm:flex sm:px-6">
<div className="flex w-full flex-col">
<h1 className="font-cal text-emphasis mb-1 text-xl font-semibold leading-5 tracking-wide">