* chore: Integrate booking confirmation booking audit Co-Authored-By: [email protected] <[email protected]> * fix: Use BookingStatus type instead of string for booking.status Co-Authored-By: [email protected] <[email protected]> * Make booking-audit integration test utils reusable * refactor: enhance handlePaymentSuccess to accept appSlug and actor identification - Updated handlePaymentSuccess function to accept an object with paymentId, bookingId, appSlug, and traceContext. - Introduced getActor function to determine the actor based on appSlug and credentialId. - Modified webhook handlers for Alby, BTCPayServer, HitPay, PayPal, and Stripe to pass the new parameters. - Improved logging for missing credentialId in payment processing. - Adjusted related schemas to ensure proper type handling for booking status and actor identification. * fix: Correct import paths and getActor function call Co-Authored-By: [email protected] <[email protected]> * fix: Use ActorIdentification type instead of AuditActor in getActor Co-Authored-By: [email protected] <[email protected]> * fix: Add guard clause for undefined actor in fireBookingAcceptedEvent Co-Authored-By: [email protected] <[email protected]> * fix: Add actionSource to all confirm calls Co-Authored-By: [email protected] <[email protected]> * refactor: Integrate actor identification and action source updates across booking services - Added `makeUserActor` to various booking service files to enhance actor identification. - Updated action source handling in booking confirmation and related functions to include "MAGIC_LINK". - Refactored schemas to accommodate new actor and action source requirements, ensuring consistent type handling. - Improved error handling and logging for booking actions to enhance traceability. * Remvoe formatting changes * Add test * refactor: Enhance booking confirmation tests and event handling - Updated `confirm.handler.test.ts` to improve test coverage for booking acceptance and rejection scenarios, including bulk bookings. - Refactored the mock implementation of `BookingEventHandlerService` to streamline event handling for accepted and rejected bookings. - Adjusted the `handleConfirmation` function to utilize `acceptedBookings` for better clarity and maintainability. - Added tests to ensure correct invocation of event handler methods for both single and bulk booking confirmations and rejections. * fix tests * refactor: Use confirmHandler directly in link and verify-booking-token routes Refactors the link and verify-booking-token API routes to call confirmHandler directly instead of using the tRPC caller pattern. This simplifies the code by removing the need for session getter, legacy request building, and context creation. Changes: - apps/web/app/api/link/route.ts: Use confirmHandler directly with ctx and input - apps/web/app/api/verify-booking-token/route.ts: Use confirmHandler directly with ctx and input - Updated tests to mock confirmHandler instead of tRPC caller Co-Authored-By: [email protected] <[email protected]> * fix ts errors due to merge frommain * feat: introduce getAppActor utility for actor creation This commit adds a new utility function, getAppActor, to streamline the process of creating actor objects for apps based on their slug and credential ID. The function is integrated into handlePaymentSuccess and handleStripePaymentSuccess, replacing the previous inline actor creation logic. This refactor enhances code maintainability and readability by centralizing actor creation logic. Changes: - New file: packages/app-store/_utils/getAppActor.ts - Updated handlePaymentSuccess and handleStripePaymentSuccess to use getAppActor - Removed redundant actor creation code from these functions * refactor: Update appSlug in payment success handlers to use appConfig.slug This commit modifies the appSlug parameter in the handlePaymentSuccess function across multiple payment webhook handlers to utilize the appConfig.slug value instead of hardcoded strings. This change enhances consistency and maintainability of the code. Changes: - Updated appSlug in handlePaymentSuccess for btcpayserver, hitpay, and paypal webhooks. - Adjusted a test case to reflect the new appSlug value for consistency. * test: Enhance booking token verification and audit action tests This commit adds additional assertions to the booking token verification tests to ensure that the confirmHandler is not called when an error occurs. It also introduces a mechanism to clean up additional booking UIDs after each test in the accepted action integration tests, improving test reliability. Furthermore, it updates the action source schema comment for clarity. Changes: - Updated tests in `verify-booking-token` to check that `mockConfirmHandler` is not called on error. - Implemented cleanup logic for additional booking UIDs in `accepted-action.integration-test.ts`. - Clarified comment in `actionSource.ts` regarding the schema for action sources. - Refactored `handleConfirmation.ts` and `confirm.handler.ts` to include tracing logger for error handling in booking events. --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
421 lines
14 KiB
TypeScript
421 lines
14 KiB
TypeScript
import { z } from "zod";
|
|
import { v4 as uuidv4 } from "uuid";
|
|
import type { Tasker } from "@calcom/features/tasker/tasker";
|
|
import { IS_PRODUCTION } from "@calcom/lib/constants";
|
|
import { safeStringify } from "@calcom/lib/safeStringify";
|
|
import type { ISimpleLogger } from "@calcom/features/di/shared/services/logger.service";
|
|
|
|
import type { BookingAuditAction } from "../types/bookingAuditTask";
|
|
import type { ActionSource } from "../types/actionSource";
|
|
import type { PiiFreeActor, Actor, BookingAuditContext } from "../dto/types";
|
|
import { makeActorById, buildActorEmail } from "../makeActor";
|
|
import type { IAuditActorRepository } from "../repository/IAuditActorRepository";
|
|
import { AcceptedAuditActionService } from "../actions/AcceptedAuditActionService";
|
|
import { AttendeeAddedAuditActionService } from "../actions/AttendeeAddedAuditActionService";
|
|
import { AttendeeNoShowUpdatedAuditActionService } from "../actions/AttendeeNoShowUpdatedAuditActionService";
|
|
import { AttendeeRemovedAuditActionService } from "../actions/AttendeeRemovedAuditActionService";
|
|
import { CancelledAuditActionService } from "../actions/CancelledAuditActionService";
|
|
import { CreatedAuditActionService } from "../actions/CreatedAuditActionService";
|
|
import { HostNoShowUpdatedAuditActionService } from "../actions/HostNoShowUpdatedAuditActionService";
|
|
import { LocationChangedAuditActionService } from "../actions/LocationChangedAuditActionService";
|
|
import { ReassignmentAuditActionService } from "../actions/ReassignmentAuditActionService";
|
|
import { RejectedAuditActionService } from "../actions/RejectedAuditActionService";
|
|
import { RescheduleRequestedAuditActionService } from "../actions/RescheduleRequestedAuditActionService";
|
|
import { RescheduledAuditActionService } from "../actions/RescheduledAuditActionService";
|
|
import { SeatBookedAuditActionService } from "../actions/SeatBookedAuditActionService";
|
|
import { SeatRescheduledAuditActionService } from "../actions/SeatRescheduledAuditActionService";
|
|
import type { BookingAuditProducerService } from "./BookingAuditProducerService.interface";
|
|
|
|
interface BookingAuditTaskerProducerServiceDeps {
|
|
tasker: Tasker;
|
|
log: ISimpleLogger;
|
|
auditActorRepository: IAuditActorRepository;
|
|
}
|
|
|
|
/**
|
|
* BookingAuditTaskerProducerService - Tasker-based implementation of BookingAuditProducerService
|
|
*
|
|
* Producer that uses Tasker for local/background job processing.
|
|
* Task processing is handled by BookingAuditTaskConsumer.
|
|
*
|
|
* For future migration to trigger.dev, create BookingAuditTriggerProducerService
|
|
* that implements the same BookingAuditProducerService interface.
|
|
*/
|
|
export class BookingAuditTaskerProducerService implements BookingAuditProducerService {
|
|
private readonly tasker: Tasker;
|
|
private readonly log: BookingAuditTaskerProducerServiceDeps["log"];
|
|
private readonly auditActorRepository: IAuditActorRepository;
|
|
|
|
constructor(private readonly deps: BookingAuditTaskerProducerServiceDeps) {
|
|
this.tasker = deps.tasker;
|
|
this.log = deps.log;
|
|
this.auditActorRepository = deps.auditActorRepository;
|
|
}
|
|
|
|
private async getPIIFreeBookingAuditActor(params: {
|
|
actor: Actor;
|
|
}): Promise<PiiFreeActor> {
|
|
const { actor } = params;
|
|
|
|
if (actor.identifiedBy === "user" || actor.identifiedBy === "attendee" || actor.identifiedBy === "id") {
|
|
return actor;
|
|
}
|
|
|
|
if (actor.identifiedBy === "app") {
|
|
const piiFreeActor = await this.auditActorRepository.createIfNotExistsAppActor({
|
|
credentialId: actor.credentialId,
|
|
});
|
|
return makeActorById(piiFreeActor.id);
|
|
}
|
|
|
|
if (actor.identifiedBy === "appSlug") {
|
|
const email = buildActorEmail({ identifier: actor.appSlug, actorType: "app" });
|
|
const piiFreeActor = await this.auditActorRepository.createIfNotExistsAppActor({
|
|
email,
|
|
name: actor.name,
|
|
});
|
|
return makeActorById(piiFreeActor.id);
|
|
}
|
|
|
|
// Must be guest actor at this point
|
|
const piiFreeActor = await this.auditActorRepository.createIfNotExistsGuestActor({
|
|
email: actor.email,
|
|
name: actor.name ?? null,
|
|
phone: null,
|
|
});
|
|
return makeActorById(piiFreeActor.id);
|
|
}
|
|
|
|
/**
|
|
* Internal helper to queue audit task to Tasker
|
|
* @param params.action - Must be a valid BookingAuditAction value (TYPE from action services are string-typed)
|
|
* @param params.operationId - Optional operation ID for correlating bulk operations. If null, will be auto-generated.
|
|
*/
|
|
private async queueTask(params: {
|
|
bookingUid: string;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
action: string;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
data: unknown;
|
|
context?: BookingAuditContext;
|
|
}): Promise<void> {
|
|
// Skip queueing for non-organization bookings
|
|
if (params.organizationId === null) {
|
|
return;
|
|
}
|
|
if (IS_PRODUCTION) {
|
|
return;
|
|
}
|
|
try {
|
|
const piiFreeActor = await this.getPIIFreeBookingAuditActor({
|
|
actor: params.actor,
|
|
});
|
|
|
|
const operationId = params.operationId ?? uuidv4();
|
|
|
|
await this.tasker.create("bookingAudit", {
|
|
isBulk: false,
|
|
bookingUid: params.bookingUid,
|
|
actor: piiFreeActor,
|
|
organizationId: params.organizationId,
|
|
timestamp: Date.now(),
|
|
action: params.action as BookingAuditAction,
|
|
source: params.source,
|
|
operationId,
|
|
data: params.data,
|
|
context: params.context,
|
|
});
|
|
} catch (error) {
|
|
this.log.error(`Error while queueing ${params.action} audit`, safeStringify(error));
|
|
}
|
|
}
|
|
|
|
async queueCreatedAudit(params: {
|
|
bookingUid: string;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
data: z.infer<typeof CreatedAuditActionService.latestFieldsSchema>;
|
|
}): Promise<void> {
|
|
await this.queueTask({
|
|
...params,
|
|
action: CreatedAuditActionService.TYPE,
|
|
});
|
|
}
|
|
|
|
async queueRescheduledAudit(params: {
|
|
bookingUid: string;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
data: z.infer<typeof RescheduledAuditActionService.latestFieldsSchema>;
|
|
}): Promise<void> {
|
|
await this.queueTask({
|
|
...params,
|
|
action: RescheduledAuditActionService.TYPE,
|
|
});
|
|
}
|
|
|
|
async queueAcceptedAudit(params: {
|
|
bookingUid: string;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
data: z.infer<typeof AcceptedAuditActionService.latestFieldsSchema>;
|
|
}): Promise<void> {
|
|
await this.queueTask({
|
|
...params,
|
|
action: AcceptedAuditActionService.TYPE,
|
|
});
|
|
}
|
|
|
|
async queueCancelledAudit(params: {
|
|
bookingUid: string;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
data: z.infer<typeof CancelledAuditActionService.latestFieldsSchema>;
|
|
}): Promise<void> {
|
|
await this.queueTask({
|
|
...params,
|
|
action: CancelledAuditActionService.TYPE,
|
|
});
|
|
}
|
|
|
|
async queueRescheduleRequestedAudit(params: {
|
|
bookingUid: string;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
data: z.infer<typeof RescheduleRequestedAuditActionService.latestFieldsSchema>;
|
|
}): Promise<void> {
|
|
await this.queueTask({
|
|
...params,
|
|
action: RescheduleRequestedAuditActionService.TYPE,
|
|
});
|
|
}
|
|
|
|
async queueAttendeeAddedAudit(params: {
|
|
bookingUid: string;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
data: z.infer<typeof AttendeeAddedAuditActionService.latestFieldsSchema>;
|
|
}): Promise<void> {
|
|
await this.queueTask({
|
|
...params,
|
|
action: AttendeeAddedAuditActionService.TYPE,
|
|
});
|
|
}
|
|
|
|
async queueHostNoShowUpdatedAudit(params: {
|
|
bookingUid: string;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
data: z.infer<typeof HostNoShowUpdatedAuditActionService.latestFieldsSchema>;
|
|
}): Promise<void> {
|
|
await this.queueTask({
|
|
...params,
|
|
action: HostNoShowUpdatedAuditActionService.TYPE,
|
|
});
|
|
}
|
|
|
|
async queueRejectedAudit(params: {
|
|
bookingUid: string;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
data: z.infer<typeof RejectedAuditActionService.latestFieldsSchema>;
|
|
}): Promise<void> {
|
|
await this.queueTask({
|
|
...params,
|
|
action: RejectedAuditActionService.TYPE,
|
|
});
|
|
}
|
|
|
|
async queueAttendeeRemovedAudit(params: {
|
|
bookingUid: string;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
data: z.infer<typeof AttendeeRemovedAuditActionService.latestFieldsSchema>;
|
|
}): Promise<void> {
|
|
await this.queueTask({
|
|
...params,
|
|
action: AttendeeRemovedAuditActionService.TYPE,
|
|
});
|
|
}
|
|
|
|
async queueReassignmentAudit(params: {
|
|
bookingUid: string;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
data: z.infer<typeof ReassignmentAuditActionService.latestFieldsSchema>;
|
|
}): Promise<void> {
|
|
await this.queueTask({
|
|
...params,
|
|
action: ReassignmentAuditActionService.TYPE,
|
|
});
|
|
}
|
|
|
|
async queueLocationChangedAudit(params: {
|
|
bookingUid: string;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
data: z.infer<typeof LocationChangedAuditActionService.latestFieldsSchema>;
|
|
}): Promise<void> {
|
|
await this.queueTask({
|
|
...params,
|
|
action: LocationChangedAuditActionService.TYPE,
|
|
});
|
|
}
|
|
|
|
async queueAttendeeNoShowUpdatedAudit(params: {
|
|
bookingUid: string;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
data: z.infer<typeof AttendeeNoShowUpdatedAuditActionService.latestFieldsSchema>;
|
|
}): Promise<void> {
|
|
await this.queueTask({
|
|
...params,
|
|
action: AttendeeNoShowUpdatedAuditActionService.TYPE,
|
|
});
|
|
}
|
|
|
|
async queueSeatBookedAudit(params: {
|
|
bookingUid: string;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
data: z.infer<typeof SeatBookedAuditActionService.latestFieldsSchema>;
|
|
}): Promise<void> {
|
|
await this.queueTask({
|
|
...params,
|
|
action: SeatBookedAuditActionService.TYPE,
|
|
});
|
|
}
|
|
|
|
async queueSeatRescheduledAudit(params: {
|
|
bookingUid: string;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
data: z.infer<typeof SeatRescheduledAuditActionService.latestFieldsSchema>;
|
|
}): Promise<void> {
|
|
await this.queueTask({
|
|
...params,
|
|
action: SeatRescheduledAuditActionService.TYPE,
|
|
});
|
|
}
|
|
|
|
private async queueBulkTask(params: {
|
|
bookings: Array<{
|
|
bookingUid: string;
|
|
data: unknown;
|
|
}>;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
action: string;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
context?: BookingAuditContext;
|
|
}): Promise<void> {
|
|
// Skip queueing for non-organization bookings
|
|
if (params.organizationId === null) {
|
|
return;
|
|
}
|
|
if (IS_PRODUCTION) {
|
|
return;
|
|
}
|
|
try {
|
|
const piiFreeActor = await this.getPIIFreeBookingAuditActor({
|
|
actor: params.actor,
|
|
});
|
|
|
|
const operationId = params.operationId ?? uuidv4();
|
|
|
|
await this.tasker.create("bookingAudit", {
|
|
isBulk: true,
|
|
bookings: params.bookings,
|
|
actor: piiFreeActor,
|
|
organizationId: params.organizationId,
|
|
timestamp: Date.now(),
|
|
action: params.action as BookingAuditAction,
|
|
source: params.source,
|
|
operationId,
|
|
context: params.context,
|
|
});
|
|
} catch (error) {
|
|
this.log.error(`Error while queueing bulk ${params.action} audit`, safeStringify(error));
|
|
}
|
|
}
|
|
|
|
async queueBulkAcceptedAudit(params: {
|
|
bookings: Array<{
|
|
bookingUid: string;
|
|
data: z.infer<typeof AcceptedAuditActionService.latestFieldsSchema>;
|
|
}>;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
}): Promise<void> {
|
|
await this.queueBulkTask({
|
|
...params,
|
|
action: AcceptedAuditActionService.TYPE,
|
|
});
|
|
}
|
|
|
|
async queueBulkCancelledAudit(params: {
|
|
bookings: Array<{
|
|
bookingUid: string;
|
|
data: z.infer<typeof CancelledAuditActionService.latestFieldsSchema>;
|
|
}>;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
}): Promise<void> {
|
|
await this.queueBulkTask({
|
|
...params,
|
|
action: CancelledAuditActionService.TYPE,
|
|
});
|
|
}
|
|
|
|
async queueBulkRejectedAudit(params: {
|
|
bookings: Array<{
|
|
bookingUid: string;
|
|
data: z.infer<typeof RejectedAuditActionService.latestFieldsSchema>;
|
|
}>;
|
|
actor: Actor;
|
|
organizationId: number | null;
|
|
source: ActionSource;
|
|
operationId?: string | null;
|
|
}): Promise<void> {
|
|
await this.queueBulkTask({
|
|
...params,
|
|
action: RejectedAuditActionService.TYPE,
|
|
});
|
|
}
|
|
}
|