Merge branch 'dev' into feature/17-automated_tests

This commit is contained in:
Nicolai Ort 2020-12-09 19:42:51 +01:00
commit d2898ff60f
18 changed files with 1111 additions and 1060 deletions

View File

@ -1,12 +1,12 @@
import { config } from 'dotenv-safe'; import { config } from 'dotenv';
config(); config();
export default { export default {
type: process.env.DB_TYPE, type: process.env.DB_TYPE,
host: process.env.DB_HOST, host: process.env.DB_HOST,
port: process.env.DB_PORT, port: process.env.DB_PORT,
username: process.env.DB_USER, username: process.env.DB_USER,
password: process.env.DB_PASSWORD, password: process.env.DB_PASSWORD,
database: process.env.DB_NAME, database: process.env.DB_NAME,
entities: ["src/models/entities/*.ts"] entities: ["src/models/entities/*.ts"]
}; };

View File

@ -1,80 +1,81 @@
{ {
"name": "@lfk/backend", "name": "@lfk/backend",
"version": "1.0.0", "version": "1.0.0",
"main": "src/app.ts", "main": "src/app.ts",
"repository": "https://git.odit.services/lfk/backend", "repository": "https://git.odit.services/lfk/backend",
"author": { "author": {
"name": "ODIT.Services", "name": "ODIT.Services",
"email": "info@odit.services", "email": "info@odit.services",
"url": "https://odit.services" "url": "https://odit.services"
}, },
"contributors": [ "contributors": [
{ {
"name": "Philipp Dormann", "name": "Philipp Dormann",
"email": "philipp@philippdormann.de", "email": "philipp@philippdormann.de",
"url": "https://philippdormann.de" "url": "https://philippdormann.de"
}, },
{ {
"name": "Nicolai Ort", "name": "Nicolai Ort",
"email": "info@nicolai-ort.com", "email": "info@nicolai-ort.com",
"url": "https://nicolai-ort.com" "url": "https://nicolai-ort.com"
} }
], ],
"license": "CC-BY-NC-SA-4.0", "license": "CC-BY-NC-SA-4.0",
"dependencies": { "dependencies": {
"argon2": "^0.27.0", "argon2": "^0.27.0",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"class-transformer": "^0.3.1", "class-transformer": "^0.3.1",
"class-validator": "^0.12.2", "class-validator": "^0.12.2",
"class-validator-jsonschema": "^2.0.3", "class-validator-jsonschema": "^2.0.3",
"consola": "^2.15.0", "consola": "^2.15.0",
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.17.1", "dotenv": "^8.2.0",
"helmet": "^4.2.0", "express": "^4.17.1",
"jsonwebtoken": "^8.5.1", "helmet": "^4.2.0",
"multer": "^1.4.2", "jsonwebtoken": "^8.5.1",
"mysql": "^2.18.1", "multer": "^1.4.2",
"pg": "^8.5.1", "mysql": "^2.18.1",
"reflect-metadata": "^0.1.13", "pg": "^8.5.1",
"routing-controllers": "^0.9.0-alpha.6", "reflect-metadata": "^0.1.13",
"routing-controllers-openapi": "^2.1.0", "routing-controllers": "^0.9.0-alpha.6",
"swagger-ui-express": "^4.1.5", "routing-controllers-openapi": "^2.1.0",
"typeorm": "^0.2.29", "swagger-ui-express": "^4.1.5",
"typeorm-routing-controllers-extensions": "^0.2.0", "typeorm": "^0.2.29",
"uuid": "^8.3.1" "typeorm-routing-controllers-extensions": "^0.2.0",
}, "uuid": "^8.3.1"
"devDependencies": { },
"@types/cors": "^2.8.8", "devDependencies": {
"@types/dotenv-safe": "^8.1.1", "@types/cors": "^2.8.8",
"@types/express": "^4.17.9", "@types/dotenv": "^8.2.0",
"@types/jest": "^26.0.16", "@types/express": "^4.17.9",
"@types/jsonwebtoken": "^8.5.0", "@types/jest": "^26.0.16",
"@types/multer": "^1.4.4", "@types/jsonwebtoken": "^8.5.0",
"@types/node": "^14.14.9", "@types/multer": "^1.4.4",
"@types/swagger-ui-express": "^4.1.2", "@types/node": "^14.14.9",
"@types/uuid": "^8.3.0", "@types/swagger-ui-express": "^4.1.2",
"axios": "^0.21.0", "@types/uuid": "^8.3.0",
"dotenv-safe": "^8.2.0", "axios": "^0.21.0",
"jest": "^26.6.3", "dotenv-safe": "^8.2.0",
"nodemon": "^2.0.6", "jest": "^26.6.3",
"sqlite3": "^5.0.0", "nodemon": "^2.0.6",
"ts-jest": "^26.4.4", "sqlite3": "^5.0.0",
"ts-node": "^9.0.0", "ts-jest": "^26.4.4",
"typedoc": "^0.19.2", "ts-node": "^9.0.0",
"typescript": "^4.1.2" "typedoc": "^0.19.2",
}, "typescript": "^4.1.2"
"scripts": { },
"dev": "nodemon src/app.ts", "scripts": {
"build": "tsc", "dev": "nodemon src/app.ts",
"docs": "typedoc --out docs src", "build": "tsc",
"test": "jest", "docs": "typedoc --out docs src",
"test:watch": "jest --watchAll" "test": "jest",
}, "test:watch": "jest --watchAll"
"nodemonConfig": { },
"ignore": [ "nodemonConfig": {
"src/tests/*", "ignore": [
"docs/*" "src/tests/*",
], "docs/*"
"delay": "2500" ],
} "delay": "2500"
}
} }

View File

@ -1,26 +1,31 @@
import consola from "consola"; import consola from "consola";
import "reflect-metadata"; import "reflect-metadata";
import { createExpressServer } from "routing-controllers"; import { createExpressServer } from "routing-controllers";
import authchecker from "./authchecker"; import authchecker from "./authchecker";
import { config } from './config'; import { config, e as errors } from './config';
import loaders from "./loaders/index"; import loaders from "./loaders/index";
import { ErrorHandler } from './middlewares/ErrorHandler'; import { ErrorHandler } from './middlewares/ErrorHandler';
const app = createExpressServer({ const app = createExpressServer({
authorizationChecker: authchecker, authorizationChecker: authchecker,
middlewares: [ErrorHandler], middlewares: [ErrorHandler],
development: config.development, development: config.development,
cors: true, cors: true,
routePrefix: "/api", routePrefix: "/api",
controllers: [__dirname + "/controllers/*.ts"], controllers: [__dirname + "/controllers/*.ts"],
}); });
async function main() { async function main() {
await loaders(app); await loaders(app);
app.listen(config.internal_port, () => { app.listen(config.internal_port, () => {
consola.success( consola.success(
`⚡️[server]: Server is running at http://localhost:${config.internal_port}` `⚡️[server]: Server is running at http://localhost:${config.internal_port}`
); );
}); });
} }
main(); if (errors === 0) {
main();
} else {
console.log("error");
// something's wrong
}

View File

@ -1,7 +1,22 @@
import * as dotenvSafe from "dotenv-safe"; import { config as configDotenv } from 'dotenv';
dotenvSafe.config(); configDotenv();
export const config = { export const config = {
internal_port: process.env.APP_PORT || 4010, internal_port: parseInt(process.env.APP_PORT) || 4010,
development: process.env.NODE_ENV === "production", development: process.env.NODE_ENV === "production",
jwt_secret: process.env.JWT_SECRET || "secretjwtsecret" jwt_secret: process.env.JWT_SECRET || "secretjwtsecret",
} phone_validation_countrycode: process.env.PHONE_COUNTRYCODE || "ZZ"
}
let errors = 0
if (typeof config.internal_port !== "number") {
errors++
}
if (typeof config.phone_validation_countrycode !== "string") {
errors++
}
if (config.phone_validation_countrycode.length !== 2) {
errors++
}
if (typeof config.development !== "boolean") {
errors++
}
export let e = errors

