Files
calendar/packages/platform/atoms/hooks/stripe/useCheck.ts
T
Somay ChauhanandGitHub 59ab38db98 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
2025-04-17 16:36:31 +00:00

74 lines
2.3 KiB
TypeScript

import { useQuery, useQueryClient } from "@tanstack/react-query";
import { ERROR_STATUS, SUCCESS_STATUS } from "@calcom/platform-constants";
import type { ApiErrorResponse, ApiResponse } from "@calcom/platform-types";
import http from "../../lib/http";
import { useAtomsContext } from "../useAtomsContext";
export interface UseCheckProps {
onCheckError?: OnCheckErrorType;
onCheckSuccess?: () => void;
initialData?: {
status: typeof SUCCESS_STATUS | typeof ERROR_STATUS;
data: {
allowConnect: boolean;
checked: boolean;
};
};
teamId?: number | null;
}
const stripeTeamQueryKey = "get-stripe-check";
export type OnCheckErrorType = (err: ApiErrorResponse) => void;
export const useCheck = ({ teamId, onCheckError, initialData, onCheckSuccess }: UseCheckProps) => {
const { isInit, accessToken, organizationId } = useAtomsContext();
const queryClient = useQueryClient();
// 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 }>>(pathname)
.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([stripeTeamQueryKey, teamId, organizationId], {
status: SUCCESS_STATUS,
data: { allowConnect: false, checked: false },
});
refetch();
},
isLoading,
};
};