f1011ddd08
* feat(booking-audit): extract core audit system changes from PR 25125 This PR extracts core audit infrastructure changes without integration changes: 1. New action services introduced: - SeatBookedAuditActionService - SeatRescheduledAuditActionService 2. Simplification of ActionService interface: - Streamlined IAuditActionService interface - Reduced TypeScript burden with cleaner type definitions 3. ActionSource support: - Added BookingAuditSource enum (API_V1, API_V2, WEBAPP, WEBHOOK, UNKNOWN) - Added source and operationId fields to BookingAudit model 4. New AuditAction types: - SEAT_BOOKED - SEAT_RESCHEDULED - APP actor type 5. New BookingAuditAccessService: - Permission-based access control for audit logs - Added readTeamAuditLogs and readOrgAuditLogs permissions 6. Fixes in the logs viewer flow: - Enhanced BookingAuditViewerService with improved filtering - Local AttendeeRepository for actor enrichment Changes are contained within packages/features/booking-audit with minimal outside changes (permission registry only). Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * refactor(booking-audit): streamline action handling and enhance localization - Replaced action icon retrieval with a mapping object for improved clarity and performance. - Introduced constants for actor role labels to simplify role retrieval. - Added new localization strings for audit log permission errors and organization requirements. - Updated various service and repository interfaces to enhance type safety and clarity. - Removed deprecated architecture documentation and adjusted related imports for consistency. These changes aim to improve code maintainability and user experience in the booking audit system. * fix(booking-audit): enhance actor role localization and operation ID tracking - Updated actor role labels in the booking logs view to use lowercase for consistency. - Improved localization by wrapping actor role display in a translation function. - Added operationId field to audit logs for better correlation of actions across multiple bookings. - Enhanced BookingAuditViewerService to include operationId in enriched audit logs. - Updated integration tests to verify consistent operationId across related audit logs. These changes aim to improve localization accuracy and facilitate better tracking of user actions in the booking audit system. * feat: integrate credential repository and enhance app actor handling - Added CredentialRepository to manage app credentials, including a method to find credentials by ID. - Updated BookingAudit system to support app actors identified by credential ID, improving actor attribution and audit clarity. - Introduced a new utility function to map app slugs to display names, enhancing the user experience in audit logs. - Modified relevant interfaces and types to accommodate the new credential handling and app actor structure. - Enhanced BookingAuditViewerService to display app names based on credentials, ensuring accurate representation in audit logs. --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
339 lines
12 KiB
TypeScript
339 lines
12 KiB
TypeScript
import type { BookingAuditProducerService } from "@calcom/features/booking-audit/lib/service/BookingAuditProducerService.interface";
|
|
import type { AcceptedAuditData } from "@calcom/features/booking-audit/lib/actions/AcceptedAuditActionService";
|
|
import type { CancelledAuditData } from "@calcom/features/booking-audit/lib/actions/CancelledAuditActionService";
|
|
import type { RejectedAuditData } from "@calcom/features/booking-audit/lib/actions/RejectedAuditActionService";
|
|
import type { RescheduleRequestedAuditData } from "@calcom/features/booking-audit/lib/actions/RescheduleRequestedAuditActionService";
|
|
import type { AttendeeAddedAuditData } from "@calcom/features/booking-audit/lib/actions/AttendeeAddedAuditActionService";
|
|
import type { AttendeeRemovedAuditData } from "@calcom/features/booking-audit/lib/actions/AttendeeRemovedAuditActionService";
|
|
import type { ReassignmentAuditData } from "@calcom/features/booking-audit/lib/actions/ReassignmentAuditActionService";
|
|
import type { LocationChangedAuditData } from "@calcom/features/booking-audit/lib/actions/LocationChangedAuditActionService";
|
|
import type { HostNoShowUpdatedAuditData } from "@calcom/features/booking-audit/lib/actions/HostNoShowUpdatedAuditActionService";
|
|
import type { AttendeeNoShowUpdatedAuditData } from "@calcom/features/booking-audit/lib/actions/AttendeeNoShowUpdatedAuditActionService";
|
|
import type { SeatBookedAuditData } from "@calcom/features/booking-audit/lib/actions/SeatBookedAuditActionService";
|
|
import type { SeatRescheduledAuditData } from "@calcom/features/booking-audit/lib/actions/SeatRescheduledAuditActionService";
|
|
import type { CreatedAuditData } from "@calcom/features/booking-audit/lib/actions/CreatedAuditActionService";
|
|
import type { RescheduledAuditData } from "@calcom/features/booking-audit/lib/actions/RescheduledAuditActionService";
|
|
import type { ActionSource } from "@calcom/features/booking-audit/lib/types/actionSource";
|
|
import type { HashedLinkService } from "@calcom/features/hashedLink/lib/service/HashedLinkService";
|
|
import type { ISimpleLogger } from "@calcom/features/di/shared/services/logger.service";
|
|
import { safeStringify } from "@calcom/lib/safeStringify";
|
|
import type { Actor } from "../types/actor";
|
|
import type { BookingCreatedPayload, BookingRescheduledPayload } from "./types";
|
|
|
|
interface BookingEventHandlerDeps {
|
|
log: ISimpleLogger;
|
|
hashedLinkService: HashedLinkService;
|
|
bookingAuditProducerService: BookingAuditProducerService;
|
|
}
|
|
|
|
interface OnBookingCreatedParams {
|
|
payload: BookingCreatedPayload;
|
|
actor: Actor;
|
|
auditData: CreatedAuditData;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
}
|
|
|
|
interface OnBookingRescheduledParams {
|
|
payload: BookingRescheduledPayload;
|
|
actor: Actor;
|
|
auditData: RescheduledAuditData;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
}
|
|
|
|
interface BaseBookingEventParams<TAuditData> {
|
|
bookingUid: string;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
auditData: TAuditData;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
}
|
|
|
|
type OnBookingAcceptedParams = BaseBookingEventParams<AcceptedAuditData>;
|
|
type OnBookingCancelledParams = BaseBookingEventParams<CancelledAuditData>;
|
|
type OnRescheduleRequestedParams = BaseBookingEventParams<RescheduleRequestedAuditData>;
|
|
type OnAttendeeAddedParams = BaseBookingEventParams<AttendeeAddedAuditData>;
|
|
type OnHostNoShowUpdatedParams = BaseBookingEventParams<HostNoShowUpdatedAuditData>;
|
|
type OnBookingRejectedParams = BaseBookingEventParams<RejectedAuditData>;
|
|
type OnAttendeeRemovedParams = BaseBookingEventParams<AttendeeRemovedAuditData>;
|
|
type OnReassignmentParams = BaseBookingEventParams<ReassignmentAuditData>;
|
|
type OnLocationChangedParams = BaseBookingEventParams<LocationChangedAuditData>;
|
|
type OnAttendeeNoShowUpdatedParams = BaseBookingEventParams<AttendeeNoShowUpdatedAuditData>;
|
|
type OnSeatBookedParams = BaseBookingEventParams<SeatBookedAuditData>;
|
|
type OnSeatRescheduledParams = BaseBookingEventParams<SeatRescheduledAuditData>;
|
|
|
|
export class BookingEventHandlerService {
|
|
private readonly log: BookingEventHandlerDeps["log"];
|
|
private readonly bookingAuditProducerService: BookingEventHandlerDeps["bookingAuditProducerService"];
|
|
|
|
constructor(private readonly deps: BookingEventHandlerDeps) {
|
|
this.log = deps.log;
|
|
this.bookingAuditProducerService = deps.bookingAuditProducerService;
|
|
}
|
|
|
|
async onBookingCreated(params: OnBookingCreatedParams) {
|
|
const { payload, actor, auditData, source, operationId } = params;
|
|
this.log.debug("onBookingCreated", safeStringify(payload));
|
|
if (payload.config.isDryRun) {
|
|
return;
|
|
}
|
|
await this.onBookingCreatedOrRescheduled(payload);
|
|
await this.deps.bookingAuditProducerService.queueCreatedAudit({
|
|
bookingUid: payload.booking.uid,
|
|
actor,
|
|
organizationId: payload.organizationId,
|
|
source,
|
|
operationId,
|
|
data: auditData,
|
|
});
|
|
}
|
|
|
|
async onBookingRescheduled(params: OnBookingRescheduledParams) {
|
|
const { payload, actor, auditData, source, operationId } = params;
|
|
this.log.debug("onBookingRescheduled", safeStringify(payload));
|
|
if (payload.config.isDryRun) {
|
|
return;
|
|
}
|
|
await this.onBookingCreatedOrRescheduled(payload);
|
|
|
|
await this.bookingAuditProducerService.queueRescheduledAudit({
|
|
// In case of rescheduled booking, we send old booking uid because the action took place on that booking only
|
|
bookingUid: payload.oldBooking.uid,
|
|
actor,
|
|
organizationId: payload.organizationId,
|
|
source,
|
|
operationId,
|
|
data: auditData,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Handles common tasks that need to be executed in both booking created and rescheduled events
|
|
* A dedicated place because there are many tasks that need to be executed in both events.
|
|
*/
|
|
private async onBookingCreatedOrRescheduled(payload: BookingCreatedPayload | BookingRescheduledPayload) {
|
|
const results = await Promise.allSettled([
|
|
// TODO: Migrate other post-booking tasks here, to execute them in parallel, without affecting each other
|
|
this.updatePrivateLinkUsage(payload.bookingFormData.hashedLink),
|
|
]);
|
|
results.forEach((result) => {
|
|
if (result.status === "rejected") {
|
|
this.log.error(
|
|
"Error while executing onBookingCreatedOrRescheduled task",
|
|
safeStringify(result.reason)
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
private async updatePrivateLinkUsage(hashedLink: string | null) {
|
|
try {
|
|
if (hashedLink) {
|
|
await this.deps.hashedLinkService.validateAndIncrementUsage(hashedLink);
|
|
}
|
|
} catch (error) {
|
|
this.log.error("Error while updating hashed link", safeStringify(error));
|
|
}
|
|
}
|
|
|
|
async onBookingAccepted(params: OnBookingAcceptedParams) {
|
|
const { bookingUid, actor, organizationId, auditData, source, operationId } = params;
|
|
await this.bookingAuditProducerService.queueAcceptedAudit({
|
|
bookingUid,
|
|
actor,
|
|
organizationId,
|
|
source,
|
|
operationId,
|
|
data: auditData,
|
|
});
|
|
}
|
|
|
|
async onBookingCancelled(params: OnBookingCancelledParams) {
|
|
const { bookingUid, actor, organizationId, auditData, source, operationId } = params;
|
|
await this.bookingAuditProducerService.queueCancelledAudit({
|
|
bookingUid,
|
|
actor,
|
|
organizationId,
|
|
source,
|
|
operationId,
|
|
data: auditData,
|
|
});
|
|
}
|
|
|
|
async onRescheduleRequested(params: OnRescheduleRequestedParams) {
|
|
const { bookingUid, actor, organizationId, auditData, source, operationId } = params;
|
|
await this.bookingAuditProducerService.queueRescheduleRequestedAudit({
|
|
bookingUid,
|
|
actor,
|
|
organizationId,
|
|
source,
|
|
operationId,
|
|
data: auditData,
|
|
});
|
|
}
|
|
|
|
async onAttendeeAdded(params: OnAttendeeAddedParams) {
|
|
const { bookingUid, actor, organizationId, auditData, source, operationId } = params;
|
|
await this.bookingAuditProducerService.queueAttendeeAddedAudit({
|
|
bookingUid,
|
|
actor,
|
|
organizationId,
|
|
source,
|
|
operationId,
|
|
data: auditData,
|
|
});
|
|
}
|
|
|
|
async onHostNoShowUpdated(params: OnHostNoShowUpdatedParams) {
|
|
const { bookingUid, actor, organizationId, auditData, source, operationId } = params;
|
|
await this.bookingAuditProducerService.queueHostNoShowUpdatedAudit({
|
|
bookingUid,
|
|
actor,
|
|
organizationId,
|
|
source,
|
|
operationId,
|
|
data: auditData,
|
|
});
|
|
}
|
|
|
|
async onBookingRejected(params: OnBookingRejectedParams) {
|
|
const { bookingUid, actor, organizationId, auditData, source, operationId } = params;
|
|
await this.bookingAuditProducerService.queueRejectedAudit({
|
|
bookingUid,
|
|
actor,
|
|
organizationId,
|
|
source,
|
|
operationId,
|
|
data: auditData,
|
|
});
|
|
}
|
|
|
|
async onAttendeeRemoved(params: OnAttendeeRemovedParams) {
|
|
const { bookingUid, actor, organizationId, auditData, source, operationId } = params;
|
|
await this.bookingAuditProducerService.queueAttendeeRemovedAudit({
|
|
bookingUid,
|
|
actor,
|
|
organizationId,
|
|
source,
|
|
operationId,
|
|
data: auditData,
|
|
});
|
|
}
|
|
|
|
async onReassignment(params: OnReassignmentParams) {
|
|
const { bookingUid, actor, organizationId, auditData, source, operationId } = params;
|
|
await this.bookingAuditProducerService.queueReassignmentAudit({
|
|
bookingUid,
|
|
actor,
|
|
organizationId,
|
|
source,
|
|
operationId,
|
|
data: auditData,
|
|
});
|
|
}
|
|
|
|
async onLocationChanged(params: OnLocationChangedParams) {
|
|
const { bookingUid, actor, organizationId, auditData, source, operationId } = params;
|
|
await this.bookingAuditProducerService.queueLocationChangedAudit({
|
|
bookingUid,
|
|
actor,
|
|
organizationId,
|
|
source,
|
|
operationId,
|
|
data: auditData,
|
|
});
|
|
}
|
|
|
|
async onAttendeeNoShowUpdated(params: OnAttendeeNoShowUpdatedParams) {
|
|
const { bookingUid, actor, organizationId, auditData, source, operationId } = params;
|
|
await this.bookingAuditProducerService.queueAttendeeNoShowUpdatedAudit({
|
|
bookingUid,
|
|
actor,
|
|
organizationId,
|
|
source,
|
|
operationId,
|
|
data: auditData,
|
|
});
|
|
}
|
|
|
|
async onSeatBooked(params: OnSeatBookedParams) {
|
|
const { bookingUid, actor, organizationId, auditData, source, operationId } = params;
|
|
await this.bookingAuditProducerService.queueSeatBookedAudit({
|
|
bookingUid,
|
|
actor,
|
|
organizationId,
|
|
source,
|
|
operationId,
|
|
data: auditData,
|
|
});
|
|
}
|
|
|
|
async onSeatRescheduled(params: OnSeatRescheduledParams) {
|
|
const { bookingUid, actor, organizationId, auditData, source, operationId } = params;
|
|
await this.bookingAuditProducerService.queueSeatRescheduledAudit({
|
|
bookingUid,
|
|
actor,
|
|
organizationId,
|
|
source,
|
|
operationId,
|
|
data: auditData,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Handles bulk booking acceptance for recurring bookings
|
|
* Creates a single task that will be processed to create multiple audit logs atomically
|
|
*/
|
|
async onBulkBookingsAccepted(params: {
|
|
bookings: Array<{
|
|
bookingUid: string;
|
|
auditData: AcceptedAuditData;
|
|
}>;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
operationId?: string | null;
|
|
source: ActionSource;
|
|
}) {
|
|
const { bookings, actor, organizationId, operationId, source } = params;
|
|
await this.bookingAuditProducerService.queueBulkAcceptedAudit({
|
|
bookings: bookings.map((booking) => ({
|
|
bookingUid: booking.bookingUid,
|
|
data: booking.auditData,
|
|
})),
|
|
actor,
|
|
organizationId,
|
|
source,
|
|
operationId,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Handles bulk booking cancellation for recurring bookings
|
|
* Creates a single task that will be processed to create multiple audit logs atomically
|
|
*/
|
|
async onBulkBookingsCancelled(params: {
|
|
bookings: Array<{
|
|
bookingUid: string;
|
|
auditData: CancelledAuditData;
|
|
}>;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
operationId?: string | null;
|
|
source: ActionSource;
|
|
}) {
|
|
const { bookings, actor, organizationId, operationId, source } = params;
|
|
await this.bookingAuditProducerService.queueBulkCancelledAudit({
|
|
bookings: bookings.map((booking) => ({
|
|
bookingUid: booking.bookingUid,
|
|
data: booking.auditData,
|
|
})),
|
|
actor,
|
|
organizationId,
|
|
source,
|
|
operationId,
|
|
});
|
|
}
|
|
}
|