diff --git a/scripts/seed.ts b/scripts/seed.ts index 32374e80b4..b100c4c0e5 100644 --- a/scripts/seed.ts +++ b/scripts/seed.ts @@ -23,191 +23,6 @@ function hashAPIKey(apiKey: string): string { return createHash("sha256").update(apiKey).digest("hex"); } -type PlatformUser = { - email: string; - password: string; - username: string; - name: string; - completedOnboarding?: boolean; - timeZone?: string; - role?: UserPermissionRole; - theme?: "dark" | "light"; - avatarUrl?: string | null; -}; - -type AssociateUserAndOrgProps = { - teamId: number; - userId: number; - role: MembershipRole; - username: string; -}; - -const checkUnpublishedTeam = async (slug: string) => { - return await prisma.team.findFirst({ - where: { - metadata: { - path: ["requestedSlug"], - equals: slug, - }, - }, - }); -}; - -const setupPlatformUser = async (user: PlatformUser) => { - const { password: _password, ...restOfUser } = user; - const userData = { - ...restOfUser, - emailVerified: new Date(), - completedOnboarding: user.completedOnboarding ?? true, - locale: "en", - schedules: - (user.completedOnboarding ?? true) - ? { - create: { - name: "Working Hours", - availability: { - createMany: { - data: getAvailabilityFromSchedule(DEFAULT_SCHEDULE), - }, - }, - }, - } - : undefined, - }; - - const platformUser = await prisma.user.upsert({ - where: { email_username: { email: user.email, username: user.username } }, - update: userData, - create: userData, - }); - - await prisma.userPassword.upsert({ - where: { userId: platformUser.id }, - update: { - hash: await hashPassword(user.password), - }, - create: { - hash: await hashPassword(user.password), - user: { - connect: { - id: platformUser.id, - }, - }, - }, - }); - - return platformUser; -}; - -const createTeam = async (team: Prisma.TeamCreateInput) => { - try { - const requestedSlug = (team.metadata as z.infer)?.requestedSlug; - if (requestedSlug) { - const unpublishedTeam = await checkUnpublishedTeam(requestedSlug); - if (unpublishedTeam) { - throw Error("Unique constraint failed on the fields"); - } - } - 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 associateUserAndOrg = async ({ teamId, userId, role, username }: AssociateUserAndOrgProps) => { - await prisma.membership.create({ - data: { - createdAt: new Date(), - teamId, - userId, - role: role as MembershipRole, - accepted: true, - }, - }); - - const profile = await prisma.profile.create({ - data: { - uid: uuid(), - username, - organizationId: teamId, - userId, - }, - }); - - await prisma.user.update({ - data: { - movedToProfileId: profile.id, - }, - where: { - id: userId, - }, - }); -}; - -async function createPlatformAndSetupUser({ - teamInput, - user, -}: { - teamInput: Prisma.TeamCreateInput; - user: PlatformUser; -}) { - const team = await createTeam(teamInput); - - const platformUser = await setupPlatformUser(user); - - console.log( - `šŸ‘¤ Upserted '${user.username}' with email "${user.email}" & password "${user.password}". Booking page šŸ‘‰ ${process.env.NEXT_PUBLIC_WEBAPP_URL}/${user.username}` - ); - - const { username } = platformUser; - - const membershipRole = MembershipRole.OWNER; - - if (team) { - await associateUserAndOrg({ - teamId: team.id, - userId: platformUser.id, - role: membershipRole, - username: user.username, - }); - - await prisma.platformBilling.create({ - data: { - id: team?.id, - plan: "SCALE", - customerId: "cus_123", - subscriptionId: "sub_123", - }, - }); - - const clientId = process.env.SEED_PLATFORM_OAUTH_CLIENT_ID; - const secret = process.env.SEED_PLATFORM_OAUTH_CLIENT_SECRET; - - if (clientId && secret) { - await prisma.platformOAuthClient.create({ - data: { - name: "Acme", - redirectUris: ["http://localhost:4321"], - permissions: 1023, - areEmailsEnabled: true, - organizationId: team.id, - id: clientId, - secret, - }, - }); - } - console.log(`\tšŸ‘¤ Added '${teamInput.name}' membership for '${username}' with role '${membershipRole}'`); - } -} - async function createTeamAndAddUsers( teamInput: Prisma.TeamCreateInput, users: { id: number; username: string; role?: MembershipRole }[] = [] @@ -1159,68 +974,6 @@ async function main() { }, }); - const admin = await createUserAndEventType({ - user: { - email: "admin@example.com", - /** To comply with admin password requirements */ - password: "ADMINadmin2022!", - username: "admin", - name: "Admin Example", - role: "ADMIN", - }, - }); - - const clientId = process.env.SEED_OAUTH2_CLIENT_ID; - const clientSecret = process.env.SEED_OAUTH2_CLIENT_SECRET_HASHED; - - if (clientId && clientSecret) { - await createOAuthClientForUser(admin.id, { - clientId, - clientSecret, - name: "atoms examples app oauth 2 client", - purpose: "test atoms examples app with oauth 2", - redirectUri: "http://localhost:4321", - websiteUrl: "http://localhost:4321", - enablePkce: false, - }); - } - - await createPlatformAndSetupUser({ - teamInput: { - name: "Platform Team", - slug: "platform-admin-team", - isPlatform: true, - isOrganization: true, - eventTypes: { - createMany: { - data: [ - { - title: "Collective Seeded Team Event", - slug: "collective-seeded-team-event", - length: 15, - schedulingType: "COLLECTIVE", - }, - { - title: "Round Robin Seeded Team Event", - slug: "round-robin-seeded-team-event", - length: 15, - schedulingType: "ROUND_ROBIN", - }, - ], - }, - }, - createdAt: new Date(), - }, - user: { - email: "platform@example.com", - /** To comply with admin password requirements */ - password: "PLATFORMadmin2024!", - username: "platform", - name: "Platform Admin", - role: "USER", - }, - }); - const pro2UserTeam = await createUserAndEventType({ user: { email: "teampro2@example.com", @@ -1248,6 +1001,32 @@ async function main() { }, }); + const admin = await createUserAndEventType({ + user: { + email: "admin@example.com", + /** To comply with admin password requirements */ + password: "ADMINadmin2022!", + username: "admin", + name: "Admin Example", + role: "ADMIN", + }, + }); + + const clientId = process.env.SEED_OAUTH2_CLIENT_ID; + const clientSecret = process.env.SEED_OAUTH2_CLIENT_SECRET_HASHED; + + if (clientId && clientSecret) { + await createOAuthClientForUser(admin.id, { + clientId, + clientSecret, + name: "atoms examples app oauth 2 client", + purpose: "test atoms examples app with oauth 2", + redirectUri: "http://localhost:4321", + websiteUrl: "http://localhost:4321", + enablePkce: false, + }); + } + if (process.env.E2E_TEST_CALCOM_QA_EMAIL && process.env.E2E_TEST_CALCOM_QA_PASSWORD) { await createUserAndEventType({ user: {