-
Notifications
You must be signed in to change notification settings - Fork 8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: viewer/slots/util.ts refactor #15399
Closed
+106
−82
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -41,6 +41,15 @@ import type { GetScheduleOptions } from "./getSchedule.handler"; | |
import type { TGetScheduleInputSchema } from "./getSchedule.schema"; | ||
import { handleNotificationWhenNoSlots } from "./handleNotificationWhenNoSlots"; | ||
|
||
interface EventBusyDate { | ||
start: string; | ||
end: string; | ||
} | ||
|
||
interface CurrentSeats { | ||
startTime: Dayjs; | ||
} | ||
|
||
export const checkIfIsAvailable = ({ | ||
time, | ||
busy, | ||
|
@@ -50,44 +59,36 @@ export const checkIfIsAvailable = ({ | |
time: Dayjs; | ||
busy: EventBusyDate[]; | ||
eventLength: number; | ||
currentSeats?: CurrentSeats; | ||
currentSeats?: CurrentSeats[]; | ||
}): boolean => { | ||
if (currentSeats?.some((booking) => booking.startTime.toISOString() === time.toISOString())) { | ||
if (isTimeInCurrentSeats(time, currentSeats)) { | ||
return true; | ||
} | ||
|
||
const slotEndTime = time.add(eventLength, "minutes").utc(); | ||
const slotStartTime = time.utc(); | ||
|
||
return busy.every((busyTime) => { | ||
const startTime = dayjs.utc(busyTime.start).utc(); | ||
const endTime = dayjs.utc(busyTime.end); | ||
return busy.every(busyTime => isSlotAvailable(slotStartTime, slotEndTime, busyTime)); | ||
}; | ||
|
||
if (endTime.isBefore(slotStartTime) || startTime.isAfter(slotEndTime)) { | ||
return true; | ||
} | ||
const isTimeInCurrentSeats = (time: Dayjs, currentSeats?: CurrentSeats[]): boolean => { | ||
return currentSeats?.some(booking => booking.startTime.toISOString() === time.toISOString()) || false; | ||
}; | ||
|
||
if (slotStartTime.isBetween(startTime, endTime, null, "[)")) { | ||
return false; | ||
} else if (slotEndTime.isBetween(startTime, endTime, null, "(]")) { | ||
return false; | ||
} | ||
|
||
// Check if start times are the same | ||
if (time.utc().isBetween(startTime, endTime, null, "[)")) { | ||
return false; | ||
} | ||
// Check if slot end time is between start and end time | ||
else if (slotEndTime.isBetween(startTime, endTime)) { | ||
return false; | ||
} | ||
// Check if startTime is between slot | ||
else if (startTime.isBetween(time, slotEndTime)) { | ||
return false; | ||
} | ||
const isSlotAvailable = (slotStartTime: Dayjs, slotEndTime: Dayjs, busyTime: EventBusyDate): boolean => { | ||
const startTime = dayjs.utc(busyTime.start); | ||
const endTime = dayjs.utc(busyTime.end); | ||
|
||
if (endTime.isBefore(slotStartTime) || startTime.isAfter(slotEndTime)) { | ||
return true; | ||
}); | ||
} | ||
|
||
return !( | ||
slotStartTime.isBetween(startTime, endTime, null, "[)") || | ||
slotEndTime.isBetween(startTime, endTime, null, "(]") || | ||
startTime.isBetween(slotStartTime, slotEndTime) || | ||
slotEndTime.isBetween(startTime, endTime) | ||
); | ||
}; | ||
|
||
async function getEventTypeId({ | ||
|
@@ -132,26 +133,50 @@ async function getEventTypeId({ | |
return eventType?.id; | ||
} | ||
|
||
interface OrganizationDetails { | ||
currentOrgDomain: string | null; | ||
isValidOrgDomain: boolean; | ||
} | ||
|
||
export async function getEventType( | ||
input: TGetScheduleInputSchema, | ||
organizationDetails: { currentOrgDomain: string | null; isValidOrgDomain: boolean } | ||
organizationDetails: OrganizationDetails | ||
) { | ||
const { eventTypeSlug, usernameList, isTeamEvent } = input; | ||
const eventTypeId = | ||
input.eventTypeId || | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
(await getEventTypeId({ | ||
slug: usernameList?.[0], | ||
eventTypeSlug: eventTypeSlug, | ||
isTeamEvent, | ||
organizationDetails, | ||
})); | ||
|
||
const eventTypeId = await getEventTypeIdFromInput(input, organizationDetails); | ||
if (!eventTypeId) { | ||
return null; | ||
} | ||
|
||
const eventType = await prisma.eventType.findUnique({ | ||
const eventType = await fetchEventTypeById(eventTypeId); | ||
if (!eventType) { | ||
return null; | ||
} | ||
|
||
return { | ||
...eventType, | ||
metadata: EventTypeMetaDataSchema.parse(eventType.metadata), | ||
}; | ||
} | ||
|
||
async function getEventTypeIdFromInput( | ||
input: TGetScheduleInputSchema, | ||
organizationDetails: OrganizationDetails | ||
): Promise<string | null> { | ||
const { eventTypeSlug, usernameList, isTeamEvent } = input; | ||
if (input.eventTypeId) { | ||
return input.eventTypeId; | ||
} | ||
|
||
return await getEventTypeId({ | ||
slug: usernameList?.[0], | ||
eventTypeSlug: eventTypeSlug, | ||
isTeamEvent, | ||
organizationDetails, | ||
}); | ||
} | ||
|
||
async function fetchEventTypeById(eventTypeId: string) { | ||
return await prisma.eventType.findUnique({ | ||
where: { | ||
id: eventTypeId, | ||
}, | ||
|
@@ -218,15 +243,6 @@ export async function getEventType( | |
}, | ||
}, | ||
}); | ||
|
||
if (!eventType) { | ||
return null; | ||
} | ||
|
||
return { | ||
...eventType, | ||
metadata: EventTypeMetaDataSchema.parse(eventType.metadata), | ||
}; | ||
} | ||
|
||
export async function getDynamicEventType( | ||
|
@@ -339,41 +355,49 @@ async function getCRMContactOwnerForRRLeadSkip( | |
} | ||
} | ||
|
||
async function getCRMManagerWithRRLeadSkip(apps: z.infer<typeof EventTypeAppMetadataSchema>) { | ||
let crmRoundRobinLeadSkip; | ||
for (const appKey in apps) { | ||
const app = apps[appKey as keyof typeof apps]; | ||
if ( | ||
app.enabled && | ||
typeof app.appCategories === "object" && | ||
app.appCategories.some((category: string) => category === "crm") && | ||
app.roundRobinLeadSkip | ||
) { | ||
crmRoundRobinLeadSkip = app; | ||
break; | ||
} | ||
} | ||
async function getCRMManagerWithRRLeadSkip(apps) { | ||
const crmRoundRobinLeadSkip = findCRMApp(apps); | ||
|
||
if (crmRoundRobinLeadSkip) { | ||
const crmCredential = await prisma.credential.findUnique({ | ||
where: { | ||
id: crmRoundRobinLeadSkip.credentialId, | ||
}, | ||
include: { | ||
user: { | ||
select: { | ||
email: true, | ||
}, | ||
if (!crmRoundRobinLeadSkip) return; | ||
|
||
const crmCredential = await prisma.credential.findUnique({ | ||
where: { | ||
id: crmRoundRobinLeadSkip.credentialId, | ||
}, | ||
include: { | ||
user: { | ||
select: { | ||
email: true, | ||
}, | ||
}, | ||
}); | ||
if (crmCredential) { | ||
return new CrmManager(crmCredential); | ||
}, | ||
}); | ||
|
||
if (!crmCredential) return; | ||
|
||
return new CrmManager(crmCredential); | ||
} | ||
|
||
function findCRMApp(apps) { | ||
for (const appKey in apps) { | ||
const app = apps[appKey as keyof typeof apps]; | ||
if (isCRMAppWithRoundRobinLeadSkip(app)) { | ||
return app; | ||
} | ||
} | ||
return; | ||
return null; | ||
} | ||
|
||
function isCRMAppWithRoundRobinLeadSkip(app: any) { | ||
return ( | ||
app.enabled && | ||
typeof app.appCategories === "object" && | ||
app.appCategories.includes("crm") && | ||
app.roundRobinLeadSkip | ||
); | ||
} | ||
|
||
|
||
export async function getAvailableSlots({ input, ctx }: GetScheduleOptions): Promise<IGetAvailableSlots> { | ||
const orgDetails = input?.orgSlug | ||
? { | ||
|
@@ -410,7 +434,7 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions): Pro | |
: dayjs(startTimeAsIsoString).subtract(1, "month").toISOString(); | ||
|
||
const loggerWithEventDetails = logger.getSubLogger({ | ||
prefix: ["getAvailableSlots", `${eventType.id}:${input.usernameList}/${input.eventTypeSlug}`], | ||
prefix: ["getAvailableSlots", ${eventType.id}:${input.usernameList}/${input.eventTypeSlug}], | ||
}); | ||
|
||
loggerWithEventDetails.debug( | ||
|
@@ -809,12 +833,12 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions): Pro | |
{} as typeof slotsMappedToDate | ||
); | ||
|
||
loggerWithEventDetails.debug(`getSlots took ${getSlotsTime}ms and executed ${getSlotsCount} times`); | ||
loggerWithEventDetails.debug(getSlots took ${getSlotsTime}ms and executed ${getSlotsCount} times); | ||
|
||
loggerWithEventDetails.debug( | ||
`checkForAvailability took ${checkForAvailabilityTime}ms and executed ${checkForAvailabilityCount} times` | ||
checkForAvailability took ${checkForAvailabilityTime}ms and executed ${checkForAvailabilityCount} times | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
); | ||
loggerWithEventDetails.debug(`Available slots: ${JSON.stringify(withinBoundsSlotsMappedToDate)}`); | ||
loggerWithEventDetails.debug(Available slots: ${JSON.stringify(withinBoundsSlotsMappedToDate)}); | ||
|
||
// We only want to run this on single targeted events and not dynamic | ||
if (!Object.keys(withinBoundsSlotsMappedToDate).length && input.usernameList?.length === 1) { | ||
|
@@ -890,4 +914,4 @@ export function getAllDatesWithBookabilityStatus(availableDates: string[]) { | |
currentDate = currentDate.add(1, "day"); | ||
} | ||
return allDates; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be a template string