Skip to content

Commit

Permalink
moved listSchedules to schedule repository
Browse files Browse the repository at this point in the history
  • Loading branch information
SomayChauhan committed Oct 7, 2024
1 parent a02240b commit b5feacf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
36 changes: 36 additions & 0 deletions packages/lib/server/repository/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,40 @@ export class ScheduleRepository {
readOnly: schedule.userId !== userId && !isManagedEventType,
};
}
static async listSchedules(userId: number, userDefaultScheduleId: number | null) {
const schedules = await prisma.schedule.findMany({
where: {
userId,
},
select: {
id: true,
name: true,
availability: true,
timeZone: true,
},
orderBy: {
id: "asc",
},
});

const defaultScheduleId = await getDefaultScheduleId(userId, prisma);

if (!userDefaultScheduleId) {
await prisma.user.update({
where: {
id: userId,
},
data: {
defaultScheduleId,
},
});
}

return {
schedules: schedules.map((schedule) => ({
...schedule,
isDefault: schedule.id === defaultScheduleId,
})),
};
}
}
39 changes: 2 additions & 37 deletions packages/trpc/server/routers/viewer/availability/list.handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { prisma } from "@calcom/prisma";
import { ScheduleRepository } from "@calcom/lib/server/repository/schedule";

import type { TrpcSessionUser } from "../../../trpc";
import { getDefaultScheduleId } from "./util";

type ListOptions = {
ctx: {
Expand All @@ -11,39 +10,5 @@ type ListOptions = {

export const listHandler = async ({ ctx }: ListOptions) => {
const { user } = ctx;

const schedules = await prisma.schedule.findMany({
where: {
userId: user.id,
},
select: {
id: true,
name: true,
availability: true,
timeZone: true,
},
orderBy: {
id: "asc",
},
});

const defaultScheduleId = await getDefaultScheduleId(user.id, prisma);

if (!user.defaultScheduleId) {
await prisma.user.update({
where: {
id: user.id,
},
data: {
defaultScheduleId,
},
});
}

return {
schedules: schedules.map((schedule) => ({
...schedule,
isDefault: schedule.id === defaultScheduleId,
})),
};
return await ScheduleRepository.listSchedules(user.id, user.defaultScheduleId);
};

0 comments on commit b5feacf

Please sign in to comment.