feat: add guards to stripe teams controller (#20540)
* feat: add guards to stripe teams controller * remove logs and comments * fix return type status * refactor: move PlatformSubscription to a dedicated module * reroute to `organizations/stripe/save` for teams * fix: type errors * feat: fixed it for conferencing apps * feat: Add error handling and fallback URL support in Stripe callback * Refactor OAuth callback handling and move token validation to service layer * Add documentation for OAuth callback proxying in conferencing and stripe controllers * Move OAuthCallbackState type from organizations to stripe service module
This commit is contained in:
1 parent
11a41e0e39
commit
59ab38db98
28 files changed
+999
-494
No files matched your search
@@ -139,7 +139,7 @@ export const AppleConnect: FC<Partial<Omit<OAuthConnectProps, "redir">>> = ({
|
||||
form.reset();
|
||||
setIsDialogOpen(false);
|
||||
toast({
|
||||
description: "Calendar credentials added successfully",
|
||||
description: "Calendar credentials added successfully",
|
||||
});
|
||||
onSuccess?.();
|
||||
} else {
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { ButtonColor } from "@calcom/ui/components/button";
|
||||
import type { IconName } from "@calcom/ui/components/icon";
|
||||
|
||||
import type { OnCheckErrorType, UseCheckProps } from "../../hooks/connect/useCheck";
|
||||
import { useTeamCheck, useCheck } from "../../hooks/stripe/useCheck";
|
||||
import { useCheck } from "../../hooks/stripe/useCheck";
|
||||
import { useConnect } from "../../hooks/stripe/useConnect";
|
||||
import { AtomsWrapper } from "../../src/components/atoms-wrapper";
|
||||
import { cn } from "../../src/lib/utils";
|
||||
@@ -46,24 +46,16 @@ export const StripeConnect: FC<Partial<StripeConnectProps>> = ({
|
||||
let displayedLabel = label || t("stripe_connect_atom_label");
|
||||
|
||||
const { connect } = useConnect(redir, errorRedir, teamId);
|
||||
const { allowConnect, checked } = useCheck({
|
||||
const { allowConnect, checked, isLoading } = useCheck({
|
||||
onCheckError,
|
||||
onCheckSuccess,
|
||||
initialData,
|
||||
});
|
||||
const { allowConnect: allowConnectTeam, checked: checkedTeam } = useTeamCheck({
|
||||
teamId,
|
||||
onCheckError,
|
||||
onCheckSuccess,
|
||||
initialData,
|
||||
});
|
||||
|
||||
const isChecking = teamId ? !checkedTeam : !checked;
|
||||
const isAllowConnect = teamId ? allowConnectTeam : allowConnect;
|
||||
const isDisabled = !checked || !allowConnect;
|
||||
|
||||
const isDisabled = isChecking || !isAllowConnect;
|
||||
|
||||
if (isChecking) {
|
||||
if (!checked) {
|
||||
displayedLabel = loadingLabel || t("stripe_connect_atom_loading_label");
|
||||
} else if (!allowConnect) {
|
||||
displayedLabel = alreadyConnectedLabel || t("stripe_connect_atom_already_connected_label");
|
||||
@@ -79,7 +71,7 @@ export const StripeConnect: FC<Partial<StripeConnectProps>> = ({
|
||||
"",
|
||||
"md:rounded-md",
|
||||
className,
|
||||
isChecking && "animate-pulse",
|
||||
isLoading && "animate-pulse bg-gray-200",
|
||||
isDisabled && "cursor-not-allowed",
|
||||
!isDisabled && "cursor-pointer"
|
||||
)}
|
||||
|
||||
@@ -9,19 +9,15 @@ import useAppsData from "@calcom/lib/hooks/useAppsData";
|
||||
import { EmptyScreen } from "@calcom/ui/components/empty-screen";
|
||||
|
||||
import { StripeConnect } from "../../connect/stripe/StripeConnect";
|
||||
import { useCheck, useTeamCheck } from "../../hooks/stripe/useCheck";
|
||||
import { useCheck } from "../../hooks/stripe/useCheck";
|
||||
import { useAtomsEventTypeById } from "../hooks/useAtomEventTypeAppIntegration";
|
||||
|
||||
const EventPaymentsTabPlatformWrapper = ({ eventType }: { eventType: EventTypeSetupProps["eventType"] }) => {
|
||||
const { allowConnect, checked } = useCheck({});
|
||||
const { allowConnect: allowConnectTeam, checked: checkedTeam } = useTeamCheck({ teamId: eventType.teamId });
|
||||
const { allowConnect, checked } = useCheck({ teamId: eventType.teamId });
|
||||
|
||||
const isAllowConnect = eventType.teamId ? allowConnectTeam : allowConnect;
|
||||
const isChecking = eventType.teamId ? !checkedTeam : !checked;
|
||||
const isStripeConnected = !checked || !allowConnect;
|
||||
|
||||
const isStripeConnected = isChecking || !isAllowConnect;
|
||||
|
||||
if (isChecking) return <div>Checking...</div>;
|
||||
if (!checked) return <div>Checking...</div>;
|
||||
|
||||
return (
|
||||
<div>
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import type { CALENDARS } from "@calcom/platform-constants";
|
||||
import { ERROR_STATUS, SUCCESS_STATUS } from "@calcom/platform-constants";
|
||||
import type { ApiErrorResponse, ApiResponse } from "@calcom/platform-types";
|
||||
|
||||
@@ -17,23 +16,33 @@ export interface UseCheckProps {
|
||||
checked: boolean;
|
||||
};
|
||||
};
|
||||
teamId?: number | null;
|
||||
}
|
||||
const stripeQueryKey = ["get-stripe-check"];
|
||||
const stripeTeamQueryKey = ["get-stripe-team-check"];
|
||||
const stripeTeamQueryKey = "get-stripe-check";
|
||||
|
||||
export type OnCheckErrorType = (err: ApiErrorResponse) => void;
|
||||
export const getQueryKey = (calendar: (typeof CALENDARS)[number]) => [`get-${calendar}-check`];
|
||||
|
||||
export const useCheck = ({ onCheckError, initialData, onCheckSuccess }: UseCheckProps) => {
|
||||
const { isInit, accessToken } = useAtomsContext();
|
||||
export const useCheck = ({ teamId, onCheckError, initialData, onCheckSuccess }: UseCheckProps) => {
|
||||
const { isInit, accessToken, organizationId } = useAtomsContext();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const { data: check, refetch } = useQuery({
|
||||
queryKey: stripeQueryKey,
|
||||
// Determine the appropriate endpoint based on whether teamId is provided
|
||||
let pathname = "/stripe/check";
|
||||
|
||||
if (teamId && organizationId) {
|
||||
pathname = `/organizations/${organizationId}/teams/${teamId}/stripe/check`;
|
||||
}
|
||||
|
||||
const {
|
||||
data: check,
|
||||
refetch,
|
||||
isLoading,
|
||||
} = useQuery({
|
||||
queryKey: [stripeTeamQueryKey, teamId, organizationId],
|
||||
enabled: isInit && !!accessToken,
|
||||
queryFn: () => {
|
||||
return http
|
||||
?.get<ApiResponse<{ checked: boolean; allowConnect: boolean }>>(`/stripe/check`)
|
||||
?.get<ApiResponse<{ checked: boolean; allowConnect: boolean }>>(pathname)
|
||||
.then(({ data: responseBody }) => {
|
||||
if (responseBody.status === SUCCESS_STATUS) {
|
||||
onCheckSuccess?.();
|
||||
@@ -53,54 +62,12 @@ export const useCheck = ({ onCheckError, initialData, onCheckSuccess }: UseCheck
|
||||
allowConnect: check?.data?.allowConnect ?? false,
|
||||
checked: check?.data?.checked ?? false,
|
||||
refetch: () => {
|
||||
queryClient.setQueryData(stripeQueryKey, {
|
||||
status: SUCCESS_STATUS,
|
||||
data: { allowConnect: false, checked: false },
|
||||
});
|
||||
refetch();
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const useTeamCheck = ({
|
||||
teamId,
|
||||
onCheckError,
|
||||
initialData,
|
||||
onCheckSuccess,
|
||||
}: UseCheckProps & { teamId?: number | null }) => {
|
||||
const { isInit, accessToken } = useAtomsContext();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const { data: check, refetch } = useQuery({
|
||||
queryKey: stripeTeamQueryKey,
|
||||
enabled: isInit && !!accessToken && !!teamId,
|
||||
queryFn: () => {
|
||||
return http
|
||||
?.get<ApiResponse<{ checked: boolean; allowConnect: boolean }>>(`/stripe/check/${teamId}`)
|
||||
.then(({ data: responseBody }) => {
|
||||
if (responseBody.status === SUCCESS_STATUS) {
|
||||
onCheckSuccess?.();
|
||||
return { status: SUCCESS_STATUS, data: { allowConnect: false, checked: true } };
|
||||
}
|
||||
onCheckError?.(responseBody);
|
||||
return { status: ERROR_STATUS, data: { allowConnect: true, checked: true } };
|
||||
})
|
||||
.catch((err) => {
|
||||
onCheckError?.(err);
|
||||
return { status: ERROR_STATUS, data: { allowConnect: true, checked: true } };
|
||||
});
|
||||
},
|
||||
initialData,
|
||||
});
|
||||
return {
|
||||
allowConnect: check?.data?.allowConnect ?? false,
|
||||
checked: check?.data?.checked ?? false,
|
||||
refetch: () => {
|
||||
queryClient.setQueryData(stripeQueryKey, {
|
||||
queryClient.setQueryData([stripeTeamQueryKey, teamId, organizationId], {
|
||||
status: SUCCESS_STATUS,
|
||||
data: { allowConnect: false, checked: false },
|
||||
});
|
||||
refetch();
|
||||
},
|
||||
isLoading,
|
||||
};
|
||||
};
|
||||
@@ -4,34 +4,43 @@ import { SUCCESS_STATUS, ERROR_STATUS } from "@calcom/platform-constants";
|
||||
import type { ApiResponse } from "@calcom/platform-types";
|
||||
|
||||
import http from "../../lib/http";
|
||||
import { useAtomsContext } from "../useAtomsContext";
|
||||
|
||||
export const useGetRedirectUrl = (redir?: string, errorRedir?: string, teamId?: number | null) => {
|
||||
const authUrl = useQuery({
|
||||
queryKey: ["get-stripe-connect-redirect-uri"],
|
||||
export const useGetRedirectUrl = (returnTo?: string, onErrorReturnTo?: string, teamId?: number | null) => {
|
||||
const { organizationId } = useAtomsContext();
|
||||
|
||||
// Determine the appropriate endpoint based on whether teamId is provided
|
||||
let pathname = "/stripe/connect";
|
||||
|
||||
if (teamId && organizationId) {
|
||||
pathname = `/organizations/${organizationId}/teams/${teamId}/stripe/connect`;
|
||||
}
|
||||
|
||||
// Add query parameters
|
||||
const queryParams = new URLSearchParams();
|
||||
if (returnTo) queryParams.append("returnTo", returnTo);
|
||||
if (onErrorReturnTo) queryParams.append("onErrorReturnTo", onErrorReturnTo);
|
||||
|
||||
const fullPath = queryParams.toString() ? `${pathname}?${queryParams.toString()}` : pathname;
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["get-stripe-connect-redirect-uri", teamId, organizationId],
|
||||
staleTime: Infinity,
|
||||
enabled: false,
|
||||
queryFn: () => {
|
||||
return http
|
||||
?.get<ApiResponse<{ authUrl: string }>>(
|
||||
`/stripe/connect${redir ? `?redir=${encodeURIComponent(redir)}` : "?redir="}${
|
||||
errorRedir ? `&errorRedir=${encodeURIComponent(errorRedir)}` : ""
|
||||
}${teamId ? `&teamId=${teamId}` : ""}`
|
||||
)
|
||||
.then(({ data: responseBody }) => {
|
||||
if (responseBody.status === SUCCESS_STATUS) {
|
||||
return responseBody.data.authUrl;
|
||||
}
|
||||
if (responseBody.status === ERROR_STATUS) throw new Error(responseBody.error.message);
|
||||
return "";
|
||||
});
|
||||
return http?.get<ApiResponse<{ authUrl: string }>>(fullPath).then(({ data: responseBody }) => {
|
||||
if (responseBody.status === SUCCESS_STATUS) {
|
||||
return responseBody.data.authUrl;
|
||||
}
|
||||
if (responseBody.status === ERROR_STATUS) throw new Error(responseBody.error.message);
|
||||
return "";
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
return authUrl;
|
||||
};
|
||||
|
||||
export const useConnect = (redir?: string, errorRedir?: string, teamId?: number | null) => {
|
||||
const { refetch } = useGetRedirectUrl(redir, errorRedir, teamId);
|
||||
export const useConnect = (returnTo?: string, onErrorReturnTo?: string, teamId?: number | null) => {
|
||||
const { refetch } = useGetRedirectUrl(returnTo, onErrorReturnTo, teamId);
|
||||
|
||||
const connect = async () => {
|
||||
const redirectUri = await refetch();
|
||||
|
||||
@@ -15,7 +15,7 @@ export default function ConferencingApps(props: { calUsername: string; calEmail:
|
||||
className={`flex min-h-screen flex-col ${inter.className} main text-default flex min-h-full w-full flex-col items-center overflow-visible`}>
|
||||
<Navbar username={props.calUsername} />
|
||||
<div className="my-8">
|
||||
<ConferencingAppsSettings returnTo={callbackUri} onErrorReturnTo={callbackUri} teamId={35} />
|
||||
<ConferencingAppsSettings returnTo={callbackUri} onErrorReturnTo={callbackUri} teamId={20} />
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user