cal.pub0.org/apps/web/playwright/fixtures/bookings.ts

82 lines
2.6 KiB
TypeScript
Raw Normal View History

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) => {
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,
{ rescheduled = false, paid = false, status = "ACCEPTED" }: Partial<Prisma.BookingCreateInput> = {},
startDateParam?: Date,
endDateParam?: Date
2022-05-14 03:02: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,
endTime: endDateParam || dayjs().add(1, "day").add(30, "minutes").toDate(),
2022-05-14 03:02:10 +00:00
user: {
connect: {
id: userId,
},
},
attendees: {
create: {
email: "attendee@example.com",
name: "Attendee Example",
timeZone: "Europe/London",
},
},
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 } }))!,
};
};