2022-05-14 03:02:10 +00:00
|
|
|
import type { Page } from "@playwright/test";
|
|
|
|
import type { Booking, Prisma } from "@prisma/client";
|
|
|
|
import short from "short-uuid";
|
2022-06-28 20:40:58 +00:00
|
|
|
import { v5 as uuidv5 } from "uuid";
|
2022-05-14 03:02:10 +00:00
|
|
|
|
2022-06-28 20:40:58 +00:00
|
|
|
import dayjs from "@calcom/dayjs";
|
2022-05-14 03:02:10 +00:00
|
|
|
import { prisma } from "@calcom/prisma";
|
|
|
|
|
|
|
|
const translator = short();
|
|
|
|
|
|
|
|
type BookingFixture = ReturnType<typeof createBookingFixture>;
|
|
|
|
|
|
|
|
// creates a user fixture instance and stores the collection
|
|
|
|
export const createBookingsFixture = (page: Page) => {
|
2022-05-18 16:05:25 +00:00
|
|
|
const store = { bookings: [], page } as { bookings: BookingFixture[]; page: typeof page };
|
2022-05-14 03:02:10 +00:00
|
|
|
return {
|
|
|
|
create: async (
|
|
|
|
userId: number,
|
|
|
|
username: string | null,
|
|
|
|
eventTypeId = -1,
|
2023-03-14 04:19:05 +00:00
|
|
|
{
|
|
|
|
rescheduled = false,
|
|
|
|
paid = false,
|
|
|
|
status = "ACCEPTED",
|
|
|
|
attendees = {
|
|
|
|
create: {
|
|
|
|
email: "attendee@example.com",
|
|
|
|
name: "Attendee Example",
|
|
|
|
timeZone: "Europe/London",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}: Partial<Prisma.BookingCreateInput> = {},
|
2022-06-10 19:16:10 +00:00
|
|
|
startDateParam?: Date,
|
|
|
|
endDateParam?: Date
|
2022-05-14 03:02:10 +00:00
|
|
|
) => {
|
2022-06-10 19:16:10 +00:00
|
|
|
const startDate = startDateParam || dayjs().add(1, "day").toDate();
|
2022-05-14 03:02:10 +00:00
|
|
|
const seed = `${username}:${dayjs(startDate).utc().format()}:${new Date().getTime()}`;
|
|
|
|
const uid = translator.fromUUID(uuidv5(seed, uuidv5.URL));
|
|
|
|
const booking = await prisma.booking.create({
|
|
|
|
data: {
|
|
|
|
uid: uid,
|
|
|
|
title: "30min",
|
|
|
|
startTime: startDate,
|
2022-06-10 19:16:10 +00:00
|
|
|
endTime: endDateParam || dayjs().add(1, "day").add(30, "minutes").toDate(),
|
2022-05-14 03:02:10 +00:00
|
|
|
user: {
|
|
|
|
connect: {
|
|
|
|
id: userId,
|
|
|
|
},
|
|
|
|
},
|
2023-03-14 04:19:05 +00:00
|
|
|
attendees,
|
2022-05-14 03:02:10 +00:00
|
|
|
eventType: {
|
|
|
|
connect: {
|
|
|
|
id: eventTypeId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rescheduled,
|
|
|
|
paid,
|
|
|
|
status,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const bookingFixture = createBookingFixture(booking, store.page!);
|
|
|
|
store.bookings.push(bookingFixture);
|
|
|
|
return bookingFixture;
|
|
|
|
},
|
|
|
|
get: () => store.bookings,
|
|
|
|
delete: async (id: number) => {
|
|
|
|
await prisma.booking.delete({
|
|
|
|
where: { id },
|
|
|
|
});
|
|
|
|
store.bookings = store.bookings.filter((b) => b.id !== id);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// creates the single user fixture
|
|
|
|
const createBookingFixture = (booking: Booking, page: Page) => {
|
|
|
|
const store = { booking, page };
|
|
|
|
|
|
|
|
// self is a reflective method that return the Prisma object that references this fixture.
|
|
|
|
return {
|
|
|
|
id: store.booking.id,
|
|
|
|
uid: store.booking.uid,
|
|
|
|
self: async () => (await prisma.booking.findUnique({ where: { id: store.booking.id } }))!,
|
|
|
|
delete: async () => (await prisma.booking.delete({ where: { id: store.booking.id } }))!,
|
|
|
|
};
|
|
|
|
};
|