View File

@ -1,99 +1,102 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers'; import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
import { RunnerGroupNeededError, RunnerIdsNotMatchingError, RunnerNotFoundError } from '../errors/RunnerErrors'; import { RunnerGroupNeededError, RunnerIdsNotMatchingError, RunnerNotFoundError } from '../errors/RunnerErrors';
import { RunnerGroupNotFoundError } from '../errors/RunnerGroupErrors'; import { RunnerGroupNotFoundError } from '../errors/RunnerGroupErrors';
import { CreateRunner } from '../models/actions/CreateRunner'; import { CreateRunner } from '../models/actions/CreateRunner';
import { Runner } from '../models/entities/Runner'; import { Runner } from '../models/entities/Runner';
import { ResponseRunner } from '../models/responses/ResponseRunner'; import { ResponseEmpty } from '../models/responses/ResponseEmpty';
import { ResponseRunner } from '../models/responses/ResponseRunner';
@JsonController('/runners')
//@Authorized('RUNNERS:read') @JsonController('/runners')
export class RunnerController { //@Authorized('RUNNERS:read')
private runnerRepository: Repository<Runner>; export class RunnerController {
private runnerRepository: Repository<Runner>;
/**
* Gets the repository of this controller's model/entity. /**
*/ * Gets the repository of this controller's model/entity.
constructor() { */
this.runnerRepository = getConnectionManager().get().getRepository(Runner); constructor() {
} this.runnerRepository = getConnectionManager().get().getRepository(Runner);
}
@Get()
@ResponseSchema(ResponseRunner, { isArray: true }) @Get()
@OpenAPI({ description: 'Lists all runners.' }) @ResponseSchema(ResponseRunner, { isArray: true })
async getAll() { @OpenAPI({ description: 'Lists all runners.' })
let responseRunners: ResponseRunner[] = new Array<ResponseRunner>(); async getAll() {
const runners = await this.runnerRepository.find({ relations: ['scans', 'group'] }); let responseRunners: ResponseRunner[] = new Array<ResponseRunner>();
console.log(runners); const runners = await this.runnerRepository.find({ relations: ['scans', 'group'] });
runners.forEach(runner => { console.log(runners);
responseRunners.push(new ResponseRunner(runner)); runners.forEach(runner => {
}); responseRunners.push(new ResponseRunner(runner));
return responseRunners; });
} return responseRunners;
}
@Get('/:id')
@ResponseSchema(ResponseRunner) @Get('/:id')
@ResponseSchema(RunnerNotFoundError, { statusCode: 404 }) @ResponseSchema(ResponseRunner)
@OnUndefined(RunnerNotFoundError) @ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
@OpenAPI({ description: 'Returns a runner of a specified id (if it exists)' }) @OnUndefined(RunnerNotFoundError)
async getOne(@Param('id') id: number) { @OpenAPI({ description: 'Returns a runner of a specified id (if it exists)' })
let runner = await this.runnerRepository.findOne({ id: id }, { relations: ['scans', 'group'] }) async getOne(@Param('id') id: number) {
if (!runner) { throw new RunnerNotFoundError(); } let runner = await this.runnerRepository.findOne({ id: id }, { relations: ['scans', 'group'] })
return new ResponseRunner(runner); if (!runner) { throw new RunnerNotFoundError(); }
} return new ResponseRunner(runner);
}
@Post()
@ResponseSchema(ResponseRunner) @Post()
@ResponseSchema(RunnerGroupNeededError) @ResponseSchema(ResponseRunner)
@ResponseSchema(RunnerGroupNotFoundError) @ResponseSchema(RunnerGroupNeededError)
@OpenAPI({ description: 'Create a new runner object (id will be generated automagicly).' }) @ResponseSchema(RunnerGroupNotFoundError)
async post(@Body({ validate: true }) createRunner: CreateRunner) { @OpenAPI({ description: 'Create a new runner object (id will be generated automagicly).' })
let runner; async post(@Body({ validate: true }) createRunner: CreateRunner) {
try { let runner;
runner = await createRunner.toRunner(); try {
} catch (error) { runner = await createRunner.toRunner();
throw error; } catch (error) {
} throw error;
}
runner = await this.runnerRepository.save(runner)
return new ResponseRunner(await this.runnerRepository.findOne(runner, { relations: ['scans', 'group'] })); runner = await this.runnerRepository.save(runner)
} return new ResponseRunner(await this.runnerRepository.findOne(runner, { relations: ['scans', 'group'] }));
}
@Put('/:id')
@ResponseSchema(ResponseRunner) @Put('/:id')
@ResponseSchema(RunnerNotFoundError, { statusCode: 404 }) @ResponseSchema(ResponseRunner)
@ResponseSchema(RunnerIdsNotMatchingError, { statusCode: 406 }) @ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
@OpenAPI({ description: "Update a runner object (id can't be changed)." }) @ResponseSchema(RunnerIdsNotMatchingError, { statusCode: 406 })
async put(@Param('id') id: number, @EntityFromBody() runner: Runner) { @OpenAPI({ description: "Update a runner object (id can't be changed)." })
let oldRunner = await this.runnerRepository.findOne({ id: id }, { relations: ['scans', 'group'] }); async put(@Param('id') id: number, @EntityFromBody() runner: Runner) {
let oldRunner = await this.runnerRepository.findOne({ id: id }, { relations: ['scans', 'group'] });
if (!oldRunner) {
throw new RunnerNotFoundError(); if (!oldRunner) {
} throw new RunnerNotFoundError();
}
if (oldRunner.id != runner.id) {
throw new RunnerIdsNotMatchingError(); if (oldRunner.id != runner.id) {
} throw new RunnerIdsNotMatchingError();
}
await this.runnerRepository.update(oldRunner, runner);
return new ResponseRunner(runner); await this.runnerRepository.update(oldRunner, runner);
} return new ResponseRunner(runner);
}
@Delete('/:id')
@ResponseSchema(ResponseRunner) @Delete('/:id')
@ResponseSchema(RunnerNotFoundError, { statusCode: 404 }) @ResponseSchema(ResponseRunner)
@OpenAPI({ description: 'Delete a specified runner (if it exists).' }) @ResponseSchema(ResponseEmpty, { statusCode: 204 })
async remove(@EntityFromParam('id') runner: Runner, @QueryParam("force") force: boolean) { @OnUndefined(204)
if (!runner) { throw new RunnerNotFoundError(); } @OpenAPI({ description: 'Delete a specified runner (if it exists).' })
const responseRunner = await this.runnerRepository.findOne(runner, { relations: ['scans', 'group'] }); async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
let runner = await this.runnerRepository.findOne({ id: id });
if (!runner) { if (!runner) { return null; }
throw new RunnerNotFoundError(); const responseRunner = await this.runnerRepository.findOne(runner, { relations: ['scans', 'group'] });
}
if (!runner) {
await this.runnerRepository.delete(runner); throw new RunnerNotFoundError();
return new ResponseRunner(responseRunner); }
}
} await this.runnerRepository.delete(runner);
return new ResponseRunner(responseRunner);
}
}

View File

