feat: Workflow — Send Email to Specific Email (#14815)
* feat: Workflow — Send Email to Specific Email * disable button when verifyEmail pending * add verified emails table and routes * fix: include teamId in verifyEmailCode * fix: include email address in activate handler * fix: check team before adding email * feat: check verified email, add to team if only users * remove logs * fix: if statement Co-authored-by: Carina Wollendorfer <[email protected]> * change email to verify your email * fix: show error if saving unverified email * fix: show email in subject * verify email when editing steps * remove double calls from same block * verify email when creating new step --------- Co-authored-by: v0ltZzie <[email protected]> Co-authored-by: Carina Wollendorfer <[email protected]>
This commit is contained in:
21 files changed
+491
-70
No files matched your search
@@ -24,6 +24,7 @@ export const sendVerifyEmailCodeHandler = async ({ input, req }: SendVerifyEmail
|
||||
email: input.email,
|
||||
username: input.username,
|
||||
language: input.language,
|
||||
isVerifyingEmail: input.isVerifyingEmail,
|
||||
});
|
||||
|
||||
return email;
|
||||
|
||||
@@ -4,6 +4,7 @@ export const ZSendVerifyEmailCodeSchema = z.object({
|
||||
email: z.string().min(1),
|
||||
username: z.string().optional(),
|
||||
language: z.string().optional(),
|
||||
isVerifyingEmail: z.boolean().optional(),
|
||||
});
|
||||
|
||||
export type TSendVerifyEmailCodeSchema = z.infer<typeof ZSendVerifyEmailCodeSchema>;
|
||||
@@ -5,10 +5,12 @@ import { ZCreateInputSchema } from "./create.schema";
|
||||
import { ZDeleteInputSchema } from "./delete.schema";
|
||||
import { ZFilteredListInputSchema } from "./filteredList.schema";
|
||||
import { ZGetInputSchema } from "./get.schema";
|
||||
import { ZGetVerifiedEmailsInputSchema } from "./getVerifiedEmails.schema";
|
||||
import { ZGetVerifiedNumbersInputSchema } from "./getVerifiedNumbers.schema";
|
||||
import { ZListInputSchema } from "./list.schema";
|
||||
import { ZSendVerificationCodeInputSchema } from "./sendVerificationCode.schema";
|
||||
import { ZUpdateInputSchema } from "./update.schema";
|
||||
import { ZVerifyEmailCodeInputSchema } from "./verifyEmailCode.schema";
|
||||
import { ZVerifyPhoneNumberInputSchema } from "./verifyPhoneNumber.schema";
|
||||
|
||||
type WorkflowsRouterHandlerCache = {
|
||||
@@ -23,6 +25,8 @@ type WorkflowsRouterHandlerCache = {
|
||||
getVerifiedNumbers?: typeof import("./getVerifiedNumbers.handler").getVerifiedNumbersHandler;
|
||||
getWorkflowActionOptions?: typeof import("./getWorkflowActionOptions.handler").getWorkflowActionOptionsHandler;
|
||||
filteredList?: typeof import("./filteredList.handler").filteredListHandler;
|
||||
getVerifiedEmails?: typeof import("./getVerifiedEmails.handler").getVerifiedEmailsHandler;
|
||||
verifyEmailCode?: typeof import("./verifyEmailCode.handler").verifyEmailCodeHandler;
|
||||
};
|
||||
|
||||
const UNSTABLE_HANDLER_CACHE: WorkflowsRouterHandlerCache = {};
|
||||
@@ -182,6 +186,42 @@ export const workflowsRouter = router({
|
||||
});
|
||||
}),
|
||||
|
||||
getVerifiedEmails: authedProcedure.input(ZGetVerifiedEmailsInputSchema).query(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.getVerifiedEmails) {
|
||||
UNSTABLE_HANDLER_CACHE.getVerifiedEmails = await import("./getVerifiedEmails.handler").then(
|
||||
(mod) => mod.getVerifiedEmailsHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.getVerifiedEmails) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.getVerifiedEmails({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
}),
|
||||
|
||||
verifyEmailCode: authedProcedure.input(ZVerifyEmailCodeInputSchema).mutation(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.verifyPhoneNumber) {
|
||||
UNSTABLE_HANDLER_CACHE.verifyEmailCode = await import("./verifyEmailCode.handler").then(
|
||||
(mod) => mod.verifyEmailCodeHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.verifyEmailCode) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.verifyEmailCode({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
}),
|
||||
|
||||
getWorkflowActionOptions: authedProcedure.query(async ({ ctx }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.getWorkflowActionOptions) {
|
||||
UNSTABLE_HANDLER_CACHE.getWorkflowActionOptions = await import(
|
||||
|
||||
@@ -184,7 +184,11 @@ export const activateEventTypeHandler = async ({ ctx, input }: ActivateEventType
|
||||
},
|
||||
};
|
||||
for (const step of eventTypeWorkflow.steps) {
|
||||
if (step.action === WorkflowActions.EMAIL_ATTENDEE || step.action === WorkflowActions.EMAIL_HOST) {
|
||||
if (
|
||||
step.action === WorkflowActions.EMAIL_ATTENDEE ||
|
||||
step.action === WorkflowActions.EMAIL_HOST ||
|
||||
step.action === WorkflowActions.EMAIL_ADDRESS
|
||||
) {
|
||||
let sendTo: string[] = [];
|
||||
|
||||
switch (step.action) {
|
||||
@@ -194,6 +198,9 @@ export const activateEventTypeHandler = async ({ ctx, input }: ActivateEventType
|
||||
case WorkflowActions.EMAIL_ATTENDEE:
|
||||
sendTo = bookingInfo.attendees.map((attendee) => attendee.email);
|
||||
break;
|
||||
case WorkflowActions.EMAIL_ADDRESS:
|
||||
sendTo = step.sendTo ? [step.sendTo] : [];
|
||||
break;
|
||||
}
|
||||
|
||||
await scheduleEmailReminder({
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import type { VerifiedEmail } from "@prisma/client";
|
||||
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { TGetVerifiedEmailsInputSchema } from "./getVerifiedEmails.schema";
|
||||
|
||||
type GetVerifiedEmailsOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TGetVerifiedEmailsInputSchema;
|
||||
};
|
||||
|
||||
export const getVerifiedEmailsHandler = async ({ ctx, input }: GetVerifiedEmailsOptions) => {
|
||||
const { user } = ctx;
|
||||
const { teamId } = input;
|
||||
let verifiedEmails: VerifiedEmail[] = [];
|
||||
|
||||
if (teamId) {
|
||||
const team = await prisma.team.findFirst({
|
||||
where: {
|
||||
id: teamId,
|
||||
},
|
||||
select: {
|
||||
members: {
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!team) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Team not found" });
|
||||
}
|
||||
|
||||
const isTeamMember = team.members.some((member) => member.userId === user.id);
|
||||
|
||||
if (!isTeamMember) {
|
||||
throw new TRPCError({ code: "FORBIDDEN", message: "You are not a member of this team" });
|
||||
}
|
||||
|
||||
verifiedEmails = team.members.map((member) => ({
|
||||
email: member.user.email,
|
||||
userId: user.id,
|
||||
teamId,
|
||||
id: member.user.id,
|
||||
}));
|
||||
}
|
||||
|
||||
const emails = await prisma.verifiedEmail.findMany({
|
||||
where: {
|
||||
OR: [{ userId: user.id }, { teamId: input.teamId }],
|
||||
},
|
||||
});
|
||||
|
||||
verifiedEmails = verifiedEmails.concat(emails);
|
||||
|
||||
return verifiedEmails;
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZGetVerifiedEmailsInputSchema = z.object({
|
||||
teamId: z.number().optional(),
|
||||
});
|
||||
|
||||
export type TGetVerifiedEmailsInputSchema = z.infer<typeof ZGetVerifiedEmailsInputSchema>;
|
||||
Regular → Executable
+66
-17
@@ -39,6 +39,45 @@ type UpdateOptions = {
|
||||
input: TUpdateInputSchema;
|
||||
};
|
||||
|
||||
const verifyEmailSender = async (
|
||||
email: string,
|
||||
userId: number,
|
||||
teamId: number | null,
|
||||
prisma: PrismaClient
|
||||
) => {
|
||||
const verifiedEmail = await prisma.verifiedEmail.findFirst({
|
||||
where: {
|
||||
email,
|
||||
OR: [{ userId }, { teamId }],
|
||||
},
|
||||
});
|
||||
|
||||
if (!verifiedEmail) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: "Email not verified" });
|
||||
}
|
||||
|
||||
if (teamId) {
|
||||
if (!verifiedEmail.teamId) {
|
||||
await prisma.verifiedEmail.update({
|
||||
where: {
|
||||
id: verifiedEmail.id,
|
||||
},
|
||||
data: {
|
||||
teamId,
|
||||
},
|
||||
});
|
||||
} else if (verifiedEmail.teamId !== teamId) {
|
||||
await prisma.verifiedEmail.create({
|
||||
data: {
|
||||
email,
|
||||
userId,
|
||||
teamId,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
const { user } = ctx;
|
||||
const { id, name, activeOn, steps, trigger, time, timeUnit } = input;
|
||||
@@ -297,8 +336,8 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
};
|
||||
if (
|
||||
step.action === WorkflowActions.EMAIL_HOST ||
|
||||
step.action === WorkflowActions.EMAIL_ATTENDEE /*||
|
||||
step.action === WorkflowActions.EMAIL_ADDRESS*/
|
||||
step.action === WorkflowActions.EMAIL_ATTENDEE ||
|
||||
step.action === WorkflowActions.EMAIL_ADDRESS
|
||||
) {
|
||||
let sendTo: string[] = [];
|
||||
|
||||
@@ -309,8 +348,10 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
case WorkflowActions.EMAIL_ATTENDEE:
|
||||
sendTo = bookingInfo.attendees.map((attendee) => attendee.email);
|
||||
break;
|
||||
/*case WorkflowActions.EMAIL_ADDRESS:
|
||||
sendTo = step.sendTo || "";*/
|
||||
case WorkflowActions.EMAIL_ADDRESS:
|
||||
await verifyEmailSender(step.sendTo || "", user.id, userWorkflow.teamId, ctx.prisma);
|
||||
sendTo = [step.sendTo || ""];
|
||||
break;
|
||||
}
|
||||
|
||||
await scheduleEmailReminder({
|
||||
@@ -425,17 +466,20 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
throw new TRPCError({ code: "UNAUTHORIZED", message: "Not available on free plan" });
|
||||
}
|
||||
const requiresSender =
|
||||
newStep.action === WorkflowActions.SMS_NUMBER || newStep.action === WorkflowActions.WHATSAPP_NUMBER;
|
||||
newStep.action === WorkflowActions.SMS_NUMBER ||
|
||||
newStep.action === WorkflowActions.WHATSAPP_NUMBER ||
|
||||
newStep.action === WorkflowActions.EMAIL_ADDRESS;
|
||||
|
||||
if (newStep.action === WorkflowActions.EMAIL_ADDRESS) {
|
||||
await verifyEmailSender(newStep.sendTo || "", user.id, userWorkflow.teamId, ctx.prisma);
|
||||
}
|
||||
await ctx.prisma.workflowStep.update({
|
||||
where: {
|
||||
id: oldStep.id,
|
||||
},
|
||||
data: {
|
||||
action: newStep.action,
|
||||
sendTo: requiresSender /*||
|
||||
newStep.action === WorkflowActions.EMAIL_ADDRESS*/
|
||||
? newStep.sendTo
|
||||
: null,
|
||||
sendTo: requiresSender ? newStep.sendTo : null,
|
||||
stepNumber: newStep.stepNumber,
|
||||
workflowId: newStep.workflowId,
|
||||
reminderBody: newStep.reminderBody,
|
||||
@@ -522,8 +566,8 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
};
|
||||
if (
|
||||
newStep.action === WorkflowActions.EMAIL_HOST ||
|
||||
newStep.action === WorkflowActions.EMAIL_ATTENDEE /*||
|
||||
newStep.action === WorkflowActions.EMAIL_ADDRESS*/
|
||||
newStep.action === WorkflowActions.EMAIL_ATTENDEE ||
|
||||
newStep.action === WorkflowActions.EMAIL_ADDRESS
|
||||
) {
|
||||
let sendTo: string[] = [];
|
||||
|
||||
@@ -534,8 +578,9 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
case WorkflowActions.EMAIL_ATTENDEE:
|
||||
sendTo = bookingInfo.attendees.map((attendee) => attendee.email);
|
||||
break;
|
||||
/*case WorkflowActions.EMAIL_ADDRESS:
|
||||
sendTo = newStep.sendTo || "";*/
|
||||
case WorkflowActions.EMAIL_ADDRESS:
|
||||
sendTo = newStep.sendTo ? [newStep.sendTo] : [];
|
||||
break;
|
||||
}
|
||||
|
||||
await scheduleEmailReminder({
|
||||
@@ -611,6 +656,9 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
sender: newStep.sender || null,
|
||||
senderName: senderName,
|
||||
});
|
||||
if (newStep.action === WorkflowActions.EMAIL_ADDRESS) {
|
||||
await verifyEmailSender(newStep.sendTo || "", user.id, userWorkflow.teamId, ctx.prisma);
|
||||
}
|
||||
const createdStep = await ctx.prisma.workflowStep.create({
|
||||
data: { ...newStep, numberVerificationPending: false },
|
||||
});
|
||||
@@ -665,8 +713,8 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
|
||||
if (
|
||||
step.action === WorkflowActions.EMAIL_ATTENDEE ||
|
||||
step.action === WorkflowActions.EMAIL_HOST /*||
|
||||
step.action === WorkflowActions.EMAIL_ADDRESS*/
|
||||
step.action === WorkflowActions.EMAIL_HOST ||
|
||||
step.action === WorkflowActions.EMAIL_ADDRESS
|
||||
) {
|
||||
let sendTo: string[] = [];
|
||||
|
||||
@@ -677,8 +725,9 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
case WorkflowActions.EMAIL_ATTENDEE:
|
||||
sendTo = bookingInfo.attendees.map((attendee) => attendee.email);
|
||||
break;
|
||||
/*case WorkflowActions.EMAIL_ADDRESS:
|
||||
sendTo = step.sendTo || "";*/
|
||||
case WorkflowActions.EMAIL_ADDRESS:
|
||||
sendTo = step.sendTo ? [step.sendTo] : [];
|
||||
break;
|
||||
}
|
||||
|
||||
await scheduleEmailReminder({
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { createHash } from "crypto";
|
||||
|
||||
import { totpRawCheck } from "@calcom/lib/totp";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { TVerifyEmailCodeInputSchema } from "./verifyEmailCode.schema";
|
||||
|
||||
type VerifyEmailCodeOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TVerifyEmailCodeInputSchema;
|
||||
};
|
||||
|
||||
export const verifyEmailCodeHandler = async ({ ctx, input }: VerifyEmailCodeOptions) => {
|
||||
const { code, email, teamId } = input;
|
||||
const { id } = ctx.user;
|
||||
|
||||
if (!code || !email) throw new TRPCError({ code: "BAD_REQUEST" });
|
||||
|
||||
const secret = createHash("md5")
|
||||
.update(email + process.env.CALENDSO_ENCRYPTION_KEY)
|
||||
.digest("hex");
|
||||
|
||||
const isValidToken = totpRawCheck(code, secret, { step: 900 });
|
||||
|
||||
if (!isValidToken) throw new TRPCError({ code: "BAD_REQUEST", message: "invalid_code" });
|
||||
|
||||
await prisma.verifiedEmail.create({
|
||||
data: {
|
||||
email,
|
||||
userId: id,
|
||||
teamId,
|
||||
},
|
||||
});
|
||||
|
||||
return isValidToken;
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZVerifyEmailCodeInputSchema = z.object({
|
||||
teamId: z.number().optional(),
|
||||
code: z.string(),
|
||||
email: z.string(),
|
||||
});
|
||||
|
||||
export type TVerifyEmailCodeInputSchema = z.infer<typeof ZVerifyEmailCodeInputSchema>;
|
||||
Reference in new issue
Block a user