2023-04-25 22:39:47 +00:00
|
|
|
import type { Workflow } from "@prisma/client";
|
2023-05-02 11:44:05 +00:00
|
|
|
|
|
|
|
import emailReminderTemplate from "@calcom/ee/workflows/lib/reminders/templates/emailReminderTemplate";
|
|
|
|
import { SENDER_NAME } from "@calcom/lib/constants";
|
2023-07-19 14:30:37 +00:00
|
|
|
import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat";
|
2023-08-24 09:14:10 +00:00
|
|
|
import type { PrismaClient } from "@calcom/prisma";
|
2023-05-02 11:44:05 +00:00
|
|
|
import { prisma } from "@calcom/prisma";
|
2023-04-25 22:39:47 +00:00
|
|
|
import {
|
|
|
|
MembershipRole,
|
|
|
|
TimeUnit,
|
|
|
|
WorkflowActions,
|
|
|
|
WorkflowTemplates,
|
|
|
|
WorkflowTriggerEvents,
|
2023-05-02 11:44:05 +00:00
|
|
|
} from "@calcom/prisma/enums";
|
2023-04-25 22:39:47 +00:00
|
|
|
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
|
|
|
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
|
|
|
|
import type { TCreateInputSchema } from "./create.schema";
|
|
|
|
|
|
|
|
type CreateOptions = {
|
|
|
|
ctx: {
|
|
|
|
user: NonNullable<TrpcSessionUser>;
|
|
|
|
prisma: PrismaClient;
|
|
|
|
};
|
|
|
|
input: TCreateInputSchema;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const createHandler = async ({ ctx, input }: CreateOptions) => {
|
|
|
|
const { teamId } = input;
|
|
|
|
|
|
|
|
const userId = ctx.user.id;
|
|
|
|
|
|
|
|
if (teamId) {
|
|
|
|
const team = await prisma.team.findFirst({
|
|
|
|
where: {
|
|
|
|
id: teamId,
|
|
|
|
members: {
|
|
|
|
some: {
|
|
|
|
userId: ctx.user.id,
|
|
|
|
accepted: true,
|
|
|
|
NOT: {
|
|
|
|
role: MembershipRole.MEMBER,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!team) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "UNAUTHORIZED",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const workflow: Workflow = await prisma.workflow.create({
|
|
|
|
data: {
|
|
|
|
name: "",
|
|
|
|
trigger: WorkflowTriggerEvents.BEFORE_EVENT,
|
|
|
|
time: 24,
|
|
|
|
timeUnit: TimeUnit.HOUR,
|
|
|
|
userId,
|
|
|
|
teamId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-07-19 14:30:37 +00:00
|
|
|
const renderedEmailTemplate = emailReminderTemplate(
|
|
|
|
true,
|
|
|
|
WorkflowActions.EMAIL_ATTENDEE,
|
|
|
|
getTimeFormatStringFromUserTimeFormat(ctx.user.timeFormat)
|
|
|
|
);
|
|
|
|
|
2023-04-25 22:39:47 +00:00
|
|
|
await ctx.prisma.workflowStep.create({
|
|
|
|
data: {
|
|
|
|
stepNumber: 1,
|
|
|
|
action: WorkflowActions.EMAIL_ATTENDEE,
|
|
|
|
template: WorkflowTemplates.REMINDER,
|
2023-07-19 14:30:37 +00:00
|
|
|
reminderBody: renderedEmailTemplate.emailBody,
|
|
|
|
emailSubject: renderedEmailTemplate.emailSubject,
|
2023-04-25 22:39:47 +00:00
|
|
|
workflowId: workflow.id,
|
|
|
|
sender: SENDER_NAME,
|
|
|
|
numberVerificationPending: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return { workflow };
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|