Compare commits
55 Commits
a1105f06ab
...
5c259484ee
Author | SHA1 | Date | |
---|---|---|---|
5c259484ee | |||
740d7f10f5 | |||
993096741d | |||
8607af62b5 | |||
76e19ca28d | |||
3ac150331a | |||
5a4a6cdcef | |||
e5b605cc55 | |||
7e4ce00c30 | |||
13d568ba3f | |||
65b2399eaa | |||
4352910d54 | |||
8c229dba82 | |||
675717f8ca | |||
0d21497c2f | |||
e5f65d0b80 | |||
51addd4a31 | |||
126799dab9 | |||
82f31185a1 | |||
c0c95056bf | |||
093f6f5f78 | |||
2f902755c4 | |||
975d30e411 | |||
a0fe8c0017 | |||
c33097f773 | |||
28c2b862f0 | |||
d23ed002b2 | |||
8870b26ce6 | |||
0e3cf07b91 | |||
179add80f4 | |||
45675b0699 | |||
9c63a34fe1 | |||
1850dd542d | |||
2a1b65f424 | |||
bd0c7ce042 | |||
d46ad59546 | |||
b8bc39d691 | |||
52dfe83354 | |||
aca13f7308 | |||
ef54dd5e9c | |||
6ae0c1b955 | |||
6244c969af | |||
d803704eee | |||
5d7d80d2e7 | |||
a5b1804e19 | |||
3e38bc5950 | |||
92cd58e641 | |||
6cb01090d0 | |||
c4b7ece974 | |||
c5c3058f3d | |||
a7afcf4cd1 | |||
f251b7acdb | |||
b0a24c6a74 | |||
b9bbdee826 | |||
1f3b312675 |
@ -1,9 +1,9 @@
|
|||||||
import "reflect-metadata";
|
|
||||||
import * as dotenvSafe from "dotenv-safe";
|
|
||||||
import { createExpressServer } from "routing-controllers";
|
|
||||||
import consola from "consola";
|
import consola from "consola";
|
||||||
import loaders from "./loaders/index";
|
import * as dotenvSafe from "dotenv-safe";
|
||||||
|
import "reflect-metadata";
|
||||||
|
import { createExpressServer } from "routing-controllers";
|
||||||
import authchecker from "./authchecker";
|
import authchecker from "./authchecker";
|
||||||
|
import loaders from "./loaders/index";
|
||||||
import { ErrorHandler } from './middlewares/ErrorHandler';
|
import { ErrorHandler } from './middlewares/ErrorHandler';
|
||||||
|
|
||||||
dotenvSafe.config();
|
dotenvSafe.config();
|
||||||
|
@ -1,13 +1,8 @@
|
|||||||
import * as jwt from "jsonwebtoken";
|
import * as jwt from "jsonwebtoken";
|
||||||
import { Action, HttpError } from "routing-controllers";
|
import { Action } from "routing-controllers";
|
||||||
// -----------
|
import { getConnectionManager } from 'typeorm';
|
||||||
const sampletoken = jwt.sign({
|
import { IllegalJWTError, NoPermissionError, UserNonexistantOrRefreshtokenInvalidError } from './errors/AuthError';
|
||||||
"permissions": {
|
import { User } from './models/entities/User';
|
||||||
"TRACKS": ["read", "update", "delete", "add"]
|
|
||||||
// "TRACKS": []
|
|
||||||
}
|
|
||||||
}, process.env.JWT_SECRET || "secretjwtsecret")
|
|
||||||
console.log(`sampletoken: ${sampletoken}`);
|
|
||||||
// -----------
|
// -----------
|
||||||
const authchecker = async (action: Action, permissions: string | string[]) => {
|
const authchecker = async (action: Action, permissions: string | string[]) => {
|
||||||
let required_permissions = undefined
|
let required_permissions = undefined
|
||||||
@ -20,9 +15,14 @@ const authchecker = async (action: Action, permissions: string | string[]) => {
|
|||||||
const provided_token = action.request.query["auth"];
|
const provided_token = action.request.query["auth"];
|
||||||
let jwtPayload = undefined
|
let jwtPayload = undefined
|
||||||
try {
|
try {
|
||||||
jwtPayload = <any>jwt.verify(provided_token, process.env.JWT_SECRET || "secretjwtsecret");
|
jwtPayload = <any>jwt.verify(provided_token, "securekey");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpError(401, "jwt_illegal")
|
console.log(error);
|
||||||
|
throw new IllegalJWTError()
|
||||||
|
}
|
||||||
|
const count = await getConnectionManager().get().getRepository(User).count({ id: jwtPayload["userdetails"]["id"], refreshTokenCount: jwtPayload["userdetails"]["refreshTokenCount"] })
|
||||||
|
if (count !== 1) {
|
||||||
|
throw new UserNonexistantOrRefreshtokenInvalidError()
|
||||||
}
|
}
|
||||||
if (jwtPayload.permissions) {
|
if (jwtPayload.permissions) {
|
||||||
action.response.local = {}
|
action.response.local = {}
|
||||||
@ -34,11 +34,11 @@ const authchecker = async (action: Action, permissions: string | string[]) => {
|
|||||||
if (actual_accesslevel_for_permission.includes(permission_access_level)) {
|
if (actual_accesslevel_for_permission.includes(permission_access_level)) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
throw new HttpError(403, "no")
|
throw new NoPermissionError()
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
throw new HttpError(403, "no")
|
throw new NoPermissionError()
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
try {
|
try {
|
||||||
|
71
src/controllers/AuthController.ts
Normal file
71
src/controllers/AuthController.ts
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
import { Body, JsonController, Post } from 'routing-controllers';
|
||||||
|
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||||
|
import { IllegalJWTError, InvalidCredentialsError, JwtNotProvidedError, PasswordNeededError, RefreshTokenCountInvalidError, UsernameOrEmailNeededError } from '../errors/AuthError';
|
||||||
|
import { UserNotFoundError } from '../errors/UserErrors';
|
||||||
|
import { CreateAuth } from '../models/creation/CreateAuth';
|
||||||
|
import { HandleLogout } from '../models/creation/HandleLogout';
|
||||||
|
import { RefreshAuth } from '../models/creation/RefreshAuth';
|
||||||
|
import { Auth } from '../models/responses/Auth';
|
||||||
|
import { Logout } from '../models/responses/Logout';
|
||||||
|
|
||||||
|
@JsonController('/auth')
|
||||||
|
export class AuthController {
|
||||||
|
constructor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post("/login")
|
||||||
|
@ResponseSchema(Auth)
|
||||||
|
@ResponseSchema(InvalidCredentialsError)
|
||||||
|
@ResponseSchema(UserNotFoundError)
|
||||||
|
@ResponseSchema(UsernameOrEmailNeededError)
|
||||||
|
@ResponseSchema(PasswordNeededError)
|
||||||
|
@ResponseSchema(InvalidCredentialsError)
|
||||||
|
@OpenAPI({ description: 'Create a new access token object' })
|
||||||
|
async login(@Body({ validate: true }) createAuth: CreateAuth) {
|
||||||
|
let auth;
|
||||||
|
try {
|
||||||
|
auth = await createAuth.toAuth();
|
||||||
|
console.log(auth);
|
||||||
|
} catch (error) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
return auth
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post("/logout")
|
||||||
|
@ResponseSchema(Logout)
|
||||||
|
@ResponseSchema(InvalidCredentialsError)
|
||||||
|
@ResponseSchema(UserNotFoundError)
|
||||||
|
@ResponseSchema(UsernameOrEmailNeededError)
|
||||||
|
@ResponseSchema(PasswordNeededError)
|
||||||
|
@ResponseSchema(InvalidCredentialsError)
|
||||||
|
@OpenAPI({ description: 'Create a new access token object' })
|
||||||
|
async logout(@Body({ validate: true }) handleLogout: HandleLogout) {
|
||||||
|
let logout;
|
||||||
|
try {
|
||||||
|
logout = await handleLogout.logout()
|
||||||
|
console.log(logout);
|
||||||
|
} catch (error) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
return logout
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post("/refresh")
|
||||||
|
@ResponseSchema(Auth)
|
||||||
|
@ResponseSchema(JwtNotProvidedError)
|
||||||
|
@ResponseSchema(IllegalJWTError)
|
||||||
|
@ResponseSchema(UserNotFoundError)
|
||||||
|
@ResponseSchema(RefreshTokenCountInvalidError)
|
||||||
|
@OpenAPI({ description: 'refresh a access token' })
|
||||||
|
async refresh(@Body({ validate: true }) refreshAuth: RefreshAuth) {
|
||||||
|
let auth;
|
||||||
|
try {
|
||||||
|
auth = await refreshAuth.toAuth();
|
||||||
|
console.log(auth);
|
||||||
|
} catch (error) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
return auth
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
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, RunnerGroupNotFoundError, RunnerIdsNotMatchingError, RunnerNotFoundError, RunnerOnlyOneGroupAllowedError } from '../errors/RunnerErrors';
|
import { RunnerGroupNeededError, RunnerGroupNotFoundError, RunnerIdsNotMatchingError, RunnerNotFoundError, RunnerOnlyOneGroupAllowedError } from '../errors/RunnerErrors';
|
||||||
import { CreateRunner } from '../models/creation/CreateRunner';
|
import { CreateRunner } from '../models/creation/CreateRunner';
|
||||||
import { Runner } from '../models/entities/Runner';
|
import { Runner } from '../models/entities/Runner';
|
||||||
@ -55,7 +55,8 @@ export class RunnerController {
|
|||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ResponseRunner(await this.runnerRepository.save(runner));
|
runner = await this.runnerRepository.save(runner)
|
||||||
|
return new ResponseRunner(await this.runnerRepository.findOne(runner, { relations: ['scans', 'group'] }));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put('/:id')
|
@Put('/:id')
|
||||||
@ -64,7 +65,7 @@ export class RunnerController {
|
|||||||
@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 });
|
let oldRunner = await this.runnerRepository.findOne({ id: id }, { relations: ['scans', 'group'] });
|
||||||
|
|
||||||
if (!oldRunner) {
|
if (!oldRunner) {
|
||||||
throw new RunnerNotFoundError();
|
throw new RunnerNotFoundError();
|
||||||
@ -82,14 +83,15 @@ export class RunnerController {
|
|||||||
@ResponseSchema(ResponseRunner)
|
@ResponseSchema(ResponseRunner)
|
||||||
@ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
|
@ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
|
||||||
@OpenAPI({ description: 'Delete a specified runner (if it exists).' })
|
@OpenAPI({ description: 'Delete a specified runner (if it exists).' })
|
||||||
async remove(@Param('id') id: number, @QueryParam("force") force: boolean) {
|
async remove(@EntityFromParam('id') runner: Runner, @QueryParam("force") force: boolean) {
|
||||||
let runner = await this.runnerRepository.findOne({ id: id });
|
if (!runner) { throw new RunnerNotFoundError(); }
|
||||||
|
const responseRunner = await this.runnerRepository.findOne(runner, { relations: ['scans', 'group'] });
|
||||||
|
|
||||||
if (!runner) {
|
if (!runner) {
|
||||||
throw new RunnerNotFoundError();
|
throw new RunnerNotFoundError();
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.runnerRepository.delete(runner);
|
await this.runnerRepository.delete(runner);
|
||||||
return new ResponseRunner(runner);
|
return new ResponseRunner(responseRunner);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
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/creation/CreateRunnerOrganisation';
|
import { CreateRunnerOrganisation } from '../models/creation/CreateRunnerOrganisation';
|
||||||
import { Runner } from '../models/entities/Runner';
|
|
||||||
import { RunnerOrganisation } from '../models/entities/RunnerOrganisation';
|
import { RunnerOrganisation } from '../models/entities/RunnerOrganisation';
|
||||||
import { RunnerTeam } from '../models/entities/RunnerTeam';
|
|
||||||
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';
|
||||||
@ -58,9 +56,8 @@ export class RunnerOrganisationController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
runnerOrganisation = await this.runnerOrganisationRepository.save(runnerOrganisation);
|
runnerOrganisation = await this.runnerOrganisationRepository.save(runnerOrganisation);
|
||||||
runnerOrganisation = await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] });
|
|
||||||
|
|
||||||
return new ResponseRunnerOrganisation(runnerOrganisation);
|
return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] }));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put('/:id')
|
@Put('/:id')
|
||||||
@ -88,39 +85,35 @@ export class RunnerOrganisationController {
|
|||||||
@Delete('/:id')
|
@Delete('/:id')
|
||||||
@ResponseSchema(ResponseRunnerOrganisation)
|
@ResponseSchema(ResponseRunnerOrganisation)
|
||||||
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
|
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
|
||||||
|
@ResponseSchema(RunnerOrganisationHasTeamsError, { statusCode: 406 })
|
||||||
@ResponseSchema(RunnerOrganisationHasRunnersError, { statusCode: 406 })
|
@ResponseSchema(RunnerOrganisationHasRunnersError, { statusCode: 406 })
|
||||||
@OpenAPI({ description: 'Delete a specified runnerOrganisation (if it exists).' })
|
@OpenAPI({ description: 'Delete a specified runnerOrganisation (if it exists).' })
|
||||||
async remove(@Param('id') id: number, @QueryParam("force") force: boolean) {
|
async remove(@EntityFromParam('id') organisation: RunnerOrganisation, @QueryParam("force") force: boolean) {
|
||||||
let runnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id }, { relations: ['address', 'contact', 'teams'] });
|
if (!organisation) { throw new RunnerOrganisationNotFoundError() }
|
||||||
|
let runnerOrganisation = await this.runnerOrganisationRepository.findOne(organisation, { relations: ['address', 'contact', 'runners', 'teams'] });
|
||||||
|
|
||||||
if (!runnerOrganisation) {
|
|
||||||
throw new RunnerOrganisationNotFoundError();
|
|
||||||
}
|
|
||||||
|
|
||||||
let runners: Runner[] = await runnerOrganisation.getRunners()
|
|
||||||
if (!force) {
|
if (!force) {
|
||||||
if (runners.length != 0) {
|
if (runnerOrganisation.teams.length != 0) {
|
||||||
throw new RunnerOrganisationHasRunnersError();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const runnerController = new RunnerController()
|
|
||||||
runners.forEach(runner => {
|
|
||||||
runnerController.remove(runner.id, true)
|
|
||||||
});
|
|
||||||
|
|
||||||
let teams: RunnerTeam[] = await runnerOrganisation.getTeams()
|
|
||||||
if (!force) {
|
|
||||||
if (teams.length != 0) {
|
|
||||||
throw new RunnerOrganisationHasTeamsError();
|
throw new RunnerOrganisationHasTeamsError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const teamController = new RunnerTeamController()
|
const teamController = new RunnerTeamController()
|
||||||
teams.forEach(team => {
|
for (let team of runnerOrganisation.teams) {
|
||||||
teamController.remove(team.id, true)
|
await teamController.remove(team, true);
|
||||||
});
|
}
|
||||||
|
|
||||||
|
if (!force) {
|
||||||
|
if (runnerOrganisation.runners.length != 0) {
|
||||||
|
throw new RunnerOrganisationHasRunnersError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const runnerController = new RunnerController()
|
||||||
|
for (let runner of runnerOrganisation.runners) {
|
||||||
|
await runnerController.remove(runner, true);
|
||||||
|
}
|
||||||
|
|
||||||
const responseOrganisation = new ResponseRunnerOrganisation(runnerOrganisation);
|
const responseOrganisation = new ResponseRunnerOrganisation(runnerOrganisation);
|
||||||
await this.runnerOrganisationRepository.delete({ id: runnerOrganisation.id });
|
await this.runnerOrganisationRepository.delete(organisation);
|
||||||
return responseOrganisation;
|
return responseOrganisation;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
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/creation/CreateRunnerTeam';
|
import { CreateRunnerTeam } from '../models/creation/CreateRunnerTeam';
|
||||||
import { Runner } from '../models/entities/Runner';
|
|
||||||
import { RunnerTeam } from '../models/entities/RunnerTeam';
|
import { RunnerTeam } from '../models/entities/RunnerTeam';
|
||||||
import { ResponseRunnerTeam } from '../models/responses/ResponseRunnerTeam';
|
import { ResponseRunnerTeam } from '../models/responses/ResponseRunnerTeam';
|
||||||
import { RunnerController } from './RunnerController';
|
import { RunnerController } from './RunnerController';
|
||||||
@ -88,26 +87,22 @@ export class RunnerTeamController {
|
|||||||
@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 })
|
@ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 })
|
||||||
@ResponseSchema(RunnerTeamHasRunnersError, { statusCode: 406 })
|
@ResponseSchema(RunnerTeamHasRunnersError, { statusCode: 406 })
|
||||||
@OpenAPI({ description: 'Delete a specified runnerTeam (if it exists).' })
|
@OpenAPI({ description: 'Delete a specified runnerTeam (if it exists).' })
|
||||||
async remove(@Param('id') id: number, @QueryParam("force") force: boolean) {
|
async remove(@EntityFromParam('id') team: RunnerTeam, @QueryParam("force") force: boolean) {
|
||||||
let runnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] });
|
if (!team) { throw new RunnerTeamNotFoundError(); }
|
||||||
|
let runnerTeam = await this.runnerTeamRepository.findOne(team, { relations: ['parentGroup', 'contact', 'runners'] });
|
||||||
|
|
||||||
if (!runnerTeam) {
|
|
||||||
throw new RunnerTeamNotFoundError();
|
|
||||||
}
|
|
||||||
|
|
||||||
let runners: Runner[] = await runnerTeam.getRunners()
|
|
||||||
if (!force) {
|
if (!force) {
|
||||||
if (runners.length != 0) {
|
if (runnerTeam.runners.length != 0) {
|
||||||
throw new RunnerTeamHasRunnersError();
|
throw new RunnerTeamHasRunnersError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const runnerController = new RunnerController()
|
const runnerController = new RunnerController()
|
||||||
runners.forEach(runner => {
|
for (let runner of runnerTeam.runners) {
|
||||||
runnerController.remove(runner.id, true)
|
await runnerController.remove(runner, true);
|
||||||
});
|
}
|
||||||
|
|
||||||
const responseTeam = new ResponseRunnerTeam(runnerTeam);
|
const responseTeam = new ResponseRunnerTeam(runnerTeam);
|
||||||
await this.runnerTeamRepository.delete({ id: runnerTeam.id });
|
await this.runnerTeamRepository.delete(team);
|
||||||
return responseTeam;
|
return responseTeam;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
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/creation/CreateTrack';
|
import { CreateTrack } from '../models/creation/CreateTrack';
|
||||||
import { Track } from '../models/entities/Track';
|
import { Track } from '../models/entities/Track';
|
||||||
@ -74,12 +74,8 @@ export class TrackController {
|
|||||||
@ResponseSchema(ResponseTrack)
|
@ResponseSchema(ResponseTrack)
|
||||||
@ResponseSchema(TrackNotFoundError, { statusCode: 404 })
|
@ResponseSchema(TrackNotFoundError, { statusCode: 404 })
|
||||||
@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) {
|
|
||||||
throw new TrackNotFoundError();
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.trackRepository.delete(track);
|
await this.trackRepository.delete(track);
|
||||||
return new ResponseTrack(track);
|
return new ResponseTrack(track);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
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 { UserGroupNotFoundError, UserIdsNotMatchingError, UserNotFoundError } from '../errors/UserErrors';
|
import { UserGroupNotFoundError, UserIdsNotMatchingError, UserNotFoundError } from '../errors/UserErrors';
|
||||||
import { CreateUser } from '../models/creation/CreateUser';
|
import { CreateUser } from '../models/creation/CreateUser';
|
||||||
import { User } from '../models/entities/User';
|
import { User } from '../models/entities/User';
|
||||||
@ -73,14 +73,12 @@ export class UserController {
|
|||||||
@ResponseSchema(User)
|
@ResponseSchema(User)
|
||||||
@ResponseSchema(UserNotFoundError, { statusCode: 404 })
|
@ResponseSchema(UserNotFoundError, { statusCode: 404 })
|
||||||
@OpenAPI({ description: 'Delete a specified runner (if it exists).' })
|
@OpenAPI({ description: 'Delete a specified runner (if it exists).' })
|
||||||
async remove(@Param('id') id: number) {
|
async remove(@EntityFromParam('id') user: User) {
|
||||||
let runner = await this.userRepository.findOne({ id: id });
|
if (!user) {
|
||||||
|
|
||||||
if (!runner) {
|
|
||||||
throw new UserNotFoundError();
|
throw new UserNotFoundError();
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.userRepository.delete(runner);
|
await this.userRepository.delete(user);
|
||||||
return runner;
|
return user;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
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/creation/CreateUserGroup';
|
import { CreateUserGroup } from '../models/creation/CreateUserGroup';
|
||||||
import { UserGroup } from '../models/entities/UserGroup';
|
import { UserGroup } from '../models/entities/UserGroup';
|
||||||
@ -73,14 +73,12 @@ export class UserGroupController {
|
|||||||
@ResponseSchema(UserGroup)
|
@ResponseSchema(UserGroup)
|
||||||
@ResponseSchema(UserGroupNotFoundError, { statusCode: 404 })
|
@ResponseSchema(UserGroupNotFoundError, { statusCode: 404 })
|
||||||
@OpenAPI({ description: 'Delete a specified usergroup (if it exists).' })
|
@OpenAPI({ description: 'Delete a specified usergroup (if it exists).' })
|
||||||
async remove(@Param('id') id: number) {
|
async remove(@EntityFromParam('id') group: UserGroup) {
|
||||||
let userGroup = await this.userGroupsRepository.findOne({ id: id });
|
if (!group) {
|
||||||
|
|
||||||
if (!userGroup) {
|
|
||||||
throw new UserGroupNotFoundError();
|
throw new UserGroupNotFoundError();
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.userGroupsRepository.delete(userGroup);
|
await this.userGroupsRepository.delete(group);
|
||||||
return userGroup;
|
return group;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
18
src/errors/AddressErrors.ts
Normal file
18
src/errors/AddressErrors.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { IsString } from 'class-validator';
|
||||||
|
import { NotAcceptableError, NotFoundError } from 'routing-controllers';
|
||||||
|
|
||||||
|
export class AddressWrongTypeError extends NotAcceptableError {
|
||||||
|
@IsString()
|
||||||
|
name = "AddressWrongTypeError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "The address must be an existing adress's id. \n You provided a object of another type."
|
||||||
|
}
|
||||||
|
|
||||||
|
export class AddressNotFoundError extends NotFoundError {
|
||||||
|
@IsString()
|
||||||
|
name = "AddressNotFoundError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "The address you provided couldn't be located in the system. \n Please check your request."
|
||||||
|
}
|
123
src/errors/AuthError.ts
Normal file
123
src/errors/AuthError.ts
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
import { IsString } from 'class-validator';
|
||||||
|
import { ForbiddenError, NotAcceptableError, NotFoundError, UnauthorizedError } from 'routing-controllers';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to throw when a jwt is expired
|
||||||
|
*/
|
||||||
|
export class ExpiredJWTError extends UnauthorizedError {
|
||||||
|
@IsString()
|
||||||
|
name = "ExpiredJWTError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "your provided jwt is expired"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to throw when a jwt could not be parsed
|
||||||
|
*/
|
||||||
|
export class IllegalJWTError extends UnauthorizedError {
|
||||||
|
@IsString()
|
||||||
|
name = "IllegalJWTError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "your provided jwt could not be parsed"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to throw when user is nonexistant or refreshtoken is invalid
|
||||||
|
*/
|
||||||
|
export class UserNonexistantOrRefreshtokenInvalidError extends UnauthorizedError {
|
||||||
|
@IsString()
|
||||||
|
name = "UserNonexistantOrRefreshtokenInvalidError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "user is nonexistant or refreshtoken is invalid"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to throw when provided credentials are invalid
|
||||||
|
*/
|
||||||
|
export class InvalidCredentialsError extends UnauthorizedError {
|
||||||
|
@IsString()
|
||||||
|
name = "InvalidCredentialsError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "your provided credentials are invalid"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to throw when a jwt does not have permission for this route/ action
|
||||||
|
*/
|
||||||
|
export class NoPermissionError extends ForbiddenError {
|
||||||
|
@IsString()
|
||||||
|
name = "NoPermissionError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "your provided jwt does not have permission for this route/ action"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to thow when no username and no email is set
|
||||||
|
*/
|
||||||
|
export class UsernameOrEmailNeededError extends NotAcceptableError {
|
||||||
|
@IsString()
|
||||||
|
name = "UsernameOrEmailNeededError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "Auth needs to have email or username set! \n You provided neither."
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to thow when no password is provided
|
||||||
|
*/
|
||||||
|
export class PasswordNeededError extends NotAcceptableError {
|
||||||
|
@IsString()
|
||||||
|
name = "PasswordNeededError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "no password is provided - you need to provide it"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to thow when no user could be found for provided credential
|
||||||
|
*/
|
||||||
|
export class UserNotFoundError extends NotFoundError {
|
||||||
|
@IsString()
|
||||||
|
name = "UserNotFoundError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "no user could be found for provided credential"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to thow when no jwt token was provided
|
||||||
|
*/
|
||||||
|
export class JwtNotProvidedError extends NotAcceptableError {
|
||||||
|
@IsString()
|
||||||
|
name = "JwtNotProvidedError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "no jwt token was provided"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to thow when user was not found or refresh token count was invalid
|
||||||
|
*/
|
||||||
|
export class UserNotFoundOrRefreshTokenCountInvalidError extends NotAcceptableError {
|
||||||
|
@IsString()
|
||||||
|
name = "UserNotFoundOrRefreshTokenCountInvalidError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "user was not found or refresh token count was invalid"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to thow when refresh token count was invalid
|
||||||
|
*/
|
||||||
|
export class RefreshTokenCountInvalidError extends NotAcceptableError {
|
||||||
|
@IsString()
|
||||||
|
name = "RefreshTokenCountInvalidError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "refresh token count was invalid"
|
||||||
|
}
|
18
src/errors/GroupContactErrors.ts
Normal file
18
src/errors/GroupContactErrors.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { IsString } from 'class-validator';
|
||||||
|
import { NotAcceptableError, NotFoundError } from 'routing-controllers';
|
||||||
|
|
||||||
|
export class GroupContactWrongTypeError extends NotAcceptableError {
|
||||||
|
@IsString()
|
||||||
|
name = "GroupContactWrongTypeError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "The groupContact must be an existing groupContact's id. \n You provided a object of another type."
|
||||||
|
}
|
||||||
|
|
||||||
|
export class GroupContactNotFoundError extends NotFoundError {
|
||||||
|
@IsString()
|
||||||
|
name = "GroupContactNotFoundError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "The groupContact you provided couldn't be located in the system. \n Please check your request."
|
||||||
|
}
|
@ -1,18 +0,0 @@
|
|||||||
import { IsString } from 'class-validator';
|
|
||||||
import { NotAcceptableError, NotFoundError } from 'routing-controllers';
|
|
||||||
|
|
||||||
export class ParticipantOnlyOneAddressAllowedError extends NotAcceptableError {
|
|
||||||
@IsString()
|
|
||||||
name = "ParticipantOnlyOneAddressAllowedError"
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
message = "Participant's can only have one address! \n You provided an id and address object.."
|
|
||||||
}
|
|
||||||
|
|
||||||
export class ParticipantAddressNotFoundError extends NotFoundError {
|
|
||||||
@IsString()
|
|
||||||
name = "ParticipantAddressNotFoundError"
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
message = "The address you provided couldn't be located in the system. \n Please check your request."
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
import { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined, NotAcceptableError } from 'routing-controllers';
|
import { IsString } from 'class-validator';
|
||||||
import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator';
|
import { NotAcceptableError, NotFoundError } from 'routing-controllers';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Error to throw when a runner couldn't be found.
|
* Error to throw when a runner couldn't be found.
|
||||||
|
@ -49,3 +49,11 @@ export class RunnerOrganisationHasTeamsError extends NotAcceptableError {
|
|||||||
@IsString()
|
@IsString()
|
||||||
message = "This organisation still has teams associated with it. \n If you want to delete this organisation with all it's runners and teams ass `?force` to your query."
|
message = "This organisation still has teams associated with it. \n If you want to delete this organisation with all it's runners and teams ass `?force` to your query."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class RunnerOrganisationWrongTypeError extends NotAcceptableError {
|
||||||
|
@IsString()
|
||||||
|
name = "RunnerOrganisationWrongTypeError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "The runner organisation must be an existing organisation's id. \n You provided a object of another type."
|
||||||
|
}
|
||||||
|
@ -37,3 +37,15 @@ export class RunnerTeamHasRunnersError extends NotAcceptableError {
|
|||||||
@IsString()
|
@IsString()
|
||||||
message = "This team still has runners associated with it. \n If you want to delete this team with all it's runners and teams ass `?force` to your query."
|
message = "This team still has runners associated with it. \n If you want to delete this team with all it's runners and teams ass `?force` to your query."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error to throw when a team still has runners associated.
|
||||||
|
* Implemented this waysto work with the json-schema conversion for openapi.
|
||||||
|
*/
|
||||||
|
export class RunnerTeamNeedsParentError extends NotAcceptableError {
|
||||||
|
@IsString()
|
||||||
|
name = "RunnerTeamNeedsParentError"
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
message = "You provided no runner organisation as this team's parent group."
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
|
import { validationMetadatasToSchemas } from "class-validator-jsonschema";
|
||||||
import { Application } from "express";
|
import { Application } from "express";
|
||||||
import * as swaggerUiExpress from "swagger-ui-express";
|
|
||||||
import { getMetadataArgsStorage } from "routing-controllers";
|
import { getMetadataArgsStorage } from "routing-controllers";
|
||||||
import { routingControllersToSpec } from "routing-controllers-openapi";
|
import { routingControllersToSpec } from "routing-controllers-openapi";
|
||||||
import { validationMetadatasToSchemas } from "class-validator-jsonschema";
|
import * as swaggerUiExpress from "swagger-ui-express";
|
||||||
|
|
||||||
export default async (app: Application) => {
|
export default async (app: Application) => {
|
||||||
const storage = getMetadataArgsStorage();
|
const storage = getMetadataArgsStorage();
|
||||||
@ -17,6 +17,13 @@ export default async (app: Application) => {
|
|||||||
{
|
{
|
||||||
components: {
|
components: {
|
||||||
schemas,
|
schemas,
|
||||||
|
"securitySchemes": {
|
||||||
|
"AuthToken": {
|
||||||
|
"type": "http",
|
||||||
|
"scheme": "bearer",
|
||||||
|
"bearerFormat": "JWT"
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
info: {
|
info: {
|
||||||
description: "The the backend API for the LfK! runner system.",
|
description: "The the backend API for the LfK! runner system.",
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
|
||||||
// import bodyParser from 'body-parser';
|
|
||||||
// import cors from 'cors';
|
|
||||||
import * as jwt from "jsonwebtoken";
|
|
||||||
|
|
||||||
export default (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
const token = <string>req.headers["auth"];
|
|
||||||
try {
|
|
||||||
const jwtPayload = <any>jwt.verify(token, "secretjwtsecret");
|
|
||||||
// const jwtPayload = <any>jwt.verify(token, process.env.JWT_SECRET);
|
|
||||||
res.locals.jwtPayload = jwtPayload;
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error);
|
|
||||||
return res.status(401).send();
|
|
||||||
}
|
|
||||||
next();
|
|
||||||
};
|
|
57
src/models/creation/CreateAuth.ts
Normal file
57
src/models/creation/CreateAuth.ts
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import * as argon2 from "argon2";
|
||||||
|
import { IsEmail, IsOptional, IsString } from 'class-validator';
|
||||||
|
import * as jsonwebtoken from 'jsonwebtoken';
|
||||||
|
import { getConnectionManager } from 'typeorm';
|
||||||
|
import { InvalidCredentialsError, PasswordNeededError, UserNotFoundError } from '../../errors/AuthError';
|
||||||
|
import { UsernameOrEmailNeededError } from '../../errors/UserErrors';
|
||||||
|
import { User } from '../entities/User';
|
||||||
|
import { Auth } from '../responses/Auth';
|
||||||
|
|
||||||
|
export class CreateAuth {
|
||||||
|
@IsOptional()
|
||||||
|
@IsString()
|
||||||
|
username?: string;
|
||||||
|
@IsString()
|
||||||
|
password: string;
|
||||||
|
@IsOptional()
|
||||||
|
@IsEmail()
|
||||||
|
@IsString()
|
||||||
|
email?: string;
|
||||||
|
|
||||||
|
public async toAuth(): Promise<Auth> {
|
||||||
|
let newAuth: Auth = new Auth();
|
||||||
|
|
||||||
|
if (this.email === undefined && this.username === undefined) {
|
||||||
|
throw new UsernameOrEmailNeededError();
|
||||||
|
}
|
||||||
|
if (!this.password) {
|
||||||
|
throw new PasswordNeededError()
|
||||||
|
}
|
||||||
|
const found_users = await getConnectionManager().get().getRepository(User).find({ where: [{ username: this.username }, { email: this.email }] });
|
||||||
|
if (found_users.length === 0) {
|
||||||
|
throw new UserNotFoundError()
|
||||||
|
} else {
|
||||||
|
const found_user = found_users[0]
|
||||||
|
if (await argon2.verify(found_user.password, this.password + found_user.uuid)) {
|
||||||
|
const timestamp_accesstoken_expiry = Math.floor(Date.now() / 1000) + 5 * 60
|
||||||
|
delete found_user.password;
|
||||||
|
newAuth.access_token = jsonwebtoken.sign({
|
||||||
|
userdetails: found_user,
|
||||||
|
exp: timestamp_accesstoken_expiry
|
||||||
|
}, "securekey")
|
||||||
|
newAuth.access_token_expires_at = timestamp_accesstoken_expiry
|
||||||
|
//
|
||||||
|
const timestamp_refresh_expiry = Math.floor(Date.now() / 1000) + 10 * 36000
|
||||||
|
newAuth.refresh_token = jsonwebtoken.sign({
|
||||||
|
refreshtokencount: found_user.refreshTokenCount,
|
||||||
|
userid: found_user.id,
|
||||||
|
exp: timestamp_refresh_expiry
|
||||||
|
}, "securekey")
|
||||||
|
newAuth.refresh_token_expires_at = timestamp_refresh_expiry
|
||||||
|
} else {
|
||||||
|
throw new InvalidCredentialsError()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newAuth;
|
||||||
|
}
|
||||||
|
}
|
83
src/models/creation/CreateGroupContact.ts
Normal file
83
src/models/creation/CreateGroupContact.ts
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
|
||||||
|
import { getConnectionManager } from 'typeorm';
|
||||||
|
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
|
||||||
|
import { Address } from '../entities/Address';
|
||||||
|
import { GroupContact } from '../entities/GroupContact';
|
||||||
|
|
||||||
|
export class CreateGroupContact {
|
||||||
|
/**
|
||||||
|
* The contact's first name.
|
||||||
|
*/
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsString()
|
||||||
|
firstname: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The contact's middle name.
|
||||||
|
* Optional
|
||||||
|
*/
|
||||||
|
@IsOptional()
|
||||||
|
@IsString()
|
||||||
|
middlename?: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The contact's last name.
|
||||||
|
*/
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsString()
|
||||||
|
lastname: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The contact's address.
|
||||||
|
* Optional
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
@IsOptional()
|
||||||
|
address?: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The contact's phone number.
|
||||||
|
* Optional
|
||||||
|
*/
|
||||||
|
@IsOptional()
|
||||||
|
@IsPhoneNumber("DE")
|
||||||
|
phone?: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The contact's email address.
|
||||||
|
* Optional
|
||||||
|
*/
|
||||||
|
@IsOptional()
|
||||||
|
@IsEmail()
|
||||||
|
email?: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get's this participant's address from this.address.
|
||||||
|
*/
|
||||||
|
public async getAddress(): Promise<Address> {
|
||||||
|
if (this.address === undefined) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!isNaN(this.address)) {
|
||||||
|
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
|
||||||
|
if (!address) { throw new AddressNotFoundError; }
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new AddressWrongTypeError;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Address object based on this.
|
||||||
|
*/
|
||||||
|
public async toGroupContact(): Promise<GroupContact> {
|
||||||
|
let contact: GroupContact = new GroupContact();
|
||||||
|
contact.firstname = this.firstname;
|
||||||
|
contact.middlename = this.middlename;
|
||||||
|
contact.lastname = this.lastname;
|
||||||
|
contact.email = this.email;
|
||||||
|
contact.phone = this.phone;
|
||||||
|
contact.address = await this.getAddress();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,7 @@
|
|||||||
import { IsEmail, IsInt, IsNotEmpty, IsObject, 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 { ParticipantOnlyOneAddressAllowedError } from '../../errors/ParticipantErrors';
|
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
|
||||||
import { Address } from '../entities/Address';
|
import { Address } from '../entities/Address';
|
||||||
import { CreateAddress } from './CreateAddress';
|
|
||||||
|
|
||||||
export abstract class CreateParticipant {
|
export abstract class CreateParticipant {
|
||||||
/**
|
/**
|
||||||
@ -46,38 +45,27 @@ export abstract class CreateParticipant {
|
|||||||
email?: string;
|
email?: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The new participant's address's id.
|
* The new participant's address.
|
||||||
* Optional - please provide either addressId or address.
|
* Must be of type number (address id), createAddress (new address) or address (existing address)
|
||||||
|
* Optional.
|
||||||
*/
|
*/
|
||||||
@IsInt()
|
@IsInt()
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
addressId?: number;
|
address?: number;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The new participant's address.
|
* Get's this participant's address from this.address.
|
||||||
* Optional - please provide either addressId or address.
|
|
||||||
*/
|
|
||||||
@IsObject()
|
|
||||||
@IsOptional()
|
|
||||||
address?: CreateAddress;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a Participant entity from this.
|
|
||||||
*/
|
*/
|
||||||
public async getAddress(): Promise<Address> {
|
public async getAddress(): Promise<Address> {
|
||||||
let address: Address;
|
if (this.address === undefined) {
|
||||||
|
|
||||||
if (this.addressId !== undefined && this.address !== undefined) {
|
|
||||||
throw new ParticipantOnlyOneAddressAllowedError
|
|
||||||
}
|
|
||||||
if (this.addressId === undefined && this.address === undefined) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if (!isNaN(this.address)) {
|
||||||
if (this.addressId) {
|
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
|
||||||
return await getConnectionManager().get().getRepository(Address).findOne({ id: this.addressId });
|
if (!address) { throw new AddressNotFoundError; }
|
||||||
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.address.toAddress();
|
throw new AddressWrongTypeError;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,10 +1,10 @@
|
|||||||
import { IsInt, IsOptional } from 'class-validator';
|
import { IsInt } from 'class-validator';
|
||||||
import { getConnectionManager } from 'typeorm';
|
import { getConnectionManager } from 'typeorm';
|
||||||
import { RunnerGroupNeededError, RunnerGroupNotFoundError, RunnerOnlyOneGroupAllowedError } from '../../errors/RunnerErrors';
|
import { RunnerGroupNotFoundError } from '../../errors/RunnerErrors';
|
||||||
|
import { RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors';
|
||||||
|
import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors';
|
||||||
import { Runner } from '../entities/Runner';
|
import { Runner } from '../entities/Runner';
|
||||||
import { RunnerGroup } from '../entities/RunnerGroup';
|
import { RunnerGroup } from '../entities/RunnerGroup';
|
||||||
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
|
||||||
import { RunnerTeam } from '../entities/RunnerTeam';
|
|
||||||
import { CreateParticipant } from './CreateParticipant';
|
import { CreateParticipant } from './CreateParticipant';
|
||||||
|
|
||||||
export class CreateRunner extends CreateParticipant {
|
export class CreateRunner extends CreateParticipant {
|
||||||
@ -14,16 +14,7 @@ export class CreateRunner extends CreateParticipant {
|
|||||||
* Either provide this or his organisation's id.
|
* Either provide this or his organisation's id.
|
||||||
*/
|
*/
|
||||||
@IsInt()
|
@IsInt()
|
||||||
@IsOptional()
|
group: number;
|
||||||
teamId?: number;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The new runner's organisation's id.
|
|
||||||
* Either provide this or his teams's id.
|
|
||||||
*/
|
|
||||||
@IsInt()
|
|
||||||
@IsOptional()
|
|
||||||
orgId?: number;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a Runner entity from this.
|
* Creates a Runner entity from this.
|
||||||
@ -46,23 +37,15 @@ export class CreateRunner extends CreateParticipant {
|
|||||||
* Manages all the different ways a group can be provided.
|
* Manages all the different ways a group can be provided.
|
||||||
*/
|
*/
|
||||||
public async getGroup(): Promise<RunnerGroup> {
|
public async getGroup(): Promise<RunnerGroup> {
|
||||||
let group: RunnerGroup;
|
if (this.group === undefined) {
|
||||||
if (this.teamId !== undefined && this.orgId !== undefined) {
|
throw new RunnerTeamNeedsParentError();
|
||||||
throw new RunnerOnlyOneGroupAllowedError();
|
|
||||||
}
|
|
||||||
if (this.teamId === undefined && this.orgId === undefined) {
|
|
||||||
throw new RunnerGroupNeededError();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.teamId) {
|
|
||||||
group = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: this.teamId });
|
|
||||||
}
|
|
||||||
if (this.orgId) {
|
|
||||||
group = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.orgId });
|
|
||||||
}
|
|
||||||
if (!group) {
|
|
||||||
throw new RunnerGroupNotFoundError();
|
|
||||||
}
|
}
|
||||||
|
if (!isNaN(this.group)) {
|
||||||
|
let group = await getConnectionManager().get().getRepository(RunnerGroup).findOne({ id: this.group });
|
||||||
|
if (!group) { throw new RunnerGroupNotFoundError; }
|
||||||
return group;
|
return group;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new RunnerOrganisationWrongTypeError;
|
||||||
|
}
|
||||||
}
|
}
|
37
src/models/creation/CreateRunnerGroup.ts
Normal file
37
src/models/creation/CreateRunnerGroup.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import { IsInt, IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||||
|
import { getConnectionManager } from 'typeorm';
|
||||||
|
import { GroupContactNotFoundError, GroupContactWrongTypeError } from '../../errors/GroupContactErrors';
|
||||||
|
import { GroupContact } from '../entities/GroupContact';
|
||||||
|
|
||||||
|
export abstract class CreateRunnerGroup {
|
||||||
|
/**
|
||||||
|
* The group's name.
|
||||||
|
*/
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsString()
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The group's contact.
|
||||||
|
* Optional
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
@IsOptional()
|
||||||
|
contact?: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deals with the contact for groups this.
|
||||||
|
*/
|
||||||
|
public async getContact(): Promise<GroupContact> {
|
||||||
|
if (this.contact === undefined) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!isNaN(this.contact)) {
|
||||||
|
let address = await getConnectionManager().get().getRepository(GroupContact).findOne({ id: this.contact });
|
||||||
|
if (!address) { throw new GroupContactNotFoundError; }
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new GroupContactWrongTypeError;
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +1,35 @@
|
|||||||
import { IsNotEmpty, IsString } from 'class-validator';
|
import { IsInt, IsOptional } from 'class-validator';
|
||||||
|
import { getConnectionManager } from 'typeorm';
|
||||||
|
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
|
||||||
|
import { Address } from '../entities/Address';
|
||||||
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
||||||
|
import { CreateRunnerGroup } from './CreateRunnerGroup';
|
||||||
|
|
||||||
export class CreateRunnerOrganisation {
|
export class CreateRunnerOrganisation extends CreateRunnerGroup {
|
||||||
/**
|
/**
|
||||||
* The Organisation's name.
|
* The new organisation's address.
|
||||||
|
* Must be of type number (address id), createAddress (new address) or address (existing address)
|
||||||
|
* Optional.
|
||||||
*/
|
*/
|
||||||
@IsString()
|
@IsInt()
|
||||||
@IsNotEmpty()
|
@IsOptional()
|
||||||
name: string;
|
address?: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Participant entity from this.
|
||||||
|
*/
|
||||||
|
public async getAddress(): Promise<Address> {
|
||||||
|
if (this.address === undefined) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!isNaN(this.address)) {
|
||||||
|
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
|
||||||
|
if (!address) { throw new AddressNotFoundError; }
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new AddressWrongTypeError;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a RunnerOrganisation entity from this.
|
* Creates a RunnerOrganisation entity from this.
|
||||||
@ -16,6 +38,8 @@ export class CreateRunnerOrganisation {
|
|||||||
let newRunnerOrganisation: RunnerOrganisation = new RunnerOrganisation();
|
let newRunnerOrganisation: RunnerOrganisation = new RunnerOrganisation();
|
||||||
|
|
||||||
newRunnerOrganisation.name = this.name;
|
newRunnerOrganisation.name = this.name;
|
||||||
|
newRunnerOrganisation.contact = await this.getContact();
|
||||||
|
newRunnerOrganisation.address = await this.getAddress();
|
||||||
|
|
||||||
return newRunnerOrganisation;
|
return newRunnerOrganisation;
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,32 @@
|
|||||||
import { IsInt, IsNotEmpty, IsString } from 'class-validator';
|
import { IsInt, IsNotEmpty } from 'class-validator';
|
||||||
import { getConnectionManager } from 'typeorm';
|
import { getConnectionManager } from 'typeorm';
|
||||||
import { RunnerOrganisationNotFoundError } from '../../errors/RunnerOrganisationErrors';
|
import { RunnerOrganisationNotFoundError, RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors';
|
||||||
|
import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors';
|
||||||
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
||||||
import { RunnerTeam } from '../entities/RunnerTeam';
|
import { RunnerTeam } from '../entities/RunnerTeam';
|
||||||
|
import { CreateRunnerGroup } from './CreateRunnerGroup';
|
||||||
|
|
||||||
export class CreateRunnerTeam {
|
export class CreateRunnerTeam extends CreateRunnerGroup {
|
||||||
/**
|
|
||||||
* The teams's name.
|
|
||||||
*/
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
name: string;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The team's parent group (organisation).
|
* The team's parent group (organisation).
|
||||||
*/
|
*/
|
||||||
@IsInt()
|
@IsInt()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
parentId: number
|
parentGroup: number;
|
||||||
|
|
||||||
|
public async getParent(): Promise<RunnerOrganisation> {
|
||||||
|
if (this.parentGroup === undefined) {
|
||||||
|
throw new RunnerTeamNeedsParentError();
|
||||||
|
}
|
||||||
|
if (!isNaN(this.parentGroup)) {
|
||||||
|
let parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentGroup });
|
||||||
|
if (!parentGroup) { throw new RunnerOrganisationNotFoundError();; }
|
||||||
|
return parentGroup;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new RunnerOrganisationWrongTypeError;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a RunnerTeam entity from this.
|
* Creates a RunnerTeam entity from this.
|
||||||
@ -26,10 +35,8 @@ export class CreateRunnerTeam {
|
|||||||
let newRunnerTeam: RunnerTeam = new RunnerTeam();
|
let newRunnerTeam: RunnerTeam = new RunnerTeam();
|
||||||
|
|
||||||
newRunnerTeam.name = this.name;
|
newRunnerTeam.name = this.name;
|
||||||
newRunnerTeam.parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentId });
|
newRunnerTeam.parentGroup = await this.getParent();
|
||||||
if (!newRunnerTeam.parentGroup) {
|
newRunnerTeam.contact = await this.getContact()
|
||||||
throw new RunnerOrganisationNotFoundError();
|
|
||||||
}
|
|
||||||
|
|
||||||
return newRunnerTeam;
|
return newRunnerTeam;
|
||||||
}
|
}
|
||||||
|
35
src/models/creation/HandleLogout.ts
Normal file
35
src/models/creation/HandleLogout.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { IsString } from 'class-validator';
|
||||||
|
import * as jsonwebtoken from 'jsonwebtoken';
|
||||||
|
import { getConnectionManager } from 'typeorm';
|
||||||
|
import { IllegalJWTError, JwtNotProvidedError, RefreshTokenCountInvalidError, UserNotFoundError } from '../../errors/AuthError';
|
||||||
|
import { User } from '../entities/User';
|
||||||
|
import { Logout } from '../responses/Logout';
|
||||||
|
|
||||||
|
export class HandleLogout {
|
||||||
|
@IsString()
|
||||||
|
token: string;
|
||||||
|
|
||||||
|
public async logout(): Promise<Logout> {
|
||||||
|
let logout: Logout = new Logout();
|
||||||
|
if (!this.token || this.token === undefined) {
|
||||||
|
throw new JwtNotProvidedError()
|
||||||
|
}
|
||||||
|
let decoded;
|
||||||
|
try {
|
||||||
|
decoded = jsonwebtoken.verify(this.token, 'securekey')
|
||||||
|
} catch (error) {
|
||||||
|
throw new IllegalJWTError()
|
||||||
|
}
|
||||||
|
logout.timestamp = Math.floor(Date.now() / 1000)
|
||||||
|
let found_user: User = await getConnectionManager().get().getRepository(User).findOne({ id: decoded["userid"] });
|
||||||
|
if (!found_user) {
|
||||||
|
throw new UserNotFoundError()
|
||||||
|
}
|
||||||
|
if (found_user.refreshTokenCount !== decoded["refreshtokencount"]) {
|
||||||
|
throw new RefreshTokenCountInvalidError()
|
||||||
|
}
|
||||||
|
found_user.refreshTokenCount++;
|
||||||
|
await getConnectionManager().get().getRepository(User).update({ id: found_user.id }, found_user)
|
||||||
|
return logout;
|
||||||
|
}
|
||||||
|
}
|
49
src/models/creation/RefreshAuth.ts
Normal file
49
src/models/creation/RefreshAuth.ts
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import { IsString } from 'class-validator';
|
||||||
|
import * as jsonwebtoken from 'jsonwebtoken';
|
||||||
|
import { getConnectionManager } from 'typeorm';
|
||||||
|
import { IllegalJWTError, JwtNotProvidedError, RefreshTokenCountInvalidError, UserNotFoundError } from '../../errors/AuthError';
|
||||||
|
import { User } from '../entities/User';
|
||||||
|
import { Auth } from '../responses/Auth';
|
||||||
|
|
||||||
|
export class RefreshAuth {
|
||||||
|
@IsString()
|
||||||
|
token: string;
|
||||||
|
|
||||||
|
public async toAuth(): Promise<Auth> {
|
||||||
|
let newAuth: Auth = new Auth();
|
||||||
|
if (!this.token || this.token === undefined) {
|
||||||
|
throw new JwtNotProvidedError()
|
||||||
|
}
|
||||||
|
let decoded
|
||||||
|
try {
|
||||||
|
decoded = jsonwebtoken.verify(this.token, 'securekey')
|
||||||
|
} catch (error) {
|
||||||
|
throw new IllegalJWTError()
|
||||||
|
}
|
||||||
|
const found_user = await getConnectionManager().get().getRepository(User).findOne({ id: decoded["userid"] });
|
||||||
|
if (!found_user) {
|
||||||
|
throw new UserNotFoundError()
|
||||||
|
}
|
||||||
|
if (found_user.refreshTokenCount !== decoded["refreshtokencount"]) {
|
||||||
|
throw new RefreshTokenCountInvalidError()
|
||||||
|
}
|
||||||
|
delete found_user.password;
|
||||||
|
const timestamp_accesstoken_expiry = Math.floor(Date.now() / 1000) + 5 * 60
|
||||||
|
delete found_user.password;
|
||||||
|
newAuth.access_token = jsonwebtoken.sign({
|
||||||
|
userdetails: found_user,
|
||||||
|
exp: timestamp_accesstoken_expiry
|
||||||
|
}, "securekey")
|
||||||
|
newAuth.access_token_expires_at = timestamp_accesstoken_expiry
|
||||||
|
//
|
||||||
|
const timestamp_refresh_expiry = Math.floor(Date.now() / 1000) + 10 * 36000
|
||||||
|
newAuth.refresh_token = jsonwebtoken.sign({
|
||||||
|
refreshtokencount: found_user.refreshTokenCount,
|
||||||
|
userid: found_user.id,
|
||||||
|
exp: timestamp_refresh_expiry
|
||||||
|
}, "securekey")
|
||||||
|
newAuth.refresh_token_expires_at = timestamp_refresh_expiry
|
||||||
|
|
||||||
|
return newAuth;
|
||||||
|
}
|
||||||
|
}
|
@ -28,18 +28,10 @@ export class DistanceDonation extends Donation {
|
|||||||
* The donation's amount in cents (or whatever your currency's smallest unit is.).
|
* The donation's amount in cents (or whatever your currency's smallest unit is.).
|
||||||
* The exact implementation may differ for each type of donation.
|
* The exact implementation may differ for each type of donation.
|
||||||
*/
|
*/
|
||||||
@IsInt()
|
public get amount(): number {
|
||||||
public get amount() {
|
|
||||||
return this.getAmount();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The function that calculates the amount based on the runner object's distance.
|
|
||||||
*/
|
|
||||||
public async getAmount(): Promise<number> {
|
|
||||||
let calculatedAmount = -1;
|
let calculatedAmount = -1;
|
||||||
try {
|
try {
|
||||||
calculatedAmount = this.amountPerDistance * await this.runner.distance();
|
calculatedAmount = this.amountPerDistance * this.runner.distance;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { IsInt, IsNotEmpty } from "class-validator";
|
import { IsInt, IsNotEmpty } from "class-validator";
|
||||||
import { ChildEntity, getConnectionManager, ManyToOne, OneToMany } from "typeorm";
|
import { ChildEntity, ManyToOne, OneToMany } from "typeorm";
|
||||||
import { DistanceDonation } from "./DistanceDonation";
|
import { DistanceDonation } from "./DistanceDonation";
|
||||||
import { Participant } from "./Participant";
|
import { Participant } from "./Participant";
|
||||||
import { RunnerCard } from "./RunnerCard";
|
import { RunnerCard } from "./RunnerCard";
|
||||||
@ -36,25 +36,18 @@ export class Runner extends Participant {
|
|||||||
@OneToMany(() => Scan, scan => scan.runner, { nullable: true })
|
@OneToMany(() => Scan, scan => scan.runner, { nullable: true })
|
||||||
scans: Scan[];
|
scans: Scan[];
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns all scans associated with this runner.
|
|
||||||
*/
|
|
||||||
public async getScans(): Promise<Scan[]> {
|
|
||||||
return await getConnectionManager().get().getRepository(Scan).find({ runner: this });
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all valid scans associated with this runner.
|
* Returns all valid scans associated with this runner.
|
||||||
*/
|
*/
|
||||||
public async getValidScans(): Promise<Scan[]> {
|
public get validScans(): Scan[] {
|
||||||
return (await this.getScans()).filter(scan => { scan.valid === true });
|
return this.scans.filter(scan => { scan.valid === true });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the total distance ran by this runner.
|
* Returns the total distance ran by this runner.
|
||||||
*/
|
*/
|
||||||
@IsInt()
|
@IsInt()
|
||||||
public async distance(): Promise<number> {
|
public get distance(): number {
|
||||||
return await (await this.getValidScans()).reduce((sum, current) => sum + current.distance, 0);
|
return this.validScans.reduce((sum, current) => sum + current.distance, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -42,6 +42,4 @@ export abstract class RunnerGroup {
|
|||||||
*/
|
*/
|
||||||
@OneToMany(() => Runner, runner => runner.group, { nullable: true })
|
@OneToMany(() => Runner, runner => runner.group, { nullable: true })
|
||||||
runners: Runner[];
|
runners: Runner[];
|
||||||
|
|
||||||
public abstract getRunners();
|
|
||||||
}
|
}
|
@ -1,7 +1,6 @@
|
|||||||
import { IsOptional } from "class-validator";
|
import { IsOptional } from "class-validator";
|
||||||
import { ChildEntity, getConnectionManager, ManyToOne, OneToMany } from "typeorm";
|
import { ChildEntity, ManyToOne, OneToMany } from "typeorm";
|
||||||
import { Address } from "./Address";
|
import { Address } from "./Address";
|
||||||
import { Runner } from './Runner';
|
|
||||||
import { RunnerGroup } from "./RunnerGroup";
|
import { RunnerGroup } from "./RunnerGroup";
|
||||||
import { RunnerTeam } from "./RunnerTeam";
|
import { RunnerTeam } from "./RunnerTeam";
|
||||||
|
|
||||||
@ -24,27 +23,4 @@ export class RunnerOrganisation extends RunnerGroup {
|
|||||||
*/
|
*/
|
||||||
@OneToMany(() => RunnerTeam, team => team.parentGroup, { nullable: true })
|
@OneToMany(() => RunnerTeam, team => team.parentGroup, { nullable: true })
|
||||||
teams: RunnerTeam[];
|
teams: RunnerTeam[];
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns all runners associated with this organisation or it's teams.
|
|
||||||
*/
|
|
||||||
public async getRunners() {
|
|
||||||
let runners: Runner[] = new Array<Runner>();
|
|
||||||
const teams = await this.getTeams();
|
|
||||||
|
|
||||||
await teams.forEach(async team => {
|
|
||||||
runners.push(... await team.getRunners());
|
|
||||||
});
|
|
||||||
await runners.push(... await getConnectionManager().get().getRepository(Runner).find({ group: this }));
|
|
||||||
|
|
||||||
return runners;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns all teams associated with this organisation.
|
|
||||||
*/
|
|
||||||
public async getTeams() {
|
|
||||||
return await getConnectionManager().get().getRepository(RunnerTeam).find({ parentGroup: this });
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,6 +1,5 @@
|
|||||||
import { IsNotEmpty } from "class-validator";
|
import { IsNotEmpty } from "class-validator";
|
||||||
import { ChildEntity, getConnectionManager, ManyToOne } from "typeorm";
|
import { ChildEntity, ManyToOne } from "typeorm";
|
||||||
import { Runner } from './Runner';
|
|
||||||
import { RunnerGroup } from "./RunnerGroup";
|
import { RunnerGroup } from "./RunnerGroup";
|
||||||
import { RunnerOrganisation } from "./RunnerOrganisation";
|
import { RunnerOrganisation } from "./RunnerOrganisation";
|
||||||
|
|
||||||
@ -15,13 +14,6 @@ export class RunnerTeam extends RunnerGroup {
|
|||||||
* Optional
|
* Optional
|
||||||
*/
|
*/
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@ManyToOne(() => RunnerOrganisation, org => org.teams, { nullable: false })
|
@ManyToOne(() => RunnerOrganisation, org => org.teams, { nullable: true })
|
||||||
parentGroup?: RunnerOrganisation;
|
parentGroup?: RunnerOrganisation;
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns all runners associated with this team.
|
|
||||||
*/
|
|
||||||
public async getRunners() {
|
|
||||||
return await getConnectionManager().get().getRepository(Runner).find({ group: this });
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -19,14 +19,14 @@ export class User {
|
|||||||
/**
|
/**
|
||||||
* uuid
|
* uuid
|
||||||
*/
|
*/
|
||||||
@Column()
|
@Column({ unique: true })
|
||||||
@IsUUID(4)
|
@IsUUID(4)
|
||||||
uuid: string;
|
uuid: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* user email
|
* user email
|
||||||
*/
|
*/
|
||||||
@Column({ nullable: true })
|
@Column({ nullable: true, unique: true })
|
||||||
@IsEmail()
|
@IsEmail()
|
||||||
email?: string;
|
email?: string;
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ export class User {
|
|||||||
/**
|
/**
|
||||||
* username
|
* username
|
||||||
*/
|
*/
|
||||||
@Column({ nullable: true })
|
@Column({ nullable: true, unique: true })
|
||||||
@IsString()
|
@IsString()
|
||||||
username?: string;
|
username?: string;
|
||||||
|
|
||||||
@ -109,7 +109,7 @@ export class User {
|
|||||||
/**
|
/**
|
||||||
* profilepic
|
* profilepic
|
||||||
*/
|
*/
|
||||||
@Column({ nullable: true })
|
@Column({ nullable: true, unique: true })
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
profilePic?: string;
|
profilePic?: string;
|
||||||
|
27
src/models/responses/Auth.ts
Normal file
27
src/models/responses/Auth.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import { IsInt, IsString } from 'class-validator';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines a auth object
|
||||||
|
*/
|
||||||
|
export class Auth {
|
||||||
|
/**
|
||||||
|
* access_token - JWT shortterm access token
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
access_token: string;
|
||||||
|
/**
|
||||||
|
* refresh_token - longterm refresh token (used for requesting new access tokens)
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
refresh_token: string;
|
||||||
|
/**
|
||||||
|
* access_token_expires_at - unix timestamp of access token expiry
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
access_token_expires_at: number;
|
||||||
|
/**
|
||||||
|
* refresh_token_expires_at - unix timestamp of access token expiry
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
refresh_token_expires_at: number;
|
||||||
|
}
|
12
src/models/responses/Logout.ts
Normal file
12
src/models/responses/Logout.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { IsString } from 'class-validator';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines a Logout object
|
||||||
|
*/
|
||||||
|
export class Logout {
|
||||||
|
/**
|
||||||
|
* timestamp of logout
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
timestamp: number;
|
||||||
|
}
|
@ -1,10 +0,0 @@
|
|||||||
import { Router } from "express";
|
|
||||||
import jwtauth from "../../middlewares/jwtauth";
|
|
||||||
|
|
||||||
const router = Router();
|
|
||||||
|
|
||||||
router.use("*", jwtauth, async (req, res, next) => {
|
|
||||||
return res.send("ok");
|
|
||||||
});
|
|
||||||
|
|
||||||
export default router;
|
|
Loading…
x
Reference in New Issue
Block a user