Skip to content

Commit 1660ade

Browse files
authored
cleanup(seed): remove platform team seeding from scripts/seed.ts (calcom#29007)
* refactor(seed): drop platform team fixtures * refactor(seed): drop platform team helpers * fix(seed): restore team fixture users
1 parent f3eefc1 commit 1660ade

1 file changed

Lines changed: 26 additions & 247 deletions

File tree

scripts/seed.ts

Lines changed: 26 additions & 247 deletions
Original file line numberDiff line numberDiff line change
@@ -23,191 +23,6 @@ function hashAPIKey(apiKey: string): string {
2323
return createHash("sha256").update(apiKey).digest("hex");
2424
}
2525

26-
type PlatformUser = {
27-
email: string;
28-
password: string;
29-
username: string;
30-
name: string;
31-
completedOnboarding?: boolean;
32-
timeZone?: string;
33-
role?: UserPermissionRole;
34-
theme?: "dark" | "light";
35-
avatarUrl?: string | null;
36-
};
37-
38-
type AssociateUserAndOrgProps = {
39-
teamId: number;
40-
userId: number;
41-
role: MembershipRole;
42-
username: string;
43-
};
44-
45-
const checkUnpublishedTeam = async (slug: string) => {
46-
return await prisma.team.findFirst({
47-
where: {
48-
metadata: {
49-
path: ["requestedSlug"],
50-
equals: slug,
51-
},
52-
},
53-
});
54-
};
55-
56-
const setupPlatformUser = async (user: PlatformUser) => {
57-
const { password: _password, ...restOfUser } = user;
58-
const userData = {
59-
...restOfUser,
60-
emailVerified: new Date(),
61-
completedOnboarding: user.completedOnboarding ?? true,
62-
locale: "en",
63-
schedules:
64-
(user.completedOnboarding ?? true)
65-
? {
66-
create: {
67-
name: "Working Hours",
68-
availability: {
69-
createMany: {
70-
data: getAvailabilityFromSchedule(DEFAULT_SCHEDULE),
71-
},
72-
},
73-
},
74-
}
75-
: undefined,
76-
};
77-
78-
const platformUser = await prisma.user.upsert({
79-
where: { email_username: { email: user.email, username: user.username } },
80-
update: userData,
81-
create: userData,
82-
});
83-
84-
await prisma.userPassword.upsert({
85-
where: { userId: platformUser.id },
86-
update: {
87-
hash: await hashPassword(user.password),
88-
},
89-
create: {
90-
hash: await hashPassword(user.password),
91-
user: {
92-
connect: {
93-
id: platformUser.id,
94-
},
95-
},
96-
},
97-
});
98-
99-
return platformUser;
100-
};
101-
102-
const createTeam = async (team: Prisma.TeamCreateInput) => {
103-
try {
104-
const requestedSlug = (team.metadata as z.infer<typeof teamMetadataSchema>)?.requestedSlug;
105-
if (requestedSlug) {
106-
const unpublishedTeam = await checkUnpublishedTeam(requestedSlug);
107-
if (unpublishedTeam) {
108-
throw Error("Unique constraint failed on the fields");
109-
}
110-
}
111-
return await prisma.team.create({
112-
data: {
113-
...team,
114-
},
115-
});
116-
} catch (_err) {
117-
if (_err instanceof Error && _err.message.indexOf("Unique constraint failed on the fields") !== -1) {
118-
console.log(`Team '${team.name}' already exists, skipping.`);
119-
return;
120-
}
121-
throw _err;
122-
}
123-
};
124-
125-
const associateUserAndOrg = async ({ teamId, userId, role, username }: AssociateUserAndOrgProps) => {
126-
await prisma.membership.create({
127-
data: {
128-
createdAt: new Date(),
129-
teamId,
130-
userId,
131-
role: role as MembershipRole,
132-
accepted: true,
133-
},
134-
});
135-
136-
const profile = await prisma.profile.create({
137-
data: {
138-
uid: uuid(),
139-
username,
140-
organizationId: teamId,
141-
userId,
142-
},
143-
});
144-
145-
await prisma.user.update({
146-
data: {
147-
movedToProfileId: profile.id,
148-
},
149-
where: {
150-
id: userId,
151-
},
152-
});
153-
};
154-
155-
async function createPlatformAndSetupUser({
156-
teamInput,
157-
user,
158-
}: {
159-
teamInput: Prisma.TeamCreateInput;
160-
user: PlatformUser;
161-
}) {
162-
const team = await createTeam(teamInput);
163-
164-
const platformUser = await setupPlatformUser(user);
165-
166-
console.log(
167-
`👤 Upserted '${user.username}' with email "${user.email}" & password "${user.password}". Booking page 👉 ${process.env.NEXT_PUBLIC_WEBAPP_URL}/${user.username}`
168-
);
169-
170-
const { username } = platformUser;
171-
172-
const membershipRole = MembershipRole.OWNER;
173-
174-
if (team) {
175-
await associateUserAndOrg({
176-
teamId: team.id,
177-
userId: platformUser.id,
178-
role: membershipRole,
179-
username: user.username,
180-
});
181-
182-
await prisma.platformBilling.create({
183-
data: {
184-
id: team?.id,
185-
plan: "SCALE",
186-
customerId: "cus_123",
187-
subscriptionId: "sub_123",
188-
},
189-
});
190-
191-
const clientId = process.env.SEED_PLATFORM_OAUTH_CLIENT_ID;
192-
const secret = process.env.SEED_PLATFORM_OAUTH_CLIENT_SECRET;
193-
194-
if (clientId && secret) {
195-
await prisma.platformOAuthClient.create({
196-
data: {
197-
name: "Acme",
198-
redirectUris: ["http://localhost:4321"],
199-
permissions: 1023,
200-
areEmailsEnabled: true,
201-
organizationId: team.id,
202-
id: clientId,
203-
secret,
204-
},
205-
});
206-
}
207-
console.log(`\t👤 Added '${teamInput.name}' membership for '${username}' with role '${membershipRole}'`);
208-
}
209-
}
210-
21126
async function createTeamAndAddUsers(
21227
teamInput: Prisma.TeamCreateInput,
21328
users: { id: number; username: string; role?: MembershipRole }[] = []
@@ -1159,68 +974,6 @@ async function main() {
1159974
},
1160975
});
1161976

