Added example endpoint for stats auth

This commit is contained in:
Nicolai Ort 2020-12-29 21:34:49 +01:00
parent 7c5a3893ef
commit 345851bf1d
1 changed files with 24 additions and 1 deletions

View File

@ -1,6 +1,7 @@
import { Get, JsonController } from 'routing-controllers';
import { Get, JsonController, UseBefore } from 'routing-controllers';
import { OpenAPI } 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 { RunnerOrganisation } from '../models/entities/RunnerOrganisation';
@ -31,4 +32,26 @@ export class StatsController {
"total_donation_amount": donations.reduce((sum, current) => sum + current.amount, 0),
};
}
@Get("/authorized")
@UseBefore(StatsAuth)
@OpenAPI({ description: "A demo endpoint for authorized stats." })
async getAuthorized() {
let connection = getConnection();
let runners = await connection.getRepository(Runner).find({ relations: ["scans"] });
let teams = await connection.getRepository(RunnerTeam).find();
let orgs = await connection.getRepository(RunnerOrganisation).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"] });
return {
"total_runners": runners.length,
"total_teams": teams.length,
"total_orgs": orgs.length,
"total_users": users.length,
"total_scans": scans.filter(scan => { scan.valid === true }).length,
"total_distance": runners.reduce((sum, current) => sum + current.distance, 0),
"total_donation_amount": donations.reduce((sum, current) => sum + current.amount, 0),
};
}
}