@ -1,121 +1,124 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers'; import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
import { RunnerOrganisationHasRunnersError, RunnerOrganisationHasTeamsError, RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors'; import { RunnerOrganisationHasRunnersError, RunnerOrganisationHasTeamsError, RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors';
import { CreateRunnerOrganisation } from '../models/actions/CreateRunnerOrganisation'; import { CreateRunnerOrganisation } from '../models/actions/CreateRunnerOrganisation';
import { RunnerOrganisation } from '../models/entities/RunnerOrganisation'; import { RunnerOrganisation } from '../models/entities/RunnerOrganisation';
import { ResponseRunnerOrganisation } from '../models/responses/ResponseRunnerOrganisation'; import { ResponseEmpty } from '../models/responses/ResponseEmpty';
import { RunnerController } from './RunnerController'; import { ResponseRunnerOrganisation } from '../models/responses/ResponseRunnerOrganisation';
import { RunnerTeamController } from './RunnerTeamController'; import { RunnerController } from './RunnerController';
import { RunnerTeamController } from './RunnerTeamController';
@JsonController('/organisations')
//@Authorized('RUNNERS:read') @JsonController('/organisations')
export class RunnerOrganisationController { //@Authorized('RUNNERS:read')
private runnerOrganisationRepository: Repository<RunnerOrganisation>; export class RunnerOrganisationController {
private runnerOrganisationRepository: Repository<RunnerOrganisation>;
/**
* Gets the repository of this controller's model/entity. /**
*/ * Gets the repository of this controller's model/entity.
constructor() { */
this.runnerOrganisationRepository = getConnectionManager().get().getRepository(RunnerOrganisation); constructor() {
} this.runnerOrganisationRepository = getConnectionManager().get().getRepository(RunnerOrganisation);
}
@Get()
@ResponseSchema(ResponseRunnerOrganisation, { isArray: true }) @Get()
@OpenAPI({ description: 'Lists all runnerOrganisations.' }) @ResponseSchema(ResponseRunnerOrganisation, { isArray: true })
async getAll() { @OpenAPI({ description: 'Lists all runnerOrganisations.' })
let responseTeams: ResponseRunnerOrganisation[] = new Array<ResponseRunnerOrganisation>(); async getAll() {
const runners = await this.runnerOrganisationRepository.find({ relations: ['address', 'contact', 'teams'] }); let responseTeams: ResponseRunnerOrganisation[] = new Array<ResponseRunnerOrganisation>();
console.log(runners); const runners = await this.runnerOrganisationRepository.find({ relations: ['address', 'contact', 'teams'] });
runners.forEach(runner => { console.log(runners);
responseTeams.push(new ResponseRunnerOrganisation(runner)); runners.forEach(runner => {
}); responseTeams.push(new ResponseRunnerOrganisation(runner));
return responseTeams; });
} return responseTeams;
}
@Get('/:id')
@ResponseSchema(ResponseRunnerOrganisation) @Get('/:id')
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 }) @ResponseSchema(ResponseRunnerOrganisation)
@OnUndefined(RunnerOrganisationNotFoundError) @ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
@OpenAPI({ description: 'Returns a runnerOrganisation of a specified id (if it exists)' }) @OnUndefined(RunnerOrganisationNotFoundError)
async getOne(@Param('id') id: number) { @OpenAPI({ description: 'Returns a runnerOrganisation of a specified id (if it exists)' })
let runnerOrg = await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] }); async getOne(@Param('id') id: number) {
if (!runnerOrg) { throw new RunnerOrganisationNotFoundError(); } let runnerOrg = await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] });
return new ResponseRunnerOrganisation(runnerOrg); if (!runnerOrg) { throw new RunnerOrganisationNotFoundError(); }
} return new ResponseRunnerOrganisation(runnerOrg);
}
@Post()
@ResponseSchema(ResponseRunnerOrganisation) @Post()
@OpenAPI({ description: 'Create a new runnerOrganisation object (id will be generated automagicly).' }) @ResponseSchema(ResponseRunnerOrganisation)
async post(@Body({ validate: true }) createRunnerOrganisation: CreateRunnerOrganisation) { @OpenAPI({ description: 'Create a new runnerOrganisation object (id will be generated automagicly).' })
let runnerOrganisation; async post(@Body({ validate: true }) createRunnerOrganisation: CreateRunnerOrganisation) {
try { let runnerOrganisation;
runnerOrganisation = await createRunnerOrganisation.toRunnerOrganisation(); try {
} catch (error) { runnerOrganisation = await createRunnerOrganisation.toRunnerOrganisation();
throw error; } catch (error) {
} throw error;
}
runnerOrganisation = await this.runnerOrganisationRepository.save(runnerOrganisation);
runnerOrganisation = await this.runnerOrganisationRepository.save(runnerOrganisation);
return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] }));
} return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] }));
}
@Put('/:id')
@ResponseSchema(ResponseRunnerOrganisation) @Put('/:id')
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 }) @ResponseSchema(ResponseRunnerOrganisation)
@ResponseSchema(RunnerOrganisationIdsNotMatchingError, { statusCode: 406 }) @ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
@OpenAPI({ description: "Update a runnerOrganisation object (id can't be changed)." }) @ResponseSchema(RunnerOrganisationIdsNotMatchingError, { statusCode: 406 })
async put(@Param('id') id: number, @EntityFromBody() runnerOrganisation: RunnerOrganisation) { @OpenAPI({ description: "Update a runnerOrganisation object (id can't be changed)." })
let oldRunnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id }); async put(@Param('id') id: number, @EntityFromBody() runnerOrganisation: RunnerOrganisation) {
let oldRunnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id });
if (!oldRunnerOrganisation) {
throw new RunnerOrganisationNotFoundError(); if (!oldRunnerOrganisation) {
} throw new RunnerOrganisationNotFoundError();
}
if (oldRunnerOrganisation.id != runnerOrganisation.id) {
throw new RunnerOrganisationIdsNotMatchingError(); if (oldRunnerOrganisation.id != runnerOrganisation.id) {
} throw new RunnerOrganisationIdsNotMatchingError();
}
await this.runnerOrganisationRepository.update(oldRunnerOrganisation, runnerOrganisation);
await this.runnerOrganisationRepository.update(oldRunnerOrganisation, runnerOrganisation);
runnerOrganisation = await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] });
return new ResponseRunnerOrganisation(runnerOrganisation); runnerOrganisation = await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] });
} return new ResponseRunnerOrganisation(runnerOrganisation);
}
@Delete('/:id')
@ResponseSchema(ResponseRunnerOrganisation) @Delete('/:id')
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 }) @ResponseSchema(ResponseRunnerOrganisation)
@ResponseSchema(RunnerOrganisationHasTeamsError, { statusCode: 406 }) @ResponseSchema(ResponseEmpty, { statusCode: 204 })
@ResponseSchema(RunnerOrganisationHasRunnersError, { statusCode: 406 }) @ResponseSchema(RunnerOrganisationHasTeamsError, { statusCode: 406 })
@OpenAPI({ description: 'Delete a specified runnerOrganisation (if it exists).' }) @ResponseSchema(RunnerOrganisationHasRunnersError, { statusCode: 406 })
async remove(@EntityFromParam('id') organisation: RunnerOrganisation, @QueryParam("force") force: boolean) { @OnUndefined(204)
if (!organisation) { throw new RunnerOrganisationNotFoundError() } @OpenAPI({ description: 'Delete a specified runnerOrganisation (if it exists).' })
let runnerOrganisation = await this.runnerOrganisationRepository.findOne(organisation, { relations: ['address', 'contact', 'runners', 'teams'] }); async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
let organisation = await this.runnerOrganisationRepository.findOne({ id: id });
if (!force) { if (!organisation) { return null; }
if (runnerOrganisation.teams.length != 0) { let runnerOrganisation = await this.runnerOrganisationRepository.findOne(organisation, { relations: ['address', 'contact', 'runners', 'teams'] });
throw new RunnerOrganisationHasTeamsError();
} if (!force) {
} if (runnerOrganisation.teams.length != 0) {
const teamController = new RunnerTeamController() throw new RunnerOrganisationHasTeamsError();
for (let team of runnerOrganisation.teams) { }
await teamController.remove(team, true); }
} const teamController = new RunnerTeamController()
for (let team of runnerOrganisation.teams) {
if (!force) { await teamController.remove(team.id, true);
if (runnerOrganisation.runners.length != 0) { }
throw new RunnerOrganisationHasRunnersError();
} if (!force) {
} if (runnerOrganisation.runners.length != 0) {
const runnerController = new RunnerController() throw new RunnerOrganisationHasRunnersError();
for (let runner of runnerOrganisation.runners) { }
await runnerController.remove(runner, true); }
} const runnerController = new RunnerController()
for (let runner of runnerOrganisation.runners) {
const responseOrganisation = new ResponseRunnerOrganisation(runnerOrganisation); await runnerController.remove(runner.id, true);
await this.runnerOrganisationRepository.delete(organisation); }
return responseOrganisation;
} const responseOrganisation = new ResponseRunnerOrganisation(runnerOrganisation);
} await this.runnerOrganisationRepository.delete(organisation);
return responseOrganisation;
}
}

