From 2f2ee8e214ff15955d70351a48cb238f507ee00e Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 12 Jan 2020 11:47:34 +0100 Subject: [PATCH] Add joined field to Event api type - Add joined field which accepts a userId and returns if the user has joined the event - Add joined function to Event model --- CHANGELOG.md | 1 + src/graphql/schema.graphql | 3 +++ src/lib/models/Event.ts | 17 +++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e56d89..8ed2ad1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - createActivity mutation - activities table - event and eventCount to UserData gql interface +- joined field to Event gql type ### Removed diff --git a/src/graphql/schema.graphql b/src/graphql/schema.graphql index 1d3e953..d7fb96e 100644 --- a/src/graphql/schema.graphql +++ b/src/graphql/schema.graphql @@ -405,6 +405,9 @@ type Event { "The participants of the event." participants(first: Int=10, offset: Int=0): [User!]! + + "Returns if the user with the specified id has joined the event" + joined(userId: Int!): Boolean } "respresents an access token entry with the value as the acutal token and expires as the date the token expires." diff --git a/src/lib/models/Event.ts b/src/lib/models/Event.ts index efd069d..37858c8 100644 --- a/src/lib/models/Event.ts +++ b/src/lib/models/Event.ts @@ -24,13 +24,30 @@ export class Event extends Model { @BelongsToMany(() => User, () => EventParticipant) public rParticipants: User[]; + /** + * Returns the group the event belongs to + */ public async group(): Promise { return await this.$get("rGroup") as Group; } + /** + * Returns the participants of the event + * @param first + * @param offset + */ public async participants({first, offset}: {first: number, offset: number}): Promise { const limit = first ?? 10; offset = offset ?? 0; return await this.$get("rParticipants", {limit, offset}) as User[]; } + + /** + * Returns if the specified user has joined the event + * @param userId + */ + public async joined({userId}: {userId: number}): Promise { + const participants = await this.$get("rParticipants", {where: {id: userId}}) as User[]; + return participants.length !== 0; + } }