* Implement secondary email * Fix already existing tests failing * Added tests for secondary email feature * Skip email verification if user is changing the primary email to a verified secondary email * Fix type errors in tests * Fix email becoming unverified when switched between primary and secondary email * Added a check to prevent prisma error from throwing up due to duplicate records * Improved error handling when adding a secondary email * Add test for resend verification email flow for secondary emails and validation of invite link * Add a new column to link secondary emails with verification tokens * Fix failing to update email to an unverified secondary email of the same user * Fix failing tests * Change text of resend verification email * Add ability to use the verified secondary emails to get the event details to * Fix type errors * Fix failing e2e tests * Fix failing unit tests * Fix failing type checks * Fix secondary verification email subject * Fix failing e2e tests --------- Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com>
58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
import type { GetServerSidePropsContext, NextApiResponse } from "next";
|
|
|
|
import { sendEmailVerification } from "@calcom/features/auth/lib/verifyEmail";
|
|
import { prisma } from "@calcom/prisma";
|
|
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TAddSecondaryEmailInputSchema } from "./addSecondaryEmail.schema";
|
|
|
|
type AddSecondaryEmailOptions = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
res?: NextApiResponse | GetServerSidePropsContext["res"];
|
|
};
|
|
input: TAddSecondaryEmailInputSchema;
|
|
};
|
|
|
|
export const addSecondaryEmailHandler = async ({ ctx, input }: AddSecondaryEmailOptions) => {
|
|
const { user } = ctx;
|
|
|
|
const existingPrimaryEmail = await prisma.user.findUnique({
|
|
where: {
|
|
email: input.email,
|
|
},
|
|
});
|
|
|
|
if (existingPrimaryEmail) {
|
|
throw new TRPCError({ code: "BAD_REQUEST", message: "Email already taken" });
|
|
}
|
|
|
|
const existingSecondaryEmail = await prisma.secondaryEmail.findUnique({
|
|
where: {
|
|
email: input.email,
|
|
},
|
|
});
|
|
|
|
if (existingSecondaryEmail) {
|
|
throw new TRPCError({ code: "BAD_REQUEST", message: "Email already taken" });
|
|
}
|
|
|
|
const updatedData = await prisma.secondaryEmail.create({
|
|
data: { ...input, userId: user.id },
|
|
});
|
|
|
|
await sendEmailVerification({
|
|
email: updatedData.email,
|
|
username: user?.username ?? undefined,
|
|
language: user.locale,
|
|
secondaryEmailId: updatedData.id,
|
|
});
|
|
|
|
return {
|
|
data: updatedData,
|
|
message: "Secondary email added successfully",
|
|
};
|
|
};
|