Switched runner orgs to the cleaner syntax via a update entity

ref #39
This commit is contained in:
2020-12-20 18:07:33 +01:00
parent 18ede29ea5
commit 532b5a56a5
2 changed files with 57 additions and 6 deletions

View File

@@ -0,0 +1,52 @@
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 { CreateRunnerGroup } from './CreateRunnerGroup';
export class UpdateRunnerOrganisation extends CreateRunnerGroup {
/**
* The updated orgs's id.
*/
@IsInt()
id: number;
/**
* The updated organisation's address.
* Must be of type number (address id).
* Optional.
*/
@IsInt()
@IsOptional()
address?: number;
/**
* Get's this org'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 RunnerTeam entity from this.
*/
public async updateRunnerOrganisation(organisation: RunnerOrganisation): Promise<RunnerOrganisation> {
organisation.name = this.name;
organisation.contact = await this.getContact();
organisation.address = await this.getAddress();
return organisation;
}
}