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-22 19:52:38 +00:00
|
|
|
|
|
|
|
|
|
import { hashPassword } from "../lib/auth";
|
|
|
|
|
|
2021-09-02 15:20:36 +00:00
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
|
|
|
|
|
|
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-28 09:16:02 +00:00
|
|
|
|
eventTypes: Array<
|
|
|
|
|
Prisma.EventTypeCreateInput & {
|
|
|
|
|
_bookings?: Prisma.BookingCreateInput[];
|
|
|
|
|
}
|
|
|
|
|
>;
|
2021-09-02 15:20:36 +00:00
|
|
|
|
}) {
|
|
|
|
|
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
|
|
|
|
);
|
2021-09-28 09:16:02 +00:00
|
|
|
|
for (const eventTypeInput of opts.eventTypes) {
|
|
|
|
|
const { _bookings: bookingInputs = [], ...eventTypeData } = eventTypeInput;
|
2021-09-02 15:20:36 +00:00
|
|
|
|
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) {
|
2021-09-28 09:16:02 +00:00
|
|
|
|
console.log(
|
|
|
|
|
`\t📆 Event type ${eventTypeData.slug} already seems seeded - http://localhost:3000/${user.username}/${eventTypeData.slug}`
|
|
|
|
|
);
|
|
|
|
|
continue;
|
2021-09-14 08:45:28 +00:00
|
|
|
|
}
|
2021-09-28 09:16:02 +00:00
|
|
|
|
const { id } = await prisma.eventType.create({
|
|
|
|
|
data: eventTypeData,
|
|
|
|
|
});
|
2021-09-14 08:45:28 +00:00
|
|
|
|
|
2021-09-02 15:20:36 +00:00
|
|
|
|
console.log(
|
2021-09-28 09:16:02 +00:00
|
|
|
|
`\t📆 Event type ${eventTypeData.slug}, length ${eventTypeData.length}min - http://localhost:3000/${user.username}/${eventTypeData.slug}`
|
2021-09-02 15:20:36 +00:00
|
|
|
|
);
|
2021-09-28 09:16:02 +00:00
|
|
|
|
for (const bookingInput of bookingInputs) {
|
|
|
|
|
await prisma.booking.create({
|
|
|
|
|
data: {
|
|
|
|
|
...bookingInput,
|
|
|
|
|
user: {
|
|
|
|
|
connect: {
|
|
|
|
|
email: opts.user.email,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
attendees: {
|
|
|
|
|
create: {
|
|
|
|
|
email: opts.user.email,
|
|
|
|
|
name: opts.user.name,
|
|
|
|
|
timeZone: "Europe/London",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
eventType: {
|
|
|
|
|
connect: {
|
|
|
|
|
id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
confirmed: bookingInput.confirmed,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
console.log(
|
|
|
|
|
`\t\t☎️ Created booking ${bookingInput.title} at ${new Date(
|
|
|
|
|
bookingInput.startTime
|
|
|
|
|
).toLocaleDateString()}`
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-09-02 15:20:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
2021-09-28 09:16:02 +00:00
|
|
|
|
_bookings: [
|
|
|
|
|
{
|
|
|
|
|
uid: uuid(),
|
|
|
|
|
title: "30min",
|
|
|
|
|
startTime: dayjs().add(1, "day").toDate(),
|
|
|
|
|
endTime: dayjs().add(1, "day").add(30, "minutes").toDate(),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
uid: uuid(),
|
|
|
|
|
title: "30min",
|
|
|
|
|
startTime: dayjs().add(2, "day").toDate(),
|
|
|
|
|
endTime: dayjs().add(2, "day").add(30, "minutes").toDate(),
|
|
|
|
|
confirmed: false,
|
|
|
|
|
},
|
|
|
|
|
],
|
2021-09-02 15:20:36 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: "60min",
|
|
|
|
|
slug: "60min",
|
|
|
|
|
length: 60,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
2021-09-14 08:45:28 +00:00
|
|
|
|
|
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);
|
|
|
|
|
});
|