View File

@ -1,110 +1,113 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers'; import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
import { RunnerTeamHasRunnersError, RunnerTeamIdsNotMatchingError, RunnerTeamNotFoundError } from '../errors/RunnerTeamErrors'; import { RunnerTeamHasRunnersError, RunnerTeamIdsNotMatchingError, RunnerTeamNotFoundError } from '../errors/RunnerTeamErrors';
import { CreateRunnerTeam } from '../models/actions/CreateRunnerTeam'; import { CreateRunnerTeam } from '../models/actions/CreateRunnerTeam';
import { RunnerTeam } from '../models/entities/RunnerTeam'; import { RunnerTeam } from '../models/entities/RunnerTeam';
import { ResponseRunnerTeam } from '../models/responses/ResponseRunnerTeam'; import { ResponseEmpty } from '../models/responses/ResponseEmpty';
import { RunnerController } from './RunnerController'; import { ResponseRunnerTeam } from '../models/responses/ResponseRunnerTeam';
import { RunnerController } from './RunnerController';
@JsonController('/teams')
//@Authorized('RUNNERS:read') @JsonController('/teams')
export class RunnerTeamController { //@Authorized('RUNNERS:read')
private runnerTeamRepository: Repository<RunnerTeam>; export class RunnerTeamController {
private runnerTeamRepository: Repository<RunnerTeam>;
/**
* Gets the repository of this controller's model/entity. /**
*/ * Gets the repository of this controller's model/entity.
constructor() { */
this.runnerTeamRepository = getConnectionManager().get().getRepository(RunnerTeam); constructor() {
} this.runnerTeamRepository = getConnectionManager().get().getRepository(RunnerTeam);
}
@Get()
@ResponseSchema(ResponseRunnerTeam, { isArray: true }) @Get()
@OpenAPI({ description: 'Lists all runnerTeams.' }) @ResponseSchema(ResponseRunnerTeam, { isArray: true })
async getAll() { @OpenAPI({ description: 'Lists all runnerTeams.' })
let responseTeams: ResponseRunnerTeam[] = new Array<ResponseRunnerTeam>(); async getAll() {
const runners = await this.runnerTeamRepository.find({ relations: ['parentGroup', 'contact'] }); let responseTeams: ResponseRunnerTeam[] = new Array<ResponseRunnerTeam>();
console.log(runners); const runners = await this.runnerTeamRepository.find({ relations: ['parentGroup', 'contact'] });
runners.forEach(runner => { console.log(runners);
responseTeams.push(new ResponseRunnerTeam(runner)); runners.forEach(runner => {
}); responseTeams.push(new ResponseRunnerTeam(runner));
return responseTeams; });
} return responseTeams;
}
@Get('/:id')
@ResponseSchema(ResponseRunnerTeam) @Get('/:id')
@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 }) @ResponseSchema(ResponseRunnerTeam)
@OnUndefined(RunnerTeamNotFoundError) @ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 })
@OpenAPI({ description: 'Returns a runnerTeam of a specified id (if it exists)' }) @OnUndefined(RunnerTeamNotFoundError)
async getOne(@Param('id') id: number) { @OpenAPI({ description: 'Returns a runnerTeam of a specified id (if it exists)' })
let runnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] }); async getOne(@Param('id') id: number) {
if (!runnerTeam) { throw new RunnerTeamNotFoundError(); } let runnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] });
return new ResponseRunnerTeam(runnerTeam); if (!runnerTeam) { throw new RunnerTeamNotFoundError(); }
} return new ResponseRunnerTeam(runnerTeam);
}
@Post()
@ResponseSchema(ResponseRunnerTeam) @Post()
@OpenAPI({ description: 'Create a new runnerTeam object (id will be generated automagicly).' }) @ResponseSchema(ResponseRunnerTeam)
async post(@Body({ validate: true }) createRunnerTeam: CreateRunnerTeam) { @OpenAPI({ description: 'Create a new runnerTeam object (id will be generated automagicly).' })
let runnerTeam; async post(@Body({ validate: true }) createRunnerTeam: CreateRunnerTeam) {
try { let runnerTeam;
runnerTeam = await createRunnerTeam.toRunnerTeam(); try {
} catch (error) { runnerTeam = await createRunnerTeam.toRunnerTeam();
throw error; } catch (error) {
} throw error;
}
runnerTeam = await this.runnerTeamRepository.save(runnerTeam);
runnerTeam = await this.runnerTeamRepository.findOne(runnerTeam, { relations: ['parentGroup', 'contact'] }); runnerTeam = await this.runnerTeamRepository.save(runnerTeam);
runnerTeam = await this.runnerTeamRepository.findOne(runnerTeam, { relations: ['parentGroup', 'contact'] });
return new ResponseRunnerTeam(runnerTeam);
} return new ResponseRunnerTeam(runnerTeam);
}
@Put('/:id')
@ResponseSchema(ResponseRunnerTeam) @Put('/:id')
@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 }) @ResponseSchema(ResponseRunnerTeam)
@ResponseSchema(RunnerTeamIdsNotMatchingError, { statusCode: 406 }) @ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 })
@OpenAPI({ description: "Update a runnerTeam object (id can't be changed)." }) @ResponseSchema(RunnerTeamIdsNotMatchingError, { statusCode: 406 })
async put(@Param('id') id: number, @EntityFromBody() runnerTeam: RunnerTeam) { @OpenAPI({ description: "Update a runnerTeam object (id can't be changed)." })
let oldRunnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] }); async put(@Param('id') id: number, @EntityFromBody() runnerTeam: RunnerTeam) {
let oldRunnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] });
if (!oldRunnerTeam) {
throw new RunnerTeamNotFoundError(); if (!oldRunnerTeam) {
} throw new RunnerTeamNotFoundError();
}
if (oldRunnerTeam.id != runnerTeam.id) {
throw new RunnerTeamIdsNotMatchingError(); if (oldRunnerTeam.id != runnerTeam.id) {
} throw new RunnerTeamIdsNotMatchingError();
}
await this.runnerTeamRepository.update(oldRunnerTeam, runnerTeam);
await this.runnerTeamRepository.update(oldRunnerTeam, runnerTeam);
runnerTeam = await this.runnerTeamRepository.findOne(runnerTeam, { relations: ['parentGroup', 'contact'] });
return new ResponseRunnerTeam(runnerTeam); runnerTeam = await this.runnerTeamRepository.findOne(runnerTeam, { relations: ['parentGroup', 'contact'] });
} return new ResponseRunnerTeam(runnerTeam);
}
@Delete('/:id')
@ResponseSchema(ResponseRunnerTeam) @Delete('/:id')
@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 }) @ResponseSchema(ResponseRunnerTeam)
@ResponseSchema(RunnerTeamHasRunnersError, { statusCode: 406 }) @ResponseSchema(ResponseEmpty, { statusCode: 204 })
@OpenAPI({ description: 'Delete a specified runnerTeam (if it exists).' }) @ResponseSchema(RunnerTeamHasRunnersError, { statusCode: 406 })
async remove(@EntityFromParam('id') team: RunnerTeam, @QueryParam("force") force: boolean) { @OnUndefined(204)
if (!team) { throw new RunnerTeamNotFoundError(); } @OpenAPI({ description: 'Delete a specified runnerTeam (if it exists).' })
let runnerTeam = await this.runnerTeamRepository.findOne(team, { relations: ['parentGroup', 'contact', 'runners'] }); async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
let team = await this.runnerTeamRepository.findOne({ id: id });
if (!force) { if (!team) { return null; }
if (runnerTeam.runners.length != 0) { let runnerTeam = await this.runnerTeamRepository.findOne(team, { relations: ['parentGroup', 'contact', 'runners'] });
throw new RunnerTeamHasRunnersError();
} if (!force) {
} if (runnerTeam.runners.length != 0) {
const runnerController = new RunnerController() throw new RunnerTeamHasRunnersError();
for (let runner of runnerTeam.runners) { }
await runnerController.remove(runner, true); }
} const runnerController = new RunnerController()
for (let runner of runnerTeam.runners) {
const responseTeam = new ResponseRunnerTeam(runnerTeam); await runnerController.remove(runner.id, true);
await this.runnerTeamRepository.delete(team); }
return responseTeam;
} const responseTeam = new ResponseRunnerTeam(runnerTeam);
} await this.runnerTeamRepository.delete(team);
return responseTeam;
}
}

View File

@ -1,10 +1,11 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers'; import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
import { TrackIdsNotMatchingError, TrackNotFoundError } from "../errors/TrackErrors"; import { TrackIdsNotMatchingError, TrackNotFoundError } from "../errors/TrackErrors";
import { CreateTrack } from '../models/actions/CreateTrack'; import { CreateTrack } from '../models/actions/CreateTrack';
import { Track } from '../models/entities/Track'; import { Track } from '../models/entities/Track';
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
import { ResponseTrack } from '../models/responses/ResponseTrack'; import { ResponseTrack } from '../models/responses/ResponseTrack';
@JsonController('/tracks') @JsonController('/tracks')
@ -74,10 +75,12 @@ export class TrackController {
@Delete('/:id') @Delete('/:id')
@ResponseSchema(ResponseTrack) @ResponseSchema(ResponseTrack)
@ResponseSchema(TrackNotFoundError, { statusCode: 404 }) @ResponseSchema(ResponseEmpty, { statusCode: 204 })
@OnUndefined(204)
@OpenAPI({ description: "Delete a specified track (if it exists)." }) @OpenAPI({ description: "Delete a specified track (if it exists)." })
async remove(@EntityFromParam('id') track: Track) { async remove(@Param("id") id: number) {
if (!track) { throw new TrackNotFoundError(); } let track = await this.trackRepository.findOne({ id: id });
if (!track) { return null; }
await this.trackRepository.delete(track); await this.trackRepository.delete(track);
return new ResponseTrack(track); return new ResponseTrack(track);

View File

@ -1,85 +1,88 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers'; import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
import { UserIdsNotMatchingError, UserNotFoundError } from '../errors/UserErrors'; import { UserIdsNotMatchingError, UserNotFoundError } from '../errors/UserErrors';
import { UserGroupNotFoundError } from '../errors/UserGroupErrors'; import { UserGroupNotFoundError } from '../errors/UserGroupErrors';
import { CreateUser } from '../models/actions/CreateUser'; import { CreateUser } from '../models/actions/CreateUser';
import { User } from '../models/entities/User'; import { User } from '../models/entities/User';
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
@JsonController('/users')
export class UserController { @JsonController('/users')
private userRepository: Repository<User>; export class UserController {
private userRepository: Repository<User>;
/**
* Gets the repository of this controller's model/entity. /**
*/ * Gets the repository of this controller's model/entity.
constructor() { */
this.userRepository = getConnectionManager().get().getRepository(User); constructor() {
} this.userRepository = getConnectionManager().get().getRepository(User);
}
@Get()
@ResponseSchema(User, { isArray: true }) @Get()
@OpenAPI({ description: 'Lists all users.' }) @ResponseSchema(User, { isArray: true })
getAll() { @OpenAPI({ description: 'Lists all users.' })
return this.userRepository.find(); getAll() {
} return this.userRepository.find();
}
@Get('/:id')
@ResponseSchema(User) @Get('/:id')
@ResponseSchema(UserNotFoundError, { statusCode: 404 }) @ResponseSchema(User)
@OnUndefined(UserNotFoundError) @ResponseSchema(UserNotFoundError, { statusCode: 404 })
@OpenAPI({ description: 'Returns a user of a specified id (if it exists)' }) @OnUndefined(UserNotFoundError)
getOne(@Param('id') id: number) { @OpenAPI({ description: 'Returns a user of a specified id (if it exists)' })
return this.userRepository.findOne({ id: id }); getOne(@Param('id') id: number) {
} return this.userRepository.findOne({ id: id });
}
@Post()
@ResponseSchema(User) @Post()
@ResponseSchema(UserGroupNotFoundError) @ResponseSchema(User)
@OpenAPI({ description: 'Create a new user object (id will be generated automagicly).' }) @ResponseSchema(UserGroupNotFoundError)
async post(@Body({ validate: true }) createUser: CreateUser) { @OpenAPI({ description: 'Create a new user object (id will be generated automagicly).' })
let user; async post(@Body({ validate: true }) createUser: CreateUser) {
try { let user;
user = await createUser.toUser(); try {
} catch (error) { user = await createUser.toUser();
throw error; } catch (error) {
} throw error;
}
return this.userRepository.save(user);
} return this.userRepository.save(user);
}
@Put('/:id')
@ResponseSchema(User) @Put('/:id')
@ResponseSchema(UserNotFoundError, { statusCode: 404 }) @ResponseSchema(User)
@ResponseSchema(UserIdsNotMatchingError, { statusCode: 406 }) @ResponseSchema(UserNotFoundError, { statusCode: 404 })
@OpenAPI({ description: "Update a user object (id can't be changed)." }) @ResponseSchema(UserIdsNotMatchingError, { statusCode: 406 })
async put(@Param('id') id: number, @EntityFromBody() user: User) { @OpenAPI({ description: "Update a user object (id can't be changed)." })
let oldUser = await this.userRepository.findOne({ id: id }); async put(@Param('id') id: number, @EntityFromBody() user: User) {
let oldUser = await this.userRepository.findOne({ id: id });
if (!oldUser) {
throw new UserNotFoundError(); if (!oldUser) {
} throw new UserNotFoundError();
}
if (oldUser.id != user.id) {
throw new UserIdsNotMatchingError(); if (oldUser.id != user.id) {
} throw new UserIdsNotMatchingError();
}
await this.userRepository.update(oldUser, user);
return user; await this.userRepository.update(oldUser, user);
} return user;
}
@Delete('/:id')
@ResponseSchema(User) @Delete('/:id')
@ResponseSchema(UserNotFoundError, { statusCode: 404 }) @ResponseSchema(User)
@OpenAPI({ description: 'Delete a specified runner (if it exists).' }) @ResponseSchema(ResponseEmpty, { statusCode: 204 })
async remove(@EntityFromParam('id') user: User) { @OnUndefined(204)
if (!user) { @OpenAPI({ description: 'Delete a specified runner (if it exists).' })
throw new UserNotFoundError(); async remove(@Param("id") id: number) {
} let user = await this.userRepository.findOne({ id: id });
if (!user) {
await this.userRepository.delete(user); return null;
return user; }
}
} await this.userRepository.delete(user);
return user;
}
}

View File

@ -1,84 +1,87 @@
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers'; import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { EntityFromBody, EntityFromParam } from 'typeorm-routing-controllers-extensions'; import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
import { UserGroupIdsNotMatchingError, UserGroupNotFoundError } from '../errors/UserGroupErrors'; import { UserGroupIdsNotMatchingError, UserGroupNotFoundError } from '../errors/UserGroupErrors';
import { CreateUserGroup } from '../models/actions/CreateUserGroup'; import { CreateUserGroup } from '../models/actions/CreateUserGroup';
import { UserGroup } from '../models/entities/UserGroup'; import { UserGroup } from '../models/entities/UserGroup';
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
@JsonController('/usergroups')
export class UserGroupController { @JsonController('/usergroups')
private userGroupsRepository: Repository<UserGroup>; export class UserGroupController {
private userGroupsRepository: Repository<UserGroup>;
/**
* Gets the repository of this controller's model/entity. /**
*/ * Gets the repository of this controller's model/entity.
constructor() { */
this.userGroupsRepository = getConnectionManager().get().getRepository(UserGroup); constructor() {
} this.userGroupsRepository = getConnectionManager().get().getRepository(UserGroup);
}
@Get()
@ResponseSchema(UserGroup, { isArray: true }) @Get()
@OpenAPI({ description: 'Lists all usergroups.' }) @ResponseSchema(UserGroup, { isArray: true })
getAll() { @OpenAPI({ description: 'Lists all usergroups.' })
return this.userGroupsRepository.find(); getAll() {
} return this.userGroupsRepository.find();
}
@Get('/:id')
@ResponseSchema(UserGroup) @Get('/:id')
@ResponseSchema(UserGroupNotFoundError, { statusCode: 404 }) @ResponseSchema(UserGroup)
@OnUndefined(UserGroupNotFoundError) @ResponseSchema(UserGroupNotFoundError, { statusCode: 404 })
@OpenAPI({ description: 'Returns a usergroup of a specified id (if it exists)' }) @OnUndefined(UserGroupNotFoundError)
getOne(@Param('id') id: number) { @OpenAPI({ description: 'Returns a usergroup of a specified id (if it exists)' })
return this.userGroupsRepository.findOne({ id: id }); getOne(@Param('id') id: number) {
} return this.userGroupsRepository.findOne({ id: id });
}
@Post()
@ResponseSchema(UserGroup) @Post()
@ResponseSchema(UserGroupNotFoundError) @ResponseSchema(UserGroup)
@OpenAPI({ description: 'Create a new usergroup object (id will be generated automagicly).' }) @ResponseSchema(UserGroupNotFoundError)
async post(@Body({ validate: true }) createUserGroup: CreateUserGroup) { @OpenAPI({ description: 'Create a new usergroup object (id will be generated automagicly).' })
let userGroup; async post(@Body({ validate: true }) createUserGroup: CreateUserGroup) {
try { let userGroup;
userGroup = await createUserGroup.toUserGroup(); try {
} catch (error) { userGroup = await createUserGroup.toUserGroup();
throw error; } catch (error) {
} throw error;
}
return this.userGroupsRepository.save(userGroup);
} return this.userGroupsRepository.save(userGroup);
}
@Put('/:id')
@ResponseSchema(UserGroup) @Put('/:id')
@ResponseSchema(UserGroupNotFoundError, { statusCode: 404 }) @ResponseSchema(UserGroup)
@ResponseSchema(UserGroupIdsNotMatchingError, { statusCode: 406 }) @ResponseSchema(UserGroupNotFoundError, { statusCode: 404 })
@OpenAPI({ description: "Update a usergroup object (id can't be changed)." }) @ResponseSchema(UserGroupIdsNotMatchingError, { statusCode: 406 })
async put(@Param('id') id: number, @EntityFromBody() userGroup: UserGroup) { @OpenAPI({ description: "Update a usergroup object (id can't be changed)." })
let oldUserGroup = await this.userGroupsRepository.findOne({ id: id }); async put(@Param('id') id: number, @EntityFromBody() userGroup: UserGroup) {
let oldUserGroup = await this.userGroupsRepository.findOne({ id: id });
if (!oldUserGroup) {
throw new UserGroupNotFoundError() if (!oldUserGroup) {
} throw new UserGroupNotFoundError()
}
if (oldUserGroup.id != userGroup.id) {
throw new UserGroupIdsNotMatchingError(); if (oldUserGroup.id != userGroup.id) {
} throw new UserGroupIdsNotMatchingError();
}
await this.userGroupsRepository.update(oldUserGroup, userGroup);
return userGroup; await this.userGroupsRepository.update(oldUserGroup, userGroup);
} return userGroup;
}
@Delete('/:id')
@ResponseSchema(UserGroup) @Delete('/:id')
@ResponseSchema(UserGroupNotFoundError, { statusCode: 404 }) @ResponseSchema(UserGroup)
@OpenAPI({ description: 'Delete a specified usergroup (if it exists).' }) @ResponseSchema(ResponseEmpty, { statusCode: 204 })
async remove(@EntityFromParam('id') group: UserGroup) { @OnUndefined(204)
if (!group) { @OpenAPI({ description: 'Delete a specified usergroup (if it exists).' })
throw new UserGroupNotFoundError(); async remove(@Param("id") id: number) {
} let group = await this.userGroupsRepository.findOne({ id: id });
if (!group) {
await this.userGroupsRepository.delete(group); return null;
return group; }
}
} await this.userGroupsRepository.delete(group);
return group;
}
}

View File

@ -1,83 +1,84 @@
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { config } from '../../config';
import { Address } from '../entities/Address'; import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
import { GroupContact } from '../entities/GroupContact'; import { Address } from '../entities/Address';
import { GroupContact } from '../entities/GroupContact';
export class CreateGroupContact {
/** export class CreateGroupContact {
* The contact's first name. /**
*/ * The contact's first name.
@IsNotEmpty() */
@IsString() @IsNotEmpty()
firstname: string; @IsString()
firstname: string;
/**
* The contact's middle name. /**
* Optional * The contact's middle name.
*/ * Optional
@IsOptional() */
@IsString() @IsOptional()
middlename?: string; @IsString()
middlename?: string;
/**
* The contact's last name. /**
*/ * The contact's last name.
@IsNotEmpty() */
@IsString() @IsNotEmpty()
lastname: string; @IsString()
lastname: string;
/**
* The contact's address. /**
* Optional * The contact's address.
*/ * Optional
@IsInt() */
@IsOptional() @IsInt()
address?: number; @IsOptional()
address?: number;
/**
* The contact's phone number. /**
* Optional * The contact's phone number.
*/ * Optional
@IsOptional() */
@IsPhoneNumber("DE") @IsOptional()
phone?: string; @IsPhoneNumber(config.phone_validation_countrycode)
phone?: string;
/**
* The contact's email address. /**
* Optional * The contact's email address.
*/ * Optional
@IsOptional() */
@IsEmail() @IsOptional()
email?: string; @IsEmail()
email?: string;
/**
* Get's this participant's address from this.address. /**
*/ * Get's this participant's address from this.address.
public async getAddress(): Promise<Address> { */
if (this.address === undefined) { public async getAddress(): Promise<Address> {
return null; if (this.address === undefined) {
} return null;
if (!isNaN(this.address)) { }
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address }); if (!isNaN(this.address)) {
if (!address) { throw new AddressNotFoundError; } let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
return address; if (!address) { throw new AddressNotFoundError; }
} return address;
}
throw new AddressWrongTypeError;
} throw new AddressWrongTypeError;
}
/**
* Creates a Address object based on this. /**
*/ * Creates a Address object based on this.
public async toGroupContact(): Promise<GroupContact> { */
let contact: GroupContact = new GroupContact(); public async toGroupContact(): Promise<GroupContact> {
contact.firstname = this.firstname; let contact: GroupContact = new GroupContact();
contact.middlename = this.middlename; contact.firstname = this.firstname;
contact.lastname = this.lastname; contact.middlename = this.middlename;
contact.email = this.email; contact.lastname = this.lastname;
contact.phone = this.phone; contact.email = this.email;
contact.address = await this.getAddress(); contact.phone = this.phone;
return null; contact.address = await this.getAddress();
} return null;
}
} }

