diff --git a/.changeset/lemon-rice-smile.md b/.changeset/lemon-rice-smile.md new file mode 100644 index 0000000000..e28fb4b96f --- /dev/null +++ b/.changeset/lemon-rice-smile.md @@ -0,0 +1,5 @@ +--- +"@calcom/atoms": minor +--- + +This PR adds an atom to list a user's schedule. Furthermore, there was also another atom introduced which can be used to create a new schedule for a user. diff --git a/apps/api/v2/src/modules/atoms/controllers/atoms.schedules.controller.ts b/apps/api/v2/src/modules/atoms/controllers/atoms.schedules.controller.ts index 7527b44e2f..227a2decd6 100644 --- a/apps/api/v2/src/modules/atoms/controllers/atoms.schedules.controller.ts +++ b/apps/api/v2/src/modules/atoms/controllers/atoms.schedules.controller.ts @@ -25,6 +25,11 @@ import { import { z } from "zod"; import { SCHEDULE_READ, SCHEDULE_WRITE, SUCCESS_STATUS } from "@calcom/platform-constants"; +import type { + GetAvailabilityListHandlerReturn, + DuplicateScheduleHandlerReturn, +} from "@calcom/platform-libraries/schedules"; +import { getAvailabilityListHandler, duplicateScheduleHandler } from "@calcom/platform-libraries/schedules"; import type { CreateScheduleHandlerReturn, CreateScheduleSchema } from "@calcom/platform-libraries/schedules"; import { createScheduleHandler } from "@calcom/platform-libraries/schedules"; import { FindDetailedScheduleByIdReturnType } from "@calcom/platform-libraries/schedules"; @@ -68,6 +73,21 @@ export class AtomsSchedulesController { }; } + @Get("/schedules/all") + @Version(VERSION_NEUTRAL) + @UseGuards(ApiAuthGuard) + @Permissions([SCHEDULE_READ]) + async getAllUserSchedules( + @GetUser() user: UserWithProfile + ): Promise>> { + const userSchedules = await getAvailabilityListHandler({ ctx: { user } }); + + return { + status: SUCCESS_STATUS, + data: userSchedules, + }; + } + @Patch("schedules/:scheduleId") @Permissions([SCHEDULE_WRITE]) @UseGuards(ApiAuthGuard) @@ -104,4 +124,20 @@ export class AtomsSchedulesController { data: createdSchedule, }; } + + @Post("schedules/:scheduleId/duplicate") + @Permissions([SCHEDULE_WRITE]) + @UseGuards(ApiAuthGuard) + @ApiOperation({ summary: "Duplicate existing schedule" }) + async duplicateExistingSchedule( + @GetUser() user: UserWithProfile, + @Param("scheduleId", ParseIntPipe) scheduleId: number + ): Promise>> { + const duplicatedSchedule = await duplicateScheduleHandler({ ctx: { user }, input: { scheduleId } }); + + return { + status: SUCCESS_STATUS, + data: duplicatedSchedule, + }; + } } diff --git a/apps/web/app/(use-page-wrapper)/(main-nav)/availability/page.tsx b/apps/web/app/(use-page-wrapper)/(main-nav)/availability/page.tsx index 5ca5e9f438..e9ae0e7de0 100644 --- a/apps/web/app/(use-page-wrapper)/(main-nav)/availability/page.tsx +++ b/apps/web/app/(use-page-wrapper)/(main-nav)/availability/page.tsx @@ -9,6 +9,7 @@ import { getServerSession } from "@calcom/features/auth/lib/getServerSession"; import { OrganizationRepository } from "@calcom/features/ee/organizations/repositories/OrganizationRepository"; import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service"; import { AvailabilitySliderTable } from "@calcom/features/timezone-buddy/components/AvailabilitySliderTable"; +import { getScheduleListItemData } from "@calcom/lib/schedules/transformers/getScheduleListItemData"; import { MembershipRole } from "@calcom/prisma/enums"; import { availabilityRouter } from "@calcom/trpc/server/routers/viewer/availability/_router"; @@ -56,15 +57,7 @@ const Page = async ({ searchParams: _searchParams }: PageProps) => { // This is because the data is cached and as a result the data is converted to a string const availabilities = { ...cachedAvailabilities, - schedules: cachedAvailabilities.schedules.map((schedule) => ({ - ...schedule, - availability: schedule.availability.map((avail) => ({ - ...avail, - startTime: new Date(avail.startTime), - endTime: new Date(avail.endTime), - date: avail.date ? new Date(avail.date) : null, - })), - })), + schedules: cachedAvailabilities.schedules.map((schedule) => getScheduleListItemData(schedule)), }; const organizationId = session?.user?.profile?.organizationId ?? session?.user.org?.id; diff --git a/apps/web/modules/availability/availability-view.tsx b/apps/web/modules/availability/availability-view.tsx index 6b048655f1..9bdb907538 100644 --- a/apps/web/modules/availability/availability-view.tsx +++ b/apps/web/modules/availability/availability-view.tsx @@ -141,6 +141,7 @@ export function AvailabilityList({ availabilities }: AvailabilityListProps) {