2021-09-02 15:20:36 +00:00
|
|
|
import { hashPassword } from "../lib/auth";
|
2021-09-14 08:45:28 +00:00
|
|
|
import { Prisma, PrismaClient, UserPlan } from "@prisma/client";
|
|
|
|
import dayjs from "dayjs";
|
|
|
|
import { uuid } from "short-uuid";
|
2021-09-02 15:20:36 +00:00
|
|
|
const prisma = new PrismaClient();
|
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
async function createBookingForEventType(opts: {
|
|
|
|
uid: string;
|
|
|
|
title: string;
|
|
|
|
slug: string;
|
|
|
|
startTime: Date | string;
|
|
|
|
endTime: Date | string;
|
|
|
|
userEmail: string;
|
|
|
|
}) {
|
|
|
|
const eventType = await prisma.eventType.findFirst({
|
|
|
|
where: {
|
|
|
|
slug: opts.slug,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!eventType) {
|
|
|
|
// should not happen
|
|
|
|
throw new Error("Eventtype missing");
|
|
|
|
}
|
|
|
|
|
|
|
|
const bookingData: Prisma.BookingCreateArgs["data"] = {
|
|
|
|
uid: opts.uid,
|
|
|
|
title: opts.title,
|
|
|
|
startTime: opts.startTime,
|
|
|
|
endTime: opts.endTime,
|
|
|
|
user: {
|
|
|
|
connect: {
|
|
|
|
email: opts.userEmail,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
attendees: {
|
|
|
|
create: {
|
|
|
|
email: opts.userEmail,
|
|
|
|
name: "Some name",
|
|
|
|
timeZone: "Europe/London",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
eventType: {
|
|
|
|
connect: {
|
|
|
|
id: eventType.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
await prisma.booking.create({
|
|
|
|
data: bookingData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-02 15:20:36 +00:00
|
|
|
async function createUserAndEventType(opts: {
|
2021-09-21 09:57:01 +00:00
|
|
|
user: { email: string; password: string; username: string; plan: UserPlan; name: string };
|
2021-09-02 15:20:36 +00:00
|
|
|
eventTypes: Array<Prisma.EventTypeCreateArgs["data"]>;
|
|
|
|
}) {
|
|
|
|
const userData: Prisma.UserCreateArgs["data"] = {
|
|
|
|
...opts.user,
|
|
|
|
password: await hashPassword(opts.user.password),
|
|
|
|
emailVerified: new Date(),
|
2021-09-06 13:51:15 +00:00
|
|
|
completedOnboarding: true,
|
2021-09-02 15:20:36 +00:00
|
|
|
};
|
|
|
|
const user = await prisma.user.upsert({
|
|
|
|
where: { email: opts.user.email },
|
|
|
|
update: userData,
|
|
|
|
create: userData,
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log(
|
2021-09-06 13:51:15 +00:00
|
|
|
`👤 Upserted '${opts.user.username}' with email "${opts.user.email}" & password "${opts.user.password}". Booking page 👉 http://localhost:3000/${opts.user.username}`
|
2021-09-02 15:20:36 +00:00
|
|
|
);
|
|
|
|
for (const rawData of opts.eventTypes) {
|
|
|
|
const eventTypeData: Prisma.EventTypeCreateArgs["data"] = { ...rawData };
|
|
|
|
eventTypeData.userId = user.id;
|
2021-09-21 09:57:01 +00:00
|
|
|
eventTypeData.users = { connect: { id: user.id } };
|
2021-09-14 08:45:28 +00:00
|
|
|
|
|
|
|
const eventType = await prisma.eventType.findFirst({
|
2021-09-02 15:20:36 +00:00
|
|
|
where: {
|
2021-09-14 08:45:28 +00:00
|
|
|
slug: eventTypeData.slug,
|
|
|
|
users: {
|
|
|
|
some: {
|
|
|
|
id: eventTypeData.userId,
|
|
|
|
},
|
2021-09-06 13:51:15 +00:00
|
|
|
},
|
2021-09-02 15:20:36 +00:00
|
|
|
},
|
2021-09-14 08:45:28 +00:00
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
2021-09-02 15:20:36 +00:00
|
|
|
});
|
2021-09-14 08:45:28 +00:00
|
|
|
|
|
|
|
if (eventType) {
|
|
|
|
await prisma.eventType.update({
|
|
|
|
where: {
|
|
|
|
id: eventType.id,
|
|
|
|
},
|
|
|
|
data: eventTypeData,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await prisma.eventType.create({
|
|
|
|
data: eventTypeData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-02 15:20:36 +00:00
|
|
|
console.log(
|
|
|
|
`\t📆 Event type ${eventTypeData.slug}, length ${eventTypeData.length}: http://localhost:3000/${user.username}/${eventTypeData.slug}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
await createUserAndEventType({
|
|
|
|
user: {
|
|
|
|
email: "free@example.com",
|
|
|
|
password: "free",
|
|
|
|
username: "free",
|
2021-09-21 09:57:01 +00:00
|
|
|
name: "Free Example",
|
2021-09-06 13:51:15 +00:00
|
|
|
plan: "FREE",
|
2021-09-02 15:20:36 +00:00
|
|
|
},
|
|
|
|
eventTypes: [
|
|
|
|
{
|
|
|
|
title: "30min",
|
|
|
|
slug: "30min",
|
|
|
|
length: 30,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "60min",
|
|
|
|
slug: "60min",
|
|
|
|
length: 30,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2021-09-06 13:51:15 +00:00
|
|
|
|
|
|
|
await createUserAndEventType({
|
|
|
|
user: {
|
|
|
|
email: "free-first-hidden@example.com",
|
|
|
|
password: "free-first-hidden",
|
|
|
|
username: "free-first-hidden",
|
2021-09-21 09:57:01 +00:00
|
|
|
name: "Free First Hidden Example",
|
2021-09-06 13:51:15 +00:00
|
|
|
plan: "FREE",
|
|
|
|
},
|
|
|
|
eventTypes: [
|
|
|
|
{
|
|
|
|
title: "30min",
|
|
|
|
slug: "30min",
|
|
|
|
length: 30,
|
|
|
|
hidden: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "60min",
|
|
|
|
slug: "60min",
|
|
|
|
length: 30,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2021-09-02 15:20:36 +00:00
|
|
|
await createUserAndEventType({
|
|
|
|
user: {
|
|
|
|
email: "pro@example.com",
|
2021-09-21 09:57:01 +00:00
|
|
|
name: "Pro Example",
|
2021-09-02 15:20:36 +00:00
|
|
|
password: "pro",
|
|
|
|
username: "pro",
|
2021-09-06 13:51:15 +00:00
|
|
|
plan: "PRO",
|
2021-09-02 15:20:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
eventTypes: [
|
|
|
|
{
|
|
|
|
title: "30min",
|
|
|
|
slug: "30min",
|
|
|
|
length: 30,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "60min",
|
|
|
|
slug: "60min",
|
|
|
|
length: 60,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2021-09-14 08:45:28 +00:00
|
|
|
|
|
|
|
await createBookingForEventType({
|
|
|
|
title: "30min",
|
|
|
|
slug: "30min",
|
|
|
|
startTime: dayjs().add(1, "day").toDate(),
|
|
|
|
endTime: dayjs().add(1, "day").add(60, "minutes").toDate(),
|
|
|
|
uid: uuid(),
|
|
|
|
userEmail: "pro@example.com",
|
|
|
|
});
|
|
|
|
|
2021-09-02 15:20:36 +00:00
|
|
|
await createUserAndEventType({
|
|
|
|
user: {
|
|
|
|
email: "trial@example.com",
|
|
|
|
password: "trial",
|
|
|
|
username: "trial",
|
2021-09-21 09:57:01 +00:00
|
|
|
name: "Trial Example",
|
2021-09-06 13:51:15 +00:00
|
|
|
plan: "TRIAL",
|
2021-09-02 15:20:36 +00:00
|
|
|
},
|
|
|
|
eventTypes: [
|
|
|
|
{
|
|
|
|
title: "30min",
|
|
|
|
slug: "30min",
|
|
|
|
length: 30,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "60min",
|
|
|
|
slug: "60min",
|
|
|
|
length: 60,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
await prisma.$disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
main()
|
|
|
|
.then(() => {
|
|
|
|
console.log("🌱 Seeded db");
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
process.exit(1);
|
|
|
|
});
|