Now organisations and teams can import runners

This commit is contained in:
Nicolai Ort 2020-12-17 17:14:08 +01:00
parent 97494aeaf7
commit 71228fbf33
2 changed files with 20 additions and 7 deletions

View File

@ -1,5 +1,6 @@
import { Body, ContentType, Controller, Param, Post, QueryParam, Req, UseBefore } from 'routing-controllers';
import { OpenAPI } from 'routing-controllers-openapi';
import { RunnerGroupNeededError } from '../errors/RunnerErrors';
import RawBodyMiddleware from '../middlewares/RawBody';
import { ImportRunner } from '../models/actions/ImportRunner';
import { ImportSchoolRunner } from '../models/actions/ImportSchoolRunner';
@ -21,10 +22,11 @@ export class ImportController {
@Post('/runners/import')
@ContentType("application/json")
@OpenAPI({ description: "Create new runners from json" })
async postJSON(@Body({ validate: true, type: ImportSchoolRunner }) importRunners: ImportRunner[], @QueryParam("org") orgID: number) {
async postJSON(@Body({ validate: true, type: ImportSchoolRunner }) importRunners: ImportRunner[], @QueryParam("group") groupID: number) {
if (!groupID) { throw new RunnerGroupNeededError(); }
let responseRunners: ResponseRunner[] = new Array<ResponseRunner>();
for await (let runner of importRunners) {
responseRunners.push(await this.runnerController.post(await runner.toCreateRunner(orgID)));
responseRunners.push(await this.runnerController.post(await runner.toCreateRunner(groupID)));
}
return responseRunners;
}
@ -36,6 +38,13 @@ export class ImportController {
return await this.postJSON(importRunners, id)
}
@Post('/teams/:id/import')
@ContentType("application/json")
@OpenAPI({ description: "Create new runners from json" })
async postTeamsJSON(@Body({ validate: true, type: ImportSchoolRunner }) importRunners: ImportRunner[], @Param('id') id: number) {
return await this.postJSON(importRunners, id)
}
@Post('/runners/import/csv')
@ContentType("text/csv")
@UseBefore(RawBodyMiddleware)

View File

@ -1,5 +1,6 @@
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';
@ -29,17 +30,20 @@ export class ImportSchoolRunner extends ImportRunner {
return newRunner;
}
public async getGroup(orgID: number): Promise<RunnerGroup> {
if (this.class === undefined) {
throw new Error("TODO:Error for runner missing teams");
public async getGroup(groupID: number): Promise<RunnerGroup> {
if (this.class === undefined && groupID === undefined) {
throw new RunnerGroupNeededError();
}
let org = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: orgID });
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();
}
let team = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ name: this.class, parentGroup: org });
team = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ name: this.class, parentGroup: org });
if (!team) {
let newRunnerTeam: RunnerTeam = new RunnerTeam();
newRunnerTeam.name = this.class;