124 lines
		
	
	
		
			7.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			124 lines
		
	
	
		
			7.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import { Get, JsonController, UseBefore } from 'routing-controllers';
 | 
						|
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
 | 
						|
import { getConnection } from 'typeorm';
 | 
						|
import StatsAuth from '../middlewares/StatsAuth';
 | 
						|
import { Donation } from '../models/entities/Donation';
 | 
						|
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 { User } from '../models/entities/User';
 | 
						|
import { ResponseStats } from '../models/responses/ResponseStats';
 | 
						|
import { ResponseStatsOrgnisation } from '../models/responses/ResponseStatsOrganization';
 | 
						|
import { ResponseStatsRunner } from '../models/responses/ResponseStatsRunner';
 | 
						|
import { ResponseStatsTeam } from '../models/responses/ResponseStatsTeam';
 | 
						|
 | 
						|
@JsonController('/stats')
 | 
						|
export class StatsController {
 | 
						|
 | 
						|
    @Get()
 | 
						|
    @ResponseSchema(ResponseStats)
 | 
						|
    @OpenAPI({ description: "A very basic stats endpoint providing basic counters for a dashboard or simmilar" })
 | 
						|
    async get() {
 | 
						|
        let connection = getConnection();
 | 
						|
        let runners = await connection.getRepository(Runner).find({ relations: ['scans', 'scans.track'] });
 | 
						|
        let teams = await connection.getRepository(RunnerTeam).find();
 | 
						|
        let orgs = await connection.getRepository(RunnerOrganization).find();
 | 
						|
        let users = await connection.getRepository(User).find();
 | 
						|
        let scans = await connection.getRepository(Scan).find();
 | 
						|
        let donations = await connection.getRepository(Donation).find({ relations: ['runner', 'runner.scans', 'runner.scans.track'] });
 | 
						|
        return new ResponseStats(runners, teams, orgs, users, scans, donations)
 | 
						|
    }
 | 
						|
 | 
						|
    @Get("/runners/distance")
 | 
						|
    @UseBefore(StatsAuth)
 | 
						|
    @ResponseSchema(ResponseStatsRunner, { isArray: true })
 | 
						|
    @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);
 | 
						|
        let responseRunners: ResponseStatsRunner[] = new Array<ResponseStatsRunner>();
 | 
						|
        topRunners.forEach(runner => {
 | 
						|
            responseRunners.push(new ResponseStatsRunner(runner));
 | 
						|
        });
 | 
						|
        return responseRunners;
 | 
						|
    }
 | 
						|
 | 
						|
    @Get("/runners/donations")
 | 
						|
    @UseBefore(StatsAuth)
 | 
						|
    @ResponseSchema(ResponseStatsRunner, { isArray: true })
 | 
						|
    @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);
 | 
						|
        let responseRunners: ResponseStatsRunner[] = new Array<ResponseStatsRunner>();
 | 
						|
        topRunners.forEach(runner => {
 | 
						|
            responseRunners.push(new ResponseStatsRunner(runner));
 | 
						|
        });
 | 
						|
        return responseRunners;
 | 
						|
    }
 | 
						|
 | 
						|
    @Get("/scans")
 | 
						|
    @UseBefore(StatsAuth)
 | 
						|
    @ResponseSchema(ResponseStatsRunner, { isArray: true })
 | 
						|
    @OpenAPI({ description: "Returns the top ten fastest track times (with their runner and the runner's group).", security: [{ "StatsApiToken": [] }, { "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
 | 
						|
    async getTopRunnersByTrackTime() {
 | 
						|
        throw new Error("Not implemented yet.")
 | 
						|
    }
 | 
						|
 | 
						|
    @Get("/teams/distance")
 | 
						|
    @UseBefore(StatsAuth)
 | 
						|
    @ResponseSchema(ResponseStatsTeam, { isArray: true })
 | 
						|
    @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);
 | 
						|
        let responseTeams: ResponseStatsTeam[] = new Array<ResponseStatsTeam>();
 | 
						|
        topTeams.forEach(team => {
 | 
						|
            responseTeams.push(new ResponseStatsTeam(team));
 | 
						|
        });
 | 
						|
        return responseTeams;
 | 
						|
    }
 | 
						|
 | 
						|
    @Get("/teams/donations")
 | 
						|
    @UseBefore(StatsAuth)
 | 
						|
    @ResponseSchema(ResponseStatsTeam, { isArray: true })
 | 
						|
    @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);
 | 
						|
        let responseTeams: ResponseStatsTeam[] = new Array<ResponseStatsTeam>();
 | 
						|
        topTeams.forEach(team => {
 | 
						|
            responseTeams.push(new ResponseStatsTeam(team));
 | 
						|
        });
 | 
						|
        return responseTeams;
 | 
						|
    }
 | 
						|
 | 
						|
    @Get("/organizations/distance")
 | 
						|
    @UseBefore(StatsAuth)
 | 
						|
    @ResponseSchema(ResponseStatsOrgnisation, { isArray: true })
 | 
						|
    @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);
 | 
						|
        let responseOrgs: ResponseStatsOrgnisation[] = new Array<ResponseStatsOrgnisation>();
 | 
						|
        topOrgs.forEach(org => {
 | 
						|
            responseOrgs.push(new ResponseStatsOrgnisation(org));
 | 
						|
        });
 | 
						|
        return responseOrgs;
 | 
						|
    }
 | 
						|
 | 
						|
    @Get("/organizations/donations")
 | 
						|
    @UseBefore(StatsAuth)
 | 
						|
    @ResponseSchema(ResponseStatsOrgnisation, { isArray: true })
 | 
						|
    @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);
 | 
						|
        let responseOrgs: ResponseStatsOrgnisation[] = new Array<ResponseStatsOrgnisation>();
 | 
						|
        topOrgs.forEach(org => {
 | 
						|
            responseOrgs.push(new ResponseStatsOrgnisation(org));
 | 
						|
        });
 | 
						|
        return responseOrgs;
 | 
						|
    }
 | 
						|
} |