2023-02-28 21:15:50 +00:00
|
|
|
import type { Workflow, Prisma } from "@prisma/client";
|
2022-07-14 00:10:45 +00:00
|
|
|
import {
|
|
|
|
WorkflowTemplates,
|
|
|
|
WorkflowActions,
|
|
|
|
WorkflowTriggerEvents,
|
|
|
|
BookingStatus,
|
|
|
|
WorkflowMethods,
|
2022-08-31 19:42:37 +00:00
|
|
|
TimeUnit,
|
2023-02-27 07:24:43 +00:00
|
|
|
MembershipRole,
|
2022-07-14 00:10:45 +00:00
|
|
|
} from "@prisma/client";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
2023-03-02 18:15:28 +00:00
|
|
|
import {
|
|
|
|
SMS_REMINDER_NUMBER_FIELD,
|
|
|
|
getSmsReminderNumberField,
|
|
|
|
getSmsReminderNumberSource,
|
|
|
|
} from "@calcom/features/bookings/lib/getBookingFields";
|
2023-02-27 07:24:43 +00:00
|
|
|
import type { WorkflowType } from "@calcom/features/ee/workflows/components/WorkflowListPage";
|
2022-12-17 22:51:00 +00:00
|
|
|
// import dayjs from "@calcom/dayjs";
|
2022-07-14 00:10:45 +00:00
|
|
|
import {
|
|
|
|
WORKFLOW_TEMPLATES,
|
|
|
|
WORKFLOW_TRIGGER_EVENTS,
|
|
|
|
WORKFLOW_ACTIONS,
|
|
|
|
TIME_UNIT,
|
2022-07-28 19:58:26 +00:00
|
|
|
} from "@calcom/features/ee/workflows/lib/constants";
|
2022-12-15 00:11:57 +00:00
|
|
|
import { getWorkflowActionOptions } from "@calcom/features/ee/workflows/lib/getOptions";
|
2023-01-18 14:32:39 +00:00
|
|
|
import { isSMSAction } from "@calcom/features/ee/workflows/lib/isSMSAction";
|
2022-07-14 00:10:45 +00:00
|
|
|
import {
|
|
|
|
deleteScheduledEmailReminder,
|
|
|
|
scheduleEmailReminder,
|
2022-07-28 19:58:26 +00:00
|
|
|
} from "@calcom/features/ee/workflows/lib/reminders/emailReminderManager";
|
2022-07-14 00:10:45 +00:00
|
|
|
import {
|
|
|
|
deleteScheduledSMSReminder,
|
|
|
|
scheduleSMSReminder,
|
2022-07-28 19:58:26 +00:00
|
|
|
} from "@calcom/features/ee/workflows/lib/reminders/smsReminderManager";
|
2022-12-15 21:54:40 +00:00
|
|
|
import {
|
|
|
|
verifyPhoneNumber,
|
|
|
|
sendVerificationCode,
|
|
|
|
} from "@calcom/features/ee/workflows/lib/reminders/verifyPhoneNumber";
|
2023-03-02 18:15:28 +00:00
|
|
|
import { upsertBookingField, removeBookingField } from "@calcom/features/eventtypes/lib/bookingFieldsManager";
|
2023-02-27 07:24:43 +00:00
|
|
|
import { IS_SELF_HOSTED, SENDER_ID, CAL_URL } from "@calcom/lib/constants";
|
2023-01-18 14:32:39 +00:00
|
|
|
import { SENDER_NAME } from "@calcom/lib/constants";
|
2023-02-23 22:01:51 +00:00
|
|
|
import hasKeyInMetadata from "@calcom/lib/hasKeyInMetadata";
|
2022-12-17 22:51:00 +00:00
|
|
|
// import { getErrorFromUnknown } from "@calcom/lib/errors";
|
2022-12-15 00:11:57 +00:00
|
|
|
import { getTranslation } from "@calcom/lib/server/i18n";
|
2023-02-27 07:24:43 +00:00
|
|
|
import type PrismaType from "@calcom/prisma";
|
2023-02-17 23:00:19 +00:00
|
|
|
import type { WorkflowStep } from "@calcom/prisma/client";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
|
2023-02-17 19:53:31 +00:00
|
|
|
import { router, authedProcedure } from "../../trpc";
|
2022-12-27 21:03:39 +00:00
|
|
|
import { viewerTeamsRouter } from "./teams";
|
2022-07-22 17:27:06 +00:00
|
|
|
|
2023-01-18 14:32:39 +00:00
|
|
|
function getSender(
|
|
|
|
step: Pick<WorkflowStep, "action" | "sender"> & { senderName: string | null | undefined }
|
|
|
|
) {
|
|
|
|
return isSMSAction(step.action) ? step.sender || SENDER_ID : step.senderName || SENDER_NAME;
|
2022-12-15 00:11:57 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 07:24:43 +00:00
|
|
|
async function isAuthorized(
|
|
|
|
workflow: Pick<Workflow, "id" | "teamId" | "userId"> | null,
|
|
|
|
prisma: typeof PrismaType,
|
|
|
|
currentUserId: number,
|
|
|
|
readOnly?: boolean
|
|
|
|
) {
|
|
|
|
if (!workflow) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!readOnly) {
|
|
|
|
const userWorkflow = await prisma.workflow.findFirst({
|
2022-11-10 23:40:01 +00:00
|
|
|
where: {
|
2023-02-27 07:24:43 +00:00
|
|
|
id: workflow.id,
|
|
|
|
OR: [
|
|
|
|
{ userId: currentUserId },
|
|
|
|
{
|
|
|
|
team: {
|
|
|
|
members: {
|
|
|
|
some: {
|
|
|
|
userId: currentUserId,
|
|
|
|
accepted: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2022-11-10 23:40:01 +00:00
|
|
|
},
|
2023-02-27 07:24:43 +00:00
|
|
|
});
|
|
|
|
if (userWorkflow) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const userWorkflow = await prisma.workflow.findFirst({
|
|
|
|
where: {
|
|
|
|
id: workflow.id,
|
|
|
|
OR: [
|
|
|
|
{ userId: currentUserId },
|
|
|
|
{
|
|
|
|
team: {
|
|
|
|
members: {
|
|
|
|
some: {
|
|
|
|
userId: currentUserId,
|
|
|
|
accepted: true,
|
|
|
|
NOT: {
|
|
|
|
role: MembershipRole.MEMBER,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (userWorkflow) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const workflowsRouter = router({
|
|
|
|
list: authedProcedure
|
|
|
|
.input(
|
|
|
|
z
|
|
|
|
.object({
|
|
|
|
teamId: z.number().optional(),
|
|
|
|
userId: z.number().optional(),
|
|
|
|
})
|
|
|
|
.optional()
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
if (input && input.teamId) {
|
|
|
|
const workflows: WorkflowType[] = await ctx.prisma.workflow.findMany({
|
|
|
|
where: {
|
|
|
|
team: {
|
|
|
|
id: input.teamId,
|
|
|
|
members: {
|
|
|
|
some: {
|
|
|
|
userId: ctx.user.id,
|
|
|
|
accepted: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
team: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
slug: true,
|
|
|
|
name: true,
|
|
|
|
members: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
activeOn: {
|
|
|
|
select: {
|
|
|
|
eventType: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
title: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
steps: true,
|
|
|
|
},
|
|
|
|
orderBy: {
|
|
|
|
id: "asc",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const workflowsWithReadOnly = workflows.map((workflow) => {
|
|
|
|
const readOnly = !!workflow.team?.members?.find(
|
|
|
|
(member) => member.userId === ctx.user.id && member.role === MembershipRole.MEMBER
|
|
|
|
);
|
|
|
|
return { ...workflow, readOnly };
|
|
|
|
});
|
|
|
|
|
|
|
|
return { workflows: workflowsWithReadOnly };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input && input.userId) {
|
|
|
|
const workflows: WorkflowType[] = await ctx.prisma.workflow.findMany({
|
|
|
|
where: {
|
|
|
|
userId: ctx.user.id,
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
activeOn: {
|
|
|
|
select: {
|
|
|
|
eventType: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
title: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
steps: true,
|
|
|
|
team: {
|
2022-11-10 23:40:01 +00:00
|
|
|
select: {
|
|
|
|
id: true,
|
2023-02-27 07:24:43 +00:00
|
|
|
slug: true,
|
|
|
|
name: true,
|
|
|
|
members: true,
|
2022-08-31 19:42:37 +00:00
|
|
|
},
|
2022-07-14 00:10:45 +00:00
|
|
|
},
|
|
|
|
},
|
2023-02-27 07:24:43 +00:00
|
|
|
orderBy: {
|
|
|
|
id: "asc",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return { workflows };
|
|
|
|
}
|
|
|
|
|
|
|
|
const workflows = await ctx.prisma.workflow.findMany({
|
|
|
|
where: {
|
|
|
|
OR: [
|
|
|
|
{ userId: ctx.user.id },
|
|
|
|
{
|
|
|
|
team: {
|
|
|
|
members: {
|
|
|
|
some: {
|
|
|
|
userId: ctx.user.id,
|
|
|
|
accepted: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2022-07-14 00:10:45 +00:00
|
|
|
},
|
2023-02-27 07:24:43 +00:00
|
|
|
include: {
|
|
|
|
activeOn: {
|
|
|
|
select: {
|
|
|
|
eventType: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
title: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
steps: true,
|
|
|
|
team: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
slug: true,
|
|
|
|
name: true,
|
|
|
|
members: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
orderBy: {
|
|
|
|
id: "asc",
|
|
|
|
},
|
|
|
|
});
|
2022-11-10 23:40:01 +00:00
|
|
|
|
2023-02-27 07:24:43 +00:00
|
|
|
const workflowsWithReadOnly: WorkflowType[] = workflows.map((workflow) => {
|
|
|
|
const readOnly = !!workflow.team?.members?.find(
|
|
|
|
(member) => member.userId === ctx.user.id && member.role === MembershipRole.MEMBER
|
|
|
|
);
|
|
|
|
|
|
|
|
return { readOnly, ...workflow };
|
|
|
|
});
|
|
|
|
|
|
|
|
return { workflows: workflowsWithReadOnly };
|
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
get: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
id: z.number(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
2022-07-14 00:10:45 +00:00
|
|
|
const workflow = await ctx.prisma.workflow.findFirst({
|
|
|
|
where: {
|
2022-08-09 23:27:49 +00:00
|
|
|
id: input.id,
|
2022-07-14 00:10:45 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
name: true,
|
2023-02-27 07:24:43 +00:00
|
|
|
userId: true,
|
|
|
|
teamId: true,
|
|
|
|
team: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
slug: true,
|
|
|
|
members: true,
|
|
|
|
},
|
|
|
|
},
|
2022-07-14 00:10:45 +00:00
|
|
|
time: true,
|
|
|
|
timeUnit: true,
|
|
|
|
activeOn: {
|
|
|
|
select: {
|
|
|
|
eventType: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
trigger: true,
|
|
|
|
steps: {
|
|
|
|
orderBy: {
|
|
|
|
stepNumber: "asc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2023-02-27 07:24:43 +00:00
|
|
|
|
|
|
|
const isUserAuthorized = await isAuthorized(workflow, ctx.prisma, ctx.user.id);
|
|
|
|
|
|
|
|
if (!isUserAuthorized) {
|
2022-07-21 00:33:35 +00:00
|
|
|
throw new TRPCError({
|
|
|
|
code: "UNAUTHORIZED",
|
|
|
|
});
|
|
|
|
}
|
2023-02-27 07:24:43 +00:00
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
return workflow;
|
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
create: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
2023-02-27 07:24:43 +00:00
|
|
|
teamId: z.number().optional(),
|
2022-11-10 23:40:01 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2023-02-27 07:24:43 +00:00
|
|
|
const { teamId } = input;
|
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
const userId = ctx.user.id;
|
|
|
|
|
2023-02-27 07:24:43 +00:00
|
|
|
if (teamId) {
|
|
|
|
const team = await ctx.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",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
try {
|
2023-02-27 07:24:43 +00:00
|
|
|
const workflow: Workflow = await ctx.prisma.workflow.create({
|
2022-07-14 00:10:45 +00:00
|
|
|
data: {
|
2023-02-27 07:24:43 +00:00
|
|
|
name: "",
|
|
|
|
trigger: WorkflowTriggerEvents.BEFORE_EVENT,
|
|
|
|
time: 24,
|
|
|
|
timeUnit: TimeUnit.HOUR,
|
2022-07-14 00:10:45 +00:00
|
|
|
userId,
|
2023-02-27 07:24:43 +00:00
|
|
|
teamId,
|
2022-07-14 00:10:45 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await ctx.prisma.workflowStep.create({
|
|
|
|
data: {
|
|
|
|
stepNumber: 1,
|
2023-02-27 07:24:43 +00:00
|
|
|
action: WorkflowActions.EMAIL_HOST,
|
|
|
|
template: WorkflowTemplates.REMINDER,
|
2022-07-14 00:10:45 +00:00
|
|
|
workflowId: workflow.id,
|
2023-02-27 07:24:43 +00:00
|
|
|
sender: SENDER_NAME,
|
|
|
|
numberVerificationPending: false,
|
2022-07-14 00:10:45 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
return { workflow };
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
2022-11-10 23:40:01 +00:00
|
|
|
}),
|
|
|
|
delete: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
id: z.number(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2022-07-14 00:10:45 +00:00
|
|
|
const { id } = input;
|
|
|
|
|
2022-08-09 23:27:49 +00:00
|
|
|
const workflowToDelete = await ctx.prisma.workflow.findFirst({
|
2022-07-14 00:10:45 +00:00
|
|
|
where: {
|
2022-08-09 23:27:49 +00:00
|
|
|
id,
|
2022-07-14 00:10:45 +00:00
|
|
|
},
|
2023-03-09 19:39:42 +00:00
|
|
|
include: {
|
|
|
|
activeOn: true,
|
|
|
|
},
|
2022-07-14 00:10:45 +00:00
|
|
|
});
|
|
|
|
|
2023-02-27 07:24:43 +00:00
|
|
|
const isUserAuthorized = await isAuthorized(workflowToDelete, ctx.prisma, ctx.user.id, true);
|
2022-08-09 23:27:49 +00:00
|
|
|
|
2023-02-27 07:24:43 +00:00
|
|
|
if (!isUserAuthorized || !workflowToDelete) {
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
}
|
2022-08-09 23:27:49 +00:00
|
|
|
|
2023-02-27 07:24:43 +00:00
|
|
|
const scheduledReminders = await ctx.prisma.workflowReminder.findMany({
|
|
|
|
where: {
|
|
|
|
workflowStep: {
|
|
|
|
workflowId: id,
|
2022-08-09 23:27:49 +00:00
|
|
|
},
|
2023-02-27 07:24:43 +00:00
|
|
|
scheduled: true,
|
|
|
|
NOT: {
|
|
|
|
referenceId: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
//cancel workflow reminders of deleted workflow
|
|
|
|
scheduledReminders.forEach((reminder) => {
|
|
|
|
if (reminder.method === WorkflowMethods.EMAIL) {
|
2023-03-10 14:28:42 +00:00
|
|
|
deleteScheduledEmailReminder(reminder.id, reminder.referenceId);
|
2023-02-27 07:24:43 +00:00
|
|
|
} else if (reminder.method === WorkflowMethods.SMS) {
|
|
|
|
deleteScheduledSMSReminder(reminder.id, reminder.referenceId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-03-09 19:39:42 +00:00
|
|
|
for (const activeOn of workflowToDelete.activeOn) {
|
|
|
|
await removeSmsReminderFieldForBooking({ workflowId: id, eventTypeId: activeOn.eventTypeId });
|
|
|
|
}
|
|
|
|
|
2023-02-27 07:24:43 +00:00
|
|
|
await ctx.prisma.workflow.deleteMany({
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
});
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
};
|
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
update: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
id: z.number(),
|
|
|
|
name: z.string(),
|
|
|
|
activeOn: z.number().array(),
|
|
|
|
steps: z
|
|
|
|
.object({
|
|
|
|
id: z.number(),
|
|
|
|
stepNumber: z.number(),
|
|
|
|
action: z.enum(WORKFLOW_ACTIONS),
|
|
|
|
workflowId: z.number(),
|
|
|
|
sendTo: z.string().optional().nullable(),
|
|
|
|
reminderBody: z.string().optional().nullable(),
|
|
|
|
emailSubject: z.string().optional().nullable(),
|
|
|
|
template: z.enum(WORKFLOW_TEMPLATES),
|
|
|
|
numberRequired: z.boolean().nullable(),
|
2022-11-11 15:01:17 +00:00
|
|
|
sender: z.string().optional().nullable(),
|
2023-01-18 14:32:39 +00:00
|
|
|
senderName: z.string().optional().nullable(),
|
2022-11-10 23:40:01 +00:00
|
|
|
})
|
|
|
|
.array(),
|
|
|
|
trigger: z.enum(WORKFLOW_TRIGGER_EVENTS),
|
|
|
|
time: z.number().nullable(),
|
|
|
|
timeUnit: z.enum(TIME_UNIT).nullable(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2022-07-14 00:10:45 +00:00
|
|
|
const { user } = ctx;
|
|
|
|
const { id, name, activeOn, steps, trigger, time, timeUnit } = input;
|
|
|
|
|
|
|
|
const userWorkflow = await ctx.prisma.workflow.findUnique({
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
select: {
|
2023-02-27 07:24:43 +00:00
|
|
|
id: true,
|
2022-07-14 00:10:45 +00:00
|
|
|
userId: true,
|
2023-02-27 07:24:43 +00:00
|
|
|
teamId: true,
|
2022-12-15 00:11:57 +00:00
|
|
|
user: {
|
|
|
|
select: {
|
|
|
|
teams: true,
|
|
|
|
},
|
|
|
|
},
|
2022-07-14 00:10:45 +00:00
|
|
|
steps: true,
|
2023-03-09 19:39:42 +00:00
|
|
|
activeOn: true,
|
2022-07-14 00:10:45 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-02-27 07:24:43 +00:00
|
|
|
const isUserAuthorized = await isAuthorized(userWorkflow, ctx.prisma, ctx.user.id, true);
|
|
|
|
|
|
|
|
if (!isUserAuthorized || !userWorkflow) {
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (steps.find((step) => step.workflowId != id)) {
|
2022-10-14 09:52:20 +00:00
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
2023-02-27 07:24:43 +00:00
|
|
|
}
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
const oldActiveOnEventTypes = await ctx.prisma.workflowsOnEventTypes.findMany({
|
|
|
|
where: {
|
|
|
|
workflowId: id,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
eventTypeId: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-08-09 23:27:49 +00:00
|
|
|
const newActiveEventTypes = activeOn.filter((eventType) => {
|
|
|
|
if (
|
|
|
|
!oldActiveOnEventTypes ||
|
|
|
|
!oldActiveOnEventTypes
|
|
|
|
.map((oldEventType) => {
|
|
|
|
return oldEventType.eventTypeId;
|
|
|
|
})
|
|
|
|
.includes(eventType)
|
|
|
|
) {
|
|
|
|
return eventType;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-27 07:24:43 +00:00
|
|
|
//check if new event types belong to user or team
|
2022-08-09 23:27:49 +00:00
|
|
|
for (const newEventTypeId of newActiveEventTypes) {
|
|
|
|
const newEventType = await ctx.prisma.eventType.findFirst({
|
|
|
|
where: {
|
|
|
|
id: newEventTypeId,
|
|
|
|
},
|
|
|
|
include: {
|
2022-10-24 07:38:05 +00:00
|
|
|
users: true,
|
2022-08-09 23:27:49 +00:00
|
|
|
team: {
|
|
|
|
include: {
|
|
|
|
members: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2023-02-27 07:24:43 +00:00
|
|
|
|
|
|
|
if (newEventType) {
|
|
|
|
if (userWorkflow.teamId && userWorkflow.teamId !== newEventType.teamId) {
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!userWorkflow.teamId &&
|
|
|
|
userWorkflow.userId &&
|
|
|
|
newEventType.userId !== userWorkflow.userId &&
|
|
|
|
!newEventType?.users.find((eventTypeUser) => eventTypeUser.id === userWorkflow.userId)
|
|
|
|
) {
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
}
|
2022-08-09 23:27:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//remove all scheduled Email and SMS reminders for eventTypes that are not active any more
|
2022-07-14 00:10:45 +00:00
|
|
|
const removedEventTypes = oldActiveOnEventTypes
|
|
|
|
.map((eventType) => {
|
|
|
|
return eventType.eventTypeId;
|
|
|
|
})
|
|
|
|
.filter((eventType) => {
|
|
|
|
if (!activeOn.includes(eventType)) {
|
|
|
|
return eventType;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-28 21:15:50 +00:00
|
|
|
const remindersToDeletePromise: Prisma.PrismaPromise<
|
2022-07-14 00:10:45 +00:00
|
|
|
{
|
|
|
|
id: number;
|
|
|
|
referenceId: string | null;
|
|
|
|
method: string;
|
|
|
|
scheduled: boolean;
|
|
|
|
}[]
|
|
|
|
>[] = [];
|
2023-03-02 18:15:28 +00:00
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
removedEventTypes.forEach((eventTypeId) => {
|
|
|
|
const reminderToDelete = ctx.prisma.workflowReminder.findMany({
|
|
|
|
where: {
|
|
|
|
booking: {
|
|
|
|
eventTypeId: eventTypeId,
|
|
|
|
userId: ctx.user.id,
|
|
|
|
},
|
|
|
|
workflowStepId: {
|
|
|
|
in: userWorkflow.steps.map((step) => {
|
|
|
|
return step.id;
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
referenceId: true,
|
|
|
|
method: true,
|
|
|
|
scheduled: true,
|
|
|
|
},
|
|
|
|
});
|
2023-03-02 18:15:28 +00:00
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
remindersToDeletePromise.push(reminderToDelete);
|
|
|
|
});
|
|
|
|
|
|
|
|
const remindersToDelete = await Promise.all(remindersToDeletePromise);
|
|
|
|
|
2023-02-20 17:40:08 +00:00
|
|
|
//cancel workflow reminders for all bookings from event types that got disabled
|
2022-07-14 00:10:45 +00:00
|
|
|
remindersToDelete.flat().forEach((reminder) => {
|
2023-02-20 17:40:08 +00:00
|
|
|
if (reminder.method === WorkflowMethods.EMAIL) {
|
|
|
|
deleteScheduledEmailReminder(reminder.id, reminder.referenceId);
|
|
|
|
} else if (reminder.method === WorkflowMethods.SMS) {
|
|
|
|
deleteScheduledSMSReminder(reminder.id, reminder.referenceId);
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//update active on & reminders for new eventTypes
|
|
|
|
await ctx.prisma.workflowsOnEventTypes.deleteMany({
|
|
|
|
where: {
|
|
|
|
workflowId: id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
let newEventTypes: number[] = [];
|
|
|
|
if (activeOn.length) {
|
2022-10-07 18:18:28 +00:00
|
|
|
if (trigger === WorkflowTriggerEvents.BEFORE_EVENT || trigger === WorkflowTriggerEvents.AFTER_EVENT) {
|
2022-08-09 23:27:49 +00:00
|
|
|
newEventTypes = newActiveEventTypes;
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
if (newEventTypes.length > 0) {
|
|
|
|
//create reminders for all bookings with newEventTypes
|
|
|
|
const bookingsForReminders = await ctx.prisma.booking.findMany({
|
|
|
|
where: {
|
|
|
|
eventTypeId: { in: newEventTypes },
|
|
|
|
status: BookingStatus.ACCEPTED,
|
|
|
|
startTime: {
|
|
|
|
gte: new Date(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
attendees: true,
|
|
|
|
eventType: true,
|
|
|
|
user: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
steps.forEach(async (step) => {
|
|
|
|
if (step.action !== WorkflowActions.SMS_ATTENDEE) {
|
|
|
|
//as we do not have attendees phone number (user is notified about that when setting this action)
|
|
|
|
bookingsForReminders.forEach(async (booking) => {
|
|
|
|
const bookingInfo = {
|
|
|
|
uid: booking.uid,
|
|
|
|
attendees: booking.attendees.map((attendee) => {
|
2023-02-08 17:06:25 +00:00
|
|
|
return {
|
|
|
|
name: attendee.name,
|
|
|
|
email: attendee.email,
|
|
|
|
timeZone: attendee.timeZone,
|
|
|
|
language: { locale: attendee.locale || "" },
|
|
|
|
};
|
2022-07-14 00:10:45 +00:00
|
|
|
}),
|
|
|
|
organizer: booking.user
|
2022-07-14 23:05:01 +00:00
|
|
|
? {
|
2022-08-03 22:22:38 +00:00
|
|
|
language: { locale: booking.user.locale || "" },
|
2022-07-14 23:05:01 +00:00
|
|
|
name: booking.user.name || "",
|
|
|
|
email: booking.user.email,
|
|
|
|
timeZone: booking.user.timeZone,
|
|
|
|
}
|
2022-08-03 22:22:38 +00:00
|
|
|
: { name: "", email: "", timeZone: "", language: { locale: "" } },
|
2022-07-14 00:10:45 +00:00
|
|
|
startTime: booking.startTime.toISOString(),
|
2022-08-04 08:52:05 +00:00
|
|
|
endTime: booking.endTime.toISOString(),
|
2022-07-14 00:10:45 +00:00
|
|
|
title: booking.title,
|
2022-08-03 22:22:38 +00:00
|
|
|
language: { locale: booking?.user?.locale || "" },
|
2022-07-14 00:10:45 +00:00
|
|
|
};
|
|
|
|
if (
|
|
|
|
step.action === WorkflowActions.EMAIL_HOST ||
|
2022-12-17 11:58:28 +00:00
|
|
|
step.action === WorkflowActions.EMAIL_ATTENDEE /*||
|
|
|
|
step.action === WorkflowActions.EMAIL_ADDRESS*/
|
2022-07-14 00:10:45 +00:00
|
|
|
) {
|
2022-10-10 13:40:20 +00:00
|
|
|
let sendTo = "";
|
|
|
|
|
|
|
|
switch (step.action) {
|
|
|
|
case WorkflowActions.EMAIL_HOST:
|
|
|
|
sendTo = bookingInfo.organizer?.email;
|
|
|
|
break;
|
|
|
|
case WorkflowActions.EMAIL_ATTENDEE:
|
|
|
|
sendTo = bookingInfo.attendees[0].email;
|
|
|
|
break;
|
2022-12-17 11:58:28 +00:00
|
|
|
/*case WorkflowActions.EMAIL_ADDRESS:
|
|
|
|
sendTo = step.sendTo || "";*/
|
2022-10-10 13:40:20 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
await scheduleEmailReminder(
|
|
|
|
bookingInfo,
|
2022-10-07 18:18:28 +00:00
|
|
|
trigger,
|
2022-07-14 00:10:45 +00:00
|
|
|
step.action,
|
|
|
|
{
|
|
|
|
time,
|
|
|
|
timeUnit,
|
|
|
|
},
|
|
|
|
sendTo,
|
|
|
|
step.emailSubject || "",
|
|
|
|
step.reminderBody || "",
|
|
|
|
step.id,
|
2023-01-18 14:32:39 +00:00
|
|
|
step.template,
|
|
|
|
step.senderName || SENDER_NAME
|
2022-07-14 00:10:45 +00:00
|
|
|
);
|
|
|
|
} else if (step.action === WorkflowActions.SMS_NUMBER) {
|
|
|
|
await scheduleSMSReminder(
|
|
|
|
bookingInfo,
|
|
|
|
step.sendTo || "",
|
2022-10-07 18:18:28 +00:00
|
|
|
trigger,
|
2022-07-14 00:10:45 +00:00
|
|
|
step.action,
|
|
|
|
{
|
|
|
|
time,
|
|
|
|
timeUnit,
|
|
|
|
},
|
|
|
|
step.reminderBody || "",
|
|
|
|
step.id,
|
2022-11-11 15:01:17 +00:00
|
|
|
step.template,
|
2022-12-15 21:54:40 +00:00
|
|
|
step.sender || SENDER_ID,
|
2023-02-27 07:24:43 +00:00
|
|
|
user.id,
|
|
|
|
userWorkflow.teamId
|
2022-07-14 00:10:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
//create all workflow - eventtypes relationships
|
|
|
|
activeOn.forEach(async (eventTypeId) => {
|
|
|
|
await ctx.prisma.workflowsOnEventTypes.createMany({
|
|
|
|
data: {
|
|
|
|
workflowId: id,
|
|
|
|
eventTypeId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
userWorkflow.steps.map(async (oldStep) => {
|
|
|
|
const newStep = steps.filter((s) => s.id === oldStep.id)[0];
|
|
|
|
const remindersFromStep = await ctx.prisma.workflowReminder.findMany({
|
|
|
|
where: {
|
|
|
|
workflowStepId: oldStep.id,
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
booking: true,
|
|
|
|
},
|
|
|
|
});
|
2023-03-09 19:39:42 +00:00
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
//step was deleted
|
|
|
|
if (!newStep) {
|
2023-02-20 17:40:08 +00:00
|
|
|
// cancel all workflow reminders from deleted steps
|
2022-07-14 00:10:45 +00:00
|
|
|
if (remindersFromStep.length > 0) {
|
|
|
|
remindersFromStep.forEach((reminder) => {
|
2023-02-20 17:40:08 +00:00
|
|
|
if (reminder.method === WorkflowMethods.EMAIL) {
|
2023-03-10 14:28:42 +00:00
|
|
|
deleteScheduledEmailReminder(reminder.id, reminder.referenceId);
|
2023-02-20 17:40:08 +00:00
|
|
|
} else if (reminder.method === WorkflowMethods.SMS) {
|
|
|
|
deleteScheduledSMSReminder(reminder.id, reminder.referenceId);
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
await ctx.prisma.workflowStep.delete({
|
|
|
|
where: {
|
|
|
|
id: oldStep.id,
|
|
|
|
},
|
|
|
|
});
|
2023-03-09 19:39:42 +00:00
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
//step was edited
|
|
|
|
} else if (JSON.stringify(oldStep) !== JSON.stringify(newStep)) {
|
2022-12-15 00:11:57 +00:00
|
|
|
if (
|
2023-02-27 07:24:43 +00:00
|
|
|
!userWorkflow.teamId &&
|
|
|
|
!userWorkflow.user?.teams.length &&
|
2022-12-15 00:11:57 +00:00
|
|
|
!isSMSAction(oldStep.action) &&
|
|
|
|
isSMSAction(newStep.action)
|
|
|
|
) {
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
}
|
2022-07-14 00:10:45 +00:00
|
|
|
await ctx.prisma.workflowStep.update({
|
|
|
|
where: {
|
|
|
|
id: oldStep.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
action: newStep.action,
|
2022-10-10 13:40:20 +00:00
|
|
|
sendTo:
|
2022-12-17 11:58:28 +00:00
|
|
|
newStep.action === WorkflowActions.SMS_NUMBER /*||
|
|
|
|
newStep.action === WorkflowActions.EMAIL_ADDRESS*/
|
2022-10-10 13:40:20 +00:00
|
|
|
? newStep.sendTo
|
|
|
|
: null,
|
2022-07-14 00:10:45 +00:00
|
|
|
stepNumber: newStep.stepNumber,
|
|
|
|
workflowId: newStep.workflowId,
|
|
|
|
reminderBody: newStep.template === WorkflowTemplates.CUSTOM ? newStep.reminderBody : null,
|
|
|
|
emailSubject: newStep.template === WorkflowTemplates.CUSTOM ? newStep.emailSubject : null,
|
|
|
|
template: newStep.template,
|
2022-10-18 12:47:15 +00:00
|
|
|
numberRequired: newStep.numberRequired,
|
2023-01-18 14:32:39 +00:00
|
|
|
sender: getSender({
|
|
|
|
action: newStep.action,
|
|
|
|
sender: newStep.sender || null,
|
|
|
|
senderName: newStep.senderName,
|
|
|
|
}),
|
2022-12-15 21:54:40 +00:00
|
|
|
numberVerificationPending: false,
|
2022-07-14 00:10:45 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
//cancel all reminders of step and create new ones (not for newEventTypes)
|
|
|
|
const remindersToUpdate = remindersFromStep.filter((reminder) => {
|
|
|
|
if (reminder.booking?.eventTypeId && !newEventTypes.includes(reminder.booking?.eventTypeId)) {
|
|
|
|
return reminder;
|
|
|
|
}
|
|
|
|
});
|
2023-02-20 17:40:08 +00:00
|
|
|
|
|
|
|
//cancel all workflow reminders from steps that were edited
|
2022-07-14 00:10:45 +00:00
|
|
|
remindersToUpdate.forEach(async (reminder) => {
|
2023-02-20 17:40:08 +00:00
|
|
|
if (reminder.method === WorkflowMethods.EMAIL) {
|
|
|
|
deleteScheduledEmailReminder(reminder.id, reminder.referenceId);
|
|
|
|
} else if (reminder.method === WorkflowMethods.SMS) {
|
|
|
|
deleteScheduledSMSReminder(reminder.id, reminder.referenceId);
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
const eventTypesToUpdateReminders = activeOn.filter((eventTypeId) => {
|
|
|
|
if (!newEventTypes.includes(eventTypeId)) {
|
|
|
|
return eventTypeId;
|
|
|
|
}
|
|
|
|
});
|
2022-10-07 18:18:28 +00:00
|
|
|
if (
|
|
|
|
eventTypesToUpdateReminders &&
|
|
|
|
(trigger === WorkflowTriggerEvents.BEFORE_EVENT || trigger === WorkflowTriggerEvents.AFTER_EVENT)
|
|
|
|
) {
|
2022-07-14 00:10:45 +00:00
|
|
|
const bookingsOfEventTypes = await ctx.prisma.booking.findMany({
|
|
|
|
where: {
|
|
|
|
eventTypeId: {
|
|
|
|
in: eventTypesToUpdateReminders,
|
|
|
|
},
|
|
|
|
status: BookingStatus.ACCEPTED,
|
|
|
|
startTime: {
|
|
|
|
gte: new Date(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
attendees: true,
|
|
|
|
eventType: true,
|
|
|
|
user: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
bookingsOfEventTypes.forEach(async (booking) => {
|
|
|
|
const bookingInfo = {
|
|
|
|
uid: booking.uid,
|
|
|
|
attendees: booking.attendees.map((attendee) => {
|
2023-02-08 17:06:25 +00:00
|
|
|
return {
|
|
|
|
name: attendee.name,
|
|
|
|
email: attendee.email,
|
|
|
|
timeZone: attendee.timeZone,
|
|
|
|
language: { locale: attendee.locale || "" },
|
|
|
|
};
|
2022-07-14 00:10:45 +00:00
|
|
|
}),
|
|
|
|
organizer: booking.user
|
2022-07-14 23:05:01 +00:00
|
|
|
? {
|
2022-08-03 22:22:38 +00:00
|
|
|
language: { locale: booking.user.locale || "" },
|
2022-07-14 23:05:01 +00:00
|
|
|
name: booking.user.name || "",
|
|
|
|
email: booking.user.email,
|
|
|
|
timeZone: booking.user.timeZone,
|
|
|
|
}
|
2022-08-03 22:22:38 +00:00
|
|
|
: { name: "", email: "", timeZone: "", language: { locale: "" } },
|
2022-07-14 00:10:45 +00:00
|
|
|
startTime: booking.startTime.toISOString(),
|
2022-08-04 08:52:05 +00:00
|
|
|
endTime: booking.endTime.toISOString(),
|
2022-07-14 00:10:45 +00:00
|
|
|
title: booking.title,
|
2022-08-03 22:22:38 +00:00
|
|
|
language: { locale: booking?.user?.locale || "" },
|
2022-07-14 00:10:45 +00:00
|
|
|
};
|
|
|
|
if (
|
|
|
|
newStep.action === WorkflowActions.EMAIL_HOST ||
|
2022-12-17 11:58:28 +00:00
|
|
|
newStep.action === WorkflowActions.EMAIL_ATTENDEE /*||
|
|
|
|
newStep.action === WorkflowActions.EMAIL_ADDRESS*/
|
2022-07-14 00:10:45 +00:00
|
|
|
) {
|
2022-10-10 13:40:20 +00:00
|
|
|
let sendTo = "";
|
|
|
|
|
|
|
|
switch (newStep.action) {
|
|
|
|
case WorkflowActions.EMAIL_HOST:
|
|
|
|
sendTo = bookingInfo.organizer?.email;
|
|
|
|
break;
|
|
|
|
case WorkflowActions.EMAIL_ATTENDEE:
|
|
|
|
sendTo = bookingInfo.attendees[0].email;
|
|
|
|
break;
|
2022-12-17 11:58:28 +00:00
|
|
|
/*case WorkflowActions.EMAIL_ADDRESS:
|
|
|
|
sendTo = newStep.sendTo || "";*/
|
2022-10-10 13:40:20 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
await scheduleEmailReminder(
|
|
|
|
bookingInfo,
|
2022-10-07 18:18:28 +00:00
|
|
|
trigger,
|
2022-07-14 00:10:45 +00:00
|
|
|
newStep.action,
|
|
|
|
{
|
|
|
|
time,
|
|
|
|
timeUnit,
|
|
|
|
},
|
|
|
|
sendTo,
|
|
|
|
newStep.emailSubject || "",
|
|
|
|
newStep.reminderBody || "",
|
|
|
|
newStep.id,
|
2023-01-18 14:32:39 +00:00
|
|
|
newStep.template,
|
|
|
|
newStep.senderName || SENDER_NAME
|
2022-07-14 00:10:45 +00:00
|
|
|
);
|
|
|
|
} else if (newStep.action === WorkflowActions.SMS_NUMBER) {
|
|
|
|
await scheduleSMSReminder(
|
|
|
|
bookingInfo,
|
|
|
|
newStep.sendTo || "",
|
2022-10-07 18:18:28 +00:00
|
|
|
trigger,
|
2022-07-14 00:10:45 +00:00
|
|
|
newStep.action,
|
|
|
|
{
|
|
|
|
time,
|
|
|
|
timeUnit,
|
|
|
|
},
|
|
|
|
newStep.reminderBody || "",
|
|
|
|
newStep.id || 0,
|
2022-11-11 15:01:17 +00:00
|
|
|
newStep.template,
|
2022-12-15 21:54:40 +00:00
|
|
|
newStep.sender || SENDER_ID,
|
2023-02-27 07:24:43 +00:00
|
|
|
user.id,
|
|
|
|
userWorkflow.teamId
|
2022-07-14 00:10:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//added steps
|
|
|
|
const addedSteps = steps.map((s) => {
|
|
|
|
if (s.id <= 0) {
|
2023-02-27 07:24:43 +00:00
|
|
|
if (!userWorkflow.user?.teams.length && isSMSAction(s.action)) {
|
2022-12-15 00:11:57 +00:00
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
}
|
2023-02-28 21:40:19 +00:00
|
|
|
const { id: _stepId, ...stepToAdd } = s;
|
2022-10-14 09:52:20 +00:00
|
|
|
return stepToAdd;
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (addedSteps) {
|
|
|
|
const eventTypesToCreateReminders = activeOn.map((activeEventType) => {
|
|
|
|
if (activeEventType && !newEventTypes.includes(activeEventType)) {
|
|
|
|
return activeEventType;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
addedSteps.forEach(async (step) => {
|
|
|
|
if (step) {
|
2023-01-25 08:42:57 +00:00
|
|
|
const { senderName, ...newStep } = step;
|
2023-01-18 14:32:39 +00:00
|
|
|
newStep.sender = getSender({
|
|
|
|
action: newStep.action,
|
|
|
|
sender: newStep.sender || null,
|
2023-01-25 08:42:57 +00:00
|
|
|
senderName: senderName,
|
2023-01-18 14:32:39 +00:00
|
|
|
});
|
2022-07-14 00:10:45 +00:00
|
|
|
const createdStep = await ctx.prisma.workflowStep.create({
|
2023-01-25 08:42:57 +00:00
|
|
|
data: { ...newStep, numberVerificationPending: false },
|
2022-07-14 00:10:45 +00:00
|
|
|
});
|
|
|
|
if (
|
2022-10-07 18:18:28 +00:00
|
|
|
(trigger === WorkflowTriggerEvents.BEFORE_EVENT ||
|
|
|
|
trigger === WorkflowTriggerEvents.AFTER_EVENT) &&
|
2022-07-14 00:10:45 +00:00
|
|
|
eventTypesToCreateReminders &&
|
|
|
|
step.action !== WorkflowActions.SMS_ATTENDEE
|
|
|
|
) {
|
|
|
|
const bookingsForReminders = await ctx.prisma.booking.findMany({
|
|
|
|
where: {
|
|
|
|
eventTypeId: { in: eventTypesToCreateReminders as number[] },
|
|
|
|
status: BookingStatus.ACCEPTED,
|
|
|
|
startTime: {
|
|
|
|
gte: new Date(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
attendees: true,
|
|
|
|
eventType: true,
|
|
|
|
user: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
bookingsForReminders.forEach(async (booking) => {
|
|
|
|
const bookingInfo = {
|
|
|
|
uid: booking.uid,
|
|
|
|
attendees: booking.attendees.map((attendee) => {
|
2023-02-08 17:06:25 +00:00
|
|
|
return {
|
|
|
|
name: attendee.name,
|
|
|
|
email: attendee.email,
|
|
|
|
timeZone: attendee.timeZone,
|
|
|
|
language: { locale: attendee.locale || "" },
|
|
|
|
};
|
2022-07-14 00:10:45 +00:00
|
|
|
}),
|
|
|
|
organizer: booking.user
|
2022-07-14 23:05:01 +00:00
|
|
|
? {
|
|
|
|
name: booking.user.name || "",
|
|
|
|
email: booking.user.email,
|
|
|
|
timeZone: booking.user.timeZone,
|
2022-08-03 22:22:38 +00:00
|
|
|
language: { locale: booking.user.locale || "" },
|
2022-07-14 23:05:01 +00:00
|
|
|
}
|
2022-08-03 22:22:38 +00:00
|
|
|
: { name: "", email: "", timeZone: "", language: { locale: "" } },
|
2022-07-14 00:10:45 +00:00
|
|
|
startTime: booking.startTime.toISOString(),
|
2022-08-04 08:52:05 +00:00
|
|
|
endTime: booking.endTime.toISOString(),
|
2022-07-14 00:10:45 +00:00
|
|
|
title: booking.title,
|
2022-08-03 22:22:38 +00:00
|
|
|
language: { locale: booking?.user?.locale || "" },
|
2022-07-14 00:10:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (
|
|
|
|
step.action === WorkflowActions.EMAIL_ATTENDEE ||
|
2022-12-17 11:58:28 +00:00
|
|
|
step.action === WorkflowActions.EMAIL_HOST /*||
|
|
|
|
step.action === WorkflowActions.EMAIL_ADDRESS*/
|
2022-07-14 00:10:45 +00:00
|
|
|
) {
|
2022-10-10 13:40:20 +00:00
|
|
|
let sendTo = "";
|
|
|
|
|
|
|
|
switch (step.action) {
|
|
|
|
case WorkflowActions.EMAIL_HOST:
|
|
|
|
sendTo = bookingInfo.organizer?.email;
|
|
|
|
break;
|
|
|
|
case WorkflowActions.EMAIL_ATTENDEE:
|
|
|
|
sendTo = bookingInfo.attendees[0].email;
|
|
|
|
break;
|
2022-12-17 11:58:28 +00:00
|
|
|
/*case WorkflowActions.EMAIL_ADDRESS:
|
|
|
|
sendTo = step.sendTo || "";*/
|
2022-10-10 13:40:20 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
await scheduleEmailReminder(
|
|
|
|
bookingInfo,
|
|
|
|
trigger,
|
|
|
|
step.action,
|
|
|
|
{
|
|
|
|
time,
|
|
|
|
timeUnit,
|
|
|
|
},
|
|
|
|
sendTo,
|
|
|
|
step.emailSubject || "",
|
|
|
|
step.reminderBody || "",
|
|
|
|
createdStep.id,
|
2023-01-18 14:32:39 +00:00
|
|
|
step.template,
|
|
|
|
step.senderName || SENDER_NAME
|
2022-07-14 00:10:45 +00:00
|
|
|
);
|
|
|
|
} else if (step.action === WorkflowActions.SMS_NUMBER && step.sendTo) {
|
|
|
|
await scheduleSMSReminder(
|
|
|
|
bookingInfo,
|
|
|
|
step.sendTo,
|
2022-10-07 18:18:28 +00:00
|
|
|
trigger,
|
2022-07-14 00:10:45 +00:00
|
|
|
step.action,
|
|
|
|
{
|
|
|
|
time,
|
|
|
|
timeUnit,
|
|
|
|
},
|
|
|
|
step.reminderBody || "",
|
|
|
|
createdStep.id,
|
2022-11-11 15:01:17 +00:00
|
|
|
step.template,
|
2022-12-15 21:54:40 +00:00
|
|
|
step.sender || SENDER_ID,
|
2023-02-27 07:24:43 +00:00
|
|
|
user.id,
|
|
|
|
userWorkflow.teamId
|
2022-07-14 00:10:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//update trigger, name, time, timeUnit
|
|
|
|
await ctx.prisma.workflow.update({
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
name,
|
|
|
|
trigger,
|
|
|
|
time,
|
|
|
|
timeUnit,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const workflow = await ctx.prisma.workflow.findFirst({
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
activeOn: {
|
|
|
|
select: {
|
|
|
|
eventType: true,
|
|
|
|
},
|
|
|
|
},
|
2023-02-27 07:24:43 +00:00
|
|
|
team: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
slug: true,
|
|
|
|
members: true,
|
|
|
|
},
|
|
|
|
},
|
2023-02-13 12:39:06 +00:00
|
|
|
steps: {
|
|
|
|
orderBy: {
|
|
|
|
stepNumber: "asc",
|
|
|
|
},
|
|
|
|
},
|
2022-07-14 00:10:45 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-03-09 19:39:42 +00:00
|
|
|
// Remove or add booking field for sms reminder number
|
|
|
|
const smsReminderNumberNeeded =
|
|
|
|
activeOn.length && steps.some((step) => step.action === WorkflowActions.SMS_ATTENDEE);
|
|
|
|
|
|
|
|
for (const removedEventType of removedEventTypes) {
|
|
|
|
await removeSmsReminderFieldForBooking({
|
|
|
|
workflowId: id,
|
|
|
|
eventTypeId: removedEventType,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const eventTypeId of activeOn) {
|
|
|
|
if (smsReminderNumberNeeded) {
|
|
|
|
await upsertSmsReminderFieldForBooking({
|
|
|
|
workflowId: id,
|
|
|
|
isSmsReminderNumberRequired: steps.some(
|
|
|
|
(s) => s.action === WorkflowActions.SMS_ATTENDEE && s.numberRequired
|
|
|
|
),
|
|
|
|
eventTypeId,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await removeSmsReminderFieldForBooking({ workflowId: id, eventTypeId });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
return {
|
|
|
|
workflow,
|
|
|
|
};
|
2022-08-26 01:04:44 +00:00
|
|
|
}),
|
2023-01-12 16:03:30 +00:00
|
|
|
/* testAction: authedRateLimitedProcedure({ intervalInMs: 10000, limit: 3 })
|
2022-11-10 23:40:01 +00:00
|
|
|
.input(
|
|
|
|
z.object({
|
2022-12-15 20:48:22 +00:00
|
|
|
step: z.object({
|
|
|
|
id: z.number(),
|
|
|
|
stepNumber: z.number(),
|
|
|
|
action: z.enum(WORKFLOW_ACTIONS),
|
|
|
|
workflowId: z.number(),
|
|
|
|
sendTo: z.string().optional().nullable(),
|
|
|
|
reminderBody: z.string().optional().nullable(),
|
|
|
|
emailSubject: z.string().optional().nullable(),
|
|
|
|
template: z.enum(WORKFLOW_TEMPLATES),
|
|
|
|
numberRequired: z.boolean().nullable(),
|
|
|
|
sender: z.string().optional().nullable(),
|
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
emailSubject: z.string(),
|
|
|
|
reminderBody: z.string(),
|
|
|
|
})
|
|
|
|
)
|
2023-01-12 16:03:30 +00:00
|
|
|
.mutation(async ({ ctx, input }) => {
|
|
|
|
const { user } = ctx;
|
|
|
|
const { step, emailSubject, reminderBody } = input;
|
|
|
|
const { action, template, sendTo, sender } = step;
|
2022-12-15 20:48:22 +00:00
|
|
|
|
2023-01-12 16:03:30 +00:00
|
|
|
const senderID = sender || SENDER_ID;
|
2022-12-15 20:48:22 +00:00
|
|
|
|
2023-01-12 16:03:30 +00:00
|
|
|
if (action === WorkflowActions.SMS_NUMBER) {
|
|
|
|
if (!sendTo) throw new TRPCError({ code: "BAD_REQUEST", message: "Missing sendTo" });
|
|
|
|
const verifiedNumbers = await ctx.prisma.verifiedNumber.findFirst({
|
|
|
|
where: {
|
|
|
|
userId: ctx.user.id,
|
|
|
|
phoneNumber: sendTo,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!verifiedNumbers)
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED", message: "Phone number is not verified" });
|
|
|
|
}
|
2022-12-15 21:54:40 +00:00
|
|
|
|
2023-01-12 16:03:30 +00:00
|
|
|
try {
|
|
|
|
const userWorkflow = await ctx.prisma.workflow.findUnique({
|
|
|
|
where: {
|
|
|
|
id: step.workflowId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
userId: true,
|
|
|
|
steps: true,
|
|
|
|
},
|
|
|
|
});
|
2022-12-15 20:48:22 +00:00
|
|
|
|
2023-01-12 16:03:30 +00:00
|
|
|
if (!userWorkflow || userWorkflow.userId !== user.id) {
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
}
|
2022-12-15 20:48:22 +00:00
|
|
|
|
2023-01-12 16:03:30 +00:00
|
|
|
if (isSMSAction(step.action) /*|| step.action === WorkflowActions.EMAIL_ADDRESS*/ /*) {
|
2023-02-08 17:06:25 +00:00
|
|
|
const hasTeamPlan = (await ctx.prisma.membership.count({ where: { userId: user.id } })) > 0;
|
|
|
|
if (!hasTeamPlan) {
|
2023-02-20 17:40:08 +00:00
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED", message: "Team plan needed" });
|
2023-02-08 17:06:25 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-26 01:04:44 +00:00
|
|
|
|
2023-02-08 17:06:25 +00:00
|
|
|
const booking = await ctx.prisma.booking.findFirst({
|
|
|
|
orderBy: {
|
2023-02-20 17:40:08 +00:00
|
|
|
createdAt: "desc",
|
2023-02-08 17:06:25 +00:00
|
|
|
},
|
|
|
|
where: {
|
2023-02-20 17:40:08 +00:00
|
|
|
userId: ctx.user.id,
|
2023-02-08 17:06:25 +00:00
|
|
|
},
|
|
|
|
include: {
|
2023-02-20 17:40:08 +00:00
|
|
|
attendees: true,
|
|
|
|
user: true,
|
2023-02-08 17:06:25 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
let evt: BookingInfo;
|
|
|
|
if (booking) {
|
|
|
|
evt = {
|
2023-02-20 17:40:08 +00:00
|
|
|
uid: booking?.uid,
|
|
|
|
attendees:
|
|
|
|
booking?.attendees.map((attendee) => {
|
|
|
|
return { name: attendee.name, email: attendee.email, timeZone: attendee.timeZone };
|
|
|
|
}) || [],
|
|
|
|
organizer: {
|
|
|
|
language: {
|
|
|
|
locale: booking?.user?.locale || "",
|
|
|
|
},
|
|
|
|
name: booking?.user?.name || "",
|
|
|
|
email: booking?.user?.email || "",
|
|
|
|
timeZone: booking?.user?.timeZone || "",
|
|
|
|
},
|
|
|
|
startTime: booking?.startTime.toISOString() || "",
|
|
|
|
endTime: booking?.endTime.toISOString() || "",
|
|
|
|
title: booking?.title || "",
|
|
|
|
location: booking?.location || null,
|
|
|
|
additionalNotes: booking?.description || null,
|
|
|
|
customInputs: booking?.customInputs,
|
2023-02-08 17:06:25 +00:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
//if no booking exists create an example booking
|
|
|
|
evt = {
|
2023-02-20 17:40:08 +00:00
|
|
|
attendees: [{ name: "John Doe", email: "john.doe@example.com", timeZone: "Europe/London" }],
|
|
|
|
organizer: {
|
|
|
|
language: {
|
2023-03-09 19:39:42 +00:00
|
|
|
locale: ctx.user.locale,
|
2023-02-20 17:40:08 +00:00
|
|
|
},
|
|
|
|
name: ctx.user.name || "",
|
|
|
|
email: ctx.user.email,
|
|
|
|
timeZone: ctx.user.timeZone,
|
|
|
|
},
|
|
|
|
startTime: dayjs().add(10, "hour").toISOString(),
|
|
|
|
endTime: dayjs().add(11, "hour").toISOString(),
|
|
|
|
title: "Example Booking",
|
|
|
|
location: "Office",
|
|
|
|
additionalNotes: "These are additional notes",
|
2023-02-08 17:06:25 +00:00
|
|
|
};
|
|
|
|
}
|
2022-08-26 01:04:44 +00:00
|
|
|
|
2023-02-08 17:06:25 +00:00
|
|
|
if (
|
|
|
|
action === WorkflowActions.EMAIL_ATTENDEE ||
|
|
|
|
action === WorkflowActions.EMAIL_HOST /*||
|
|
|
|
action === WorkflowActions.EMAIL_ADDRESS*/
|
2023-01-12 16:03:30 +00:00
|
|
|
/*) {
|
|
|
|
scheduleEmailReminder(
|
|
|
|
evt,
|
|
|
|
WorkflowTriggerEvents.NEW_EVENT,
|
|
|
|
action,
|
|
|
|
{ time: null, timeUnit: null },
|
|
|
|
ctx.user.email,
|
|
|
|
emailSubject,
|
|
|
|
reminderBody,
|
|
|
|
0,
|
|
|
|
template
|
|
|
|
);
|
|
|
|
return { message: "Notification sent" };
|
|
|
|
} else if (action === WorkflowActions.SMS_NUMBER && sendTo) {
|
|
|
|
scheduleSMSReminder(
|
|
|
|
evt,
|
|
|
|
sendTo,
|
|
|
|
WorkflowTriggerEvents.NEW_EVENT,
|
|
|
|
action,
|
|
|
|
{ time: null, timeUnit: null },
|
|
|
|
reminderBody,
|
|
|
|
0,
|
|
|
|
template,
|
|
|
|
senderID,
|
|
|
|
ctx.user.id
|
|
|
|
);
|
|
|
|
return { message: "Notification sent" };
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
ok: false,
|
|
|
|
status: 500,
|
|
|
|
message: "Notification could not be sent",
|
|
|
|
};
|
|
|
|
} catch (_err) {
|
|
|
|
const error = getErrorFromUnknown(_err);
|
|
|
|
return {
|
|
|
|
ok: false,
|
|
|
|
status: 500,
|
|
|
|
message: error.message,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
*/
|
2022-11-10 23:40:01 +00:00
|
|
|
activateEventType: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
eventTypeId: z.number(),
|
|
|
|
workflowId: z.number(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2022-08-31 19:42:37 +00:00
|
|
|
const { eventTypeId, workflowId } = input;
|
|
|
|
|
2023-02-27 07:24:43 +00:00
|
|
|
// Check that vent type belong to the user or team
|
2022-12-13 17:44:53 +00:00
|
|
|
const userEventType = await ctx.prisma.eventType.findFirst({
|
|
|
|
where: {
|
|
|
|
id: eventTypeId,
|
2023-02-27 07:24:43 +00:00
|
|
|
OR: [
|
|
|
|
{ userId: ctx.user.id },
|
|
|
|
{
|
|
|
|
team: {
|
|
|
|
members: {
|
|
|
|
some: {
|
|
|
|
userId: ctx.user.id,
|
|
|
|
accepted: true,
|
|
|
|
NOT: {
|
|
|
|
role: MembershipRole.MEMBER,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-12-13 17:44:53 +00:00
|
|
|
},
|
2023-02-27 07:24:43 +00:00
|
|
|
],
|
2022-12-13 17:44:53 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!userEventType)
|
2023-02-27 07:24:43 +00:00
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED", message: "Not authorized to edit this event type" });
|
2022-12-13 17:44:53 +00:00
|
|
|
|
2023-02-27 07:24:43 +00:00
|
|
|
// Check that the workflow belongs to the user or team
|
2022-12-13 17:44:53 +00:00
|
|
|
const eventTypeWorkflow = await ctx.prisma.workflow.findFirst({
|
|
|
|
where: {
|
|
|
|
id: workflowId,
|
2023-02-27 07:24:43 +00:00
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
userId: ctx.user.id,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
teamId: userEventType.teamId,
|
|
|
|
},
|
|
|
|
],
|
2022-12-13 17:44:53 +00:00
|
|
|
},
|
2023-03-02 18:15:28 +00:00
|
|
|
include: {
|
|
|
|
steps: true,
|
|
|
|
},
|
2022-12-13 17:44:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!eventTypeWorkflow)
|
2023-02-27 07:24:43 +00:00
|
|
|
throw new TRPCError({
|
|
|
|
code: "UNAUTHORIZED",
|
|
|
|
message: "Not authorized to enable/disable this workflow",
|
|
|
|
});
|
2022-12-13 17:44:53 +00:00
|
|
|
|
2022-08-31 19:42:37 +00:00
|
|
|
//check if event type is already active
|
|
|
|
const isActive = await ctx.prisma.workflowsOnEventTypes.findFirst({
|
|
|
|
where: {
|
|
|
|
workflowId,
|
|
|
|
eventTypeId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isActive) {
|
|
|
|
await ctx.prisma.workflowsOnEventTypes.deleteMany({
|
|
|
|
where: {
|
|
|
|
workflowId,
|
|
|
|
eventTypeId,
|
|
|
|
},
|
|
|
|
});
|
2023-03-09 19:39:42 +00:00
|
|
|
|
|
|
|
await removeSmsReminderFieldForBooking({
|
|
|
|
workflowId,
|
|
|
|
eventTypeId,
|
|
|
|
});
|
2022-08-31 19:42:37 +00:00
|
|
|
} else {
|
|
|
|
await ctx.prisma.workflowsOnEventTypes.create({
|
|
|
|
data: {
|
|
|
|
workflowId,
|
|
|
|
eventTypeId,
|
|
|
|
},
|
|
|
|
});
|
2023-03-02 18:15:28 +00:00
|
|
|
|
2023-03-09 19:39:42 +00:00
|
|
|
if (
|
|
|
|
eventTypeWorkflow.steps.some((step) => {
|
|
|
|
return step.action === WorkflowActions.SMS_ATTENDEE;
|
|
|
|
})
|
|
|
|
) {
|
|
|
|
const isSmsReminderNumberRequired = eventTypeWorkflow.steps.some((step) => {
|
|
|
|
return step.action === WorkflowActions.SMS_ATTENDEE && step.numberRequired;
|
|
|
|
});
|
|
|
|
await upsertSmsReminderFieldForBooking({
|
|
|
|
workflowId,
|
|
|
|
isSmsReminderNumberRequired,
|
|
|
|
eventTypeId,
|
|
|
|
});
|
|
|
|
}
|
2022-08-31 19:42:37 +00:00
|
|
|
}
|
2022-11-10 23:40:01 +00:00
|
|
|
}),
|
2022-12-15 21:54:40 +00:00
|
|
|
sendVerificationCode: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
phoneNumber: z.string(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ input }) => {
|
|
|
|
const { phoneNumber } = input;
|
|
|
|
return sendVerificationCode(phoneNumber);
|
|
|
|
}),
|
|
|
|
verifyPhoneNumber: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
phoneNumber: z.string(),
|
|
|
|
code: z.string(),
|
2023-02-27 07:24:43 +00:00
|
|
|
teamId: z.number().optional(),
|
2022-12-15 21:54:40 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2023-02-27 07:24:43 +00:00
|
|
|
const { phoneNumber, code, teamId } = input;
|
2022-12-15 21:54:40 +00:00
|
|
|
const { user } = ctx;
|
2023-02-27 07:24:43 +00:00
|
|
|
const verifyStatus = await verifyPhoneNumber(phoneNumber, code, user.id, teamId);
|
2022-12-15 21:54:40 +00:00
|
|
|
return verifyStatus;
|
|
|
|
}),
|
2023-02-27 07:24:43 +00:00
|
|
|
getVerifiedNumbers: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
teamId: z.number().optional(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
const { user } = ctx;
|
|
|
|
const verifiedNumbers = await ctx.prisma.verifiedNumber.findMany({
|
|
|
|
where: {
|
|
|
|
OR: [{ userId: user.id }, { teamId: input.teamId }],
|
|
|
|
},
|
|
|
|
});
|
2022-12-15 21:54:40 +00:00
|
|
|
|
2023-02-27 07:24:43 +00:00
|
|
|
return verifiedNumbers;
|
|
|
|
}),
|
2022-12-15 00:11:57 +00:00
|
|
|
getWorkflowActionOptions: authedProcedure.query(async ({ ctx }) => {
|
2023-02-23 22:01:51 +00:00
|
|
|
const { user } = ctx;
|
|
|
|
|
|
|
|
const isCurrentUsernamePremium = user && user.metadata && hasKeyInMetadata(user, "isPremium");
|
|
|
|
|
|
|
|
let isTeamsPlan = false;
|
|
|
|
if (!isCurrentUsernamePremium) {
|
|
|
|
const { hasTeamPlan } = await viewerTeamsRouter.createCaller(ctx).hasTeamPlan();
|
|
|
|
isTeamsPlan = !!hasTeamPlan;
|
|
|
|
}
|
2022-12-15 00:11:57 +00:00
|
|
|
const t = await getTranslation(ctx.user.locale, "common");
|
2023-02-23 22:01:51 +00:00
|
|
|
return getWorkflowActionOptions(t, IS_SELF_HOSTED || isCurrentUsernamePremium || isTeamsPlan);
|
2022-12-15 00:11:57 +00:00
|
|
|
}),
|
2023-02-27 07:24:43 +00:00
|
|
|
getByViewer: authedProcedure.query(async ({ ctx }) => {
|
|
|
|
const { prisma } = ctx;
|
|
|
|
|
|
|
|
const user = await prisma.user.findUnique({
|
|
|
|
where: {
|
|
|
|
id: ctx.user.id,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
username: true,
|
|
|
|
avatar: true,
|
|
|
|
name: true,
|
|
|
|
startTime: true,
|
|
|
|
endTime: true,
|
|
|
|
bufferTime: true,
|
|
|
|
workflows: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
name: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
teams: {
|
|
|
|
where: {
|
|
|
|
accepted: true,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
role: true,
|
|
|
|
team: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
name: true,
|
|
|
|
slug: true,
|
|
|
|
members: {
|
|
|
|
select: {
|
|
|
|
userId: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
workflows: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
name: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
throw new TRPCError({ code: "INTERNAL_SERVER_ERROR" });
|
|
|
|
}
|
|
|
|
|
|
|
|
const userWorkflows = user.workflows;
|
|
|
|
|
|
|
|
type WorkflowGroup = {
|
|
|
|
teamId?: number | null;
|
|
|
|
profile: {
|
|
|
|
slug: (typeof user)["username"];
|
|
|
|
name: (typeof user)["name"];
|
|
|
|
image?: string;
|
|
|
|
};
|
|
|
|
metadata?: {
|
|
|
|
readOnly: boolean;
|
|
|
|
};
|
|
|
|
workflows: typeof userWorkflows;
|
|
|
|
};
|
|
|
|
|
|
|
|
let workflowGroups: WorkflowGroup[] = [];
|
|
|
|
|
|
|
|
workflowGroups.push({
|
|
|
|
teamId: null,
|
|
|
|
profile: {
|
|
|
|
slug: user.username,
|
|
|
|
name: user.name,
|
|
|
|
image: user.avatar || undefined,
|
|
|
|
},
|
|
|
|
workflows: userWorkflows,
|
|
|
|
metadata: {
|
|
|
|
readOnly: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
workflowGroups = ([] as WorkflowGroup[]).concat(
|
|
|
|
workflowGroups,
|
|
|
|
user.teams.map((membership) => ({
|
|
|
|
teamId: membership.team.id,
|
|
|
|
profile: {
|
|
|
|
name: membership.team.name,
|
|
|
|
slug: "team/" + membership.team.slug,
|
|
|
|
image: `${CAL_URL}/team/${membership.team.slug}/avatar.png`,
|
|
|
|
},
|
|
|
|
metadata: {
|
|
|
|
readOnly: membership.role === MembershipRole.MEMBER,
|
|
|
|
},
|
|
|
|
workflows: membership.team.workflows,
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
workflowGroups: workflowGroups.filter((groupBy) => !!groupBy.workflows?.length),
|
|
|
|
profiles: workflowGroups.map((group) => ({
|
|
|
|
teamId: group.teamId,
|
|
|
|
...group.profile,
|
|
|
|
...group.metadata,
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
});
|
2023-03-02 18:15:28 +00:00
|
|
|
|
|
|
|
async function upsertSmsReminderFieldForBooking({
|
|
|
|
workflowId,
|
|
|
|
eventTypeId,
|
|
|
|
isSmsReminderNumberRequired,
|
|
|
|
}: {
|
|
|
|
workflowId: number;
|
|
|
|
isSmsReminderNumberRequired: boolean;
|
|
|
|
eventTypeId: number;
|
|
|
|
}) {
|
|
|
|
await upsertBookingField(
|
|
|
|
getSmsReminderNumberField(),
|
|
|
|
getSmsReminderNumberSource({
|
|
|
|
workflowId,
|
|
|
|
isSmsReminderNumberRequired,
|
|
|
|
}),
|
|
|
|
eventTypeId
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeSmsReminderFieldForBooking({
|
|
|
|
workflowId,
|
|
|
|
eventTypeId,
|
|
|
|
}: {
|
|
|
|
workflowId: number;
|
|
|
|
eventTypeId: number;
|
|
|
|
}) {
|
|
|
|
await removeBookingField(
|
|
|
|
{
|
|
|
|
name: SMS_REMINDER_NUMBER_FIELD,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "" + workflowId,
|
|
|
|
type: "workflow",
|
|
|
|
},
|
|
|
|
eventTypeId
|
|
|
|
);
|
|
|
|
}
|