improve seeder with users within new teams, fixed host-users avatars on collective

This commit is contained in:
Alan 2023-09-11 18:51:23 -07:00
parent 49944c223f
commit 472b955ef4
3 changed files with 151 additions and 61 deletions

View File

@ -394,7 +394,9 @@ export const EventTypeList = ({ data, readonly }: EventTypeListProps): JSX.Eleme
const isChildrenManagedEventType = !!eventType.parentId; const isChildrenManagedEventType = !!eventType.parentId;
const isReadOnly = readonly || isChildrenManagedEventType; const isReadOnly = readonly || isChildrenManagedEventType;
const hosts = !!eventType?.hosts?.length
? eventType?.hosts.map((user) => user.user)
: eventType.users;
return ( return (
<li key={eventType.id}> <li key={eventType.id}>
<div className="hover:bg-muted flex w-full items-center justify-between"> <div className="hover:bg-muted flex w-full items-center justify-between">
@ -413,15 +415,13 @@ export const EventTypeList = ({ data, readonly }: EventTypeListProps): JSX.Eleme
className="relative right-3 top-1" className="relative right-3 top-1"
size="sm" size="sm"
truncateAfter={4} truncateAfter={4}
items={eventType.users.map( items={hosts.map((organizer: { name: string | null; username: string | null }) => ({
(organizer: { name: string | null; username: string | null }) => ({ alt: organizer.name || "",
alt: organizer.name || "", image: `${orgBranding?.fullDomain ?? WEBAPP_URL}/${
image: `${orgBranding?.fullDomain ?? WEBAPP_URL}/${ organizer.username
organizer.username }/avatar.png`,
}/avatar.png`, title: organizer.name || "",
title: organizer.name || "", }))}
})
)}
/> />
)} )}
{isManagedEventType && eventType.children?.length > 0 && ( {isManagedEventType && eventType.children?.length > 0 && (

View File

@ -26,36 +26,54 @@ async function createEventTypesForUserAndForTeams() {
} }
async function createManyEventTypes({ userId, teamId }: { userId?: number; teamId?: number }) { async function createManyEventTypes({ userId, teamId }: { userId?: number; teamId?: number }) {
const eventTypes: Prisma.EventTypeUncheckedCreateInput[] = []; // If teamId is provided then we fetch membership users from that team
for (let i = 0; i < 20; i++) {
eventTypes.push({ const membershipUsers: number[] = [];
title: `Event Type ${i + 1}`, if (teamId) {
slug: `event-type-${teamId ? `team-${teamId}` : ""}${i + 1}`, const memberships = await prisma.membership.findMany({ where: { teamId } });
userId: teamId ? null : userId, for (const membership of memberships) {
teamId: teamId ? teamId : null, if (!membership.userId) {
length: 30, // Do nothing
description: "This is a description", } else {
}); membershipUsers.push(membership.userId);
}
}
} }
await prisma.eventType.createMany({ const eventTypesPromises: Promise<
data: eventTypes, Prisma.EventTypeGetPayload<{
}); select: {
id: true;
schedulingType: true;
teamId: true;
};
}>
>[] = [];
// Load event types recently created for (let i = 0; i < 20; i++) {
const eventTypesFound = await prisma.eventType.findMany({ const schedulingType = randomSchedulingType();
where: { eventTypesPromises.push(
slug: { prisma.eventType.create({
startsWith: `event-type-${teamId ? "teams-" : ""}`, data: {
}, title: `${schedulingType.toLowerCase() ?? ""} Event Type ${i + 1} - ${
}, teamId ? `Team ${teamId}` : ""
select: { }`,
id: true, slug: `event-type-${teamId ? `team-${teamId}` : ""}${i + 1}`,
}, userId: teamId ? null : userId,
}); teamId: teamId ? teamId : null,
length: 30,
description: "This is a description",
...(teamId ? { schedulingType: schedulingType } : {}),
},
})
);
}
const resultEventTypes = await Promise.all(eventTypesPromises);
const updateRelationships: Promise<any>[] = []; const updateRelationships: Promise<any>[] = [];
for (const eventType of eventTypesFound) { for (const eventType of resultEventTypes) {
const schedulingType = eventType.schedulingType;
updateRelationships.push( updateRelationships.push(
prisma.eventType.update({ prisma.eventType.update({
where: { where: {
@ -67,6 +85,16 @@ async function createManyEventTypes({ userId, teamId }: { userId?: number; teamI
id: userId, id: userId,
}, },
}, },
...(schedulingType === "COLLECTIVE" || schedulingType === "ROUND_ROBIN"
? {
hosts: {
create: membershipUsers.map((userId) => ({
userId,
isFixed: schedulingType === "COLLECTIVE" ? true : false,
})),
},
}
: {}),
}, },
}) })
); );
@ -75,42 +103,53 @@ async function createManyEventTypes({ userId, teamId }: { userId?: number; teamI
} }
async function createManyTeams(userId: number) { async function createManyTeams(userId: number) {
const teams: Prisma.TeamUncheckedCreateInput[] = []; const teamsPromises: Promise<
Prisma.TeamGetPayload<{
select: {
id: true;
};
}>
>[] = [];
for (let i = 0; i < 20; i++) { for (let i = 0; i < 20; i++) {
teams.push({ teamsPromises.push(
name: `Team ${i + 1}`, prisma.team.create({
slug: `team-${i + 1}`, data: {
}); name: `Team ${i + 1}`,
slug: `team-${i + 1}`,
},
})
);
} }
const createdTeams = await prisma.team.createMany({ const result = await Promise.all(teamsPromises);
data: teams, if (result.length > 0) {
}); console.log(`👥 Created ${result.length} teams`);
if (!createdTeams.count) {
throw new Error("No team created");
} }
const teamIds = result.map((team) => team.id);
// Load teams recently created const usersForTeam = await createManyUsers();
const teamsFound = await prisma.team.findMany({
where: { if (usersForTeam.length > 0) {
slug: { console.log(`👥 Created ${usersForTeam.length} users for teams`);
startsWith: "team-", }
},
},
select: {
id: true,
},
});
// Create memberships for the user // Create memberships for the user
const memberships: Prisma.MembershipUncheckedCreateInput[] = []; const memberships: Prisma.MembershipUncheckedCreateInput[] = [];
for (const team of teamsFound) { for (const teamId of teamIds) {
for (const userId of usersForTeam) {
memberships.push({
userId: userId,
teamId: teamId,
role: randomTeamRole(),
accepted: true,
});
}
// Add main user to all teams as Owner
memberships.push({ memberships.push({
userId, userId: userId,
teamId: team.id, teamId: teamId,
role: "ADMIN", role: "OWNER",
accepted: true, accepted: true,
}); });
} }
@ -139,4 +178,44 @@ async function createManyTeams(userId: number) {
} }
} }
async function createManyUsers() {
const userPromises: Promise<
Prisma.UserGetPayload<{
select: { id: true };
}>
>[] = [];
for (let i = 0; i < 20; i++) {
userPromises.push(
prisma.user.create({
data: {
username: `user-${i + 1}`,
email: `user-${i + 1}`,
password: "password",
name: `User ${i + 1}`,
role: randomUserRole(),
completedOnboarding: true,
},
})
);
}
const result = await Promise.all(userPromises);
return result.map((user) => user.id);
}
createEventTypesForUserAndForTeams(); createEventTypesForUserAndForTeams();
function randomUserRole(): any {
const roles = ["USER", "ADMIN"];
return roles[Math.floor(Math.random() * roles.length)];
}
function randomTeamRole(): any {
const roles = ["ADMIN", "OWNER", "MEMBER"];
return roles[Math.floor(Math.random() * roles.length)];
}
function randomSchedulingType(): any {
const roles = ["MANAGED", "COLLECTIVE", "ROUND_ROBIN"];
return roles[Math.floor(Math.random() * roles.length)];
}

View File

@ -63,6 +63,17 @@ export const paginateHandler = async ({ ctx, input }: EventTypesPaginateProps) =
name: true, name: true,
}, },
}, },
hosts: {
select: {
user: {
select: {
id: true,
username: true,
name: true,
},
},
},
},
children: { children: {
select: { select: {
id: true, id: true,