feat: report booking (#24324)
* feat: report booking * refactor: improvements * test: add unit test * test: add unit test * chore * refactor: feedback * refactor: feedback * refactor: feedback * fix: use string * fix: schema * chore: improvements * refactor: create service * fix: udate test * fix: feedback * fix: schema * fix: type * fix: type * refactor: address feedback * refactor: move to new file * refactor: move to new file * chor: remove * fix UserRepository import * fix type of bookingUid * fix: import path * fix: remove cancellation * chore: duplicate * fix: tests * refactor: feedback * chore: remove table from here * fix: types * fix: types --------- Co-authored-by: CarinaWolli <[email protected]>
This commit is contained in:
1 parent
a2bee76da6
commit
06d6c180d1
19 files changed
+1272
-4
No files matched your search
@@ -79,6 +79,7 @@ async function handler(input: CancelBookingInput) {
|
||||
cancelledBy,
|
||||
cancelSubsequentBookings,
|
||||
internalNote,
|
||||
skipCancellationReasonValidation = false,
|
||||
} = bookingCancelInput.parse(body);
|
||||
const bookingToDelete = await getBookingToDelete(id, uid);
|
||||
const {
|
||||
@@ -117,7 +118,7 @@ async function handler(input: CancelBookingInput) {
|
||||
const isCancellationUserHost =
|
||||
bookingToDelete.userId == userId || bookingToDelete.user.email === cancelledBy;
|
||||
|
||||
if (!platformClientId && !cancellationReason?.trim() && isCancellationUserHost) {
|
||||
if (!platformClientId && !cancellationReason?.trim() && isCancellationUserHost && !skipCancellationReasonValidation) {
|
||||
throw new HttpError({
|
||||
statusCode: 400,
|
||||
message: "Cancellation reason is required when you are the host",
|
||||
|
||||
@@ -131,6 +131,65 @@ export class BookingRepository {
|
||||
});
|
||||
}
|
||||
|
||||
async getBookingWithEventTypeTeamId({ bookingId }: { bookingId: number }) {
|
||||
return await this.prismaClient.booking.findUnique({
|
||||
where: {
|
||||
id: bookingId,
|
||||
},
|
||||
select: {
|
||||
userId: true,
|
||||
eventType: {
|
||||
select: {
|
||||
teamId: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findByUidIncludeEventType({ bookingUid }: { bookingUid: string }) {
|
||||
return await this.prismaClient.booking.findUnique({
|
||||
where: {
|
||||
uid: bookingUid,
|
||||
},
|
||||
select: {
|
||||
userId: true,
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
attendees: {
|
||||
select: {
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
eventType: {
|
||||
select: {
|
||||
teamId: true,
|
||||
hosts: {
|
||||
select: {
|
||||
userId: true,
|
||||
user: {
|
||||
select: {
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
users: {
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/** Determines if the user is the organizer, team admin, or org admin that the booking was created under */
|
||||
async doesUserIdHaveAccessToBooking({ userId, bookingId }: { userId: number; bookingId: number }) {
|
||||
const booking = await this.prismaClient.booking.findUnique({
|
||||
@@ -187,6 +246,59 @@ export class BookingRepository {
|
||||
});
|
||||
}
|
||||
|
||||
async findByUidIncludeReport({ bookingUid }: { bookingUid: string }) {
|
||||
return await this.prismaClient.booking.findUnique({
|
||||
where: {
|
||||
uid: bookingUid,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
uid: true,
|
||||
startTime: true,
|
||||
status: true,
|
||||
recurringEventId: true,
|
||||
attendees: {
|
||||
select: {
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
seatsReferences: {
|
||||
select: {
|
||||
referenceUid: true,
|
||||
attendee: {
|
||||
select: {
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
report: {
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async getActiveRecurringBookingsFromDate({
|
||||
recurringEventId,
|
||||
fromDate,
|
||||
}: {
|
||||
recurringEventId: string;
|
||||
fromDate: Date;
|
||||
}) {
|
||||
return await this.prismaClient.booking.findMany({
|
||||
where: {
|
||||
recurringEventId,
|
||||
startTime: { gte: fromDate },
|
||||
status: { in: [BookingStatus.ACCEPTED, BookingStatus.PENDING] },
|
||||
},
|
||||
select: { id: true, uid: true },
|
||||
orderBy: { startTime: "asc" },
|
||||
});
|
||||
}
|
||||
|
||||
private async _findAllExistingBookingsForEventTypeBetween({
|
||||
eventTypeId,
|
||||
seatedEvent = false,
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
import { UserRepository } from "@calcom/features/users/repositories/UserRepository";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
|
||||
import { BookingRepository } from "../repositories/BookingRepository";
|
||||
|
||||
type BookingForAccessCheck = NonNullable<Awaited<ReturnType<BookingRepository["findByUidIncludeEventType"]>>>;
|
||||
|
||||
export class BookingAccessService {
|
||||
constructor(private prismaClient: PrismaClient) {}
|
||||
|
||||
private isUserAHost(userId: number, booking: BookingForAccessCheck): boolean {
|
||||
const hostMap = new Map<number, { id: number; email: string }>();
|
||||
|
||||
const addHost = (id: number, email: string) => {
|
||||
if (!hostMap.has(id)) {
|
||||
hostMap.set(id, { id, email });
|
||||
}
|
||||
};
|
||||
|
||||
booking?.eventType?.hosts?.forEach((host: { userId: number; user: { email: string } }) =>
|
||||
addHost(host.userId, host.user.email)
|
||||
);
|
||||
booking?.eventType?.users?.forEach((user: { id: number; email: string }) => addHost(user.id, user.email));
|
||||
|
||||
if (booking?.user?.id && booking?.user?.email) {
|
||||
addHost(booking.user.id, booking.user.email);
|
||||
}
|
||||
|
||||
const attendeeEmails = new Set(booking.attendees?.map((attendee: { email: string }) => attendee.email));
|
||||
const filteredHosts = Array.from(hostMap.values()).filter(
|
||||
(host) => attendeeEmails.has(host.email) || host.id === booking.user?.id
|
||||
);
|
||||
|
||||
return filteredHosts.some((host) => host.id === userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a user has access to a booking based on:
|
||||
* 1. Being the booking organizer
|
||||
* 2. Being one of the hosts in a multi-host booking
|
||||
* 3. Being a team/org admin where the event type belongs
|
||||
* 4. Being an org admin where the booking organizer belongs (for personal bookings)
|
||||
* 5. Being a team admin of any team the booking organizer belongs to (for personal bookings)
|
||||
*/
|
||||
async doesUserIdHaveAccessToBooking({
|
||||
userId,
|
||||
bookingUid,
|
||||
}: {
|
||||
userId: number;
|
||||
bookingUid: string;
|
||||
}): Promise<boolean> {
|
||||
const bookingRepo = new BookingRepository(this.prismaClient);
|
||||
const userRepo = new UserRepository(this.prismaClient);
|
||||
|
||||
const booking = await bookingRepo.findByUidIncludeEventType({ bookingUid });
|
||||
|
||||
if (!booking) return false;
|
||||
|
||||
if (userId === booking.userId) return true;
|
||||
|
||||
if (this.isUserAHost(userId, booking)) return true;
|
||||
|
||||
// If booking has a teamId, check if user is admin of that team/org
|
||||
if (booking.eventType?.teamId) {
|
||||
const isAdminOrUser = await userRepo.isAdminOfTeamOrParentOrg({
|
||||
userId,
|
||||
teamId: booking.eventType.teamId,
|
||||
});
|
||||
return isAdminOrUser;
|
||||
}
|
||||
|
||||
if (!booking.userId) return false;
|
||||
|
||||
const bookingOwner = await userRepo.getUserOrganizationAndTeams({ userId: booking.userId });
|
||||
|
||||
if (!bookingOwner) return false;
|
||||
|
||||
// Check if user is admin of booking organizer's organization
|
||||
if (bookingOwner.organizationId) {
|
||||
const isOrgAdmin = await userRepo.isAdminOfTeamOrParentOrg({
|
||||
userId,
|
||||
teamId: bookingOwner.organizationId,
|
||||
});
|
||||
if (isOrgAdmin) return true;
|
||||
}
|
||||
|
||||
// Check if user is admin of any team the booking organizer belongs to
|
||||
for (const membership of bookingOwner.teams) {
|
||||
const isTeamAdmin = await userRepo.isAdminOfTeamOrParentOrg({
|
||||
userId,
|
||||
teamId: membership.teamId,
|
||||
});
|
||||
if (isTeamAdmin) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -770,6 +770,19 @@ export class UserRepository {
|
||||
});
|
||||
return !!isAdminOrOwnerOfTeam;
|
||||
}
|
||||
|
||||
async getUserOrganizationAndTeams({ userId }: { userId: number }) {
|
||||
return await this.prismaClient.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: {
|
||||
organizationId: true,
|
||||
teams: {
|
||||
where: { accepted: true },
|
||||
select: { teamId: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
async getTimeZoneAndDefaultScheduleId({ userId }: { userId: number }) {
|
||||
return await this.prismaClient.user.findUnique({
|
||||
where: {
|
||||
|
||||
Reference in new issue
Block a user