feat: show ooo forwarding and emoji when not available (#18054)

* feat: show ooo when not available

* fix: send correct username for toUser if user belongs to org

* enrich ooo user with profile in getUserAvailability

* fix: set correct ooo slots

---------

Co-authored-by: Udit Takkar <[email protected]>
Co-authored-by: Anik Dhabal Babu <[email protected]>
This commit is contained in:
authored and GitHub committed 2025-01-30 16:36:27 +01:00
1 parent 7ec928f5c2
commit a0f6e50d5b
2 files changed
+39 -10

No files matched your search

+18 -9
View File
@@ -22,8 +22,7 @@ import { safeStringify } from "@calcom/lib/safeStringify";
import { EventTypeRepository } from "@calcom/lib/server/repository/eventType";
import { UserRepository } from "@calcom/lib/server/repository/user";
import prisma from "@calcom/prisma";
import { SchedulingType } from "@calcom/prisma/enums";
import { BookingStatus } from "@calcom/prisma/enums";
import { BookingStatus, SchedulingType } from "@calcom/prisma/enums";
import { EventTypeMetaDataSchema, stringToDayjsZod } from "@calcom/prisma/zod-utils";
import type { EventBusyDetails, IntervalLimitUnit } from "@calcom/types/Calendar";
import type { TimeRange } from "@calcom/types/schedule";
@@ -542,7 +541,7 @@ const _getUserAvailability = async function getUsersWorkingHoursLifeTheUniverseA
},
}));
const datesOutOfOffice: IOutOfOfficeData = calculateOutOfOfficeRanges(outOfOfficeDays, availability);
const datesOutOfOffice: IOutOfOfficeData = await calculateOutOfOfficeRanges(outOfOfficeDays, availability);
const { dateRanges, oooExcludedDateRanges } = buildDateRanges({
dateFrom,
@@ -629,15 +628,17 @@ export interface IOutOfOfficeData {
};
}
const calculateOutOfOfficeRanges = (
const calculateOutOfOfficeRanges = async (
outOfOfficeDays: GetUserAvailabilityInitialData["outOfOfficeDays"],
availability: GetUserAvailabilityParamsDTO["availability"]
): IOutOfOfficeData => {
): Promise<IOutOfOfficeData> => {
if (!outOfOfficeDays || outOfOfficeDays.length === 0) {
return {};
}
return outOfOfficeDays.reduce((acc: IOutOfOfficeData, { start, end, toUser, user, reason }) => {
const acc: IOutOfOfficeData = {};
for (const { start, end, toUser, user, reason } of outOfOfficeDays) {
// here we should use startDate or today if start is before today
// consider timezone in start and end date range
const startDateRange = dayjs(start).utc().isBefore(dayjs().startOf("day").utc())
@@ -658,19 +659,27 @@ const calculateOutOfOfficeRanges = (
continue; // Skip to the next iteration if day not found in flattenDays
}
const enrichedToUser = toUser ? await UserRepository.enrichUserWithItsProfile({ user: toUser }) : null;
acc[date.format("YYYY-MM-DD")] = {
// @TODO: would be good having start and end availability time here, but for now should be good
// you can obtain that from user availability defined outside of here
fromUser: { id: user.id, displayName: user.name },
// optional chaining destructuring toUser
toUser: !!toUser ? { id: toUser.id, displayName: toUser.name, username: toUser.username } : null,
...(!!enrichedToUser && {
toUser: {
id: enrichedToUser.id,
username: enrichedToUser.username,
displayName: enrichedToUser.name,
},
}),
reason: !!reason ? reason.reason : null,
emoji: !!reason ? reason.emoji : null,
};
}
}
return acc;
}, {});
return acc;
};
type GetUsersAvailabilityProps = {
+21 -1
View File
@@ -184,6 +184,7 @@ function buildSlotsWithDateRanges({
}
}
const processedOOODates = new Set<string>();
dateRanges.forEach((range) => {
const dateYYYYMMDD = range.start.format("YYYY-MM-DD");
const startTimeWithMinNotice = dayjs.utc().add(minimumBookingNotice, "minute");
@@ -206,6 +207,25 @@ function buildSlotsWithDateRanges({
slotStartTime = slotStartTime.add(offsetStart ?? 0, "minutes").tz(timeZone);
// Add OOO slot if exists and is before current range
if (datesOutOfOffice) {
Object.entries(datesOutOfOffice).forEach(([dateStr, oooData]) => {
const oooDate = dayjs(dateStr);
if (oooDate.isBefore(dateYYYYMMDD, "day") && !processedOOODates.has(dateStr)) {
slots.push({
time: oooDate.startOf("day"),
away: true,
...(oooData.fromUser && { fromUser: oooData.fromUser }),
...(oooData.toUser && { toUser: oooData.toUser }),
...(oooData.reason && { reason: oooData.reason }),
...(oooData.emoji && { emoji: oooData.emoji }),
});
// when the date range increases in the next iteration, we don't want to process this date again
processedOOODates.add(dateStr);
}
});
}
while (!slotStartTime.add(eventLength, "minutes").subtract(1, "second").utc().isAfter(rangeEnd)) {
const dateOutOfOfficeExists = datesOutOfOffice?.[dateYYYYMMDD];
let slotData: {
@@ -231,8 +251,8 @@ function buildSlotsWithDateRanges({
...(reason && { reason }),
...(emoji && { emoji }),
};
processedOOODates.add(dateYYYYMMDD);
}
slots.push(slotData);
slotStartTime = slotStartTime.add(frequency + (offsetStart ?? 0), "minutes");
}