feat: add availability shown in rr reassign (#17287)

* (feat) add availability status to select

* add logic for confirmation modal on unavailable host

* fix type error

* fix type error

* more type fixes

* fix types
This commit is contained in:
sean-brydon authored and GitHub committed 2024-10-24 12:38:03 +01:00
1 parent d0a1e90199
commit 0949bbf167
4 files changed
+178 -84

No files matched your search

@@ -1,4 +1,10 @@
import dayjs from "@calcom/dayjs";
import { ensureAvailableUsers } from "@calcom/features/bookings/lib/handleNewBooking/ensureAvailableUsers";
import { getEventTypesFromDB } from "@calcom/features/bookings/lib/handleNewBooking/getEventTypesFromDB";
import type { IsFixedAwareUser } from "@calcom/features/bookings/lib/handleNewBooking/types";
import logger from "@calcom/lib/logger";
import type { PrismaClient } from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/enums";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
import type { TGetRoundRobinHostsToReassignInputSchema } from "./getRoundRobinHostsToReasign.schema";
@@ -11,21 +17,32 @@ type GetRoundRobinHostsToReassignOptions = {
input: TGetRoundRobinHostsToReassignInputSchema;
};
async function isTeamAdmin(prisma: PrismaClient, userId: number, teamId: number) {
const membership = await prisma.membership.findFirst({
where: { userId, teamId, role: { in: [MembershipRole.ADMIN, MembershipRole.OWNER] } },
});
return membership?.role ?? false;
}
export const getRoundRobinHostsToReassign = async ({ ctx, input }: GetRoundRobinHostsToReassignOptions) => {
const { prisma } = ctx;
const { isOrgAdmin } = ctx.user.organization;
const hasPermsToView = !ctx.user.organization.isPrivate || isOrgAdmin;
if (!hasPermsToView) {
return [];
}
const gettingRoundRobinHostsToReassignLogger = logger.getSubLogger({
prefix: ["gettingRoundRobinHostsToReassign", `${input.bookingId}`],
});
const booking = await prisma.booking.findUniqueOrThrow({
where: { id: input.bookingId },
select: {
userId: true,
startTime: true,
endTime: true,
eventType: {
select: {
id: true,
timeZone: true,
teamId: true,
},
},
},
@@ -35,27 +52,49 @@ export const getRoundRobinHostsToReassign = async ({ ctx, input }: GetRoundRobin
throw new Error("Booking not found");
}
// If input.exclude is "fixedHosts", exclude fixed hosts from the list
const excludeFixedHosts = input.exclude === "fixedHosts";
// Get event type
const eventType = await getEventTypesFromDB(booking.eventType.id);
const eventTypeHosts = await prisma.host.findMany({
where: {
eventTypeId: booking.eventType.id,
isFixed: excludeFixedHosts ? false : undefined,
if (!eventType || !eventType.teamId) {
throw new Error("Event type not found");
}
const availableEventTypeUsers = eventType.hosts
.filter((h) => !h.isFixed && h.user.id !== booking.userId)
.map((host) => ({
...host.user,
isFixed: host.isFixed,
priority: host?.priority ?? 2,
}));
const availableUsers = await ensureAvailableUsers(
{ ...eventType, users: availableEventTypeUsers as IsFixedAwareUser[] },
{
dateFrom: dayjs(booking.startTime).format(),
dateTo: dayjs(booking.endTime).format(),
timeZone: eventType.timeZone || "UTC",
},
select: {
user: {
select: {
id: true,
name: true,
},
},
},
});
gettingRoundRobinHostsToReassignLogger
);
const hosts = eventTypeHosts.map((host) => host.user);
const availableUserIds = new Set(availableUsers.map((u) => u.id));
return hosts;
const canViewReassignBusyUsers = isOrgAdmin || (await isTeamAdmin(prisma, ctx.user.id, eventType.teamId));
const roundRobinHostsToReassign = availableEventTypeUsers.reduce((acc, host) => {
const status = availableUserIds.has(host.id) ? "available" : "unavailable";
if (canViewReassignBusyUsers || status === "available") {
acc.push({
id: host.id,
name: host.name,
email: host.email,
status,
});
}
return acc;
}, [] as { id: number; name: string | null; email: string; status: string }[]);
return roundRobinHostsToReassign;
};
export default getRoundRobinHostsToReassign;