* fix: hide branding for teams * fix: remove unused organizationId and username fields from profiles select Addresses Cubic AI review feedback (confidence 9/10) to select only the profile fields that are actually used. The organizationId and username fields were fetched but never referenced in this function. Co-Authored-By: unknown <> * fix: unit tests * fix: add prisma named export to test mock Co-Authored-By: rajiv@cal.com <sahalrajiv6900@gmail.com> * Add tests: packages/features/profile/lib/hideBranding.test.ts Generated by Paragon from proposal for PR #27643 * chore: implement cubic feedback * fix: merge conflicts * fix: unit tests * fixup * refactor: implement DI pattern for event type service * fix: atoms build --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
111 lines
3.5 KiB
TypeScript
111 lines
3.5 KiB
TypeScript
import { useMutation } from "@tanstack/react-query";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import { useState } from "react";
|
|
|
|
import { useBookerStore } from "@calcom/features/bookings/Booker/store";
|
|
import { useDebounce } from "@calcom/lib/hooks/useDebounce";
|
|
import { SUCCESS_STATUS } from "@calcom/platform-constants";
|
|
import type { ApiResponse, ApiErrorResponse, ApiSuccessResponseWithoutData } from "@calcom/platform-types";
|
|
|
|
import { useMe } from "../hooks/useMe";
|
|
import http from "../lib/http";
|
|
|
|
export interface IUseVerifyEmailProps {
|
|
email: string;
|
|
onVerifyEmail?: () => void;
|
|
name?: string | { firstName: string; lastname?: string };
|
|
requiresBookerEmailVerification?: boolean;
|
|
eventTypeId?: number;
|
|
}
|
|
|
|
export type UseVerifyEmailReturnType = ReturnType<typeof useVerifyEmail>;
|
|
|
|
interface RequestEmailVerificationInput {
|
|
email: string;
|
|
username?: string;
|
|
eventTypeId?: number;
|
|
}
|
|
|
|
export const useVerifyEmail = ({
|
|
email,
|
|
name,
|
|
requiresBookerEmailVerification,
|
|
onVerifyEmail,
|
|
eventTypeId,
|
|
}: IUseVerifyEmailProps) => {
|
|
const [isEmailVerificationModalVisible, setEmailVerificationModalVisible] = useState(false);
|
|
const verifiedEmail = useBookerStore((state) => state.verifiedEmail);
|
|
const setVerifiedEmail = useBookerStore((state) => state.setVerifiedEmail);
|
|
const isRescheduling = useBookerStore((state) => Boolean(state.rescheduleUid && state.bookingData));
|
|
const debouncedEmail = useDebounce(email, 600);
|
|
const { data: user } = useMe();
|
|
|
|
const { data: isEmailVerificationRequired } = useQuery<boolean>({
|
|
queryKey: ["isEmailVerificationRequired", debouncedEmail],
|
|
queryFn: () => {
|
|
return http
|
|
?.get<ApiResponse<boolean>>("/atoms/verification/email/check", {
|
|
params: {
|
|
email: debouncedEmail,
|
|
userSessionEmail: user?.data?.email || "",
|
|
},
|
|
})
|
|
.then((res) => {
|
|
if (res.data.status === SUCCESS_STATUS) {
|
|
return res.data.data;
|
|
}
|
|
throw new Error(res.data.error.message);
|
|
});
|
|
},
|
|
enabled: !!debouncedEmail && !isRescheduling,
|
|
});
|
|
|
|
const sendEmailVerificationMutation = useMutation<
|
|
ApiSuccessResponseWithoutData,
|
|
ApiErrorResponse,
|
|
RequestEmailVerificationInput
|
|
>({
|
|
mutationFn: (props: RequestEmailVerificationInput) => {
|
|
return http
|
|
.post<ApiResponse<{ sent: boolean }>>("/atoms/verification/email/send-code", props)
|
|
.then((res) => {
|
|
if (res.data.status === SUCCESS_STATUS) {
|
|
return res.data;
|
|
}
|
|
throw new Error(res.data.error?.message || "Failed to send verification email");
|
|
});
|
|
},
|
|
onSuccess: () => {
|
|
setEmailVerificationModalVisible(true);
|
|
},
|
|
onError: (err) => {
|
|
console.error("Failed to send verification email:", err);
|
|
},
|
|
});
|
|
|
|
const handleVerifyEmail = () => {
|
|
onVerifyEmail?.();
|
|
sendEmailVerificationMutation.mutate({
|
|
email,
|
|
username: typeof name === "string" ? name : name?.firstName,
|
|
eventTypeId,
|
|
});
|
|
};
|
|
|
|
const isVerificationCodeSending = sendEmailVerificationMutation.isPending;
|
|
|
|
const renderConfirmNotVerifyEmailButtonCond =
|
|
isRescheduling ||
|
|
(!requiresBookerEmailVerification && !isEmailVerificationRequired) ||
|
|
(email && verifiedEmail && verifiedEmail === email);
|
|
|
|
return {
|
|
handleVerifyEmail,
|
|
isEmailVerificationModalVisible,
|
|
setEmailVerificationModalVisible,
|
|
setVerifiedEmail,
|
|
renderConfirmNotVerifyEmailButtonCond: Boolean(renderConfirmNotVerifyEmailButtonCond),
|
|
isVerificationCodeSending,
|
|
};
|
|
};
|