Skip to content

chore/confusing-reading-of-own-registration #463

@JohanHjelsethStorstad

Description

@JohanHjelsethStorstad

I was merging some code when I saw that the users own event registration is returned in the read operation of the event service as an array of only 1 or 0 length. I think this is very confusing. The field should just be called usersOwnRegistration ad point to one registration (or null I guess) not an array. Ideally I also think this should be a separate operation in the registration service like "read own registration" or alternatively "read registration of user" taking event id and user id and that service can then just be called with the sessions own user id.

defineOperation({
    paramsSchema: z.object({
        id: z.number(),
    }),
    authorizer: () => eventAuth.read.dynamicFields({}),
    operation: async ({ prisma, params, session }) => {
        const event = await prisma.event.findUniqueOrThrow({
            where: {
                id: params.id,
            },
            include: {
                coverImage: {
                    include: {
                        image: true
                    }
                },
                paragraph: true,
                eventTagEvents: {
                    include: {
                        tag: true
                    }
                },
                _count: {
                    select: {
                        eventRegistrations: true,
                    },
                },
                eventRegistrations: true,
            }
        })

        let onWaitingList = false

        if (!session.user) {
            event.eventRegistrations = []
        } else {
            const indexOfUser = event.eventRegistrations.findIndex(reg => reg.userId === session.user.id)
            if (indexOfUser !== -1) {
                event.eventRegistrations = [event.eventRegistrations[indexOfUser]]
                onWaitingList = indexOfUser >= event.places
            } else {
                event.eventRegistrations = []
            }
        }

        if (onWaitingList && !event.waitingList) {
            onWaitingList = false
            event.eventRegistrations = []
        }

        return {
            ...event,
            numOfRegistrations: Math.min(event._count.eventRegistrations, event.places),
            numOnWaitingList: Math.max(0, event._count.eventRegistrations - event.places),
            onWaitingList,
            tags: event.eventTagEvents.map(ete => ete.tag)
        }
    }
})

The frontend currently reads:

const event = unwrapActionReturn(await readEventAction({
        params: {
            id: decodeVevenUriHandleError((await params).nameAndId)
        }
    }))
const tags = unwrapActionReturn(await readEventTagsAction())
const ownRegitration = event.eventRegistrations.length ? event.eventRegistrations[0] : undefined

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions