Created secondary function to read the owner from users array (#9638)

This commit is contained in:
Alex van Andel 2023-06-19 17:53:31 +02:00 committed by GitHub
parent a5e25b9b28
commit 361108246e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -167,6 +167,11 @@ export const getPublicEvent = async (username: string, eventSlug: string, prisma
const eventMetaData = EventTypeMetaDataSchema.parse(event.metadata || {});
const users = getUsersFromEvent(event) || (await getOwnerFromUsersArray(prisma, event.id));
if (users === null) {
throw new Error("Event has no owner");
}
return {
...event,
bookerLayouts: bookerLayoutsSchema.parse(eventMetaData?.bookerLayouts || null),
@ -178,7 +183,7 @@ export const getPublicEvent = async (username: string, eventSlug: string, prisma
recurringEvent: isRecurringEvent(event.recurringEvent) ? parseRecurringEvent(event.recurringEvent) : null,
// Sets user data on profile object for easier access
profile: getProfileFromEvent(event),
users: getUsersFromEvent(event),
users,
};
};
@ -234,13 +239,22 @@ function getUsersFromEvent(event: Event) {
if (team) {
return (hosts || []).map(mapHostsToUsers);
}
if (!owner) throw new Error("Event has no owner");
if (!owner) {
return null;
}
const { username, name, weekStart } = owner;
return [{ username, name, weekStart }];
}
async function getOwnerFromUsersArray(prisma: PrismaClient, eventTypeId: number) {
const { users } = await prisma.eventType.findUniqueOrThrow({
where: { id: eventTypeId },
select: { users: { select: { username: true, name: true, weekStart: true } } },
});
if (!users.length) return null;
return [users[0]];
}
function mapHostsToUsers(host: { user: Pick<User, "username" | "name" | "weekStart"> }) {
return {
username: host.user.username,