Files
cal.diy/packages/features/booking-audit/lib/service/BookingHistoryViewerService.ts
T
Benny JooandDevin AI 96bec9f7b9 refactor: Move repositories from @calcom/lib to @calcom/features domain folders (#27570)
* refactor: move repositories from lib to features domain folders

- Move HolidayRepository to features/holidays/repositories
- Move PrismaTrackingRepository to features/bookings/repositories
- Move PrismaBookingPaymentRepository to features/bookings/repositories
- Move PrismaRoutingFormResponseRepository to features/routing-forms/repositories
- Move PrismaAssignmentReasonRepository to features/assignment-reason/repositories
- Move VerificationTokenRepository to features/auth/repositories
- Move WorkspacePlatformRepository to features/workspace-platform/repositories
- Move DTO files to their respective feature domains
- Merge lib DestinationCalendarRepository into features version
- Merge lib SelectedCalendarRepository into features version
- Update all import paths across the codebase

This follows the vertical slice architecture pattern by organizing
repositories by domain rather than by technical layer.

Co-Authored-By: [email protected] <[email protected]>

* fix: update VerificationTokenService import path to new location

Co-Authored-By: [email protected] <[email protected]>

* fix: update test file imports to use new repository locations

Co-Authored-By: [email protected] <[email protected]>

* mv

* fix structure

* fix

* refactor: merge unit tests for SelectedCalendarRepository into single file

Co-Authored-By: [email protected] <[email protected]>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-02-09 08:41:04 -03:00

106 lines
4.3 KiB
TypeScript

import type { RoutingFormResponseRepositoryInterface } from "@calcom/features/routing-forms/repositories/RoutingFormResponseRepository.interface";
import type { BookingAuditViewerService, DisplayBookingAuditLog } from "./BookingAuditViewerService";
import { getFieldResponseByIdentifier } from "@calcom/features/routing-forms/lib/getFieldResponseByIdentifier";
type GetHistoryForBookingParams = {
bookingUid: string;
userId: number;
userEmail: string;
userTimeZone: string;
organizationId: number | null;
};
type BookingHistoryLog = DisplayBookingAuditLog;
interface BookingHistoryViewerServiceDeps {
bookingAuditViewerService: BookingAuditViewerService;
routingFormResponseRepository: RoutingFormResponseRepositoryInterface;
}
export class BookingHistoryViewerService {
private readonly bookingAuditViewerService: BookingAuditViewerService;
private readonly routingFormResponseRepository: RoutingFormResponseRepositoryInterface;
constructor(private readonly deps: BookingHistoryViewerServiceDeps) {
this.bookingAuditViewerService = deps.bookingAuditViewerService;
this.routingFormResponseRepository = deps.routingFormResponseRepository;
}
private sortLogsReverseChronologically(historyLogs: BookingHistoryLog[]): BookingHistoryLog[] {
return historyLogs.sort((a, b) => {
const timestampA = new Date(a.timestamp).getTime();
const timestampB = new Date(b.timestamp).getTime();
return timestampB - timestampA;
});
}
private async getFormAuditLogsForBooking(bookingUid: string): Promise<BookingHistoryLog[]> {
// TODO: Form doesn't have its Audit Logs yet, so we replicate them using the Form Response directly for now.
const formResponse = await this.routingFormResponseRepository.findByBookingUidIncludeForm(bookingUid);
if (!formResponse) {
return [];
}
return [this.createFormSubmissionEntry({ formResponse, bookingUid })];
}
async getHistoryForBooking(
params: GetHistoryForBookingParams
): Promise<{ bookingUid: string; auditLogs: BookingHistoryLog[] }> {
const { bookingUid } = params;
const { auditLogs: bookingAuditLogs } = await this.bookingAuditViewerService.getAuditLogsForBooking(params);
const historyEntries: BookingHistoryLog[] = [...bookingAuditLogs, ...await this.getFormAuditLogsForBooking(bookingUid)];
const sortedLogs = this.sortLogsReverseChronologically(historyEntries);
return {
bookingUid,
auditLogs: sortedLogs,
};
}
private createFormSubmissionEntry({
formResponse,
bookingUid,
}: {
formResponse: NonNullable<
Awaited<ReturnType<RoutingFormResponseRepositoryInterface["findByBookingUidIncludeForm"]>>
>;
bookingUid: string;
}): BookingHistoryLog {
const timestamp = formResponse.createdAt.toISOString();
const emailFieldResult = getFieldResponseByIdentifier({ responsePayload: formResponse.response, formFields: formResponse.form.fields, identifier: "email" });
const emailFieldValueFromResponse = emailFieldResult.success ? emailFieldResult.data : null;
// A valid string can be the email otherwise we assume it is not an email
const submitterEmail = typeof emailFieldValueFromResponse === "string" ? emailFieldValueFromResponse : null;
const uniqueId = `form-submission-${formResponse.id}`;
return {
id: uniqueId,
bookingUid,
type: "RECORD_CREATED",
action: "CREATED",
timestamp,
createdAt: timestamp,
source: "WEBAPP",
operationId: uniqueId,
displayJson: null,
actionDisplayTitle: { key: "form_submitted" },
displayFields: null,
actor: {
id: `form-submission-actor-${formResponse.id}`,
type: "GUEST",
userUuid: null,
attendeeId: null,
name: null,
createdAt: formResponse.createdAt,
displayName: submitterEmail ? `${submitterEmail}` : "Guest",
displayEmail: submitterEmail || null,
displayAvatar: null,
},
};
}
}