Files
calendar/packages/features/booking-audit/lib/service/BookingAuditAccessService.ts
T
98b6d63164 refactor: apply biome formatting to packages/features (#27844)
* refactor: apply biome formatting to packages/features (batch 1 - small subdirs)

Format small subdirectories in packages/features: di, flags, holidays, oauth,
settings, users, assignment-reason, selectedCalendar, hashedLink, host, form,
form-builder, availability, data-table, pbac, schedules, troubleshooter,
eventtypes, calendar-subscription, and root-level files.

Also includes straggler apps/web BookEventForm.tsx.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* refactor: apply biome formatting to packages/features (batch 2 - medium subdirs)

Format medium subdirectories in packages/features: auth, credentials,
calendars, routing-forms, routing-trace, attributes, watchlist, calAIPhone,
tasker, and webhooks.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* refactor: apply biome formatting to packages/features (batch 3 - bookings + insights)

Format bookings and insights subdirectories in packages/features.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* refactor: apply biome formatting to packages/features (batch 4 - ee)

Format packages/features/ee subdirectory covering billing, workflows,
organizations, teams, managed-event-types, round-robin, dsync,
integration-attribute-sync, and payments.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* refactor: apply biome formatting to packages/features (batch 5 - booking-audit part 1)

Format booking-audit di, actions, common, dto, repository, and types
subdirectories in packages/features/booking-audit.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* refactor: apply biome formatting to packages/features (batch 6 - booking-audit part 2)

Format booking-audit service subdirectory in packages/features/booking-audit.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 15:47:14 +01:00

107 lines
3.7 KiB
TypeScript

import type { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository";
import type { MembershipRepository } from "@calcom/features/membership/repositories/MembershipRepository";
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
import { MembershipRole } from "@calcom/prisma/enums";
export enum BookingAuditErrorCode {
ORGANIZATION_ID_REQUIRED = "ORGANIZATION_ID_REQUIRED",
BOOKING_NOT_FOUND_OR_PERMISSION_DENIED = "BOOKING_NOT_FOUND_OR_PERMISSION_DENIED",
BOOKING_HAS_NO_OWNER = "BOOKING_HAS_NO_OWNER",
OWNER_NOT_IN_ORGANIZATION = "OWNER_NOT_IN_ORGANIZATION",
PERMISSION_DENIED = "PERMISSION_DENIED",
}
export class BookingAuditPermissionError extends Error {
constructor(public readonly code: BookingAuditErrorCode) {
super(code);
this.name = "BookingAuditPermissionError";
}
}
interface BookingAuditAccessServiceDeps {
bookingRepository: BookingRepository;
membershipRepository: MembershipRepository;
}
/**
* BookingAuditAccessService - Service for checking access permissions to booking audit logs
* Audit logs are admin-only for compliance and security purposes.
* Regular users (including booking organizers and hosts) cannot view audit logs.
*/
export class BookingAuditAccessService {
private readonly bookingRepository: BookingRepository;
private readonly membershipRepository: MembershipRepository;
private readonly permissionCheckService: PermissionCheckService;
constructor(deps: BookingAuditAccessServiceDeps) {
this.bookingRepository = deps.bookingRepository;
this.membershipRepository = deps.membershipRepository;
this.permissionCheckService = new PermissionCheckService();
}
/**
* Check if user has permission to view audit logs for a booking
* Throws BookingAuditPermissionError if access is denied
*/
async assertPermissions({
bookingUid,
userId,
organizationId,
}: {
bookingUid: string;
userId: number;
organizationId: number | null;
}): Promise<void> {
if (!organizationId) {
throw new BookingAuditPermissionError(BookingAuditErrorCode.ORGANIZATION_ID_REQUIRED);
}
const booking = await this.bookingRepository.findByUidIncludeEventType({ bookingUid });
if (!booking) {
throw new BookingAuditPermissionError(BookingAuditErrorCode.BOOKING_NOT_FOUND_OR_PERMISSION_DENIED);
}
const bookingEventType = booking.eventType;
const bookingEventTypeTeamId = bookingEventType?.teamId ?? bookingEventType?.parent?.teamId;
if (bookingEventTypeTeamId) {
const hasAccess = await this.permissionCheckService.checkPermission({
userId,
teamId: bookingEventTypeTeamId,
permission: "booking.readTeamAuditLogs",
fallbackRoles: [MembershipRole.OWNER, MembershipRole.ADMIN],
});
if (hasAccess) {
return;
}
}
const bookingOwnerId = booking.userId;
if (!bookingOwnerId) {
throw new BookingAuditPermissionError(BookingAuditErrorCode.BOOKING_HAS_NO_OWNER);
}
const isBookingOwnerMemberOfOrganization = await this.membershipRepository.hasMembership({
userId: bookingOwnerId,
teamId: organizationId,
});
if (!isBookingOwnerMemberOfOrganization) {
throw new BookingAuditPermissionError(BookingAuditErrorCode.OWNER_NOT_IN_ORGANIZATION);
}
const hasAccess = await this.permissionCheckService.checkPermission({
userId,
teamId: organizationId,
permission: "booking.readOrgAuditLogs",
fallbackRoles: [MembershipRole.OWNER, MembershipRole.ADMIN],
});
if (hasAccess) {
return;
}
throw new BookingAuditPermissionError(BookingAuditErrorCode.PERMISSION_DENIED);
}
}