Renamed files and classed from *Organisation* to *Organization*📝
ref #117
This commit is contained in:
@@ -1,127 +0,0 @@
|
||||
import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers';
|
||||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||
import { getConnectionManager, Repository } from 'typeorm';
|
||||
import { RunnerOrganisationHasRunnersError, RunnerOrganisationHasTeamsError, RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors';
|
||||
import { CreateRunnerOrganisation } from '../models/actions/create/CreateRunnerOrganisation';
|
||||
import { UpdateRunnerOrganisation } from '../models/actions/update/UpdateRunnerOrganisation';
|
||||
import { RunnerOrganisation } from '../models/entities/RunnerOrganisation';
|
||||
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
||||
import { ResponseRunnerOrganisation } from '../models/responses/ResponseRunnerOrganisation';
|
||||
import { RunnerController } from './RunnerController';
|
||||
import { RunnerTeamController } from './RunnerTeamController';
|
||||
|
||||
|
||||
@JsonController('/organizations')
|
||||
@OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
|
||||
export class RunnerOrganisationController {
|
||||
private runnerOrganisationRepository: Repository<RunnerOrganisation>;
|
||||
|
||||
/**
|
||||
* Gets the repository of this controller's model/entity.
|
||||
*/
|
||||
constructor() {
|
||||
this.runnerOrganisationRepository = getConnectionManager().get().getRepository(RunnerOrganisation);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@Authorized("ORGANISATION:GET")
|
||||
@ResponseSchema(ResponseRunnerOrganisation, { isArray: true })
|
||||
@OpenAPI({ description: 'Lists all organizations. <br> This includes their address, contact and teams (if existing/associated).' })
|
||||
async getAll() {
|
||||
let responseTeams: ResponseRunnerOrganisation[] = new Array<ResponseRunnerOrganisation>();
|
||||
const runners = await this.runnerOrganisationRepository.find({ relations: ['contact', 'teams'] });
|
||||
runners.forEach(runner => {
|
||||
responseTeams.push(new ResponseRunnerOrganisation(runner));
|
||||
});
|
||||
return responseTeams;
|
||||
}
|
||||
|
||||
@Get('/:id')
|
||||
@Authorized("ORGANISATION:GET")
|
||||
@ResponseSchema(ResponseRunnerOrganisation)
|
||||
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
|
||||
@OnUndefined(RunnerOrganisationNotFoundError)
|
||||
@OpenAPI({ description: 'Lists all information about the organization whose id got provided.' })
|
||||
async getOne(@Param('id') id: number) {
|
||||
let runnerOrg = await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['contact', 'teams'] });
|
||||
if (!runnerOrg) { throw new RunnerOrganisationNotFoundError(); }
|
||||
return new ResponseRunnerOrganisation(runnerOrg);
|
||||
}
|
||||
|
||||
@Post()
|
||||
@Authorized("ORGANISATION:CREATE")
|
||||
@ResponseSchema(ResponseRunnerOrganisation)
|
||||
@OpenAPI({ description: 'Create a new organsisation.' })
|
||||
async post(@Body({ validate: true }) createRunnerOrganisation: CreateRunnerOrganisation) {
|
||||
let runnerOrganisation;
|
||||
try {
|
||||
runnerOrganisation = await createRunnerOrganisation.toEntity();
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
runnerOrganisation = await this.runnerOrganisationRepository.save(runnerOrganisation);
|
||||
|
||||
return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['contact', 'teams'] }));
|
||||
}
|
||||
|
||||
@Put('/:id')
|
||||
@Authorized("ORGANISATION:UPDATE")
|
||||
@ResponseSchema(ResponseRunnerOrganisation)
|
||||
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
|
||||
@ResponseSchema(RunnerOrganisationIdsNotMatchingError, { statusCode: 406 })
|
||||
@OpenAPI({ description: "Update the organization whose id you provided. <br> Please remember that ids can't be changed." })
|
||||
async put(@Param('id') id: number, @Body({ validate: true }) updateOrganisation: UpdateRunnerOrganisation) {
|
||||
let oldRunnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id });
|
||||
|
||||
if (!oldRunnerOrganisation) {
|
||||
throw new RunnerOrganisationNotFoundError();
|
||||
}
|
||||
|
||||
if (oldRunnerOrganisation.id != updateOrganisation.id) {
|
||||
throw new RunnerOrganisationIdsNotMatchingError();
|
||||
}
|
||||
|
||||
await this.runnerOrganisationRepository.save(await updateOrganisation.update(oldRunnerOrganisation));
|
||||
|
||||
return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne(id, { relations: ['contact', 'teams'] }));
|
||||
}
|
||||
|
||||
@Delete('/:id')
|
||||
@Authorized("ORGANISATION:DELETE")
|
||||
@ResponseSchema(ResponseRunnerOrganisation)
|
||||
@ResponseSchema(ResponseEmpty, { statusCode: 204 })
|
||||
@ResponseSchema(RunnerOrganisationHasTeamsError, { statusCode: 406 })
|
||||
@ResponseSchema(RunnerOrganisationHasRunnersError, { statusCode: 406 })
|
||||
@OnUndefined(204)
|
||||
@OpenAPI({ description: 'Delete the organsisation whose id you provided. <br> If the organization still has runners and/or teams associated this will fail. <br> To delete the organization with all associated runners and teams set the force QueryParam to true (cascading deletion might take a while). <br> This won\'t delete the associated contact. <br> If no organization with this id exists it will just return 204(no content).' })
|
||||
async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
|
||||
let organization = await this.runnerOrganisationRepository.findOne({ id: id });
|
||||
if (!organization) { return null; }
|
||||
let runnerOrganisation = await this.runnerOrganisationRepository.findOne(organization, { relations: ['contact', 'runners', 'teams'] });
|
||||
|
||||
if (!force) {
|
||||
if (runnerOrganisation.teams.length != 0) {
|
||||
throw new RunnerOrganisationHasTeamsError();
|
||||
}
|
||||
}
|
||||
const teamController = new RunnerTeamController()
|
||||
for (let team of runnerOrganisation.teams) {
|
||||
await teamController.remove(team.id, true);
|
||||
}
|
||||
|
||||
if (!force) {
|
||||
if (runnerOrganisation.runners.length != 0) {
|
||||
throw new RunnerOrganisationHasRunnersError();
|
||||
}
|
||||
}
|
||||
const runnerController = new RunnerController()
|
||||
for (let runner of runnerOrganisation.runners) {
|
||||
await runnerController.remove(runner.id, true);
|
||||
}
|
||||
|
||||
const responseOrganisation = new ResponseRunnerOrganisation(runnerOrganisation);
|
||||
await this.runnerOrganisationRepository.delete(organization);
|
||||
return responseOrganisation;
|
||||
}
|
||||
}
|
||||
127
src/controllers/RunnerOrganizationController.ts
Normal file
127
src/controllers/RunnerOrganizationController.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers';
|
||||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||
import { getConnectionManager, Repository } from 'typeorm';
|
||||
import { RunnerOrganizationHasRunnersError, RunnerOrganizationHasTeamsError, RunnerOrganizationIdsNotMatchingError, RunnerOrganizationNotFoundError } from '../errors/RunnerOrganizationErrors';
|
||||
import { CreateRunnerOrganization } from '../models/actions/create/CreateRunnerOrganization';
|
||||
import { UpdateRunnerOrganization } from '../models/actions/update/UpdateRunnerOrganization';
|
||||
import { RunnerOrganization } from '../models/entities/RunnerOrganization';
|
||||
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
||||
import { ResponseRunnerOrganization } from '../models/responses/ResponseRunnerOrganization';
|
||||
import { RunnerController } from './RunnerController';
|
||||
import { RunnerTeamController } from './RunnerTeamController';
|
||||
|
||||
|
||||
@JsonController('/organizations')
|
||||
@OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
|
||||
export class RunnerOrganizationController {
|
||||
private runnerOrganizationRepository: Repository<RunnerOrganization>;
|
||||
|
||||
/**
|
||||
* Gets the repository of this controller's model/entity.
|
||||
*/
|
||||
constructor() {
|
||||
this.runnerOrganizationRepository = getConnectionManager().get().getRepository(RunnerOrganization);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@Authorized("ORGANISATION:GET")
|
||||
@ResponseSchema(ResponseRunnerOrganization, { isArray: true })
|
||||
@OpenAPI({ description: 'Lists all organizations. <br> This includes their address, contact and teams (if existing/associated).' })
|
||||
async getAll() {
|
||||
let responseTeams: ResponseRunnerOrganization[] = new Array<ResponseRunnerOrganization>();
|
||||
const runners = await this.runnerOrganizationRepository.find({ relations: ['contact', 'teams'] });
|
||||
runners.forEach(runner => {
|
||||
responseTeams.push(new ResponseRunnerOrganization(runner));
|
||||
});
|
||||
return responseTeams;
|
||||
}
|
||||
|
||||
@Get('/:id')
|
||||
@Authorized("ORGANISATION:GET")
|
||||
@ResponseSchema(ResponseRunnerOrganization)
|
||||
@ResponseSchema(RunnerOrganizationNotFoundError, { statusCode: 404 })
|
||||
@OnUndefined(RunnerOrganizationNotFoundError)
|
||||
@OpenAPI({ description: 'Lists all information about the organization whose id got provided.' })
|
||||
async getOne(@Param('id') id: number) {
|
||||
let runnerOrg = await this.runnerOrganizationRepository.findOne({ id: id }, { relations: ['contact', 'teams'] });
|
||||
if (!runnerOrg) { throw new RunnerOrganizationNotFoundError(); }
|
||||
return new ResponseRunnerOrganization(runnerOrg);
|
||||
}
|
||||
|
||||
@Post()
|
||||
@Authorized("ORGANISATION:CREATE")
|
||||
@ResponseSchema(ResponseRunnerOrganization)
|
||||
@OpenAPI({ description: 'Create a new organsisation.' })
|
||||
async post(@Body({ validate: true }) createRunnerOrganization: CreateRunnerOrganization) {
|
||||
let runnerOrganization;
|
||||
try {
|
||||
runnerOrganization = await createRunnerOrganization.toEntity();
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
runnerOrganization = await this.runnerOrganizationRepository.save(runnerOrganization);
|
||||
|
||||
return new ResponseRunnerOrganization(await this.runnerOrganizationRepository.findOne(runnerOrganization, { relations: ['contact', 'teams'] }));
|
||||
}
|
||||
|
||||
@Put('/:id')
|
||||
@Authorized("ORGANISATION:UPDATE")
|
||||
@ResponseSchema(ResponseRunnerOrganization)
|
||||
@ResponseSchema(RunnerOrganizationNotFoundError, { statusCode: 404 })
|
||||
@ResponseSchema(RunnerOrganizationIdsNotMatchingError, { statusCode: 406 })
|
||||
@OpenAPI({ description: "Update the organization whose id you provided. <br> Please remember that ids can't be changed." })
|
||||
async put(@Param('id') id: number, @Body({ validate: true }) updateOrganization: UpdateRunnerOrganization) {
|
||||
let oldRunnerOrganization = await this.runnerOrganizationRepository.findOne({ id: id });
|
||||
|
||||
if (!oldRunnerOrganization) {
|
||||
throw new RunnerOrganizationNotFoundError();
|
||||
}
|
||||
|
||||
if (oldRunnerOrganization.id != updateOrganization.id) {
|
||||
throw new RunnerOrganizationIdsNotMatchingError();
|
||||
}
|
||||
|
||||
await this.runnerOrganizationRepository.save(await updateOrganization.update(oldRunnerOrganization));
|
||||
|
||||
return new ResponseRunnerOrganization(await this.runnerOrganizationRepository.findOne(id, { relations: ['contact', 'teams'] }));
|
||||
}
|
||||
|
||||
@Delete('/:id')
|
||||
@Authorized("ORGANISATION:DELETE")
|
||||
@ResponseSchema(ResponseRunnerOrganization)
|
||||
@ResponseSchema(ResponseEmpty, { statusCode: 204 })
|
||||
@ResponseSchema(RunnerOrganizationHasTeamsError, { statusCode: 406 })
|
||||
@ResponseSchema(RunnerOrganizationHasRunnersError, { statusCode: 406 })
|
||||
@OnUndefined(204)
|
||||
@OpenAPI({ description: 'Delete the organsisation whose id you provided. <br> If the organization still has runners and/or teams associated this will fail. <br> To delete the organization with all associated runners and teams set the force QueryParam to true (cascading deletion might take a while). <br> This won\'t delete the associated contact. <br> If no organization with this id exists it will just return 204(no content).' })
|
||||
async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
|
||||
let organization = await this.runnerOrganizationRepository.findOne({ id: id });
|
||||
if (!organization) { return null; }
|
||||
let runnerOrganization = await this.runnerOrganizationRepository.findOne(organization, { relations: ['contact', 'runners', 'teams'] });
|
||||
|
||||
if (!force) {
|
||||
if (runnerOrganization.teams.length != 0) {
|
||||
throw new RunnerOrganizationHasTeamsError();
|
||||
}
|
||||
}
|
||||
const teamController = new RunnerTeamController()
|
||||
for (let team of runnerOrganization.teams) {
|
||||
await teamController.remove(team.id, true);
|
||||
}
|
||||
|
||||
if (!force) {
|
||||
if (runnerOrganization.runners.length != 0) {
|
||||
throw new RunnerOrganizationHasRunnersError();
|
||||
}
|
||||
}
|
||||
const runnerController = new RunnerController()
|
||||
for (let runner of runnerOrganization.runners) {
|
||||
await runnerController.remove(runner.id, true);
|
||||
}
|
||||
|
||||
const responseOrganization = new ResponseRunnerOrganization(runnerOrganization);
|
||||
await this.runnerOrganizationRepository.delete(organization);
|
||||
return responseOrganization;
|
||||
}
|
||||
}
|
||||
@@ -5,27 +5,27 @@ import { getConnectionManager, Repository } from 'typeorm';
|
||||
import { config } from '../config';
|
||||
import { InvalidCredentialsError, JwtNotProvidedError } from '../errors/AuthError';
|
||||
import { RunnerEmailNeededError, RunnerNotFoundError } from '../errors/RunnerErrors';
|
||||
import { RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors';
|
||||
import { RunnerOrganizationNotFoundError } from '../errors/RunnerOrganizationErrors';
|
||||
import { JwtCreator } from '../jwtcreator';
|
||||
import { CreateSelfServiceCitizenRunner } from '../models/actions/create/CreateSelfServiceCitizenRunner';
|
||||
import { CreateSelfServiceRunner } from '../models/actions/create/CreateSelfServiceRunner';
|
||||
import { Runner } from '../models/entities/Runner';
|
||||
import { RunnerGroup } from '../models/entities/RunnerGroup';
|
||||
import { RunnerOrganisation } from '../models/entities/RunnerOrganisation';
|
||||
import { RunnerOrganization } from '../models/entities/RunnerOrganization';
|
||||
import { ResponseSelfServiceRunner } from '../models/responses/ResponseSelfServiceRunner';
|
||||
|
||||
|
||||
@JsonController('/runners')
|
||||
export class RunnerSelfServiceController {
|
||||
private runnerRepository: Repository<Runner>;
|
||||
private orgRepository: Repository<RunnerOrganisation>;
|
||||
private orgRepository: Repository<RunnerOrganization>;
|
||||
|
||||
/**
|
||||
* Gets the repository of this controller's model/entity.
|
||||
*/
|
||||
constructor() {
|
||||
this.runnerRepository = getConnectionManager().get().getRepository(Runner);
|
||||
this.orgRepository = getConnectionManager().get().getRepository(RunnerOrganisation);
|
||||
this.orgRepository = getConnectionManager().get().getRepository(RunnerOrganization);
|
||||
}
|
||||
|
||||
@Get('/me/:jwt')
|
||||
@@ -52,9 +52,9 @@ export class RunnerSelfServiceController {
|
||||
|
||||
@Post('/register/:token')
|
||||
@ResponseSchema(ResponseSelfServiceRunner)
|
||||
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
|
||||
@ResponseSchema(RunnerOrganizationNotFoundError, { statusCode: 404 })
|
||||
@OpenAPI({ description: 'Create a new selfservice runner in a provided org. <br> The orgs get provided and authorized via api tokens that can be optained via the /organizations endpoint.' })
|
||||
async registerOrganisationRunner(@Param('token') token: string, @Body({ validate: true }) createRunner: CreateSelfServiceRunner) {
|
||||
async registerOrganizationRunner(@Param('token') token: string, @Body({ validate: true }) createRunner: CreateSelfServiceRunner) {
|
||||
const org = await this.getOrgansisation(token);
|
||||
|
||||
let runner = await createRunner.toEntity(org);
|
||||
@@ -91,7 +91,7 @@ export class RunnerSelfServiceController {
|
||||
token = Buffer.from(token, 'base64').toString('utf8');
|
||||
|
||||
const organization = await this.orgRepository.findOne({ key: token });
|
||||
if (!organization) { throw new RunnerOrganisationNotFoundError; }
|
||||
if (!organization) { throw new RunnerOrganizationNotFoundError; }
|
||||
|
||||
return organization;
|
||||
}
|
||||
|
||||
@@ -4,12 +4,12 @@ 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';
|
||||
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/ResponseStatsOrganisation';
|
||||
import { ResponseStatsOrgnisation } from '../models/responses/ResponseStatsOrganization';
|
||||
import { ResponseStatsRunner } from '../models/responses/ResponseStatsRunner';
|
||||
import { ResponseStatsTeam } from '../models/responses/ResponseStatsTeam';
|
||||
|
||||
@@ -23,7 +23,7 @@ export class StatsController {
|
||||
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(RunnerOrganisation).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'] });
|
||||
@@ -99,7 +99,7 @@ export class StatsController {
|
||||
@ResponseSchema(ResponseStatsOrgnisation, { isArray: true })
|
||||
@OpenAPI({ description: "Returns the top ten organizations by distance.", security: [{ "StatsApiToken": [] }, { "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
|
||||
async getTopOrgsByDistance() {
|
||||
let orgs = await getConnection().getRepository(RunnerOrganisation).find({ relations: ['runners', 'runners.scans', 'runners.distanceDonations', 'runners.scans.track', 'teams', 'teams.runners', 'teams.runners.scans', 'teams.runners.distanceDonations', 'teams.runners.scans.track'] });
|
||||
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 => {
|
||||
@@ -113,7 +113,7 @@ export class StatsController {
|
||||
@ResponseSchema(ResponseStatsOrgnisation, { isArray: true })
|
||||
@OpenAPI({ description: "Returns the top ten organizations by donations.", security: [{ "StatsApiToken": [] }, { "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
|
||||
async getTopOrgsByDonations() {
|
||||
let orgs = await getConnection().getRepository(RunnerOrganisation).find({ relations: ['runners', 'runners.scans', 'runners.distanceDonations', 'runners.scans.track', 'teams', 'teams.runners', 'teams.runners.scans', 'teams.runners.distanceDonations', 'teams.runners.scans.track'] });
|
||||
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 => {
|
||||
|
||||
Reference in New Issue
Block a user