diff --git a/scripts/seed.ts b/scripts/seed.ts index dc166327dc..c47181bd82 100644 --- a/scripts/seed.ts +++ b/scripts/seed.ts @@ -1,4 +1,4 @@ -import { Prisma, PrismaClient, UserPlan } from "@prisma/client"; +import { MembershipRole, Prisma, PrismaClient, UserPlan } from "@prisma/client"; import dayjs from "dayjs"; import { uuid } from "short-uuid"; @@ -34,8 +34,9 @@ async function createUserAndEventType(opts: { }); console.log( - `šŸ‘¤ Upserted '${opts.user.username}' with email "${opts.user.email}" & password "${opts.user.password}". Booking page šŸ‘‰ http://localhost:3000/${opts.user.username}` + `šŸ‘¤ Upserted '${opts.user.username}' with email "${opts.user.email}" & password "${opts.user.password}". Booking page šŸ‘‰ ${process.env.BASE_URL}/${opts.user.username}` ); + for (const eventTypeInput of opts.eventTypes) { const { _bookings: bookingInputs = [], ...eventTypeData } = eventTypeInput; eventTypeData.userId = user.id; @@ -57,7 +58,7 @@ async function createUserAndEventType(opts: { if (eventType) { console.log( - `\tšŸ“† Event type ${eventTypeData.slug} already seems seeded - http://localhost:3000/${user.username}/${eventTypeData.slug}` + `\tšŸ“† Event type ${eventTypeData.slug} already seems seeded - ${process.env.BASE_URL}/${user.username}/${eventTypeData.slug}` ); continue; } @@ -66,7 +67,7 @@ async function createUserAndEventType(opts: { }); console.log( - `\tšŸ“† Event type ${eventTypeData.slug}, length ${eventTypeData.length}min - http://localhost:3000/${user.username}/${eventTypeData.slug}` + `\tšŸ“† Event type ${eventTypeData.slug}, length ${eventTypeData.length}min - ${process.env.BASE_URL}/${user.username}/${eventTypeData.slug}` ); for (const bookingInput of bookingInputs) { await prisma.booking.create({ @@ -99,6 +100,49 @@ async function createUserAndEventType(opts: { ); } } + + return user; +} + +async function createTeamAndAddUsers( + teamInput: Prisma.TeamCreateInput, + users: { id: number; username: string; role?: MembershipRole }[] +) { + const createTeam = async (team: Prisma.TeamCreateInput) => { + try { + return await prisma.team.create({ + data: { + ...team, + }, + }); + } catch (_err) { + if (_err instanceof Error && _err.message.indexOf("Unique constraint failed on the fields") !== -1) { + console.log(`Team '${team.name}' already exists, skipping.`); + return; + } + throw _err; + } + }; + + const team = await createTeam(teamInput); + if (!team) { + return; + } + + console.log(`šŸ¢ Created team '${teamInput.name}' - ${process.env.BASE_URL}/team/${team.slug}`); + + for (const user of users) { + const { role = MembershipRole.OWNER, id, username } = user; + await prisma.membership.create({ + data: { + teamId: team.id, + userId: id, + role: role, + accepted: true, + }, + }); + console.log(`\tšŸ‘¤ Added '${teamInput.name}' membership for '${username}' with role '${role}'`); + } } async function main() { @@ -218,6 +262,45 @@ async function main() { ], }); + const freeUserTeam = await createUserAndEventType({ + user: { + email: "teamfree@example.com", + password: "teamfree", + username: "teamfree", + name: "Team Free Example", + plan: "FREE", + }, + eventTypes: [], + }); + + const proUserTeam = await createUserAndEventType({ + user: { + email: "teampro@example.com", + password: "teampro", + username: "teampro", + name: "Team Pro Example", + plan: "PRO", + }, + eventTypes: [], + }); + + await createTeamAndAddUsers( + { + name: "Seeded Team", + slug: "seeded-team", + }, + [ + { + id: proUserTeam.id, + username: proUserTeam.name || "Unknown", + }, + { + id: freeUserTeam.id, + username: freeUserTeam.name || "Unknown", + }, + ] + ); + await prisma.$disconnect(); }