Merge branch 'dev' into feature/17-automated_tests
# Conflicts: # src/controllers/RunnerController.ts # src/models/actions/CreateParticipant.ts
This commit is contained in:
		@@ -1,10 +1,10 @@
 | 
			
		||||
import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers';
 | 
			
		||||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
 | 
			
		||||
import { getConnectionManager, Repository } from 'typeorm';
 | 
			
		||||
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
 | 
			
		||||
import { RunnerGroupNeededError, RunnerIdsNotMatchingError, RunnerNotFoundError } from '../errors/RunnerErrors';
 | 
			
		||||
import { RunnerGroupNotFoundError } from '../errors/RunnerGroupErrors';
 | 
			
		||||
import { CreateRunner } from '../models/actions/CreateRunner';
 | 
			
		||||
import { UpdateRunner } from '../models/actions/UpdateRunner';
 | 
			
		||||
import { Runner } from '../models/entities/Runner';
 | 
			
		||||
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
 | 
			
		||||
import { ResponseRunner } from '../models/responses/ResponseRunner';
 | 
			
		||||
@@ -66,8 +66,8 @@ export class RunnerController {
 | 
			
		||||
	@ResponseSchema(RunnerNotFoundError, { statusCode: 404 })
 | 
			
		||||
	@ResponseSchema(RunnerIdsNotMatchingError, { statusCode: 406 })
 | 
			
		||||
	@OpenAPI({ description: "Update a runner object (id can't be changed)." })
 | 
			
		||||
	async put(@Param('id') id: number, @EntityFromBody() runner: Runner) {
 | 
			
		||||
		let oldRunner = await this.runnerRepository.findOne({ id: id }, { relations: ['scans', 'group'] });
 | 
			
		||||
	async put(@Param('id') id: number, @Body({ validate: true }) runner: UpdateRunner) {
 | 
			
		||||
		let oldRunner = await this.runnerRepository.findOne({ id: id }, { relations: ['group'] });
 | 
			
		||||
 | 
			
		||||
		if (!oldRunner) {
 | 
			
		||||
			throw new RunnerNotFoundError();
 | 
			
		||||
@@ -77,8 +77,8 @@ export class RunnerController {
 | 
			
		||||
			throw new RunnerIdsNotMatchingError();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		await this.runnerRepository.update(oldRunner, runner);
 | 
			
		||||
		return new ResponseRunner(runner);
 | 
			
		||||
		await this.runnerRepository.update(oldRunner, await runner.toRunner());
 | 
			
		||||
		return new ResponseRunner(await this.runnerRepository.findOne({ id: id }, { relations: ['scans', 'group'] }));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Delete('/:id')
 | 
			
		||||
 
 | 
			
		||||
@@ -47,7 +47,7 @@ export abstract class CreateParticipant {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 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).
 | 
			
		||||
     * Optional.
 | 
			
		||||
     */
 | 
			
		||||
    @IsInt()
 | 
			
		||||
 
 | 
			
		||||
@@ -10,8 +10,7 @@ import { CreateParticipant } from './CreateParticipant';
 | 
			
		||||
export class CreateRunner extends CreateParticipant {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The new runner's team's id.
 | 
			
		||||
     * Either provide this or his organisation's id.
 | 
			
		||||
     * The new runner's group's id.
 | 
			
		||||
     */
 | 
			
		||||
    @IsInt()
 | 
			
		||||
    group: number;
 | 
			
		||||
 
 | 
			
		||||
@@ -20,16 +20,16 @@ export abstract class CreateRunnerGroup {
 | 
			
		||||
    contact?: number;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Deals with the contact for groups this.
 | 
			
		||||
     * Get's this group's contact from this.address.
 | 
			
		||||
     */
 | 
			
		||||
    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;
 | 
			
		||||
            let contact = await getConnectionManager().get().getRepository(GroupContact).findOne({ id: this.contact });
 | 
			
		||||
            if (!contact) { throw new GroupContactNotFoundError; }
 | 
			
		||||
            return contact;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        throw new GroupContactWrongTypeError;
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,7 @@ import { CreateRunnerGroup } from './CreateRunnerGroup';
 | 
			
		||||
export class CreateRunnerOrganisation extends CreateRunnerGroup {
 | 
			
		||||
    /**
 | 
			
		||||
     * The new organisation's address.
 | 
			
		||||
     * Must be of type number (address id), createAddress (new address) or address (existing address)
 | 
			
		||||
     * Must be of type number (address id).
 | 
			
		||||
     * Optional.
 | 
			
		||||
     */
 | 
			
		||||
    @IsInt()
 | 
			
		||||
@@ -16,7 +16,7 @@ export class CreateRunnerOrganisation extends CreateRunnerGroup {
 | 
			
		||||
    address?: number;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates a Participant entity from this.
 | 
			
		||||
     * Get's this org's address from this.address.
 | 
			
		||||
     */
 | 
			
		||||
    public async getAddress(): Promise<Address> {
 | 
			
		||||
        if (this.address === undefined) {
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,7 @@ export class CreateTrack {
 | 
			
		||||
    name: string;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The track's distance in meters (must be greater 0).
 | 
			
		||||
     * The track's distance in meters (must be greater than 0).
 | 
			
		||||
     */
 | 
			
		||||
    @IsInt()
 | 
			
		||||
    @IsPositive()
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										57
									
								
								src/models/actions/UpdateRunner.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								src/models/actions/UpdateRunner.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,57 @@
 | 
			
		||||
import { IsInt, IsObject } from 'class-validator';
 | 
			
		||||
import { getConnectionManager } from 'typeorm';
 | 
			
		||||
import { RunnerGroupNotFoundError } from '../../errors/RunnerGroupErrors';
 | 
			
		||||
import { RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors';
 | 
			
		||||
import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors';
 | 
			
		||||
import { Runner } from '../entities/Runner';
 | 
			
		||||
import { RunnerGroup } from '../entities/RunnerGroup';
 | 
			
		||||
import { CreateParticipant } from './CreateParticipant';
 | 
			
		||||
 | 
			
		||||
export class UpdateRunner extends CreateParticipant {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The updated runner's id.
 | 
			
		||||
     */
 | 
			
		||||
    @IsInt()
 | 
			
		||||
    id: number;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The updated runner's new team/org.
 | 
			
		||||
     */
 | 
			
		||||
    @IsObject()
 | 
			
		||||
    group: RunnerGroup;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Creates a Runner entity from this.
 | 
			
		||||
     */
 | 
			
		||||
    public async toRunner(): Promise<Runner> {
 | 
			
		||||
        let newRunner: Runner = new Runner();
 | 
			
		||||
 | 
			
		||||
        newRunner.id = this.id;
 | 
			
		||||
        newRunner.firstname = this.firstname;
 | 
			
		||||
        newRunner.middlename = this.middlename;
 | 
			
		||||
        newRunner.lastname = this.lastname;
 | 
			
		||||
        newRunner.phone = this.phone;
 | 
			
		||||
        newRunner.email = this.email;
 | 
			
		||||
        newRunner.group = await this.getGroup();
 | 
			
		||||
        newRunner.address = await this.getAddress();
 | 
			
		||||
 | 
			
		||||
        return newRunner;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Manages all the different ways a group can be provided.
 | 
			
		||||
     */
 | 
			
		||||
    public async getGroup(): Promise<RunnerGroup> {
 | 
			
		||||
        if (this.group === undefined) {
 | 
			
		||||
            throw new RunnerTeamNeedsParentError();
 | 
			
		||||
        }
 | 
			
		||||
        if (!isNaN(this.group.id)) {
 | 
			
		||||
            let group = await getConnectionManager().get().getRepository(RunnerGroup).findOne({ id: this.group.id });
 | 
			
		||||
            if (!group) { throw new RunnerGroupNotFoundError; }
 | 
			
		||||
            return group;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        throw new RunnerOrganisationWrongTypeError;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user