forked from calcom/cal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: v2 slots timeZone parameter (calcom#18488)
* fix: v2 slots timeZone parameter * test: v2 slots
- Loading branch information
1 parent
df4608c
commit 8d4052f
Showing
10 changed files
with
638 additions
and
66 deletions.
There are no files selected for viewing
490 changes: 490 additions & 0 deletions
490
apps/api/v2/src/modules/slots/controllers/slots.controller.e2e-spec.ts
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
apps/api/v2/src/modules/slots/services/slots-output.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { EventTypesRepository_2024_04_15 } from "@/ee/event-types/event-types_2024_04_15/event-types.repository"; | ||
import { Injectable, BadRequestException } from "@nestjs/common"; | ||
import { DateTime } from "luxon"; | ||
|
||
import { SlotFormat } from "@calcom/platform-enums"; | ||
|
||
type TimeSlots = { slots: Record<string, { time: string }[]> }; | ||
type RangeSlots = { slots: Record<string, { startTime: string; endTime: string }[]> }; | ||
|
||
@Injectable() | ||
export class SlotsOutputService { | ||
constructor(private readonly eventTypesRepository: EventTypesRepository_2024_04_15) {} | ||
|
||
async getOutputSlots( | ||
availableSlots: TimeSlots, | ||
duration?: number, | ||
eventTypeId?: number, | ||
slotFormat?: SlotFormat, | ||
timeZone?: string | ||
): Promise<TimeSlots | RangeSlots> { | ||
if (!slotFormat) { | ||
return timeZone ? this.setTimeZone(availableSlots, timeZone) : availableSlots; | ||
} | ||
|
||
const formattedSlots = await this.formatSlots(availableSlots, duration, eventTypeId, slotFormat); | ||
return timeZone ? this.setTimeZoneRange(formattedSlots, timeZone) : formattedSlots; | ||
} | ||
|
||
private setTimeZone(slots: TimeSlots, timeZone: string): TimeSlots { | ||
const formattedSlots = Object.entries(slots.slots).reduce((acc, [date, daySlots]) => { | ||
acc[date] = daySlots.map((slot) => ({ | ||
time: DateTime.fromISO(slot.time).setZone(timeZone).toISO() || "unknown-time", | ||
})); | ||
return acc; | ||
}, {} as Record<string, { time: string }[]>); | ||
|
||
return { slots: formattedSlots }; | ||
} | ||
|
||
private setTimeZoneRange(slots: RangeSlots, timeZone: string): RangeSlots { | ||
const formattedSlots = Object.entries(slots.slots).reduce((acc, [date, daySlots]) => { | ||
acc[date] = daySlots.map((slot) => ({ | ||
startTime: DateTime.fromISO(slot.startTime).setZone(timeZone).toISO() || "unknown-start-time", | ||
endTime: DateTime.fromISO(slot.endTime).setZone(timeZone).toISO() || "unknown-end-time", | ||
})); | ||
return acc; | ||
}, {} as Record<string, { startTime: string; endTime: string }[]>); | ||
|
||
return { slots: formattedSlots }; | ||
} | ||
|
||
private async formatSlots( | ||
availableSlots: TimeSlots, | ||
duration?: number, | ||
eventTypeId?: number, | ||
slotFormat?: SlotFormat | ||
): Promise<RangeSlots> { | ||
if (slotFormat && !Object.values(SlotFormat).includes(slotFormat)) { | ||
throw new BadRequestException("Invalid slot format. Must be either 'range' or 'time'"); | ||
} | ||
|
||
const slotDuration = await this.getDuration(duration, eventTypeId); | ||
|
||
const slots = Object.entries(availableSlots.slots).reduce< | ||
Record<string, { startTime: string; endTime: string }[]> | ||
>((acc, [date, slots]) => { | ||
acc[date] = (slots as { time: string }[]).map((slot) => { | ||
const startTime = new Date(slot.time); | ||
const endTime = new Date(startTime.getTime() + slotDuration * 60000); | ||
return { | ||
startTime: startTime.toISOString(), | ||
endTime: endTime.toISOString(), | ||
}; | ||
}); | ||
return acc; | ||
}, {}); | ||
|
||
return { slots }; | ||
} | ||
|
||
private async getDuration(duration?: number, eventTypeId?: number): Promise<number> { | ||
if (duration) { | ||
return duration; | ||
} | ||
|
||
if (eventTypeId) { | ||
const eventType = await this.eventTypesRepository.getEventTypeWithDuration(eventTypeId); | ||
if (!eventType) { | ||
throw new Error("Event type not found"); | ||
} | ||
return eventType.length; | ||
} | ||
|
||
throw new Error("duration or eventTypeId is required"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
apps/api/v2/test/fixtures/repository/selected-slots.repository.fixture.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { PrismaReadService } from "@/modules/prisma/prisma-read.service"; | ||
import { PrismaWriteService } from "@/modules/prisma/prisma-write.service"; | ||
import { TestingModule } from "@nestjs/testing"; | ||
import { SelectedSlots } from "@prisma/client"; | ||
|
||
export class SelectedSlotsRepositoryFixture { | ||
private prismaReadClient: PrismaReadService["prisma"]; | ||
private prismaWriteClient: PrismaWriteService["prisma"]; | ||
|
||
constructor(private readonly module: TestingModule) { | ||
this.prismaReadClient = module.get(PrismaReadService).prisma; | ||
this.prismaWriteClient = module.get(PrismaWriteService).prisma; | ||
} | ||
|
||
async deleteByUId(uid: SelectedSlots["uid"]) { | ||
return this.prismaWriteClient.selectedSlots.deleteMany({ where: { uid } }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters