2022-09-06 22:58:16 +00:00
|
|
|
import { Availability as AvailabilityModel, Prisma, Schedule as ScheduleModel, User } from "@prisma/client";
|
2022-03-17 16:48:23 +00:00
|
|
|
import { z } from "zod";
|
|
|
|
|
2022-08-12 17:16:55 +00:00
|
|
|
import { getUserAvailability } from "@calcom/core/getUserAvailability";
|
2022-12-14 17:30:55 +00:00
|
|
|
import dayjs from "@calcom/dayjs";
|
|
|
|
import { DEFAULT_SCHEDULE, getAvailabilityFromSchedule, getWorkingHours } from "@calcom/lib/availability";
|
|
|
|
import { yyyymmdd } from "@calcom/lib/date-fns";
|
2022-09-06 22:58:16 +00:00
|
|
|
import { PrismaClient } from "@calcom/prisma/client";
|
2022-08-12 17:16:55 +00:00
|
|
|
import { stringOrNumber } from "@calcom/prisma/zod-utils";
|
2022-12-14 17:30:55 +00:00
|
|
|
import { Schedule, TimeRange } from "@calcom/types/schedule";
|
2022-03-17 16:48:23 +00:00
|
|
|
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
|
2022-12-14 17:30:55 +00:00
|
|
|
import { authedProcedure, router } from "../../trpc";
|
2022-07-22 17:27:06 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
export const availabilityRouter = router({
|
|
|
|
list: authedProcedure.query(async ({ ctx }) => {
|
|
|
|
const { prisma, user } = ctx;
|
2022-10-25 00:29:49 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const schedules = await prisma.schedule.findMany({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
name: true,
|
|
|
|
availability: true,
|
|
|
|
timeZone: true,
|
|
|
|
},
|
|
|
|
orderBy: {
|
|
|
|
id: "asc",
|
|
|
|
},
|
|
|
|
});
|
2022-10-25 00:29:49 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const defaultScheduleId = await getDefaultScheduleId(user.id, prisma);
|
2022-10-25 00:29:49 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
return {
|
|
|
|
schedules: schedules.map((schedule) => ({
|
|
|
|
...schedule,
|
|
|
|
isDefault: schedule.id === defaultScheduleId,
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
user: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
username: z.string(),
|
|
|
|
dateFrom: z.string(),
|
|
|
|
dateTo: z.string(),
|
|
|
|
eventTypeId: stringOrNumber.optional(),
|
|
|
|
withSource: z.boolean().optional(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(({ input }) => {
|
|
|
|
return getUserAvailability(input);
|
2022-03-17 16:48:23 +00:00
|
|
|
}),
|
2022-12-14 17:30:55 +00:00
|
|
|
defaultValues: authedProcedure.input(z.object({ scheduleId: z.number() })).query(async ({ ctx, input }) => {
|
|
|
|
const { prisma, user } = ctx;
|
|
|
|
const schedule = await prisma.schedule.findUnique({
|
|
|
|
where: {
|
|
|
|
id: input.scheduleId || (await getDefaultScheduleId(user.id, prisma)),
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
userId: true,
|
|
|
|
name: true,
|
|
|
|
availability: true,
|
|
|
|
timeZone: true,
|
|
|
|
eventType: {
|
|
|
|
select: {
|
|
|
|
_count: true,
|
|
|
|
id: true,
|
|
|
|
eventName: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!schedule || schedule.userId !== user.id) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "UNAUTHORIZED",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const availability = convertScheduleToAvailability(schedule);
|
|
|
|
return {
|
|
|
|
name: schedule.name,
|
|
|
|
rawSchedule: schedule,
|
2022-12-21 16:13:50 +00:00
|
|
|
schedule: availability.map((a) =>
|
|
|
|
a.map((startAndEnd) => ({
|
|
|
|
...startAndEnd,
|
|
|
|
// Turn our limited granularity into proper end of day.
|
|
|
|
end: new Date(startAndEnd.end.toISOString().replace("23:59:00.000Z", "23:59:59.999Z")),
|
|
|
|
}))
|
|
|
|
),
|
2022-12-14 17:30:55 +00:00
|
|
|
dateOverrides: schedule.availability.reduce((acc, override) => {
|
|
|
|
// only iff future date override
|
|
|
|
if (!override.date || override.date < new Date()) {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
const newValue = {
|
|
|
|
start: dayjs
|
|
|
|
.utc(override.date)
|
|
|
|
.hour(override.startTime.getUTCHours())
|
|
|
|
.minute(override.startTime.getUTCMinutes())
|
|
|
|
.toDate(),
|
|
|
|
end: dayjs
|
|
|
|
.utc(override.date)
|
|
|
|
.hour(override.endTime.getUTCHours())
|
|
|
|
.minute(override.endTime.getUTCMinutes())
|
|
|
|
.toDate(),
|
|
|
|
};
|
|
|
|
const dayRangeIndex = acc.findIndex(
|
|
|
|
// early return prevents override.date from ever being empty.
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
(item) => yyyymmdd(item.ranges[0].start) === yyyymmdd(override.date!)
|
|
|
|
);
|
|
|
|
if (dayRangeIndex === -1) {
|
|
|
|
acc.push({ ranges: [newValue] });
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
acc[dayRangeIndex].ranges.push(newValue);
|
|
|
|
return acc;
|
|
|
|
}, [] as { ranges: TimeRange[] }[]),
|
|
|
|
timeZone: schedule.timeZone || user.timeZone,
|
|
|
|
isDefault: !input.scheduleId || user.defaultScheduleId === schedule.id,
|
|
|
|
};
|
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
schedule: router({
|
|
|
|
get: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
scheduleId: z.optional(z.number()),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
const { prisma, user } = ctx;
|
|
|
|
const schedule = await prisma.schedule.findUnique({
|
|
|
|
where: {
|
|
|
|
id: input.scheduleId || (await getDefaultScheduleId(user.id, prisma)),
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
userId: true,
|
|
|
|
name: true,
|
|
|
|
availability: true,
|
|
|
|
timeZone: true,
|
|
|
|
eventType: {
|
|
|
|
select: {
|
|
|
|
_count: true,
|
|
|
|
id: true,
|
|
|
|
eventName: true,
|
|
|
|
},
|
2022-08-24 20:18:42 +00:00
|
|
|
},
|
|
|
|
},
|
2022-03-17 16:48:23 +00:00
|
|
|
});
|
2022-11-10 23:40:01 +00:00
|
|
|
if (!schedule || schedule.userId !== user.id) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "UNAUTHORIZED",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const availability = convertScheduleToAvailability(schedule);
|
|
|
|
return {
|
|
|
|
schedule,
|
2022-12-14 17:30:55 +00:00
|
|
|
workingHours: getWorkingHours(
|
|
|
|
{ timeZone: schedule.timeZone || undefined },
|
|
|
|
schedule.availability || []
|
|
|
|
),
|
2022-11-10 23:40:01 +00:00
|
|
|
availability,
|
|
|
|
timeZone: schedule.timeZone || user.timeZone,
|
|
|
|
isDefault: !input.scheduleId || user.defaultScheduleId === schedule.id,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
create: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
name: z.string(),
|
|
|
|
schedule: z
|
|
|
|
.array(
|
|
|
|
z.array(
|
|
|
|
z.object({
|
|
|
|
start: z.date(),
|
|
|
|
end: z.date(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.optional(),
|
|
|
|
eventTypeId: z.number().optional(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ input, ctx }) => {
|
|
|
|
const { user, prisma } = ctx;
|
2023-01-05 19:55:55 +00:00
|
|
|
if (input.eventTypeId) {
|
|
|
|
const eventType = await prisma.eventType.findUnique({
|
|
|
|
where: {
|
|
|
|
id: input.eventTypeId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
userId: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!eventType || eventType.userId !== user.id) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "UNAUTHORIZED",
|
|
|
|
message: "You are not authorized to create a schedule for this event type",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-11-10 23:40:01 +00:00
|
|
|
const data: Prisma.ScheduleCreateInput = {
|
|
|
|
name: input.name,
|
|
|
|
user: {
|
|
|
|
connect: {
|
|
|
|
id: user.id,
|
|
|
|
},
|
2022-03-17 16:48:23 +00:00
|
|
|
},
|
2022-11-10 23:40:01 +00:00
|
|
|
// If an eventTypeId is provided then connect the new schedule to that event type
|
|
|
|
...(input.eventTypeId && { eventType: { connect: { id: input.eventTypeId } } }),
|
|
|
|
};
|
2022-03-17 16:48:23 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const availability = getAvailabilityFromSchedule(input.schedule || DEFAULT_SCHEDULE);
|
|
|
|
data.availability = {
|
|
|
|
createMany: {
|
|
|
|
data: availability.map((schedule) => ({
|
|
|
|
days: schedule.days,
|
|
|
|
startTime: schedule.startTime,
|
|
|
|
endTime: schedule.endTime,
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
};
|
2022-03-17 16:48:23 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const schedule = await prisma.schedule.create({
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
const hasDefaultScheduleId = await hasDefaultSchedule(user, prisma);
|
|
|
|
if (!hasDefaultScheduleId) {
|
|
|
|
await setupDefaultSchedule(user.id, schedule.id, prisma);
|
|
|
|
}
|
2022-05-23 11:29:29 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
return { schedule };
|
|
|
|
}),
|
|
|
|
delete: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
scheduleId: z.number(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ input, ctx }) => {
|
|
|
|
const { user, prisma } = ctx;
|
2022-05-23 11:29:29 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const scheduleToDelete = await prisma.schedule.findFirst({
|
2022-09-15 07:20:04 +00:00
|
|
|
where: {
|
2022-11-10 23:40:01 +00:00
|
|
|
id: input.scheduleId,
|
2022-09-15 07:20:04 +00:00
|
|
|
},
|
|
|
|
select: {
|
2022-11-10 23:40:01 +00:00
|
|
|
userId: true,
|
2022-09-15 07:20:04 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
if (scheduleToDelete?.userId !== user.id) throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
|
|
|
|
if (user.defaultScheduleId === input.scheduleId) {
|
|
|
|
// set a new default or unset default if no other schedule
|
|
|
|
const scheduleToSetAsDefault = await prisma.schedule.findFirst({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
NOT: {
|
|
|
|
id: input.scheduleId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await prisma.user.update({
|
|
|
|
where: {
|
|
|
|
id: user.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
defaultScheduleId: scheduleToSetAsDefault?.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
await prisma.schedule.delete({
|
2022-03-17 16:48:23 +00:00
|
|
|
where: {
|
2022-11-10 23:40:01 +00:00
|
|
|
id: input.scheduleId,
|
2022-03-17 16:48:23 +00:00
|
|
|
},
|
|
|
|
});
|
2022-11-10 23:40:01 +00:00
|
|
|
}),
|
|
|
|
update: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
scheduleId: z.number(),
|
|
|
|
timeZone: z.string().optional(),
|
|
|
|
name: z.string().optional(),
|
|
|
|
isDefault: z.boolean().optional(),
|
2022-12-14 17:30:55 +00:00
|
|
|
schedule: z
|
|
|
|
.array(
|
|
|
|
z.array(
|
|
|
|
z.object({
|
|
|
|
start: z.date(),
|
|
|
|
end: z.date(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.optional(),
|
|
|
|
dateOverrides: z
|
|
|
|
.array(
|
2022-11-10 23:40:01 +00:00
|
|
|
z.object({
|
|
|
|
start: z.date(),
|
|
|
|
end: z.date(),
|
|
|
|
})
|
|
|
|
)
|
2022-12-14 17:30:55 +00:00
|
|
|
.optional(),
|
2022-11-10 23:40:01 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ input, ctx }) => {
|
|
|
|
const { user, prisma } = ctx;
|
2022-12-14 17:30:55 +00:00
|
|
|
const availability = input.schedule
|
|
|
|
? getAvailabilityFromSchedule(input.schedule)
|
|
|
|
: (input.dateOverrides || []).map((dateOverride) => ({
|
|
|
|
startTime: dateOverride.start,
|
|
|
|
endTime: dateOverride.end,
|
|
|
|
date: dateOverride.start,
|
|
|
|
days: [],
|
|
|
|
}));
|
2022-03-17 16:48:23 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
// Not able to update the schedule with userId where clause, so fetch schedule separately and then validate
|
|
|
|
// Bug: https://github.com/prisma/prisma/issues/7290
|
|
|
|
const userSchedule = await prisma.schedule.findUnique({
|
|
|
|
where: {
|
|
|
|
id: input.scheduleId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
userId: true,
|
2022-12-14 17:30:55 +00:00
|
|
|
name: true,
|
|
|
|
id: true,
|
2022-11-10 23:40:01 +00:00
|
|
|
},
|
|
|
|
});
|
2022-04-12 09:22:29 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
if (userSchedule?.userId !== user.id) throw new TRPCError({ code: "UNAUTHORIZED" });
|
2022-05-23 11:29:29 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
if (!userSchedule || userSchedule.userId !== user.id) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "UNAUTHORIZED",
|
|
|
|
});
|
|
|
|
}
|
2022-04-12 09:22:29 +00:00
|
|
|
|
2022-12-14 17:30:55 +00:00
|
|
|
let updatedUser;
|
|
|
|
if (input.isDefault) {
|
|
|
|
const setupDefault = await setupDefaultSchedule(user.id, input.scheduleId, prisma);
|
|
|
|
updatedUser = setupDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!input.name) {
|
|
|
|
// TODO: Improve
|
|
|
|
// We don't want to pass the full schedule for just a set as default update
|
|
|
|
// but in the current logic, this wipes the existing availability.
|
|
|
|
// Return early to prevent this from happening.
|
|
|
|
return {
|
|
|
|
schedule: userSchedule,
|
|
|
|
isDefault: updatedUser
|
|
|
|
? updatedUser.defaultScheduleId === input.scheduleId
|
|
|
|
: user.defaultScheduleId === input.scheduleId,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const schedule = await prisma.schedule.update({
|
|
|
|
where: {
|
|
|
|
id: input.scheduleId,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
timeZone: input.timeZone,
|
|
|
|
name: input.name,
|
|
|
|
availability: {
|
|
|
|
deleteMany: {
|
|
|
|
scheduleId: {
|
|
|
|
equals: input.scheduleId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
createMany: {
|
2022-12-14 17:30:55 +00:00
|
|
|
data: [
|
|
|
|
...availability,
|
|
|
|
...(input.dateOverrides || []).map((override) => ({
|
|
|
|
date: override.start,
|
|
|
|
startTime: override.start,
|
|
|
|
endTime: override.end,
|
|
|
|
})),
|
|
|
|
],
|
2022-03-17 16:48:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-11-10 23:40:01 +00:00
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
userId: true,
|
|
|
|
name: true,
|
|
|
|
availability: true,
|
|
|
|
timeZone: true,
|
|
|
|
eventType: {
|
|
|
|
select: {
|
|
|
|
_count: true,
|
|
|
|
id: true,
|
|
|
|
eventName: true,
|
|
|
|
},
|
2022-11-03 20:58:52 +00:00
|
|
|
},
|
|
|
|
},
|
2022-11-10 23:40:01 +00:00
|
|
|
});
|
2022-03-23 23:23:18 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const userAvailability = convertScheduleToAvailability(schedule);
|
2022-11-03 20:58:52 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
return {
|
|
|
|
schedule,
|
|
|
|
availability: userAvailability,
|
|
|
|
timeZone: schedule.timeZone || user.timeZone,
|
|
|
|
isDefault: updatedUser
|
|
|
|
? updatedUser.defaultScheduleId === schedule.id
|
|
|
|
: user.defaultScheduleId === schedule.id,
|
|
|
|
prevDefaultId: user.defaultScheduleId,
|
|
|
|
currentDefaultId: updatedUser ? updatedUser.defaultScheduleId : user.defaultScheduleId,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
});
|
2022-09-06 22:58:16 +00:00
|
|
|
|
|
|
|
export const convertScheduleToAvailability = (
|
|
|
|
schedule: Partial<ScheduleModel> & { availability: AvailabilityModel[] }
|
|
|
|
) => {
|
|
|
|
return schedule.availability.reduce(
|
|
|
|
(schedule: Schedule, availability) => {
|
|
|
|
availability.days.forEach((day) => {
|
|
|
|
schedule[day].push({
|
|
|
|
start: new Date(
|
|
|
|
Date.UTC(
|
|
|
|
new Date().getUTCFullYear(),
|
|
|
|
new Date().getUTCMonth(),
|
|
|
|
new Date().getUTCDate(),
|
|
|
|
availability.startTime.getUTCHours(),
|
|
|
|
availability.startTime.getUTCMinutes()
|
|
|
|
)
|
|
|
|
),
|
|
|
|
end: new Date(
|
|
|
|
Date.UTC(
|
|
|
|
new Date().getUTCFullYear(),
|
|
|
|
new Date().getUTCMonth(),
|
|
|
|
new Date().getUTCDate(),
|
|
|
|
availability.endTime.getUTCHours(),
|
|
|
|
availability.endTime.getUTCMinutes()
|
|
|
|
)
|
|
|
|
),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return schedule;
|
|
|
|
},
|
|
|
|
Array.from([...Array(7)]).map(() => [])
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const setupDefaultSchedule = async (userId: number, scheduleId: number, prisma: PrismaClient) => {
|
2022-11-03 20:58:52 +00:00
|
|
|
return prisma.user.update({
|
2022-09-06 22:58:16 +00:00
|
|
|
where: {
|
|
|
|
id: userId,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
defaultScheduleId: scheduleId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const isDefaultSchedule = (scheduleId: number, user: Partial<User>) => {
|
|
|
|
return !user.defaultScheduleId || user.defaultScheduleId === scheduleId;
|
|
|
|
};
|
|
|
|
|
2022-09-15 06:07:58 +00:00
|
|
|
const getDefaultScheduleId = async (userId: number, prisma: PrismaClient) => {
|
|
|
|
const user = await prisma.user.findUnique({
|
|
|
|
where: {
|
|
|
|
id: userId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
defaultScheduleId: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (user?.defaultScheduleId) {
|
|
|
|
return user.defaultScheduleId;
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultSchedule = await prisma.schedule.findFirst({
|
|
|
|
where: {
|
|
|
|
userId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return defaultSchedule?.id; // TODO: Handle no schedules AT ALL
|
|
|
|
};
|
|
|
|
|
2022-09-06 22:58:16 +00:00
|
|
|
const hasDefaultSchedule = async (user: Partial<User>, prisma: PrismaClient) => {
|
|
|
|
const defaultSchedule = await prisma.schedule.findFirst({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return !!user.defaultScheduleId || !!defaultSchedule;
|
|
|
|
};
|