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

pull/9641/head^2
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
1 changed files with 18 additions and 4 deletions

View File

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