Compare commits

..

No commits in common. "d2898ff60f47c3184d1c0c877b87df903d12a6de" and "6396fffc045da75d15b84de66d1029b01f736d79" have entirely different histories.

18 changed files with 1060 additions and 1111 deletions

View File

@ -1,12 +1,12 @@
import { config } from 'dotenv'; import { config } from 'dotenv-safe';
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,81 +1,80 @@
{ {
"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",
"dotenv": "^8.2.0", "express": "^4.17.1",
"express": "^4.17.1", "helmet": "^4.2.0",
"helmet": "^4.2.0", "jsonwebtoken": "^8.5.1",
"jsonwebtoken": "^8.5.1", "multer": "^1.4.2",
"multer": "^1.4.2", "mysql": "^2.18.1",
"mysql": "^2.18.1", "pg": "^8.5.1",
"pg": "^8.5.1", "reflect-metadata": "^0.1.13",
"reflect-metadata": "^0.1.13", "routing-controllers": "^0.9.0-alpha.6",
"routing-controllers": "^0.9.0-alpha.6", "routing-controllers-openapi": "^2.1.0",
"routing-controllers-openapi": "^2.1.0", "swagger-ui-express": "^4.1.5",
"swagger-ui-express": "^4.1.5", "typeorm": "^0.2.29",
"typeorm": "^0.2.29", "typeorm-routing-controllers-extensions": "^0.2.0",
"typeorm-routing-controllers-extensions": "^0.2.0", "uuid": "^8.3.1"
"uuid": "^8.3.1" },
}, "devDependencies": {
"devDependencies": { "@types/cors": "^2.8.8",
"@types/cors": "^2.8.8", "@types/dotenv-safe": "^8.1.1",
"@types/dotenv": "^8.2.0", "@types/express": "^4.17.9",
"@types/express": "^4.17.9", "@types/jest": "^26.0.16",
"@types/jest": "^26.0.16", "@types/jsonwebtoken": "^8.5.0",
"@types/jsonwebtoken": "^8.5.0", "@types/multer": "^1.4.4",
"@types/multer": "^1.4.4", "@types/node": "^14.14.9",
"@types/node": "^14.14.9", "@types/swagger-ui-express": "^4.1.2",
"@types/swagger-ui-express": "^4.1.2", "@types/uuid": "^8.3.0",
"@types/uuid": "^8.3.0", "axios": "^0.21.0",
"axios": "^0.21.0", "dotenv-safe": "^8.2.0",
"dotenv-safe": "^8.2.0", "jest": "^26.6.3",
"jest": "^26.6.3", "nodemon": "^2.0.6",
"nodemon": "^2.0.6", "sqlite3": "^5.0.0",
"sqlite3": "^5.0.0", "ts-jest": "^26.4.4",
"ts-jest": "^26.4.4", "ts-node": "^9.0.0",
"ts-node": "^9.0.0", "typedoc": "^0.19.2",
"typedoc": "^0.19.2", "typescript": "^4.1.2"
"typescript": "^4.1.2" },
}, "scripts": {
"scripts": { "dev": "nodemon src/app.ts",
"dev": "nodemon src/app.ts", "build": "tsc",
"build": "tsc", "docs": "typedoc --out docs src",
"docs": "typedoc --out docs src", "test": "jest",
"test": "jest", "test:watch": "jest --watchAll"
"test:watch": "jest --watchAll" },
}, "nodemonConfig": {
"nodemonConfig": { "ignore": [
"ignore": [ "src/tests/*",
"src/tests/*", "docs/*"
"docs/*" ],
], "delay": "2500"
"delay": "2500" }
}
} }

View File

@ -1,31 +1,26 @@
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, e as errors } from './config'; import { config } 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}`
); );
}); });
} }
if (errors === 0) { main();
main();
} else {
console.log("error");
// something's wrong
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,5 @@
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';
@ -36,7 +35,7 @@ export class User {
*/ */
@Column({ nullable: true }) @Column({ nullable: true })
@IsOptional() @IsOptional()
@IsPhoneNumber(config.phone_validation_countrycode) @IsPhoneNumber("ZZ")
phone?: string; phone?: string;
/** /**

View File

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

Binary file not shown.