parent
71228fbf33
commit
0d8fbf1eca
@ -3,7 +3,6 @@ import { OpenAPI } from 'routing-controllers-openapi';
|
|||||||
import { RunnerGroupNeededError } from '../errors/RunnerErrors';
|
import { RunnerGroupNeededError } from '../errors/RunnerErrors';
|
||||||
import RawBodyMiddleware from '../middlewares/RawBody';
|
import RawBodyMiddleware from '../middlewares/RawBody';
|
||||||
import { ImportRunner } from '../models/actions/ImportRunner';
|
import { ImportRunner } from '../models/actions/ImportRunner';
|
||||||
import { ImportSchoolRunner } from '../models/actions/ImportSchoolRunner';
|
|
||||||
import { ResponseRunner } from '../models/responses/ResponseRunner';
|
import { ResponseRunner } from '../models/responses/ResponseRunner';
|
||||||
import { RunnerController } from './RunnerController';
|
import { RunnerController } from './RunnerController';
|
||||||
|
|
||||||
@ -22,7 +21,7 @@ export class ImportController {
|
|||||||
@Post('/runners/import')
|
@Post('/runners/import')
|
||||||
@ContentType("application/json")
|
@ContentType("application/json")
|
||||||
@OpenAPI({ description: "Create new runners from json" })
|
@OpenAPI({ description: "Create new runners from json" })
|
||||||
async postJSON(@Body({ validate: true, type: ImportSchoolRunner }) importRunners: ImportRunner[], @QueryParam("group") groupID: number) {
|
async postJSON(@Body({ validate: true, type: ImportRunner }) importRunners: ImportRunner[], @QueryParam("group") groupID: number) {
|
||||||
if (!groupID) { throw new RunnerGroupNeededError(); }
|
if (!groupID) { throw new RunnerGroupNeededError(); }
|
||||||
let responseRunners: ResponseRunner[] = new Array<ResponseRunner>();
|
let responseRunners: ResponseRunner[] = new Array<ResponseRunner>();
|
||||||
for await (let runner of importRunners) {
|
for await (let runner of importRunners) {
|
||||||
@ -34,14 +33,14 @@ export class ImportController {
|
|||||||
@Post('/organisations/:id/import')
|
@Post('/organisations/:id/import')
|
||||||
@ContentType("application/json")
|
@ContentType("application/json")
|
||||||
@OpenAPI({ description: "Create new runners from json" })
|
@OpenAPI({ description: "Create new runners from json" })
|
||||||
async postOrgsJSON(@Body({ validate: true, type: ImportSchoolRunner }) importRunners: ImportRunner[], @Param('id') id: number) {
|
async postOrgsJSON(@Body({ validate: true, type: ImportRunner }) importRunners: ImportRunner[], @Param('id') id: number) {
|
||||||
return await this.postJSON(importRunners, id)
|
return await this.postJSON(importRunners, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post('/teams/:id/import')
|
@Post('/teams/:id/import')
|
||||||
@ContentType("application/json")
|
@ContentType("application/json")
|
||||||
@OpenAPI({ description: "Create new runners from json" })
|
@OpenAPI({ description: "Create new runners from json" })
|
||||||
async postTeamsJSON(@Body({ validate: true, type: ImportSchoolRunner }) importRunners: ImportRunner[], @Param('id') id: number) {
|
async postTeamsJSON(@Body({ validate: true, type: ImportRunner }) importRunners: ImportRunner[], @Param('id') id: number) {
|
||||||
return await this.postJSON(importRunners, id)
|
return await this.postJSON(importRunners, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,13 @@
|
|||||||
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||||
|
import { getConnectionManager } from 'typeorm';
|
||||||
|
import { RunnerGroupNeededError } from '../../errors/RunnerErrors';
|
||||||
|
import { RunnerOrganisationNotFoundError } from '../../errors/RunnerOrganisationErrors';
|
||||||
|
import { RunnerGroup } from '../entities/RunnerGroup';
|
||||||
|
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
||||||
|
import { RunnerTeam } from '../entities/RunnerTeam';
|
||||||
|
import { CreateRunner } from './CreateRunner';
|
||||||
|
|
||||||
export abstract class ImportRunner {
|
export class ImportRunner {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The new runner's first name.
|
* The new runner's first name.
|
||||||
@ -24,5 +31,52 @@ export abstract class ImportRunner {
|
|||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
lastname: string;
|
lastname: string;
|
||||||
|
|
||||||
public abstract toCreateRunner(groupID?: number);
|
/**
|
||||||
|
* The new runner's class (if not provided otherwise).
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
|
team?: string;
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@IsString()
|
||||||
|
public set class(value: string) {
|
||||||
|
this.team = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async toCreateRunner(groupID: number): Promise<CreateRunner> {
|
||||||
|
let newRunner: CreateRunner = new CreateRunner();
|
||||||
|
|
||||||
|
newRunner.firstname = this.firstname;
|
||||||
|
newRunner.middlename = this.middlename;
|
||||||
|
newRunner.lastname = this.lastname;
|
||||||
|
newRunner.group = (await this.getGroup(groupID)).id;
|
||||||
|
|
||||||
|
return newRunner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getGroup(groupID: number): Promise<RunnerGroup> {
|
||||||
|
if (this.team === undefined && groupID === undefined) {
|
||||||
|
throw new RunnerGroupNeededError();
|
||||||
|
}
|
||||||
|
|
||||||
|
let team = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: groupID });
|
||||||
|
if (team) { return team; }
|
||||||
|
|
||||||
|
let org = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: groupID });
|
||||||
|
if (!org) {
|
||||||
|
throw new RunnerOrganisationNotFoundError();
|
||||||
|
}
|
||||||
|
if (this.team === undefined) { return org; }
|
||||||
|
|
||||||
|
team = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ name: this.team, parentGroup: org });
|
||||||
|
if (!team) {
|
||||||
|
let newRunnerTeam: RunnerTeam = new RunnerTeam();
|
||||||
|
newRunnerTeam.name = this.team;
|
||||||
|
newRunnerTeam.parentGroup = org;
|
||||||
|
team = await getConnectionManager().get().getRepository(RunnerTeam).save(newRunnerTeam);
|
||||||
|
}
|
||||||
|
|
||||||
|
return team;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,56 +0,0 @@
|
|||||||
import { IsNotEmpty, IsString } from 'class-validator';
|
|
||||||
import { getConnectionManager } from 'typeorm';
|
|
||||||
import { RunnerGroupNeededError } from '../../errors/RunnerErrors';
|
|
||||||
import { RunnerOrganisationNotFoundError } from '../../errors/RunnerOrganisationErrors';
|
|
||||||
import { RunnerGroup } from '../entities/RunnerGroup';
|
|
||||||
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
|
||||||
import { RunnerTeam } from '../entities/RunnerTeam';
|
|
||||||
import { CreateRunner } from './CreateRunner';
|
|
||||||
import { ImportRunner } from './ImportRunner';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A special class for importing runners from tabels provided by schools.
|
|
||||||
*/
|
|
||||||
export class ImportSchoolRunner extends ImportRunner {
|
|
||||||
/**
|
|
||||||
* The pupils's class.
|
|
||||||
*/
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
class: string;
|
|
||||||
|
|
||||||
public async toCreateRunner(groupID: number): Promise<CreateRunner> {
|
|
||||||
let newRunner: CreateRunner = new CreateRunner();
|
|
||||||
|
|
||||||
newRunner.firstname = this.firstname;
|
|
||||||
newRunner.middlename = this.middlename;
|
|
||||||
newRunner.lastname = this.lastname;
|
|
||||||
newRunner.group = (await this.getGroup(groupID)).id;
|
|
||||||
|
|
||||||
return newRunner;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async getGroup(groupID: number): Promise<RunnerGroup> {
|
|
||||||
if (this.class === undefined && groupID === undefined) {
|
|
||||||
throw new RunnerGroupNeededError();
|
|
||||||
}
|
|
||||||
|
|
||||||
let team = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: groupID });
|
|
||||||
if (team) { return team; }
|
|
||||||
|
|
||||||
let org = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: groupID });
|
|
||||||
if (!org) {
|
|
||||||
throw new RunnerOrganisationNotFoundError();
|
|
||||||
}
|
|
||||||
|
|
||||||
team = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ name: this.class, parentGroup: org });
|
|
||||||
if (!team) {
|
|
||||||
let newRunnerTeam: RunnerTeam = new RunnerTeam();
|
|
||||||
newRunnerTeam.name = this.class;
|
|
||||||
newRunnerTeam.parentGroup = org;
|
|
||||||
team = await getConnectionManager().get().getRepository(RunnerTeam).save(newRunnerTeam);
|
|
||||||
}
|
|
||||||
|
|
||||||
return team;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user