refactor: platform default event types (#19225)

* feat: add areDefaultEventTypesEnabled to PlatformOAuthClient

* feat: specify areDefaultEventTypesEnabled when creating OAuth client

* feat: specify areDefaultEventTypesEnabled when updating OAuth client

* feat: display areDefaultEventTypesEnabled in OAuth clients list

* refactor: set areDefaultEventTypesEnabled by default to false on API level

* feat: v2 API CREATE managed user toggle default event types

* refactor: centralize OAuth inputs and outputs in platform/types

* fix: correct response types for OAuth hooks

* refactor: web/lib/hooks/settings/platform/oauth-clients/useOAuthClients.ts

* refactor: web/lib/hooks/settings/platform/oauth-clients/useOAuthClients.ts

* refactor: split web OAuth hooks into separate files

* refactor: split web OAuth hooks into separate files

* docs: v2 OAuth client inputs and outputs

* refactor: update and create oauth client inputs
This commit is contained in:
Lauris Skraucis authored and GitHub committed 2025-02-26 12:06:08 -03:00
1 parent 07789ba697
commit e437dfa000
35 files changed
+519 -138

No files matched your search

@@ -0,0 +1,20 @@
import { useQuery } from "@tanstack/react-query";
export const useCheckTeamBilling = (teamId?: number | null, isPlatformTeam?: boolean | null) => {
const QUERY_KEY = "check-team-billing";
const isTeamBilledAlready = useQuery({
queryKey: [QUERY_KEY, teamId],
queryFn: async () => {
const response = await fetch(`/api/v2/billing/${teamId}/check`, {
method: "get",
headers: { "Content-type": "application/json" },
});
const data = await response.json();
return data.data;
},
enabled: !!teamId && !!isPlatformTeam,
});
return isTeamBilledAlready;
};
@@ -0,0 +1,41 @@
import { useMutation } from "@tanstack/react-query";
import { SUCCESS_STATUS } from "@calcom/platform-constants";
import type { ApiResponse, SubscribeTeamInput } from "@calcom/platform-types";
export const useSubscribeTeamToStripe = (
{
onSuccess,
onError,
teamId,
}: { teamId?: number | null; onSuccess: (redirectUrl: string) => void; onError: () => void } = {
onSuccess: () => {
return;
},
onError: () => {
return;
},
}
) => {
const mutation = useMutation<ApiResponse<{ action: string; url: string }>, unknown, SubscribeTeamInput>({
mutationFn: (data) => {
return fetch(`/api/v2/billing/${teamId}/subscribe`, {
method: "post",
headers: { "Content-type": "application/json" },
body: JSON.stringify(data),
}).then((res) => res?.json());
},
onSuccess: (data) => {
if (data.status === SUCCESS_STATUS) {
onSuccess?.(data.data?.url);
} else {
onError?.();
}
},
onError: () => {
onError?.();
},
});
return mutation;
};
@@ -0,0 +1,41 @@
import { useMutation } from "@tanstack/react-query";
import { SUCCESS_STATUS } from "@calcom/platform-constants";
import type { ApiResponse, SubscribeTeamInput } from "@calcom/platform-types";
export const useUpgradeTeamSubscriptionInStripe = (
{
onSuccess,
onError,
teamId,
}: { teamId?: number | null; onSuccess: (redirectUrl: string) => void; onError: () => void } = {
onSuccess: () => {
return;
},
onError: () => {
return;
},
}
) => {
const mutation = useMutation<ApiResponse<{ action: string; url: string }>, unknown, SubscribeTeamInput>({
mutationFn: (data) => {
return fetch(`/api/v2/billing/${teamId}/upgrade`, {
method: "post",
headers: { "Content-type": "application/json" },
body: JSON.stringify(data),
}).then((res) => res?.json());
},
onSuccess: (data) => {
if (data.status === SUCCESS_STATUS) {
onSuccess?.(data.data?.url);
} else {
onError?.();
}
},
onError: () => {
onError?.();
},
});
return mutation;
};
@@ -0,0 +1,40 @@
import { useMutation } from "@tanstack/react-query";
import { SUCCESS_STATUS } from "@calcom/platform-constants";
import type { ApiResponse, CreateOAuthClientInput, CreateOAuthClientOutput } from "@calcom/platform-types";
export interface IPersistOAuthClient {
onSuccess?: () => void;
onError?: () => void;
}
export const useCreateOAuthClient = (
{ onSuccess, onError }: IPersistOAuthClient = {
onSuccess: () => {
return;
},
onError: () => {
return;
},
}
) => {
return useMutation<ApiResponse<CreateOAuthClientOutput>, unknown, CreateOAuthClientInput>({
mutationFn: async (data) => {
return fetch("/api/v2/oauth-clients", {
method: "post",
headers: { "Content-type": "application/json" },
body: JSON.stringify(data),
}).then((res) => res.json());
},
onSuccess: (data) => {
if (data.status === SUCCESS_STATUS) {
onSuccess?.();
} else {
onError?.();
}
},
onError: () => {
onError?.();
},
});
};
@@ -0,0 +1,39 @@
import { useMutation } from "@tanstack/react-query";
import { SUCCESS_STATUS } from "@calcom/platform-constants";
import type { ApiResponse, DeleteOAuthClientInput, PlatformOAuthClientDto } from "@calcom/platform-types";
import type { IPersistOAuthClient } from "@lib/hooks/settings/platform/oauth-clients/useCreateOAuthClient";
export const useDeleteOAuthClient = (
{ onSuccess, onError }: IPersistOAuthClient = {
onSuccess: () => {
return;
},
onError: () => {
return;
},
}
) => {
const mutation = useMutation<ApiResponse<PlatformOAuthClientDto>, unknown, DeleteOAuthClientInput>({
mutationFn: async (data) => {
const { id } = data;
return fetch(`/api/v2/oauth-clients/${id}`, {
method: "delete",
headers: { "Content-type": "application/json" },
}).then((res) => res?.json());
},
onSuccess: (data) => {
if (data.status === SUCCESS_STATUS) {
onSuccess?.();
} else {
onError?.();
}
},
onError: () => {
onError?.();
},
});
return mutation;
};
@@ -0,0 +1,39 @@
import { useQuery } from "@tanstack/react-query";
import type { ApiSuccessResponse, PlatformOAuthClientDto } from "@calcom/platform-types";
export const useOAuthClient = (clientId?: string) => {
const {
isLoading,
error,
data: response,
isFetched,
isError,
isFetching,
isSuccess,
isFetchedAfterMount,
refetch,
} = useQuery<ApiSuccessResponse<PlatformOAuthClientDto>>({
queryKey: ["oauth-client", clientId],
queryFn: () => {
return fetch(`/api/v2/oauth-clients/${clientId}`, {
method: "get",
headers: { "Content-type": "application/json" },
}).then((res) => res.json());
},
enabled: Boolean(clientId),
staleTime: Infinity,
});
return {
isLoading,
error,
data: response?.data,
isFetched,
isError,
isFetching,
isSuccess,
isFetchedAfterMount,
refetch,
};
};
@@ -2,17 +2,6 @@ import { useQuery } from "@tanstack/react-query";
import type { ApiSuccessResponse, PlatformOAuthClientDto } from "@calcom/platform-types";
export type ManagedUser = {
id: number;
email: string;
username: string | null;
timeZone: string;
weekStart: string;
createdDate: Date;
timeFormat: number | null;
defaultScheduleId: number | null;
};
export const useOAuthClients = () => {
const query = useQuery<ApiSuccessResponse<PlatformOAuthClientDto[]>>({
queryKey: ["oauth-clients"],
@@ -26,58 +15,3 @@ export const useOAuthClients = () => {
return { ...query, data: query.data?.data ?? [] };
};
export const useOAuthClient = (clientId?: string) => {
const {
isLoading,
error,
data: response,
isFetched,
isError,
isFetching,
isSuccess,
isFetchedAfterMount,
refetch,
} = useQuery<ApiSuccessResponse<PlatformOAuthClientDto>>({
queryKey: ["oauth-client", clientId],
queryFn: () => {
return fetch(`/api/v2/oauth-clients/${clientId}`, {
method: "get",
headers: { "Content-type": "application/json" },
}).then((res) => res.json());
},
enabled: Boolean(clientId),
staleTime: Infinity,
});
return {
isLoading,
error,
data: response?.data,
isFetched,
isError,
isFetching,
isSuccess,
isFetchedAfterMount,
refetch,
};
};
export const useGetOAuthClientManagedUsers = (clientId: string) => {
const {
isLoading,
error,
data: response,
refetch,
} = useQuery<ApiSuccessResponse<ManagedUser[]>>({
queryKey: ["oauth-client-managed-users", clientId],
queryFn: () => {
return fetch(`/api/v2/oauth-clients/${clientId}/managed-users`, {
method: "get",
headers: { "Content-type": "application/json" },
}).then((res) => res.json());
},
enabled: Boolean(clientId),
});
return { isLoading, error, data: response?.data, refetch };
};
@@ -0,0 +1,43 @@
import { useMutation } from "@tanstack/react-query";
import { SUCCESS_STATUS } from "@calcom/platform-constants";
import type { ApiResponse, CreateOAuthClientInput, PlatformOAuthClientDto } from "@calcom/platform-types";
import type { IPersistOAuthClient } from "@lib/hooks/settings/platform/oauth-clients/useCreateOAuthClient";
export const useUpdateOAuthClient = (
{ onSuccess, onError, clientId }: IPersistOAuthClient & { clientId?: string } = {
onSuccess: () => {
return;
},
onError: () => {
return;
},
}
) => {
const mutation = useMutation<
ApiResponse<PlatformOAuthClientDto>,
unknown,
Omit<CreateOAuthClientInput, "permissions">
>({
mutationFn: async (data) => {
return fetch(`/api/v2/oauth-clients/${clientId}`, {
method: "PATCH",
headers: { "Content-type": "application/json" },
body: JSON.stringify(data),
}).then((res) => res?.json());
},
onSuccess: (data) => {
if (data.status === SUCCESS_STATUS) {
onSuccess?.();
} else {
onError?.();
}
},
onError: () => {
onError?.();
},
});
return mutation;
};