52
src/models/actions/UpdateRunnerOrganisation.ts
Normal file
52
src/models/actions/UpdateRunnerOrganisation.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user