Compare commits
	
		
			18 Commits
		
	
	
		
			v0.10.1
			...
			6163f0a90b
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 6163f0a90b | |||
| 8f0f795a70 | |||
| 22cae39bd3 | |||
| 0b07a53ed2 | |||
| d4a02e7db2 | |||
| b9a7dc84f0 | |||
| 7111068361 | |||
| 63964fbf2c | |||
| cbcb829fbd | |||
| 057ae0d797 | |||
| 257f320ee3 | |||
| 7b15c2d88b | |||
| 988f17a795 | |||
| 4471e57438 | |||
| 51daf969cf | |||
| cb71fcd13b | |||
| a6a526dc5d | |||
| dd6d799c84 | 
| @@ -4,15 +4,17 @@ All notable changes to this project will be documented in this file. Dates are d | ||||
|  | ||||
| #### [v0.10.1](https://git.odit.services/lfk/backend/compare/v0.10.0...v0.10.1) | ||||
|  | ||||
| - Merge pull request 'Release 0.10.1' (#189) from dev into main [`e89e07d`](https://git.odit.services/lfk/backend/commit/e89e07d0fc99f14148b01204fb8ed39e2da77e38) | ||||
| - 🧾New changelog file version [CI SKIP] [skip ci] [`69afd4d`](https://git.odit.services/lfk/backend/commit/69afd4d5877401eb46df430f43a7feb273abda1e) | ||||
| - 🚀Bumped version to v0.10.1 [`24d152f`](https://git.odit.services/lfk/backend/commit/24d152fdc8fe17fffa2f2a718d7145ba8a91d79c) | ||||
| - New class: ResponseSelfServiceDonor [`d70c5b1`](https://git.odit.services/lfk/backend/commit/d70c5b1bbc9f02782f8755b6929e2d3458e10221) | ||||
| - 🧾New changelog file version [CI SKIP] [skip ci] [`4279e43`](https://git.odit.services/lfk/backend/commit/4279e4374304887e8db40eab77763b20bbce91a1) | ||||
| - Removed duplicate openapi statement [`4834a66`](https://git.odit.services/lfk/backend/commit/4834a6698b0958602421c1478a95fec7edda910b) | ||||
| - Merge pull request 'Selfservice donations reformatting feature/187-selfservice_donation' (#188) from feature/187-selfservice_donation into dev [`d837654`](https://git.odit.services/lfk/backend/commit/d837654617f7de5d055ffb06c65e2cd52f65c604) | ||||
| - New class: ResponseSelfServiceDonor [`d70c5b1`](https://git.odit.services/lfk/backend/commit/d70c5b1bbc9f02782f8755b6929e2d3458e10221) | ||||
| - Switched selfservice donation.donor from string to object [`0767943`](https://git.odit.services/lfk/backend/commit/0767943721b6964d542f580c541e744f86444ac6) | ||||
| - Adjusted runner property names [`ca87774`](https://git.odit.services/lfk/backend/commit/ca87774767807a2c4bc869b0de95cc73832a8405) | ||||
| - 🧾New changelog file version [CI SKIP] [skip ci] [`71e3d0e`](https://git.odit.services/lfk/backend/commit/71e3d0efe2cbde47aea0f26cb5a8b5cd3312707d) | ||||
| - 🧾New changelog file version [CI SKIP] [skip ci] [`c28843c`](https://git.odit.services/lfk/backend/commit/c28843c405dc4fd06a10f0fb85814acede15a769) | ||||
| - Merge pull request 'Selfservice donations reformatting feature/187-selfservice_donation' (#188) from feature/187-selfservice_donation into dev [`d837654`](https://git.odit.services/lfk/backend/commit/d837654617f7de5d055ffb06c65e2cd52f65c604) | ||||
| - Added new responsetype for new class [`f693f2c`](https://git.odit.services/lfk/backend/commit/f693f2cde9a04147155aea4de5d52e1d19d722ca) | ||||
|  | ||||
| #### [v0.10.0](https://git.odit.services/lfk/backend/compare/v0.9.2...v0.10.0) | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| import { Get, JsonController, UseBefore } from 'routing-controllers'; | ||||
| import { Get, JsonController, QueryParam, UseBefore } from 'routing-controllers'; | ||||
| import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; | ||||
| import { getConnection } from 'typeorm'; | ||||
| import StatsAuth from '../middlewares/StatsAuth'; | ||||
| @@ -7,6 +7,7 @@ import { Runner } from '../models/entities/Runner'; | ||||
| import { RunnerOrganization } from '../models/entities/RunnerOrganization'; | ||||
| import { RunnerTeam } from '../models/entities/RunnerTeam'; | ||||
| import { Scan } from '../models/entities/Scan'; | ||||
| import { TrackScan } from '../models/entities/TrackScan'; | ||||
| import { User } from '../models/entities/User'; | ||||
| import { ResponseStats } from '../models/responses/ResponseStats'; | ||||
| import { ResponseStatsOrgnisation } from '../models/responses/ResponseStatsOrganization'; | ||||
| @@ -36,7 +37,11 @@ export class StatsController { | ||||
|     @OpenAPI({ description: "Returns the top ten runners by distance.", security: [{ "StatsApiToken": [] }, { "AuthToken": [] }, { "RefreshTokenCookie": [] }] }) | ||||
|     async getTopRunnersByDistance() { | ||||
|         let runners = await getConnection().getRepository(Runner).find({ relations: ['scans', 'group', 'distanceDonations', 'scans.track'] }); | ||||
|         let topRunners = runners.sort((runner1, runner2) => runner1.distance - runner2.distance).slice(0, 9); | ||||
|         if (!runners || runners.length == 0) { | ||||
|             return []; | ||||
|         } | ||||
|         let topRunners = runners.sort((runner1, runner2) => runner2.distance - runner1.distance).slice(0, 10); | ||||
|         return runners; | ||||
|         let responseRunners: ResponseStatsRunner[] = new Array<ResponseStatsRunner>(); | ||||
|         topRunners.forEach(runner => { | ||||
|             responseRunners.push(new ResponseStatsRunner(runner)); | ||||
| @@ -50,7 +55,10 @@ export class StatsController { | ||||
|     @OpenAPI({ description: "Returns the top ten runners by donations.", security: [{ "StatsApiToken": [] }, { "AuthToken": [] }, { "RefreshTokenCookie": [] }] }) | ||||
|     async getTopRunnersByDonations() { | ||||
|         let runners = await getConnection().getRepository(Runner).find({ relations: ['scans', 'group', 'distanceDonations', 'scans.track'] }); | ||||
|         let topRunners = runners.sort((runner1, runner2) => runner1.distanceDonationAmount - runner2.distanceDonationAmount).slice(0, 9); | ||||
|         if (!runners || runners.length == 0) { | ||||
|             return []; | ||||
|         } | ||||
|         let topRunners = runners.sort((runner1, runner2) => runner2.distanceDonationAmount - runner1.distanceDonationAmount).slice(0, 10); | ||||
|         let responseRunners: ResponseStatsRunner[] = new Array<ResponseStatsRunner>(); | ||||
|         topRunners.forEach(runner => { | ||||
|             responseRunners.push(new ResponseStatsRunner(runner)); | ||||
| @@ -58,6 +66,34 @@ export class StatsController { | ||||
|         return responseRunners; | ||||
|     } | ||||
|  | ||||
|     @Get("/runners/laptime") | ||||
|     @UseBefore(StatsAuth) | ||||
|     @ResponseSchema(ResponseStatsRunner, { isArray: true }) | ||||
|     @OpenAPI({ description: "Returns the top ten runners by fastest laptime on your selected track (track by id).", security: [{ "StatsApiToken": [] }, { "AuthToken": [] }, { "RefreshTokenCookie": [] }] }) | ||||
|     async getTopRunnersByLaptime(@QueryParam("track") track: number) { | ||||
|         let scans = await getConnection().getRepository(TrackScan).find({ relations: ['track', 'runner', 'runner.group', 'runner.scans', 'runner.scans.track', 'runner.distanceDonations'] }); | ||||
|         if (!scans || scans.length == 0) { | ||||
|             return []; | ||||
|         } | ||||
|         scans = scans.filter((s) => { return s.track.id == track && s.valid == true && s.lapTime != 0 }).sort((scan1, scan2) => scan1.lapTime - scan2.lapTime); | ||||
|  | ||||
|         let topScans = new Array<TrackScan>(); | ||||
|         let knownRunners = new Array<number>(); | ||||
|         for (let i = 0; i < scans.length && topScans.length < 10; i++) { | ||||
|             const element = scans[i]; | ||||
|             if (!knownRunners.includes(element.runner.id)) { | ||||
|                 topScans.push(element); | ||||
|                 knownRunners.push(element.runner.id); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         let responseRunners: ResponseStatsRunner[] = new Array<ResponseStatsRunner>(); | ||||
|         topScans.forEach(scan => { | ||||
|             responseRunners.push(new ResponseStatsRunner(scan.runner, scan.lapTime)); | ||||
|         }); | ||||
|         return responseRunners; | ||||
|     } | ||||
|  | ||||
|     @Get("/scans") | ||||
|     @UseBefore(StatsAuth) | ||||
|     @ResponseSchema(ResponseStatsRunner, { isArray: true }) | ||||
| @@ -72,7 +108,10 @@ export class StatsController { | ||||
|     @OpenAPI({ description: "Returns the top ten teams by distance.", security: [{ "StatsApiToken": [] }, { "AuthToken": [] }, { "RefreshTokenCookie": [] }] }) | ||||
|     async getTopTeamsByDistance() { | ||||
|         let teams = await getConnection().getRepository(RunnerTeam).find({ relations: ['runners', 'runners.scans', 'runners.distanceDonations', 'runners.scans.track'] }); | ||||
|         let topTeams = teams.sort((team1, team2) => team1.distance - team2.distance).slice(0, 9); | ||||
|         if (!teams || teams.length == 0) { | ||||
|             return []; | ||||
|         } | ||||
|         let topTeams = teams.sort((team1, team2) => team2.distance - team1.distance).slice(0, 10); | ||||
|         let responseTeams: ResponseStatsTeam[] = new Array<ResponseStatsTeam>(); | ||||
|         topTeams.forEach(team => { | ||||
|             responseTeams.push(new ResponseStatsTeam(team)); | ||||
| @@ -86,7 +125,10 @@ export class StatsController { | ||||
|     @OpenAPI({ description: "Returns the top ten teams by donations.", security: [{ "StatsApiToken": [] }, { "AuthToken": [] }, { "RefreshTokenCookie": [] }] }) | ||||
|     async getTopTeamsByDonations() { | ||||
|         let teams = await getConnection().getRepository(RunnerTeam).find({ relations: ['runners', 'runners.scans', 'runners.distanceDonations', 'runners.scans.track'] }); | ||||
|         let topTeams = teams.sort((team1, team2) => team1.distanceDonationAmount - team2.distanceDonationAmount).slice(0, 9); | ||||
|         if (!teams || teams.length == 0) { | ||||
|             return []; | ||||
|         } | ||||
|         let topTeams = teams.sort((team1, team2) => team2.distanceDonationAmount - team1.distanceDonationAmount).slice(0, 10); | ||||
|         let responseTeams: ResponseStatsTeam[] = new Array<ResponseStatsTeam>(); | ||||
|         topTeams.forEach(team => { | ||||
|             responseTeams.push(new ResponseStatsTeam(team)); | ||||
| @@ -100,7 +142,10 @@ export class StatsController { | ||||
|     @OpenAPI({ description: "Returns the top ten organizations by distance.", security: [{ "StatsApiToken": [] }, { "AuthToken": [] }, { "RefreshTokenCookie": [] }] }) | ||||
|     async getTopOrgsByDistance() { | ||||
|         let orgs = await getConnection().getRepository(RunnerOrganization).find({ relations: ['runners', 'runners.scans', 'runners.distanceDonations', 'runners.scans.track', 'teams', 'teams.runners', 'teams.runners.scans', 'teams.runners.distanceDonations', 'teams.runners.scans.track'] }); | ||||
|         let topOrgs = orgs.sort((org1, org2) => org1.distance - org2.distance).slice(0, 9); | ||||
|         if (!orgs || orgs.length == 0) { | ||||
|             return []; | ||||
|         } | ||||
|         let topOrgs = orgs.sort((org1, org2) => org2.distance - org1.distance).slice(0, 10); | ||||
|         let responseOrgs: ResponseStatsOrgnisation[] = new Array<ResponseStatsOrgnisation>(); | ||||
|         topOrgs.forEach(org => { | ||||
|             responseOrgs.push(new ResponseStatsOrgnisation(org)); | ||||
| @@ -114,7 +159,10 @@ export class StatsController { | ||||
|     @OpenAPI({ description: "Returns the top ten organizations by donations.", security: [{ "StatsApiToken": [] }, { "AuthToken": [] }, { "RefreshTokenCookie": [] }] }) | ||||
|     async getTopOrgsByDonations() { | ||||
|         let orgs = await getConnection().getRepository(RunnerOrganization).find({ relations: ['runners', 'runners.scans', 'runners.distanceDonations', 'runners.scans.track', 'teams', 'teams.runners', 'teams.runners.scans', 'teams.runners.distanceDonations', 'teams.runners.scans.track'] }); | ||||
|         let topOrgs = orgs.sort((org1, org2) => org1.distanceDonationAmount - org2.distanceDonationAmount).slice(0, 9); | ||||
|         if (!orgs || orgs.length == 0) { | ||||
|             return []; | ||||
|         } | ||||
|         let topOrgs = orgs.sort((org1, org2) => org2.distanceDonationAmount - org1.distanceDonationAmount).slice(0, 10); | ||||
|         let responseOrgs: ResponseStatsOrgnisation[] = new Array<ResponseStatsOrgnisation>(); | ||||
|         topOrgs.forEach(org => { | ||||
|             responseOrgs.push(new ResponseStatsOrgnisation(org)); | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| import { | ||||
|     IsInt, | ||||
|     IsObject, | ||||
|     IsOptional, | ||||
|     IsString | ||||
| } from "class-validator"; | ||||
| import { Runner } from '../entities/Runner'; | ||||
| @@ -55,6 +56,13 @@ export class ResponseStatsRunner implements IResponse { | ||||
|     @IsInt() | ||||
|     donationAmount: number; | ||||
|  | ||||
|     /** | ||||
|      * The runner's fastest laptime in seconds. | ||||
|      */ | ||||
|     @IsInt() | ||||
|     @IsOptional() | ||||
|     minLaptime?: number; | ||||
|  | ||||
|     /** | ||||
|      * The runner's group. | ||||
|      */ | ||||
| @@ -65,13 +73,16 @@ export class ResponseStatsRunner implements IResponse { | ||||
|      * Creates a new runner stats response from a runner | ||||
|      * @param runner The runner whoes response shall be generated - the following relations have to be resolved: scans, group, distanceDonations, scans.track | ||||
|      */ | ||||
|     public constructor(runner: Runner) { | ||||
|     public constructor(runner: Runner, laptime?: number) { | ||||
|         this.id = runner.id; | ||||
|         this.firstname = runner.firstname; | ||||
|         this.middlename = runner.middlename; | ||||
|         this.lastname = runner.lastname; | ||||
|         this.distance = runner.distance; | ||||
|         this.donationAmount = runner.distanceDonationAmount; | ||||
|         if (laptime) { | ||||
|             this.minLaptime = laptime; | ||||
|         } | ||||
|         this.group = runner.group.toResponse(); | ||||
|     } | ||||
| } | ||||
|   | ||||
							
								
								
									
										90
									
								
								src/tests/stats/stats_get.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								src/tests/stats/stats_get.spec.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,90 @@ | ||||
| import axios from 'axios'; | ||||
| import { config } from '../../config'; | ||||
| const base = "http://localhost:" + config.internal_port | ||||
|  | ||||
| let axios_config_full; | ||||
| let axios_config_stats; | ||||
|  | ||||
| beforeAll(async () => { | ||||
|     jest.setTimeout(20000); | ||||
|     const res = await axios.post(base + '/api/auth/login', { username: "demo", password: "demo" }); | ||||
|     let access_token = res.data["access_token"]; | ||||
|     axios_config_full = { | ||||
|         headers: { "authorization": "Bearer " + access_token }, | ||||
|         validateStatus: undefined | ||||
|     }; | ||||
|     const res2 = await axios.post(base + '/api/statsclients', { username: "demo", password: "demo" }, axios_config_full); | ||||
|     access_token = res2.data["key"]; | ||||
|     axios_config_stats = { | ||||
|         headers: { "authorization": "Bearer " + access_token }, | ||||
|         validateStatus: undefined | ||||
|     }; | ||||
| }); | ||||
|  | ||||
| describe('GET /api/stats/runners/distance w/o auth should return 200', () => { | ||||
|     it('get with invalid token should return 401', async () => { | ||||
|         const res = await axios.get(base + '/api/stats/runners/distance', { | ||||
|             headers: { "authorization": "Bearer 123123123123123123" }, | ||||
|             validateStatus: undefined | ||||
|         }); | ||||
|         expect(res.status).toEqual(401); | ||||
|     }); | ||||
| }); | ||||
| // --------------- | ||||
| describe('GET /api/stats should return 200', () => { | ||||
|     it('get w/o auth should return 200', async () => { | ||||
|         const res = await axios.get(base + '/api/stats', { validateStatus: undefined }); | ||||
|         expect(res.status).toEqual(200); | ||||
|         expect(res.headers['content-type']).toContain("application/json"); | ||||
|     }); | ||||
|     it('get w/ auth should return 200', async () => { | ||||
|         const res = await axios.get(base + '/api/stats', axios_config_stats); | ||||
|         expect(res.status).toEqual(200); | ||||
|         expect(res.headers['content-type']).toContain("application/json"); | ||||
|     }); | ||||
| }); | ||||
| // --------------- | ||||
| describe('GET /api/stats/runners/* should return 200', () => { | ||||
|     it('get by distance w/ auth should return 200', async () => { | ||||
|         const res = await axios.get(base + '/api/stats/runners/distance', axios_config_stats); | ||||
|         expect(res.status).toEqual(200); | ||||
|         expect(res.headers['content-type']).toContain("application/json"); | ||||
|     }); | ||||
|     it('get by donations w/ auth should return 200', async () => { | ||||
|         const res = await axios.get(base + '/api/stats/runners/donations', axios_config_stats); | ||||
|         console.log(res.data) | ||||
|         expect(res.status).toEqual(200); | ||||
|         expect(res.headers['content-type']).toContain("application/json"); | ||||
|     }); | ||||
|     it('get by laptime w/ auth should return 200', async () => { | ||||
|         const res = await axios.get(base + '/api/stats/runners/laptime', axios_config_stats); | ||||
|         expect(res.status).toEqual(200); | ||||
|         expect(res.headers['content-type']).toContain("application/json"); | ||||
|     }); | ||||
| }); | ||||
| // --------------- | ||||
| describe('GET /api/stats/teams/* should return 200', () => { | ||||
|     it('get by distance w/ auth should return 200', async () => { | ||||
|         const res = await axios.get(base + '/api/stats/teams/distance', axios_config_stats); | ||||
|         expect(res.status).toEqual(200); | ||||
|         expect(res.headers['content-type']).toContain("application/json"); | ||||
|     }); | ||||
|     it('get by donations w/ auth should return 200', async () => { | ||||
|         const res = await axios.get(base + '/api/stats/teams/donations', axios_config_stats); | ||||
|         expect(res.status).toEqual(200); | ||||
|         expect(res.headers['content-type']).toContain("application/json"); | ||||
|     }); | ||||
| }); | ||||
| // --------------- | ||||
| describe('GET /api/stats/organizations/* should return 200', () => { | ||||
|     it('get by distance w/ auth should return 200', async () => { | ||||
|         const res = await axios.get(base + '/api/stats/organizations/distance', axios_config_stats); | ||||
|         expect(res.status).toEqual(200); | ||||
|         expect(res.headers['content-type']).toContain("application/json"); | ||||
|     }); | ||||
|     it('get by donations w/ auth should return 200', async () => { | ||||
|         const res = await axios.get(base + '/api/stats/organizations/donations', axios_config_stats); | ||||
|         expect(res.status).toEqual(200); | ||||
|         expect(res.headers['content-type']).toContain("application/json"); | ||||
|     }); | ||||
| }); | ||||
		Reference in New Issue
	
	Block a user