fix: allow org admin to cancel and reschedule seated bookings (#24640)
* refactor: move org admin related logic to org server * fix: update cancellation logic to make sure org admin can cancel seated bookings of a team user * fix: import path * update bookings repository * fix: update reschedule endpoint logic to let org admin reschedule bookings for a user * refactor: make logic more simple * chore: update platform libraries * more refactors * fix: add check to make sure org admin can reschedule booking * chore: remove unused comments * test: add e2e tests for org admin reschedule and cancel seated bookings - Add seated event type creation for testing - Add test for org admin rescheduling a seated booking for a managed user - Add test for org admin canceling a full seated booking for a managed user - Add test for org admin canceling a specific seat in a seated booking These tests verify the functionality added in PR #24640 which allows org admins to reschedule and cancel seated bookings for users in their organization. Co-Authored-By: [email protected] <[email protected]> * chore: add cubic feedback * fix: tests for seated booking management by org admin * chore: implement PR feedback * fixup * chore: update docs * fixup: get optional user from request and then pass it down to getBookingForReschedule * fix: validate seatUid before checking booking cancellation status Move canRescheduleBooking call to happen after input validation (including seatUid validation for seated bookings) but before the actual booking creation. This ensures that when trying to reschedule a seated booking without providing seatUid, users get the proper 'seatUid required' error instead of 'booking has been cancelled' error. Fixes failing e2e test: 'should not be able to reschedule seated booking if seatUid is not provided' Co-Authored-By: [email protected] <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
11 files changed
+313
-75
No files matched your search
@@ -28,6 +28,7 @@ import { parseRecurringEvent } from "@calcom/lib/isRecurringEvent";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import { PrismaOrgMembershipRepository } from "@calcom/lib/server/repository/PrismaOrgMembershipRepository";
|
||||
import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat";
|
||||
// TODO: Prisma import would be used from DI in a followup PR when we remove `handler` export
|
||||
import prisma from "@calcom/prisma";
|
||||
@@ -144,8 +145,18 @@ async function handler(input: CancelBookingInput) {
|
||||
|
||||
const userIsOwnerOfEventType = bookingToDelete.eventType.owner?.id === userId;
|
||||
|
||||
if (!userIsHost && !userIsOwnerOfEventType) {
|
||||
throw new HttpError({ statusCode: 401, message: "User not a host of this event" });
|
||||
const userIsOrgAdminOfBookingUser =
|
||||
userId &&
|
||||
(await PrismaOrgMembershipRepository.isLoggedInUserOrgAdminOfBookingHost(
|
||||
userId,
|
||||
bookingToDelete.userId
|
||||
));
|
||||
|
||||
if (!userIsHost && !userIsOwnerOfEventType && !userIsOrgAdminOfBookingUser) {
|
||||
throw new HttpError({
|
||||
statusCode: 401,
|
||||
message: "User not a host of this event or an admin of the booking user",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// eslint-disable-next-line no-restricted-imports
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { refreshCredentials } from "@calcom/features/bookings/lib/getAllCredentialsForUsersOnEvent/refreshCredentials";
|
||||
import EventManager from "@calcom/features/bookings/lib/EventManager";
|
||||
import { refreshCredentials } from "@calcom/features/bookings/lib/getAllCredentialsForUsersOnEvent/refreshCredentials";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import { PrismaOrgMembershipRepository } from "@calcom/lib/server/repository/PrismaOrgMembershipRepository";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { BookingStatus } from "@calcom/prisma/enums";
|
||||
import type { Person } from "@calcom/types/Calendar";
|
||||
@@ -98,9 +98,17 @@ const rescheduleSeatedBooking = async (
|
||||
};
|
||||
|
||||
if (!bookingSeat) {
|
||||
const isOrgAdmin =
|
||||
reqUserId &&
|
||||
seatedBooking.user &&
|
||||
(await PrismaOrgMembershipRepository.isLoggedInUserOrgAdminOfBookingHost(
|
||||
reqUserId,
|
||||
seatedBooking.user?.id
|
||||
));
|
||||
// if no bookingSeat is given and the userId != owner, 401.
|
||||
// if no bookingSeat is given, also check if the request user is an org admin of the booking user
|
||||
// TODO: Next step; Evaluate ownership, what about teams?
|
||||
if (seatedBooking.user?.id !== reqUserId) {
|
||||
if (seatedBooking.user?.id !== reqUserId && !isOrgAdmin) {
|
||||
throw new HttpError({ statusCode: 401 });
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
|
||||
export class PrismaOrgMembershipRepository {
|
||||
static async getOrgIdsWhereAdmin(loggedInUserId: number) {
|
||||
const loggedInUserOrgMemberships = await prisma.membership.findMany({
|
||||
where: {
|
||||
userId: loggedInUserId,
|
||||
role: {
|
||||
in: [MembershipRole.OWNER, MembershipRole.ADMIN],
|
||||
},
|
||||
team: {
|
||||
parentId: null,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
teamId: true,
|
||||
},
|
||||
});
|
||||
|
||||
return loggedInUserOrgMemberships.map((m) => m.teamId);
|
||||
}
|
||||
|
||||
static async isLoggedInUserOrgAdminOfBookingHost(loggedInUserId: number, bookingUserId: number) {
|
||||
const orgIdsWhereLoggedInUserAdmin = await this.getOrgIdsWhereAdmin(loggedInUserId);
|
||||
|
||||
if (orgIdsWhereLoggedInUserAdmin.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const bookingUserOrgMembership = await prisma.membership.findFirst({
|
||||
where: {
|
||||
userId: bookingUserId,
|
||||
teamId: {
|
||||
in: orgIdsWhereLoggedInUserAdmin,
|
||||
},
|
||||
team: {
|
||||
parentId: null,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
userId: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (bookingUserOrgMembership) return true;
|
||||
|
||||
const bookingUserOrgTeamMembership = await prisma.membership.findFirst({
|
||||
where: {
|
||||
userId: bookingUserId,
|
||||
team: {
|
||||
parentId: {
|
||||
in: orgIdsWhereLoggedInUserAdmin,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return !!bookingUserOrgTeamMembership;
|
||||
}
|
||||
}
|
||||
@@ -8,4 +8,5 @@ export type {
|
||||
InstantBookingCreateResult,
|
||||
RegularBookingCreateResult,
|
||||
} from "@calcom/features/bookings/lib/dto/types";
|
||||
export { PrismaOrgMembershipRepository } from "@calcom/lib/server/repository/PrismaOrgMembershipRepository";
|
||||
export { addGuestsHandler } from "@calcom/trpc/server/routers/viewer/bookings/addGuests.handler";
|
||||
@@ -17,6 +17,7 @@ import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType";
|
||||
import { isPrismaObjOrUndefined } from "@calcom/lib/isPrismaObj";
|
||||
import { parseRecurringEvent } from "@calcom/lib/isRecurringEvent";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import { PrismaOrgMembershipRepository } from "@calcom/lib/server/repository/PrismaOrgMembershipRepository";
|
||||
import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { Prisma } from "@calcom/prisma/client";
|
||||
@@ -411,7 +412,7 @@ export const confirmHandler = async ({ ctx, input }: ConfirmOptions) => {
|
||||
}
|
||||
}
|
||||
|
||||
const message = `Booking ${confirmed}` ? "confirmed" : "rejected";
|
||||
const message = confirmed ? "Booking confirmed" : "Booking rejected";
|
||||
const status = confirmed ? BookingStatus.ACCEPTED : BookingStatus.REJECTED;
|
||||
|
||||
return { message, status };
|
||||
@@ -472,63 +473,12 @@ const checkIfUserIsAuthorizedToConfirmBooking = async ({
|
||||
if (membership) return;
|
||||
}
|
||||
|
||||
if (bookingUserId && (await isLoggedInUserOrgAdminOfBookingUser(loggedInUserId, bookingUserId))) {
|
||||
if (
|
||||
bookingUserId &&
|
||||
(await PrismaOrgMembershipRepository.isLoggedInUserOrgAdminOfBookingHost(loggedInUserId, bookingUserId))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new TRPCError({ code: "UNAUTHORIZED", message: "User is not authorized to confirm this booking" });
|
||||
};
|
||||
|
||||
async function isLoggedInUserOrgAdminOfBookingUser(loggedInUserId: number, bookingUserId: number) {
|
||||
const orgIdsWhereLoggedInUserAdmin = await getOrgIdsWhereAdmin(loggedInUserId);
|
||||
|
||||
if (orgIdsWhereLoggedInUserAdmin.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const bookingUserOrgMembership = await prisma.membership.findFirst({
|
||||
where: {
|
||||
userId: bookingUserId,
|
||||
teamId: {
|
||||
in: orgIdsWhereLoggedInUserAdmin,
|
||||
},
|
||||
team: {
|
||||
parentId: null,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (bookingUserOrgMembership) return true;
|
||||
|
||||
const bookingUserOrgTeamMembership = await prisma.membership.findFirst({
|
||||
where: {
|
||||
userId: bookingUserId,
|
||||
team: {
|
||||
parentId: {
|
||||
in: orgIdsWhereLoggedInUserAdmin,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return !!bookingUserOrgTeamMembership;
|
||||
}
|
||||
|
||||
async function getOrgIdsWhereAdmin(loggedInUserId: number) {
|
||||
const loggedInUserOrgMemberships = await prisma.membership.findMany({
|
||||
where: {
|
||||
userId: loggedInUserId,
|
||||
role: {
|
||||
in: [MembershipRole.OWNER, MembershipRole.ADMIN],
|
||||
},
|
||||
team: {
|
||||
parentId: null,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
teamId: true,
|
||||
},
|
||||
});
|
||||
|
||||
return loggedInUserOrgMemberships.map((m) => m.teamId);
|
||||
}
|
||||
Reference in new issue
Block a user