* feat: add time-based cancellation fees for no-show fee events - Add configurable time threshold (minutes/hours/days) for cancellation fees - Show warnings during booking submission and cancellation - Automatically charge fees when bookings cancelled within threshold - Exempt organizer/admin cancellations from fees - Extend existing no-show fee infrastructure Co-Authored-By: [email protected] <[email protected]> * fix: replace any type with proper type guards for metadata - Use Record<string, unknown> instead of any for metadata type - Add proper type assertions for nested metadata properties - Maintain type safety while avoiding ESLint no-explicit-any warning Co-Authored-By: [email protected] <[email protected]> * fix: handle JsonValue type compatibility in shouldChargeCancellationFee - Add proper type guard for metadata JsonValue from Prisma - Ensure metadata is object before casting to Record<string, unknown> - Fixes TypeScript error on line 390 in handleCancelBooking.ts Co-Authored-By: [email protected] <[email protected]> * Refactor Devin changes to Stripe options * Undo Devin changes to advanced tab * Add translations * Pass props to CancelBooking * Display no show fee charge for attendee * WIP * Anstract shouldChargeNoSHowCancellationFee * Abstract `handleNoShowFee` * Add to * Refactor `chargeCard.handler` * Remove Devin code * Type fix in `shouldChargeNoShowCancellationFee` * Create `processNoSHowFeeOnCancellation` * Process no show fee on cancellation * Type fix * Skip processing no show fee if organizer or admin is cancelling * Add translation * Dynamically get and in * Remove unused translations * Undo dev change * Refactor logic * Type fix * remove any * revert WEBAPP_URL_FOR_OAUTH * Clean up console.log remnants * test: add comprehensive tests for time-based cancellation fees - Add unit tests for shouldChargeCancellationFee function with time thresholds and role exemptions - Add handleCancelBooking integration tests for organizer/team admin exemptions - Add handleCancelBooking test for attendee fee charging within time threshold - Add UI component tests for cancellation fee warning display - Add E2E test for cancellation fee warning during booking flow All 23 new tests pass successfully, covering: - Time-based logic with different units (minutes, hours, days) - Role-based exemptions (organizer, team admin vs regular attendee) - Payment charging integration with HOLD payment option - UI warning display during booking submission and cancellation - Edge cases like invalid metadata and past bookings Fixed lint issues: - Replaced 'any' types with proper Record<string, unknown> - Fixed Playwright test.skip() usage with ESLint disable comments Co-Authored-By: [email protected] <[email protected]> * test: add comprehensive tests for time-based cancellation fees - Add unit tests for shouldChargeCancellationFee function with time thresholds and role exemptions - Add handleCancelBooking integration tests for organizer/team admin exemptions - Add handleCancelBooking test for attendee fee charging within time threshold - Add UI component tests for cancellation fee warning display - Add E2E test for cancellation fee warning during booking flow All unit and integration tests pass successfully. E2E test implementation complete. Co-Authored-By: [email protected] <[email protected]> * test: add comprehensive tests for time-based cancellation fees - Add unit tests for shouldChargeCancellationFee function with time thresholds and role exemptions - Add handleCancelBooking integration tests for organizer/team admin exemptions - Add handleCancelBooking test for attendee fee charging within time threshold - Add UI component tests for cancellation fee warning display - Add E2E test for cancellation fee warning during booking flow - Fix TypeScript issues and prettier formatting - All tests follow existing Cal.com patterns and pass locally Co-Authored-By: [email protected] <[email protected]> * test: add comprehensive unit and E2E tests for time-based cancellation fees - Add unit tests for shouldChargeCancellationFee function with time thresholds and role exemptions - Add handleCancelBooking integration tests for organizer/team admin exemptions and attendee fee charging - Add UI component tests for cancellation fee warning display in CancelBooking component - Add E2E test for cancellation fee warning during booking flow - Fix TypeScript errors in CancelBooking.tsx with proper type annotations - All tests follow existing Cal.com patterns and use proper mocking/test utilities Co-Authored-By: [email protected] <[email protected]> * Revert dev change * Add missing translation * Remove Devin code * Revert Devin changes * test: fix attendee cancellation test logic and ensure comprehensive test coverage - Fix userId in attendee cancellation test to properly test fee charging - Ensure all test files follow Cal.com patterns with proper mocking - Add comprehensive coverage for time thresholds, role exemptions, and UI warnings - All tests pass locally with proper timezone handling Co-Authored-By: [email protected] <[email protected]> * Fix tests * test: add comprehensive unit tests for no-show fee payment functions - Add unit tests for handleNoShowFee.ts covering successful scenarios, error handling, and edge cases - Add unit tests for processNoShowFeeOnCancellation.ts covering cancellation logic and payment conditions - Tests include proper mocking of dependencies and comprehensive coverage of all code paths - All tests pass with TZ=UTC yarn test and type checking passes Co-Authored-By: [email protected] <[email protected]> * Don't block cancel flow for failed payment processing * Await single attendee getTranslation in `handleNoShowFee` * Add missing data * Undo old strings * Type fix --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Syed Ali Shahbaz <[email protected]> Co-authored-by: Alex van Andel <[email protected]>
995 lines
25 KiB
TypeScript
995 lines
25 KiB
TypeScript
import type { Prisma } from "@prisma/client";
|
|
|
|
import type { FormResponse } from "@calcom/app-store/routing-forms/types/types";
|
|
import { withReporting } from "@calcom/lib/sentryWrapper";
|
|
import type { PrismaClient } from "@calcom/prisma";
|
|
import { bookingMinimalSelect } from "@calcom/prisma";
|
|
import type { Booking } from "@calcom/prisma/client";
|
|
import { RRTimestampBasis } from "@calcom/prisma/enums";
|
|
import { BookingStatus } from "@calcom/prisma/enums";
|
|
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
|
|
|
|
import { UserRepository } from "./user";
|
|
|
|
type TeamBookingsParamsBase = {
|
|
user: { id: number; email: string };
|
|
teamId: number;
|
|
startDate: Date;
|
|
endDate: Date;
|
|
excludedUid?: string | null;
|
|
includeManagedEvents: boolean;
|
|
shouldReturnCount?: boolean;
|
|
};
|
|
|
|
type TeamBookingsMultipleUsersParamsBase = {
|
|
users: { id: number; email: string }[];
|
|
teamId: number;
|
|
startDate: Date;
|
|
endDate: Date;
|
|
excludedUid?: string | null;
|
|
includeManagedEvents: boolean;
|
|
shouldReturnCount?: boolean;
|
|
};
|
|
|
|
type TeamBookingsMultipleUsersParamsWithCount = TeamBookingsMultipleUsersParamsBase & {
|
|
shouldReturnCount: true;
|
|
};
|
|
|
|
type TeamBookingsMultipleUsersParamsWithoutCount = TeamBookingsMultipleUsersParamsBase;
|
|
|
|
type TeamBookingsParamsWithCount = TeamBookingsParamsBase & {
|
|
shouldReturnCount: true;
|
|
};
|
|
|
|
type TeamBookingsParamsWithoutCount = TeamBookingsParamsBase;
|
|
|
|
const buildWhereClauseForActiveBookings = ({
|
|
eventTypeId,
|
|
startDate,
|
|
endDate,
|
|
users,
|
|
virtualQueuesData,
|
|
includeNoShowInRRCalculation = false,
|
|
rrTimestampBasis,
|
|
}: {
|
|
eventTypeId: number;
|
|
startDate?: Date;
|
|
endDate?: Date;
|
|
users: { id: number; email: string }[];
|
|
virtualQueuesData: {
|
|
chosenRouteId: string;
|
|
fieldOptionData: {
|
|
fieldId: string;
|
|
selectedOptionIds: string | number | string[];
|
|
};
|
|
} | null;
|
|
includeNoShowInRRCalculation: boolean;
|
|
rrTimestampBasis: RRTimestampBasis;
|
|
}): Prisma.BookingWhereInput => ({
|
|
OR: [
|
|
{
|
|
userId: {
|
|
in: users.map((user) => user.id),
|
|
},
|
|
...(!includeNoShowInRRCalculation
|
|
? {
|
|
OR: [{ noShowHost: false }, { noShowHost: null }],
|
|
}
|
|
: {}),
|
|
},
|
|
{
|
|
attendees: {
|
|
some: {
|
|
email: {
|
|
in: users.map((user) => user.email),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
...(!includeNoShowInRRCalculation ? { attendees: { some: { noShow: false } } } : {}),
|
|
status: BookingStatus.ACCEPTED,
|
|
eventTypeId,
|
|
...(startDate || endDate
|
|
? rrTimestampBasis === RRTimestampBasis.CREATED_AT
|
|
? {
|
|
createdAt: {
|
|
...(startDate ? { gte: startDate } : {}),
|
|
...(endDate ? { lte: endDate } : {}),
|
|
},
|
|
}
|
|
: {
|
|
startTime: {
|
|
...(startDate ? { gte: startDate } : {}),
|
|
...(endDate ? { lte: endDate } : {}),
|
|
},
|
|
}
|
|
: {}),
|
|
...(virtualQueuesData
|
|
? {
|
|
routedFromRoutingFormReponse: {
|
|
chosenRouteId: virtualQueuesData.chosenRouteId,
|
|
},
|
|
}
|
|
: {}),
|
|
});
|
|
|
|
export class BookingRepository {
|
|
constructor(private prismaClient: PrismaClient) {}
|
|
|
|
async getBookingAttendees(bookingId: number) {
|
|
return await this.prismaClient.attendee.findMany({
|
|
where: {
|
|
bookingId,
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Determines if the user is the organizer, team admin, or org admin that the booking was created under */
|
|
async doesUserIdHaveAccessToBooking({ userId, bookingId }: { userId: number; bookingId: number }) {
|
|
const booking = await this.prismaClient.booking.findUnique({
|
|
where: {
|
|
id: bookingId,
|
|
},
|
|
select: {
|
|
userId: true,
|
|
eventType: {
|
|
select: {
|
|
teamId: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!booking) return false;
|
|
|
|
if (userId === booking.userId) return true;
|
|
|
|
// If the booking doesn't belong to the user and there's no team then return early
|
|
if (!booking.eventType || !booking.eventType.teamId) return false;
|
|
|
|
// TODO add checks for team and org
|
|
const userRepo = new UserRepository(this.prismaClient);
|
|
const isAdminOrUser = await userRepo.isAdminOfTeamOrParentOrg({
|
|
userId,
|
|
teamId: booking.eventType.teamId,
|
|
});
|
|
|
|
return isAdminOrUser;
|
|
}
|
|
|
|
async findFirstBookingByReschedule({ originalBookingUid }: { originalBookingUid: string }) {
|
|
return await this.prismaClient.booking.findFirst({
|
|
where: {
|
|
fromReschedule: originalBookingUid,
|
|
},
|
|
select: {
|
|
uid: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
async findReschedulerByUid({ uid }: { uid: string }) {
|
|
return await this.prismaClient.booking.findUnique({
|
|
where: {
|
|
uid,
|
|
},
|
|
select: {
|
|
rescheduledBy: true,
|
|
uid: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
private async _findAllExistingBookingsForEventTypeBetween({
|
|
eventTypeId,
|
|
seatedEvent = false,
|
|
startDate,
|
|
endDate,
|
|
userIdAndEmailMap,
|
|
}: {
|
|
startDate: Date;
|
|
endDate: Date;
|
|
eventTypeId?: number | null;
|
|
seatedEvent?: boolean;
|
|
userIdAndEmailMap: Map<number, string>;
|
|
}) {
|
|
const sharedQuery = {
|
|
startTime: { lte: endDate },
|
|
endTime: { gte: startDate },
|
|
status: {
|
|
in: [BookingStatus.ACCEPTED],
|
|
},
|
|
};
|
|
|
|
const bookingsSelect = {
|
|
id: true,
|
|
uid: true,
|
|
userId: true,
|
|
startTime: true,
|
|
endTime: true,
|
|
title: true,
|
|
attendees: true,
|
|
eventType: {
|
|
select: {
|
|
id: true,
|
|
onlyShowFirstAvailableSlot: true,
|
|
afterEventBuffer: true,
|
|
beforeEventBuffer: true,
|
|
seatsPerTimeSlot: true,
|
|
requiresConfirmationWillBlockSlot: true,
|
|
requiresConfirmation: true,
|
|
allowReschedulingPastBookings: true,
|
|
hideOrganizerEmail: true,
|
|
},
|
|
},
|
|
...(seatedEvent && {
|
|
_count: {
|
|
select: {
|
|
seatsReferences: true,
|
|
},
|
|
},
|
|
}),
|
|
} satisfies Prisma.BookingSelect;
|
|
|
|
const currentBookingsAllUsersQueryOne = this.prismaClient.booking.findMany({
|
|
where: {
|
|
...sharedQuery,
|
|
userId: {
|
|
in: Array.from(userIdAndEmailMap.keys()),
|
|
},
|
|
},
|
|
select: bookingsSelect,
|
|
});
|
|
|
|
const currentBookingsAllUsersQueryTwo = this.prismaClient.booking.findMany({
|
|
where: {
|
|
...sharedQuery,
|
|
attendees: {
|
|
some: {
|
|
email: {
|
|
in: Array.from(userIdAndEmailMap.values()),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
select: bookingsSelect,
|
|
});
|
|
|
|
const currentBookingsAllUsersQueryThree = eventTypeId
|
|
? this.prismaClient.booking.findMany({
|
|
where: {
|
|
startTime: { lte: endDate },
|
|
endTime: { gte: startDate },
|
|
eventType: {
|
|
id: eventTypeId,
|
|
requiresConfirmation: true,
|
|
requiresConfirmationWillBlockSlot: true,
|
|
},
|
|
status: {
|
|
in: [BookingStatus.PENDING],
|
|
},
|
|
},
|
|
select: bookingsSelect,
|
|
})
|
|
: [];
|
|
|
|
const [resultOne, resultTwo, resultThree] = await Promise.all([
|
|
currentBookingsAllUsersQueryOne,
|
|
currentBookingsAllUsersQueryTwo,
|
|
currentBookingsAllUsersQueryThree,
|
|
]);
|
|
// Prevent duplicate booking records when the organizer books his own event type.
|
|
//
|
|
// *Three is about PENDING, so will never overlap
|
|
// with the other two, which are about ACCEPTED. But ACCEPTED affects *One & *Two
|
|
// and WILL result in a duplicate booking if the organizer books himself.
|
|
const resultTwoWithOrganizersRemoved = resultTwo.filter((booking) => {
|
|
if (!booking.userId) return true;
|
|
const organizerEmail = userIdAndEmailMap.get(booking.userId);
|
|
return !booking.attendees.some((attendee) => attendee.email === organizerEmail);
|
|
});
|
|
|
|
return [...resultOne, ...resultTwoWithOrganizersRemoved, ...resultThree];
|
|
}
|
|
|
|
findAllExistingBookingsForEventTypeBetween = withReporting(
|
|
this._findAllExistingBookingsForEventTypeBetween.bind(this),
|
|
"findAllExistingBookingsForEventTypeBetween"
|
|
);
|
|
|
|
async getAllBookingsForRoundRobin({
|
|
users,
|
|
eventTypeId,
|
|
startDate,
|
|
endDate,
|
|
virtualQueuesData,
|
|
includeNoShowInRRCalculation,
|
|
rrTimestampBasis,
|
|
}: {
|
|
users: { id: number; email: string }[];
|
|
eventTypeId: number;
|
|
startDate?: Date;
|
|
endDate?: Date;
|
|
virtualQueuesData: {
|
|
chosenRouteId: string;
|
|
fieldOptionData: {
|
|
fieldId: string;
|
|
selectedOptionIds: string | number | string[];
|
|
};
|
|
} | null;
|
|
includeNoShowInRRCalculation: boolean;
|
|
rrTimestampBasis: RRTimestampBasis;
|
|
}) {
|
|
const allBookings = await this.prismaClient.booking.findMany({
|
|
where: buildWhereClauseForActiveBookings({
|
|
eventTypeId,
|
|
startDate,
|
|
endDate,
|
|
users,
|
|
virtualQueuesData,
|
|
includeNoShowInRRCalculation,
|
|
rrTimestampBasis,
|
|
}),
|
|
select: {
|
|
id: true,
|
|
attendees: true,
|
|
userId: true,
|
|
createdAt: true,
|
|
status: true,
|
|
startTime: true,
|
|
routedFromRoutingFormReponse: true,
|
|
},
|
|
orderBy: {
|
|
createdAt: "desc",
|
|
},
|
|
});
|
|
|
|
let queueBookings = allBookings;
|
|
|
|
if (virtualQueuesData) {
|
|
queueBookings = allBookings.filter((booking) => {
|
|
const responses = booking.routedFromRoutingFormReponse;
|
|
const fieldId = virtualQueuesData.fieldOptionData.fieldId;
|
|
const selectedOptionIds = virtualQueuesData.fieldOptionData.selectedOptionIds;
|
|
|
|
const response = responses?.response as FormResponse;
|
|
|
|
const responseValue = response[fieldId].value;
|
|
|
|
if (Array.isArray(responseValue) && Array.isArray(selectedOptionIds)) {
|
|
//check if all values are the same (this only support 'all in' not 'any in')
|
|
return (
|
|
responseValue.length === selectedOptionIds.length &&
|
|
responseValue.every((value, index) => value === selectedOptionIds[index])
|
|
);
|
|
} else {
|
|
return responseValue === selectedOptionIds;
|
|
}
|
|
});
|
|
}
|
|
return queueBookings;
|
|
}
|
|
|
|
async findBookingByUid({ bookingUid }: { bookingUid: string }) {
|
|
return await this.prismaClient.booking.findUnique({
|
|
where: {
|
|
uid: bookingUid,
|
|
},
|
|
select: bookingMinimalSelect,
|
|
});
|
|
}
|
|
|
|
async findBookingForMeetingPage({ bookingUid }: { bookingUid: string }) {
|
|
return await this.prismaClient.booking.findUnique({
|
|
where: {
|
|
uid: bookingUid,
|
|
},
|
|
select: {
|
|
...bookingMinimalSelect,
|
|
uid: true,
|
|
description: true,
|
|
isRecorded: true,
|
|
eventType: {
|
|
select: {
|
|
id: true,
|
|
hideOrganizerEmail: true,
|
|
calVideoSettings: {
|
|
select: {
|
|
disableRecordingForGuests: true,
|
|
disableRecordingForOrganizer: true,
|
|
enableAutomaticTranscription: true,
|
|
enableAutomaticRecordingForOrganizer: true,
|
|
disableTranscriptionForGuests: true,
|
|
disableTranscriptionForOrganizer: true,
|
|
redirectUrlOnExit: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
timeZone: true,
|
|
name: true,
|
|
email: true,
|
|
username: true,
|
|
},
|
|
},
|
|
references: {
|
|
select: {
|
|
id: true,
|
|
uid: true,
|
|
type: true,
|
|
meetingUrl: true,
|
|
meetingPassword: true,
|
|
},
|
|
where: {
|
|
type: "daily_video",
|
|
deleted: null,
|
|
},
|
|
orderBy: {
|
|
id: "asc",
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
async findBookingForMeetingEndedPage({ bookingUid }: { bookingUid: string }) {
|
|
return await this.prismaClient.booking.findUnique({
|
|
where: {
|
|
uid: bookingUid,
|
|
},
|
|
select: {
|
|
...bookingMinimalSelect,
|
|
uid: true,
|
|
user: {
|
|
select: {
|
|
credentials: true,
|
|
},
|
|
},
|
|
references: {
|
|
select: {
|
|
uid: true,
|
|
type: true,
|
|
meetingUrl: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
async findBookingByUidAndUserId({ bookingUid, userId }: { bookingUid: string; userId: number }) {
|
|
return await this.prismaClient.booking.findFirst({
|
|
where: {
|
|
uid: bookingUid,
|
|
OR: [
|
|
{ userId: userId },
|
|
{
|
|
eventType: {
|
|
hosts: {
|
|
some: {
|
|
userId,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
eventType: {
|
|
users: {
|
|
some: {
|
|
id: userId,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
eventType: {
|
|
team: {
|
|
members: {
|
|
some: {
|
|
userId,
|
|
accepted: true,
|
|
role: {
|
|
in: ["ADMIN", "OWNER"],
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
eventType: {
|
|
parent: {
|
|
team: {
|
|
members: {
|
|
some: {
|
|
userId,
|
|
accepted: true,
|
|
role: {
|
|
in: ["ADMIN", "OWNER"],
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
});
|
|
}
|
|
|
|
async updateLocationById({
|
|
where: { id },
|
|
data: { location, metadata, referencesToCreate, responses, iCalSequence },
|
|
}: {
|
|
where: { id: number };
|
|
data: {
|
|
location: string;
|
|
metadata: Record<string, unknown>;
|
|
referencesToCreate: Prisma.BookingReferenceCreateInput[];
|
|
responses?: Record<string, unknown>;
|
|
iCalSequence?: number;
|
|
};
|
|
}) {
|
|
await this.prismaClient.booking.update({
|
|
where: {
|
|
id,
|
|
},
|
|
data: {
|
|
location,
|
|
metadata,
|
|
...(responses && { responses }),
|
|
...(iCalSequence !== undefined && { iCalSequence }),
|
|
references: {
|
|
create: referencesToCreate,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
async getAllAcceptedTeamBookingsOfUser(params: TeamBookingsParamsWithCount): Promise<number>;
|
|
|
|
async getAllAcceptedTeamBookingsOfUser(params: TeamBookingsParamsWithoutCount): Promise<Array<Booking>>;
|
|
|
|
async getAllAcceptedTeamBookingsOfUser(params: TeamBookingsParamsBase) {
|
|
const { user, teamId, startDate, endDate, excludedUid, shouldReturnCount, includeManagedEvents } = params;
|
|
|
|
const baseWhere: Prisma.BookingWhereInput = {
|
|
status: BookingStatus.ACCEPTED,
|
|
startTime: {
|
|
gte: startDate,
|
|
},
|
|
endTime: {
|
|
lte: endDate,
|
|
},
|
|
...(excludedUid && {
|
|
uid: {
|
|
not: excludedUid,
|
|
},
|
|
}),
|
|
};
|
|
|
|
const whereCollectiveRoundRobinOwner: Prisma.BookingWhereInput = {
|
|
...baseWhere,
|
|
userId: user.id,
|
|
eventType: {
|
|
teamId,
|
|
},
|
|
};
|
|
|
|
const whereCollectiveRoundRobinBookingsAttendee: Prisma.BookingWhereInput = {
|
|
...baseWhere,
|
|
attendees: {
|
|
some: {
|
|
email: user.email,
|
|
},
|
|
},
|
|
eventType: {
|
|
teamId,
|
|
},
|
|
};
|
|
|
|
const whereManagedBookings: Prisma.BookingWhereInput = {
|
|
...baseWhere,
|
|
userId: user.id,
|
|
eventType: {
|
|
parent: {
|
|
teamId,
|
|
},
|
|
},
|
|
};
|
|
|
|
if (shouldReturnCount) {
|
|
const collectiveRoundRobinBookingsOwner = await this.prismaClient.booking.count({
|
|
where: whereCollectiveRoundRobinOwner,
|
|
});
|
|
|
|
const collectiveRoundRobinBookingsAttendee = await this.prismaClient.booking.count({
|
|
where: whereCollectiveRoundRobinBookingsAttendee,
|
|
});
|
|
|
|
let managedBookings = 0;
|
|
|
|
if (includeManagedEvents) {
|
|
managedBookings = await this.prismaClient.booking.count({
|
|
where: whereManagedBookings,
|
|
});
|
|
}
|
|
|
|
const totalNrOfBooking =
|
|
collectiveRoundRobinBookingsOwner + collectiveRoundRobinBookingsAttendee + managedBookings;
|
|
|
|
return totalNrOfBooking;
|
|
}
|
|
const collectiveRoundRobinBookingsOwner = await this.prismaClient.booking.findMany({
|
|
where: whereCollectiveRoundRobinOwner,
|
|
});
|
|
|
|
const collectiveRoundRobinBookingsAttendee = await this.prismaClient.booking.findMany({
|
|
where: whereCollectiveRoundRobinBookingsAttendee,
|
|
});
|
|
|
|
let managedBookings: typeof collectiveRoundRobinBookingsAttendee = [];
|
|
|
|
if (includeManagedEvents) {
|
|
managedBookings = await this.prismaClient.booking.findMany({
|
|
where: whereManagedBookings,
|
|
});
|
|
}
|
|
|
|
return [
|
|
...collectiveRoundRobinBookingsOwner,
|
|
...collectiveRoundRobinBookingsAttendee,
|
|
...managedBookings,
|
|
];
|
|
}
|
|
|
|
async findOriginalRescheduledBooking(uid: string, seatsEventType?: boolean) {
|
|
return await this.prismaClient.booking.findFirst({
|
|
where: {
|
|
uid: uid,
|
|
status: {
|
|
in: [BookingStatus.ACCEPTED, BookingStatus.CANCELLED, BookingStatus.PENDING],
|
|
},
|
|
},
|
|
include: {
|
|
attendees: {
|
|
select: {
|
|
name: true,
|
|
email: true,
|
|
locale: true,
|
|
timeZone: true,
|
|
phoneNumber: true,
|
|
...(seatsEventType && { bookingSeat: true, id: true }),
|
|
},
|
|
},
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
username: true,
|
|
email: true,
|
|
locale: true,
|
|
timeZone: true,
|
|
timeFormat: true,
|
|
destinationCalendar: true,
|
|
credentials: {
|
|
select: credentialForCalendarServiceSelect,
|
|
},
|
|
},
|
|
},
|
|
destinationCalendar: true,
|
|
payment: true,
|
|
references: true,
|
|
workflowReminders: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
async getAllAcceptedTeamBookingsOfUsers(params: TeamBookingsMultipleUsersParamsWithCount): Promise<number>;
|
|
|
|
async getAllAcceptedTeamBookingsOfUsers(
|
|
params: TeamBookingsMultipleUsersParamsWithoutCount
|
|
): Promise<Array<Booking>>;
|
|
|
|
async getAllAcceptedTeamBookingsOfUsers(params: TeamBookingsMultipleUsersParamsBase) {
|
|
const { users, teamId, startDate, endDate, excludedUid, shouldReturnCount, includeManagedEvents } =
|
|
params;
|
|
|
|
const baseWhere: Prisma.BookingWhereInput = {
|
|
status: BookingStatus.ACCEPTED,
|
|
startTime: {
|
|
gte: startDate,
|
|
},
|
|
endTime: {
|
|
lte: endDate,
|
|
},
|
|
...(excludedUid && {
|
|
uid: {
|
|
not: excludedUid,
|
|
},
|
|
}),
|
|
};
|
|
|
|
const userIds = users.map((user) => user.id);
|
|
const userEmails = users.map((user) => user.email);
|
|
|
|
const whereCollectiveRoundRobinOwner: Prisma.BookingWhereInput = {
|
|
...baseWhere,
|
|
userId: {
|
|
in: userIds,
|
|
},
|
|
eventType: {
|
|
teamId,
|
|
},
|
|
};
|
|
|
|
const whereCollectiveRoundRobinBookingsAttendee: Prisma.BookingWhereInput = {
|
|
...baseWhere,
|
|
attendees: {
|
|
some: {
|
|
email: {
|
|
in: userEmails,
|
|
},
|
|
},
|
|
},
|
|
eventType: {
|
|
teamId,
|
|
},
|
|
};
|
|
|
|
const whereManagedBookings: Prisma.BookingWhereInput = {
|
|
...baseWhere,
|
|
userId: {
|
|
in: userIds,
|
|
},
|
|
eventType: {
|
|
parent: {
|
|
teamId,
|
|
},
|
|
},
|
|
};
|
|
|
|
if (shouldReturnCount) {
|
|
const collectiveRoundRobinBookingsOwner = await this.prismaClient.booking.count({
|
|
where: whereCollectiveRoundRobinOwner,
|
|
});
|
|
|
|
const collectiveRoundRobinBookingsAttendee = await this.prismaClient.booking.count({
|
|
where: whereCollectiveRoundRobinBookingsAttendee,
|
|
});
|
|
|
|
let managedBookings = 0;
|
|
|
|
if (includeManagedEvents) {
|
|
managedBookings = await this.prismaClient.booking.count({
|
|
where: whereManagedBookings,
|
|
});
|
|
}
|
|
|
|
const totalNrOfBooking =
|
|
collectiveRoundRobinBookingsOwner + collectiveRoundRobinBookingsAttendee + managedBookings;
|
|
|
|
return totalNrOfBooking;
|
|
}
|
|
|
|
const collectiveRoundRobinBookingsOwner = await this.prismaClient.booking.findMany({
|
|
where: whereCollectiveRoundRobinOwner,
|
|
});
|
|
|
|
const collectiveRoundRobinBookingsAttendee = await this.prismaClient.booking.findMany({
|
|
where: whereCollectiveRoundRobinBookingsAttendee,
|
|
});
|
|
|
|
let managedBookings: typeof collectiveRoundRobinBookingsAttendee = [];
|
|
|
|
if (includeManagedEvents) {
|
|
managedBookings = await this.prismaClient.booking.findMany({
|
|
where: whereManagedBookings,
|
|
});
|
|
}
|
|
|
|
return [
|
|
...collectiveRoundRobinBookingsOwner,
|
|
...collectiveRoundRobinBookingsAttendee,
|
|
...managedBookings,
|
|
];
|
|
}
|
|
|
|
async getValidBookingFromEventTypeForAttendee({
|
|
eventTypeId,
|
|
bookerEmail,
|
|
bookerPhoneNumber,
|
|
startTime,
|
|
filterForUnconfirmed,
|
|
}: {
|
|
eventTypeId: number;
|
|
bookerEmail?: string;
|
|
bookerPhoneNumber?: string;
|
|
startTime: Date;
|
|
filterForUnconfirmed?: boolean;
|
|
}) {
|
|
return await this.prismaClient.booking.findFirst({
|
|
where: {
|
|
eventTypeId,
|
|
attendees: {
|
|
some: {
|
|
email: bookerEmail,
|
|
phoneNumber: bookerPhoneNumber,
|
|
},
|
|
},
|
|
startTime,
|
|
status: filterForUnconfirmed ? BookingStatus.PENDING : BookingStatus.ACCEPTED,
|
|
},
|
|
include: {
|
|
attendees: true,
|
|
references: true,
|
|
user: true,
|
|
payment: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
async countBookingsByEventTypeAndDateRange({
|
|
eventTypeId,
|
|
startDate,
|
|
endDate,
|
|
excludedUid,
|
|
}: {
|
|
eventTypeId: number;
|
|
startDate: Date;
|
|
endDate: Date;
|
|
excludedUid?: string;
|
|
}) {
|
|
return await this.prismaClient.booking.count({
|
|
where: {
|
|
status: BookingStatus.ACCEPTED,
|
|
eventTypeId,
|
|
startTime: {
|
|
gte: startDate,
|
|
},
|
|
endTime: {
|
|
lte: endDate,
|
|
},
|
|
uid: {
|
|
not: excludedUid,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
async findAcceptedBookingByEventTypeId({
|
|
eventTypeId,
|
|
dateFrom,
|
|
dateTo,
|
|
}: {
|
|
eventTypeId?: number;
|
|
dateFrom: string;
|
|
dateTo: string;
|
|
}) {
|
|
return this.prismaClient.booking.findMany({
|
|
where: {
|
|
eventTypeId,
|
|
startTime: {
|
|
gte: dateFrom,
|
|
lte: dateTo,
|
|
},
|
|
status: BookingStatus.ACCEPTED,
|
|
},
|
|
select: {
|
|
uid: true,
|
|
startTime: true,
|
|
attendees: {
|
|
select: {
|
|
email: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
async getTotalBookingDuration({
|
|
eventId,
|
|
startDate,
|
|
endDate,
|
|
rescheduleUid,
|
|
}: {
|
|
eventId: number;
|
|
startDate: Date;
|
|
endDate: Date;
|
|
rescheduleUid?: string;
|
|
}) {
|
|
let totalBookingTime;
|
|
|
|
if (rescheduleUid) {
|
|
[totalBookingTime] = await this.prismaClient.$queryRaw<[{ totalMinutes: number | null }]>`
|
|
SELECT SUM(EXTRACT(EPOCH FROM ("endTime" - "startTime")) / 60) as "totalMinutes"
|
|
FROM "Booking"
|
|
WHERE "status" = 'accepted'
|
|
AND "eventTypeId" = ${eventId}
|
|
AND "startTime" >= ${startDate}
|
|
AND "endTime" <= ${endDate}
|
|
AND "uid" != ${rescheduleUid};
|
|
`;
|
|
} else {
|
|
[totalBookingTime] = await this.prismaClient.$queryRaw<[{ totalMinutes: number | null }]>`
|
|
SELECT SUM(EXTRACT(EPOCH FROM ("endTime" - "startTime")) / 60) as "totalMinutes"
|
|
FROM "Booking"
|
|
WHERE "status" = 'accepted'
|
|
AND "eventTypeId" = ${eventId}
|
|
AND "startTime" >= ${startDate}
|
|
AND "endTime" <= ${endDate};
|
|
`;
|
|
}
|
|
return totalBookingTime.totalMinutes ?? 0;
|
|
}
|
|
|
|
async findOriginalRescheduledBookingUserId({ rescheduleUid }: { rescheduleUid: string }) {
|
|
return await this.prismaClient.booking.findFirst({
|
|
where: {
|
|
uid: rescheduleUid,
|
|
status: {
|
|
in: [BookingStatus.ACCEPTED, BookingStatus.CANCELLED, BookingStatus.PENDING],
|
|
},
|
|
},
|
|
select: {
|
|
userId: true,
|
|
attendees: {
|
|
select: {
|
|
email: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
async getBookingForPaymentProcessing(bookingId: number) {
|
|
return await this.prismaClient.booking.findUnique({
|
|
where: {
|
|
id: bookingId,
|
|
},
|
|
select: {
|
|
id: true,
|
|
uid: true,
|
|
title: true,
|
|
startTime: true,
|
|
endTime: true,
|
|
userPrimaryEmail: true,
|
|
status: true,
|
|
eventTypeId: true,
|
|
userId: true,
|
|
attendees: {
|
|
select: {
|
|
name: true,
|
|
email: true,
|
|
timeZone: true,
|
|
locale: true,
|
|
},
|
|
},
|
|
eventType: {
|
|
select: {
|
|
title: true,
|
|
hideOrganizerEmail: true,
|
|
teamId: true,
|
|
metadata: true,
|
|
},
|
|
},
|
|
payment: {
|
|
select: {
|
|
id: true,
|
|
amount: true,
|
|
currency: true,
|
|
paymentOption: true,
|
|
appId: true,
|
|
success: true,
|
|
data: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
}
|