Files
calendar/packages/features/bookings/lib/onBookingEvents/BookingEventHandlerService.ts
T
Hariom BalharaGitHubhariom@cal.com <hariombalhara@gmail.com>Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>Morgan
82951cd04e chore: [Booking Flow Refactor - 5] Move post booking things to separate service BookingEventHandlerService starting with HashedLink usage handling (#24025)
* chore: Move post booking stuff to separate service

* refactor: improve ISP compliance in BookingEventHandlerService

- Refactor updatePrivateLinkUsage to only accept hashedLink parameter instead of full payload
- Remove shouldProcess function and inline isDryRun check for better clarity
- Addresses feedback from PR #24025

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

* define only what is used

* Define hashed-link-service as well in api-v2

* fix: export HashedLinkService through platform-libraries

- Add HashedLinkService to platform-libraries/private-links.ts
- Update API V2 to import from platform library instead of direct path
- Fixes MODULE_NOT_FOUND error in API V2 build

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
2025-10-23 15:05:55 +03:00

67 lines
2.2 KiB
TypeScript

import type { Logger } from "tslog";
import type { HashedLinkService } from "@calcom/features/hashedLink/lib/service/HashedLinkService";
import { safeStringify } from "@calcom/lib/safeStringify";
import type { BookingCreatedPayload, BookingRescheduledPayload } from "./types";
interface BookingEventHandlerDeps {
log: Logger<unknown>;
hashedLinkService: HashedLinkService;
}
export class BookingEventHandlerService {
private readonly log: BookingEventHandlerDeps["log"];
private readonly hashedLinkService: BookingEventHandlerDeps["hashedLinkService"];
constructor(private readonly deps: BookingEventHandlerDeps) {
this.log = deps.log;
this.hashedLinkService = deps.hashedLinkService;
}
async onBookingCreated(payload: BookingCreatedPayload) {
this.log.debug("onBookingCreated", safeStringify(payload));
if (payload.config.isDryRun) {
return;
}
await this.onBookingCreatedOrRescheduled(payload);
}
async onBookingRescheduled(payload: BookingRescheduledPayload) {
this.log.debug("onBookingRescheduled", safeStringify(payload));
if (payload.config.isDryRun) {
return;
}
await this.onBookingCreatedOrRescheduled(payload);
}
/**
* 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));
}
}
}