From 21fecd755b5dc46a9af49f1e2c9d6f33505f8176 Mon Sep 17 00:00:00 2001 From: Daniel Emery Date: Sat, 4 Nov 2023 13:00:27 +0100 Subject: [PATCH] #66 Expose new statistics resolver to get individual user statistics --- src/gql.ts | 8 ++++++++ src/index.ts | 2 ++ src/statistics/statistics.dto.ts | 6 ++++++ src/statistics/statistics.gql.ts | 16 ++++++++++++++++ 4 files changed, 32 insertions(+) create mode 100644 src/statistics/statistics.dto.ts create mode 100644 src/statistics/statistics.gql.ts diff --git a/src/gql.ts b/src/gql.ts index 85e03f5..f385b16 100644 --- a/src/gql.ts +++ b/src/gql.ts @@ -116,6 +116,13 @@ const typeDefs = gql` completion: QuizCompletion } + type IndividualUserStatistic { + name: String + email: String! + totalQuizCompletions: Int! + averageScorePercentage: Float! + } + "Available filters for the quizzes query" input QuizFilters { """ @@ -144,6 +151,7 @@ const typeDefs = gql` """ users(first: Int, after: String, sortedBy: UserSortOption): UserConnection me: UserDetails + individualUserStatistics: [IndividualUserStatistic] } type Mutation { diff --git a/src/index.ts b/src/index.ts index 9484164..34ab6d7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,6 +14,7 @@ import typeDefs from './gql'; import { userQueries } from './user/user.gql'; import { quizMutations, quizQueries } from './quiz/quiz.gql'; import { Role } from './user/user.dto'; +import { statisticsQueries } from './statistics/statistics.gql'; const QUIZLORD_VERSION_HEADER = 'X-Quizlord-Api-Version'; @@ -39,6 +40,7 @@ const resolvers = { Query: { ...quizQueries, ...userQueries, + ...statisticsQueries, }, Mutation: { ...quizMutations, diff --git a/src/statistics/statistics.dto.ts b/src/statistics/statistics.dto.ts new file mode 100644 index 0000000..3e760ad --- /dev/null +++ b/src/statistics/statistics.dto.ts @@ -0,0 +1,6 @@ +export interface IndividualUserStatistic { + name?: string; + email: string; + totalQuizCompletions: number; + averageScorePercentage: number; +} diff --git a/src/statistics/statistics.gql.ts b/src/statistics/statistics.gql.ts new file mode 100644 index 0000000..8318ca3 --- /dev/null +++ b/src/statistics/statistics.gql.ts @@ -0,0 +1,16 @@ +import { QuizlordContext } from '..'; +import { authorisationService, statisticsService } from '../service.locator'; +import { IndividualUserStatistic } from './statistics.dto'; + +async function individualUserStatistics( + _p: unknown, + _: void, + context: QuizlordContext, +): Promise { + authorisationService.requireUserRole(context, 'USER'); + return statisticsService.getIndividualUserStatistics(); +} + +export const statisticsQueries = { + individualUserStatistics, +};