From e419187d2cdc8793bf17533eac37ab235db401df Mon Sep 17 00:00:00 2001 From: Miodec Date: Thu, 28 Apr 2022 12:22:54 +0200 Subject: [PATCH] added get stats endpoint closes #2892 --- backend/api/controllers/user.ts | 9 ++++++ backend/api/routes/users.ts | 10 +++++++ backend/dao/user.ts | 12 ++++++++ backend/documentation/public-swagger.json | 34 ++++++++++++++++++++++- 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/backend/api/controllers/user.ts b/backend/api/controllers/user.ts index b075312bb..86472161a 100644 --- a/backend/api/controllers/user.ts +++ b/backend/api/controllers/user.ts @@ -306,6 +306,15 @@ export async function getPersonalBests( return new MonkeyResponse("Personal bests retrieved", data); } +export async function getStats( + req: MonkeyTypes.Request +): Promise { + const { uid } = req.ctx.decodedToken; + + const data = (await UserDAL.getStats(uid)) ?? null; + return new MonkeyResponse("Personal stats retrieved", data); +} + export async function getFavoriteQuotes( req: MonkeyTypes.Request ): Promise { diff --git a/backend/api/routes/users.ts b/backend/api/routes/users.ts index 6f244972e..f32fbc3d8 100644 --- a/backend/api/routes/users.ts +++ b/backend/api/routes/users.ts @@ -307,6 +307,16 @@ router.get( asyncHandler(UserController.getPersonalBests) ); +router.get( + "/stats", + RateLimit.userGet, + authenticateRequest({ + acceptApeKeys: true, + }), + apeRateLimit, + asyncHandler(UserController.getStats) +); + router.get( "/favoriteQuotes", RateLimit.quoteFavoriteGet, diff --git a/backend/dao/user.ts b/backend/dao/user.ts index f731abd46..8b5b2060c 100644 --- a/backend/dao/user.ts +++ b/backend/dao/user.ts @@ -511,6 +511,18 @@ export async function getPersonalBests( return user.personalBests?.[mode]; } +export async function getStats( + uid: string +): Promise<{ [key: string]: number | undefined }> { + const user = await getUser(uid, "get stats"); + + return { + startedTests: user.startedTests, + completedTests: user.completedTests, + timeTyping: user.timeTyping, + }; +} + export async function getFavoriteQuotes( uid ): Promise { diff --git a/backend/documentation/public-swagger.json b/backend/documentation/public-swagger.json index 36dfb2aa4..a6bc6f507 100644 --- a/backend/documentation/public-swagger.json +++ b/backend/documentation/public-swagger.json @@ -10,7 +10,7 @@ "email": "jack@monkeytype.com" } }, - "basePath": "/", + "basePath": "api.monkeytype.com/", "consumes": ["application/json"], "produces": ["application/json"], "tags": [ @@ -54,6 +54,20 @@ } } }, + "/users/stats": { + "get": { + "tags": ["users"], + "summary": "Gets a user's typing stats data", + "responses": { + "200": { + "description": "", + "schema": { + "$ref": "#/definitions/Stats" + } + } + } + } + }, "/leaderboards": { "get": { "tags": ["leaderboards"], @@ -204,6 +218,24 @@ } } }, + "Stats": { + "type": "object", + "properties": { + "startedTests": { + "type": "integer", + "example": 578 + }, + "completedTests": { + "type": "integer", + "example": 451 + }, + "timeTyping": { + "type": "number", + "format": "double", + "example": 3941.6 + } + } + }, "LeaderboardEntry": { "type": "object", "properties": {