View File

@ -1,71 +1,72 @@
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { config } from '../../config';
import { Address } from '../entities/Address'; import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
import { Address } from '../entities/Address';
export abstract class CreateParticipant {
/** export abstract class CreateParticipant {
* The new participant's first name. /**
*/ * The new participant's first name.
@IsString() */
@IsNotEmpty() @IsString()
firstname: string; @IsNotEmpty()
firstname: string;
/**
* The new participant's middle name. /**
* Optional. * The new participant's middle name.
*/ * Optional.
@IsString() */
@IsNotEmpty() @IsString()
middlename?: string; @IsNotEmpty()
middlename?: string;
/**
* The new participant's last name. /**
*/ * The new participant's last name.
@IsString() */
@IsNotEmpty() @IsString()
lastname: string; @IsNotEmpty()
lastname: string;
/**
* The new participant's phone number. /**
* Optional. * The new participant's phone number.
*/ * Optional.
@IsString() */
@IsOptional() @IsString()
@IsPhoneNumber("ZZ") @IsOptional()
phone?: string; @IsPhoneNumber(config.phone_validation_countrycode)
phone?: string;
/**
* The new participant's e-mail address. /**
* Optional. * The new participant's e-mail address.
*/ * Optional.
@IsString() */
@IsOptional() @IsString()
@IsEmail() @IsOptional()
email?: string; @IsEmail()
email?: string;
/**
* The new participant's address. /**
* Must be of type number (address id), createAddress (new address) or address (existing address) * The new participant's address.
* Optional. * Must be of type number (address id), createAddress (new address) or address (existing address)
*/ * Optional.
@IsInt() */
@IsOptional() @IsInt()
address?: number; @IsOptional()
address?: number;
/**
* Get's this participant's address from this.address. /**
*/ * Get's this participant's address from this.address.
public async getAddress(): Promise<Address> { */
if (this.address === undefined) { public async getAddress(): Promise<Address> {
return null; if (this.address === undefined) {
} return null;
if (!isNaN(this.address)) { }
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address }); if (!isNaN(this.address)) {
if (!address) { throw new AddressNotFoundError; } let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
return address; if (!address) { throw new AddressNotFoundError; }
} return address;
}
throw new AddressWrongTypeError;
} throw new AddressWrongTypeError;
}
} }