1162-
const admin = await createUserAndEventType({
1163-
user: {
1164-
email: "admin@example.com",
1165-
/** To comply with admin password requirements */
1166-
password: "ADMINadmin2022!",
1167-
username: "admin",
1168-
name: "Admin Example",
1169-
role: "ADMIN",
1170-
},
1171-
});
1172-
1173-
const clientId = process.env.SEED_OAUTH2_CLIENT_ID;
1174-
const clientSecret = process.env.SEED_OAUTH2_CLIENT_SECRET_HASHED;
1175-
1176-
if (clientId && clientSecret) {
1177-
await createOAuthClientForUser(admin.id, {
1178-
clientId,
1179-
clientSecret,
1180-
name: "atoms examples app oauth 2 client",
1181-
purpose: "test atoms examples app with oauth 2",
1182-
redirectUri: "http://localhost:4321",
1183-
websiteUrl: "http://localhost:4321",
1184-
enablePkce: false,
1185-
});
1186-
}
1187-
1188-
await createPlatformAndSetupUser({
1189-
teamInput: {
1190-
name: "Platform Team",
1191-
slug: "platform-admin-team",
1192-
isPlatform: true,
1193-
isOrganization: true,
1194-
eventTypes: {
1195-
createMany: {
1196-
data: [
1197-
{
1198-
title: "Collective Seeded Team Event",
1199-
slug: "collective-seeded-team-event",
1200-
length: 15,
1201-
schedulingType: "COLLECTIVE",
1202-
},
1203-
{
1204-
title: "Round Robin Seeded Team Event",
1205-
slug: "round-robin-seeded-team-event",
1206-
length: 15,
1207-
schedulingType: "ROUND_ROBIN",
1208-
},
1209-
],
1210-
},
1211-
},
1212-
createdAt: new Date(),
1213-
},
1214-
user: {
1215-
email: "platform@example.com",
1216-
/** To comply with admin password requirements */
1217-
password: "PLATFORMadmin2024!",
1218-
username: "platform",
1219-
name: "Platform Admin",
1220-
role: "USER",
1221-
},
1222-
});
1223-
1224977
const pro2UserTeam = await createUserAndEventType({
1225978
user: {
1226979
email: "teampro2@example.com",
@@ -1248,6 +1001,32 @@ async function main() {
12481001
},
12491002
});
12501003

1004+
const admin = await createUserAndEventType({
1005+
user: {
1006+
email: "admin@example.com",
1007+
/** To comply with admin password requirements */
1008+
password: "ADMINadmin2022!",
1009+
username: "admin",
1010+
name: "Admin Example",
1011+
role: "ADMIN",
1012+
},
1013+
});
1014+
1015+
const clientId = process.env.SEED_OAUTH2_CLIENT_ID;
1016+
const clientSecret = process.env.SEED_OAUTH2_CLIENT_SECRET_HASHED;
1017+
1018+
if (clientId && clientSecret) {
1019+
await createOAuthClientForUser(admin.id, {
1020+
clientId,
1021+
clientSecret,
1022+
name: "atoms examples app oauth 2 client",
1023+
purpose: "test atoms examples app with oauth 2",
1024+
redirectUri: "http://localhost:4321",
1025+
websiteUrl: "http://localhost:4321",
1026+
enablePkce: false,
1027+
});
1028+
}
1029+
12511030
if (process.env.E2E_TEST_CALCOM_QA_EMAIL && process.env.E2E_TEST_CALCOM_QA_PASSWORD) {
12521031
await createUserAndEventType({
12531032
user: {

0 commit comments

Comments
 (0)