View File

@ -1,119 +1,120 @@
import * as argon2 from "argon2"; import * as argon2 from "argon2";
import { IsEmail, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { IsEmail, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import * as uuid from 'uuid'; import * as uuid from 'uuid';
import { UsernameOrEmailNeededError } from '../../errors/UserErrors'; import { config } from '../../config';
import { UserGroupNotFoundError } from '../../errors/UserGroupErrors'; import { UsernameOrEmailNeededError } from '../../errors/UserErrors';
import { User } from '../entities/User'; import { UserGroupNotFoundError } from '../../errors/UserGroupErrors';
import { UserGroup } from '../entities/UserGroup'; import { User } from '../entities/User';
import { UserGroup } from '../entities/UserGroup';
export class CreateUser {
/** export class CreateUser {
* The new user's first name. /**
*/ * The new user's first name.
@IsString() */
firstname: string; @IsString()
firstname: string;
/**
* The new user's middle name. /**
* Optinal. * The new user's middle name.
*/ * Optinal.
@IsString() */
@IsOptional() @IsString()
middlename?: string; @IsOptional()
middlename?: string;
/**
* The new user's last name. /**
*/ * The new user's last name.
@IsString() */
lastname: string; @IsString()
lastname: string;
/**
* The new user's username. /**
* You have to provide at least one of: {email, username}. * The new user's username.
*/ * You have to provide at least one of: {email, username}.
@IsOptional() */
@IsString() @IsOptional()
username?: string; @IsString()
username?: string;
/**
* The new user's email address. /**
* You have to provide at least one of: {email, username}. * The new user's email address.
*/ * You have to provide at least one of: {email, username}.
@IsEmail() */
@IsString() @IsEmail()
@IsOptional() @IsString()
email?: string; @IsOptional()
email?: string;
/**
* The new user's phone number. /**
* Optional * The new user's phone number.
*/ * Optional
@IsPhoneNumber("ZZ") */
@IsOptional() @IsPhoneNumber(config.phone_validation_countrycode)
phone?: string; @IsOptional()
phone?: string;
/**
* The new user's password. /**
* This will of course not be saved in plaintext :) * The new user's password.
*/ * This will of course not be saved in plaintext :)
@IsString() */
password: string; @IsString()
password: string;
/**
* The new user's groups' id(s). /**
* You can provide either one groupId or an array of groupIDs. * The new user's groups' id(s).
* Optional. * You can provide either one groupId or an array of groupIDs.
*/ * Optional.
@IsOptional() */
groupId?: number[] | number @IsOptional()
groupId?: number[] | number
//TODO: ProfilePics
//TODO: ProfilePics
/**
* Converts this to a User Entity. /**
*/ * Converts this to a User Entity.
public async toUser(): Promise<User> { */
let newUser: User = new User(); public async toUser(): Promise<User> {
let newUser: User = new User();
if (this.email === undefined && this.username === undefined) {
throw new UsernameOrEmailNeededError(); if (this.email === undefined && this.username === undefined) {
} throw new UsernameOrEmailNeededError();
}
if (this.groupId) {
if (!Array.isArray(this.groupId)) { if (this.groupId) {
this.groupId = [this.groupId] if (!Array.isArray(this.groupId)) {
} this.groupId = [this.groupId]
const groupIDs: number[] = this.groupId }
let errors = 0 const groupIDs: number[] = this.groupId
const validateusergroups = async () => { let errors = 0
let foundgroups = [] const validateusergroups = async () => {
for (const g of groupIDs) { let foundgroups = []
const found = await getConnectionManager().get().getRepository(UserGroup).find({ id: g }); for (const g of groupIDs) {
if (found.length === 0) { const found = await getConnectionManager().get().getRepository(UserGroup).find({ id: g });
errors++ if (found.length === 0) {
} else { errors++
foundgroups.push(found[0]) } else {
} foundgroups.push(found[0])
} }
newUser.groups = foundgroups }
} newUser.groups = foundgroups
await validateusergroups() }
if (errors !== 0) { await validateusergroups()
throw new UserGroupNotFoundError(); if (errors !== 0) {
} throw new UserGroupNotFoundError();
} }
}
newUser.email = this.email
newUser.username = this.username newUser.email = this.email
newUser.firstname = this.firstname newUser.username = this.username
newUser.middlename = this.middlename newUser.firstname = this.firstname
newUser.lastname = this.lastname newUser.middlename = this.middlename
newUser.uuid = uuid.v4() newUser.lastname = this.lastname
newUser.phone = this.phone newUser.uuid = uuid.v4()
newUser.password = await argon2.hash(this.password + newUser.uuid); newUser.phone = this.phone
//TODO: ProfilePics newUser.password = await argon2.hash(this.password + newUser.uuid);
//TODO: ProfilePics
return newUser;
} return newUser;
}
} }

View File

@ -1,82 +1,83 @@
import { import {
IsEmail, IsEmail,
IsInt, IsInt,
IsNotEmpty, IsNotEmpty,
IsOptional, IsOptional,
IsPhoneNumber, IsPhoneNumber,
IsString IsString
} from "class-validator"; } from "class-validator";
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm"; import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { Address } from "./Address"; import { config } from '../../config';
import { RunnerGroup } from "./RunnerGroup"; import { Address } from "./Address";
import { RunnerGroup } from "./RunnerGroup";
/**
* Defines a group's contact. /**
*/ * Defines a group's contact.
@Entity() */
export class GroupContact { @Entity()
/** export class GroupContact {
* Autogenerated unique id (primary key). /**
*/ * Autogenerated unique id (primary key).
@PrimaryGeneratedColumn() */
@IsInt() @PrimaryGeneratedColumn()
id: number; @IsInt()
id: number;
/**
* The contact's first name. /**
*/ * The contact's first name.
@Column() */
@IsNotEmpty() @Column()
@IsString() @IsNotEmpty()
firstname: string; @IsString()
firstname: string;
/**
* The contact's middle name. /**
* Optional * The contact's middle name.
*/ * Optional
@Column({ nullable: true }) */
@IsOptional() @Column({ nullable: true })
@IsString() @IsOptional()
middlename?: string; @IsString()
middlename?: string;
/**
* The contact's last name. /**
*/ * The contact's last name.
@Column() */
@IsNotEmpty() @Column()
@IsString() @IsNotEmpty()
lastname: string; @IsString()
lastname: string;
/**
* The contact's address. /**
* Optional * The contact's address.
*/ * Optional
@IsOptional() */
@ManyToOne(() => Address, address => address.participants, { nullable: true }) @IsOptional()
address?: Address; @ManyToOne(() => Address, address => address.participants, { nullable: true })
address?: Address;
/**
* The contact's phone number. /**
* Optional * The contact's phone number.
*/ * Optional
@Column({ nullable: true }) */
@IsOptional() @Column({ nullable: true })
@IsPhoneNumber("DE") @IsOptional()
phone?: string; @IsPhoneNumber(config.phone_validation_countrycode)
phone?: string;
/**
* The contact's email address. /**
* Optional * The contact's email address.
*/ * Optional
@Column({ nullable: true }) */
@IsOptional() @Column({ nullable: true })
@IsEmail() @IsOptional()
email?: string; @IsEmail()
email?: string;
/**
* Used to link contacts to groups. /**
*/ * Used to link contacts to groups.
@OneToMany(() => RunnerGroup, group => group.contact, { nullable: true }) */
groups: RunnerGroup[]; @OneToMany(() => RunnerGroup, group => group.contact, { nullable: true })
groups: RunnerGroup[];
} }

View File

@ -1,82 +1,83 @@
import { import {
IsEmail, IsEmail,
IsInt, IsInt,
IsNotEmpty, IsNotEmpty,
IsOptional, IsOptional,
IsPhoneNumber, IsPhoneNumber,
IsString IsString
} from "class-validator"; } from "class-validator";
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn, TableInheritance } from "typeorm"; import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn, TableInheritance } from "typeorm";
import { Address } from "./Address"; import { config } from '../../config';
import { Donation } from "./Donation"; import { Address } from "./Address";
import { Donation } from "./Donation";
/**
* Defines the participant interface. /**
*/ * Defines the participant interface.
@Entity() */
@TableInheritance({ column: { name: "type", type: "varchar" } }) @Entity()
export abstract class Participant { @TableInheritance({ column: { name: "type", type: "varchar" } })
/** export abstract class Participant {
* Autogenerated unique id (primary key). /**
*/ * Autogenerated unique id (primary key).
@PrimaryGeneratedColumn() */
@IsInt() @PrimaryGeneratedColumn()
id: number; @IsInt()
id: number;
/**
* The participant's first name. /**
*/ * The participant's first name.
@Column() */
@IsNotEmpty() @Column()
@IsString() @IsNotEmpty()
firstname: string; @IsString()
firstname: string;
/**
* The participant's middle name. /**
* Optional * The participant's middle name.
*/ * Optional
@Column({ nullable: true }) */
@IsOptional() @Column({ nullable: true })
@IsString() @IsOptional()
middlename?: string; @IsString()
middlename?: string;
/**
* The participant's last name. /**
*/ * The participant's last name.
@Column() */
@IsNotEmpty() @Column()
@IsString() @IsNotEmpty()
lastname: string; @IsString()
lastname: string;
/**
* The participant's address. /**
* Optional * The participant's address.
*/ * Optional
@ManyToOne(() => Address, address => address.participants, { nullable: true }) */
address?: Address; @ManyToOne(() => Address, address => address.participants, { nullable: true })
address?: Address;
/**
* The participant's phone number. /**
* Optional * The participant's phone number.
*/ * Optional
@Column({ nullable: true }) */
@IsOptional() @Column({ nullable: true })
@IsPhoneNumber("DE") @IsOptional()
phone?: string; @IsPhoneNumber(config.phone_validation_countrycode)
phone?: string;
/**
* The participant's email address. /**
* Optional * The participant's email address.
*/ * Optional
@Column({ nullable: true }) */
@IsOptional() @Column({ nullable: true })
@IsEmail() @IsOptional()
email?: string; @IsEmail()
email?: string;
/**
* Used to link the participant as the donor of a donation. /**
*/ * Used to link the participant as the donor of a donation.
@OneToMany(() => Donation, donation => donation.donor, { nullable: true }) */
donations: Donation[]; @OneToMany(() => Donation, donation => donation.donor, { nullable: true })
donations: Donation[];
} }

View File

@ -1,5 +1,6 @@
import { IsBoolean, IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString, IsUUID } from "class-validator"; import { IsBoolean, IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString, IsUUID } from "class-validator";
import { Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm"; import { Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { config } from '../../config';
import { Permission } from './Permission'; import { Permission } from './Permission';
import { UserAction } from './UserAction'; import { UserAction } from './UserAction';
import { UserGroup } from './UserGroup'; import { UserGroup } from './UserGroup';
@ -35,7 +36,7 @@ export class User {
*/ */
@Column({ nullable: true }) @Column({ nullable: true })
@IsOptional() @IsOptional()
@IsPhoneNumber("ZZ") @IsPhoneNumber(config.phone_validation_countrycode)
phone?: string; phone?: string;
/** /**

View File

@ -0,0 +1,6 @@
/**
* Defines a empty response object
*/
export class ResponseEmpty {
}

BIN
test.sqlite-journal Normal file

Binary file not shown.