Merge pull request 'Switched to accepting ids (numbers/number arrays) feature/90-accept_objects' (#101) from feature/90-accept_objects into dev
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is passing

Reviewed-on: #101
This commit is contained in:
Nicolai Ort 2021-01-15 17:57:45 +00:00
commit 29c8e00477
27 changed files with 110 additions and 161 deletions

View File

@ -90,7 +90,7 @@ export class PermissionController {
if (oldPermission.id != permission.id) { if (oldPermission.id != permission.id) {
throw new PermissionIdsNotMatchingError(); throw new PermissionIdsNotMatchingError();
} }
let existingPermission = await this.permissionRepository.findOne({ target: permission.target, action: permission.action, principal: permission.principal }, { relations: ['principal'] }); let existingPermission = await this.permissionRepository.findOne({ target: permission.target, action: permission.action, principal: await permission.getPrincipal() }, { relations: ['principal'] });
if (existingPermission) { if (existingPermission) {
await this.remove(permission.id, true); await this.remove(permission.id, true);
return new ResponsePermission(existingPermission); return new ResponsePermission(existingPermission);

View File

@ -32,7 +32,6 @@ export class CreateAddress {
/** /**
* The new address's postal code. * The new address's postal code.
* This will get checked against the postal code syntax for the configured country. * This will get checked against the postal code syntax for the configured country.
* TODO: Implement the config option.
*/ */
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()

View File

@ -11,7 +11,7 @@ import { CreateDonation } from './CreateDonation';
export class CreateDistanceDonation extends CreateDonation { export class CreateDistanceDonation extends CreateDonation {
/** /**
* The donation's associated runner. * The donation's associated runner's id.
* This is important to link the runner's distance ran to the donation. * This is important to link the runner's distance ran to the donation.
*/ */
@IsInt() @IsInt()

View File

@ -9,7 +9,7 @@ import { Donor } from '../../entities/Donor';
*/ */
export abstract class CreateDonation { export abstract class CreateDonation {
/** /**
* The donation's associated donor. * The donation's associated donor's id.
* This is important to link donations to donors. * This is important to link donations to donors.
*/ */
@IsInt() @IsInt()

View File

@ -1,7 +1,7 @@
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 { config } from '../../../config';
import { AddressNotFoundError, AddressWrongTypeError } from '../../../errors/AddressErrors'; import { AddressNotFoundError } from '../../../errors/AddressErrors';
import { Address } from '../../entities/Address'; import { Address } from '../../entities/Address';
import { GroupContact } from '../../entities/GroupContact'; import { GroupContact } from '../../entities/GroupContact';
@ -31,8 +31,7 @@ export class CreateGroupContact {
lastname: string; lastname: string;
/** /**
* The new contact's address. * The new contact's address's id.
* Must be the address's id.
*/ */
@IsInt() @IsInt()
@IsOptional() @IsOptional()
@ -57,16 +56,10 @@ export class CreateGroupContact {
* Gets the new contact's address by it's id. * Gets the new contact's address by it's id.
*/ */
public async getAddress(): Promise<Address> { public async getAddress(): Promise<Address> {
if (this.address === undefined || this.address === null) { if (!this.address) { return null; }
return null; let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
} if (!address) { throw new AddressNotFoundError; }
if (!isNaN(this.address)) { return address;
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
if (!address) { throw new AddressNotFoundError; }
return address;
}
throw new AddressWrongTypeError;
} }
/** /**

View File

@ -1,7 +1,7 @@
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 { config } from '../../../config';
import { AddressNotFoundError, AddressWrongTypeError } from '../../../errors/AddressErrors'; import { AddressNotFoundError } from '../../../errors/AddressErrors';
import { Address } from '../../entities/Address'; import { Address } from '../../entities/Address';
/** /**
@ -47,26 +47,19 @@ export abstract class CreateParticipant {
email?: string; email?: string;
/** /**
* The new participant's address. * The new participant's address's id.
* Must be of type number (address id).
*/ */
@IsInt() @IsInt()
@IsOptional() @IsOptional()
address?: number; address?: number;
/** /**
* Gets the new participant's address by it's address. * Gets the new participant's address by it's id.
*/ */
public async getAddress(): Promise<Address> { public async getAddress(): Promise<Address> {
if (this.address === undefined || this.address === null) { if (!this.address) { return null; }
return null; let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
} if (!address) { throw new AddressNotFoundError; }
if (!isNaN(this.address)) { return address;
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
if (!address) { throw new AddressNotFoundError; }
return address;
}
throw new AddressWrongTypeError;
} }
} }

View File

@ -9,7 +9,7 @@ import { RunnerCard } from '../../entities/RunnerCard';
*/ */
export class CreateRunnerCard { export class CreateRunnerCard {
/** /**
* The card's associated runner. * The card's associated runner's id.
*/ */
@IsInt() @IsInt()
@IsOptional() @IsOptional()

View File

@ -1,6 +1,6 @@
import { IsInt, IsNotEmpty, IsOptional, IsString } from 'class-validator'; import { IsInt, IsNotEmpty, IsOptional, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import { GroupContactNotFoundError, GroupContactWrongTypeError } from '../../../errors/GroupContactErrors'; import { GroupContactNotFoundError } from '../../../errors/GroupContactErrors';
import { GroupContact } from '../../entities/GroupContact'; import { GroupContact } from '../../entities/GroupContact';
/** /**
@ -15,7 +15,7 @@ export abstract class CreateRunnerGroup {
name: string; name: string;
/** /**
* The new group's contact. * The new group's contact's id.
* Optional * Optional
*/ */
@IsInt() @IsInt()
@ -26,15 +26,10 @@ export abstract class CreateRunnerGroup {
* Gets the new group's contact by it's id. * Gets the new group's contact by it's id.
*/ */
public async getContact(): Promise<GroupContact> { public async getContact(): Promise<GroupContact> {
if (this.contact === undefined || this.contact === null) { if (!this.contact) { return null; }
return null; let contact = await getConnectionManager().get().getRepository(GroupContact).findOne({ id: this.contact });
} if (!contact) { throw new GroupContactNotFoundError; }
if (!isNaN(this.contact)) { return contact;
let contact = await getConnectionManager().get().getRepository(GroupContact).findOne({ id: this.contact });
if (!contact) { throw new GroupContactNotFoundError; }
return contact;
}
throw new GroupContactWrongTypeError;
} }
} }

View File

@ -1,6 +1,6 @@
import { IsInt, IsOptional } from 'class-validator'; import { IsInt, IsOptional } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import { AddressNotFoundError, AddressWrongTypeError } from '../../../errors/AddressErrors'; import { AddressNotFoundError } from '../../../errors/AddressErrors';
import { Address } from '../../entities/Address'; import { Address } from '../../entities/Address';
import { RunnerOrganisation } from '../../entities/RunnerOrganisation'; import { RunnerOrganisation } from '../../entities/RunnerOrganisation';
import { CreateRunnerGroup } from './CreateRunnerGroup'; import { CreateRunnerGroup } from './CreateRunnerGroup';
@ -10,8 +10,7 @@ import { CreateRunnerGroup } from './CreateRunnerGroup';
*/ */
export class CreateRunnerOrganisation extends CreateRunnerGroup { export class CreateRunnerOrganisation extends CreateRunnerGroup {
/** /**
* The new organisation's address. * The new organisation's address's id.
* Must be of type number (address id).
*/ */
@IsInt() @IsInt()
@IsOptional() @IsOptional()
@ -21,16 +20,10 @@ export class CreateRunnerOrganisation extends CreateRunnerGroup {
* Gets the org's address by it's id. * Gets the org's address by it's id.
*/ */
public async getAddress(): Promise<Address> { public async getAddress(): Promise<Address> {
if (this.address === undefined || this.address === null) { if (!this.address) { return null; }
return null; let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
} if (!address) { throw new AddressNotFoundError; }
if (!isNaN(this.address)) { return address;
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
if (!address) { throw new AddressNotFoundError; }
return address;
}
throw new AddressWrongTypeError;
} }
/** /**
@ -41,7 +34,7 @@ export class CreateRunnerOrganisation extends CreateRunnerGroup {
newRunnerOrganisation.name = this.name; newRunnerOrganisation.name = this.name;
newRunnerOrganisation.contact = await this.getContact(); newRunnerOrganisation.contact = await this.getContact();
// newRunnerOrganisation.address = await this.getAddress(); newRunnerOrganisation.address = await this.getAddress();
return newRunnerOrganisation; return newRunnerOrganisation;
} }

View File

@ -1,6 +1,6 @@
import { IsInt, IsNotEmpty } from 'class-validator'; import { IsInt, IsNotEmpty } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import { RunnerOrganisationNotFoundError, RunnerOrganisationWrongTypeError } from '../../../errors/RunnerOrganisationErrors'; import { RunnerOrganisationNotFoundError } from '../../../errors/RunnerOrganisationErrors';
import { RunnerTeamNeedsParentError } from '../../../errors/RunnerTeamErrors'; 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';
@ -12,7 +12,7 @@ import { CreateRunnerGroup } from './CreateRunnerGroup';
export class CreateRunnerTeam extends CreateRunnerGroup { export class CreateRunnerTeam extends CreateRunnerGroup {
/** /**
* The new team's parent group (organisation). * The new team's parent org's id.
*/ */
@IsInt() @IsInt()
@IsNotEmpty() @IsNotEmpty()
@ -25,13 +25,9 @@ export class CreateRunnerTeam extends CreateRunnerGroup {
if (this.parentGroup === undefined || this.parentGroup === null) { if (this.parentGroup === undefined || this.parentGroup === null) {
throw new RunnerTeamNeedsParentError(); throw new RunnerTeamNeedsParentError();
} }
if (!isNaN(this.parentGroup)) { let parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentGroup });
let parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentGroup }); if (!parentGroup) { throw new RunnerOrganisationNotFoundError();; }
if (!parentGroup) { throw new RunnerOrganisationNotFoundError();; } return parentGroup;
return parentGroup;
}
throw new RunnerOrganisationWrongTypeError;
} }
/** /**
@ -42,7 +38,6 @@ export class CreateRunnerTeam extends CreateRunnerGroup {
newRunnerTeam.name = this.name; newRunnerTeam.name = this.name;
newRunnerTeam.parentGroup = await this.getParent(); newRunnerTeam.parentGroup = await this.getParent();
newRunnerTeam.contact = await this.getContact() newRunnerTeam.contact = await this.getContact()
return newRunnerTeam; return newRunnerTeam;

View File

@ -9,7 +9,7 @@ import { Scan } from '../../entities/Scan';
*/ */
export abstract class CreateScan { export abstract class CreateScan {
/** /**
* The scan's associated runner. * The scan's associated runner's id.
* This is important to link ran distances to runners. * This is important to link ran distances to runners.
*/ */
@IsInt() @IsInt()

View File

@ -19,7 +19,7 @@ export class CreateScanStation {
description?: string; description?: string;
/** /**
* The station's associated track. * The station's associated track's id.
*/ */
@IsInt() @IsInt()
@IsPositive() @IsPositive()

View File

@ -12,7 +12,7 @@ import { TrackScan } from '../../entities/TrackScan';
*/ */
export class CreateTrackScan { export class CreateTrackScan {
/** /**
* The runnerCard associated with the scan. * The id of the runnerCard associated with the scan.
* This get's saved for documentation and management purposes. * This get's saved for documentation and management purposes.
*/ */
@IsInt() @IsInt()
@ -20,8 +20,8 @@ export class CreateTrackScan {
card: number; card: number;
/** /**
* The scanning station that created the scan. * The scanning station's id that created the scan.
* Mainly used for logging and traceing back scans (or errors) * Mainly used for logging and traceing back scans (or errors).
*/ */
@IsInt() @IsInt()
@IsPositive() @IsPositive()

View File

@ -71,7 +71,7 @@ export class CreateUser {
enabled?: boolean = true; enabled?: boolean = true;
/** /**
* The new user's groups' id(s). * The new user's groups' ids.
* You can provide either one groupId or an array of groupIDs. * You can provide either one groupId or an array of groupIDs.
*/ */
@IsOptional() @IsOptional()

View File

@ -11,7 +11,7 @@ import { UpdateDonation } from './UpdateDonation';
export class UpdateDistanceDonation extends UpdateDonation { export class UpdateDistanceDonation extends UpdateDonation {
/** /**
* The donation's associated runner. * The donation's associated runner's id.
* This is important to link the runner's distance ran to the donation. * This is important to link the runner's distance ran to the donation.
*/ */
@IsInt() @IsInt()

View File

@ -16,7 +16,7 @@ export abstract class UpdateDonation {
id: number; id: number;
/** /**
* The updated donation's associated donor. * The updated donation's associated donor's id.
* This is important to link donations to donors. * This is important to link donations to donors.
*/ */
@IsInt() @IsInt()

View File

@ -1,7 +1,7 @@
import { IsInt, IsNotEmpty, IsObject } from 'class-validator'; import { IsInt, IsNotEmpty, IsPositive } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import { PermissionNeedsPrincipalError } from '../../../errors/PermissionErrors'; import { PermissionNeedsPrincipalError } from '../../../errors/PermissionErrors';
import { PrincipalNotFoundError, PrincipalWrongTypeError } from '../../../errors/PrincipalErrors'; import { PrincipalNotFoundError } from '../../../errors/PrincipalErrors';
import { Permission } from '../../entities/Permission'; import { Permission } from '../../entities/Permission';
import { Principal } from '../../entities/Principal'; import { Principal } from '../../entities/Principal';
import { PermissionAction } from '../../enums/PermissionAction'; import { PermissionAction } from '../../enums/PermissionAction';
@ -20,12 +20,11 @@ export class UpdatePermission {
id: number; id: number;
/** /**
* The updated permissions's principal. * The updated permissions's principal's id.
* Just has to contain the principal's id -everything else won't be checked or changed.
*/ */
@IsObject() @IsInt()
@IsNotEmpty() @IsPositive()
principal: Principal; principal: number;
/** /**
* The permissions's target. * The permissions's target.
@ -57,12 +56,8 @@ export class UpdatePermission {
if (this.principal === undefined || this.principal === null) { if (this.principal === undefined || this.principal === null) {
throw new PermissionNeedsPrincipalError(); throw new PermissionNeedsPrincipalError();
} }
if (!isNaN(this.principal.id)) { let principal = await getConnectionManager().get().getRepository(Principal).findOne({ id: this.principal });
let principal = await getConnectionManager().get().getRepository(Principal).findOne({ id: this.principal.id }); if (!principal) { throw new PrincipalNotFoundError(); }
if (!principal) { throw new PrincipalNotFoundError(); } return principal;
return principal;
}
throw new PrincipalWrongTypeError();
} }
} }

View File

@ -1,7 +1,6 @@
import { IsInt, IsObject } from 'class-validator'; import { IsInt, IsPositive } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import { RunnerGroupNotFoundError } from '../../../errors/RunnerGroupErrors'; import { RunnerGroupNotFoundError } from '../../../errors/RunnerGroupErrors';
import { RunnerOrganisationWrongTypeError } from '../../../errors/RunnerOrganisationErrors';
import { RunnerTeamNeedsParentError } from '../../../errors/RunnerTeamErrors'; 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';
@ -20,11 +19,11 @@ export class UpdateRunner extends CreateParticipant {
id: number; id: number;
/** /**
* The updated runner's new team/org. * The updated runner's group's id.
* Just has to contain the group's id -everything else won't be checked or changed.
*/ */
@IsObject() @IsInt()
group: RunnerGroup; @IsPositive()
group: number;
/** /**
* Updates a provided Runner entity based on this. * Updates a provided Runner entity based on this.
@ -48,12 +47,8 @@ export class UpdateRunner extends CreateParticipant {
if (this.group === undefined || this.group === null) { if (this.group === undefined || this.group === null) {
throw new RunnerTeamNeedsParentError(); throw new RunnerTeamNeedsParentError();
} }
if (!isNaN(this.group.id)) { let group = await getConnectionManager().get().getRepository(RunnerGroup).findOne({ id: this.group });
let group = await getConnectionManager().get().getRepository(RunnerGroup).findOne({ id: this.group.id }); if (!group) { throw new RunnerGroupNotFoundError; }
if (!group) { throw new RunnerGroupNotFoundError; } return group;
return group;
}
throw new RunnerOrganisationWrongTypeError;
} }
} }

View File

@ -17,7 +17,7 @@ export class UpdateRunnerCard {
id?: number; id?: number;
/** /**
* The updated card's associated runner. * The updated card's associated runner's id.
*/ */
@IsInt() @IsInt()
@IsOptional() @IsOptional()

View File

@ -18,22 +18,18 @@ export class UpdateRunnerOrganisation extends CreateRunnerGroup {
id: number; id: number;
/** /**
* The updated organisation's address. * The updated organisation's address's id.
* Just has to contain the address's id - everything else won't be checked or changed.
* Optional.
*/ */
@IsInt() @IsInt()
@IsOptional() @IsOptional()
address?: Address; address?: number;
/** /**
* Loads the organisation's address based on it's id. * Loads the organisation's address based on it's id.
*/ */
public async getAddress(): Promise<Address> { public async getAddress(): Promise<Address> {
if (this.address === undefined || this.address === null) { if (!this.address) { return null; }
return null; let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
}
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address.id });
if (!address) { throw new AddressNotFoundError; } if (!address) { throw new AddressNotFoundError; }
return address; return address;
} }
@ -45,7 +41,7 @@ export class UpdateRunnerOrganisation extends CreateRunnerGroup {
organisation.name = this.name; organisation.name = this.name;
organisation.contact = await this.getContact(); organisation.contact = await this.getContact();
// organisation.address = await this.getAddress(); organisation.address = await this.getAddress();
return organisation; return organisation;
} }

View File

@ -1,6 +1,6 @@
import { IsInt, IsNotEmpty, IsObject } from 'class-validator'; import { IsInt, IsPositive } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import { RunnerOrganisationNotFoundError, RunnerOrganisationWrongTypeError } from '../../../errors/RunnerOrganisationErrors'; import { RunnerOrganisationNotFoundError } from '../../../errors/RunnerOrganisationErrors';
import { RunnerTeamNeedsParentError } from '../../../errors/RunnerTeamErrors'; 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';
@ -19,12 +19,11 @@ export class UpdateRunnerTeam extends CreateRunnerGroup {
id: number; id: number;
/** /**
* The updated team's parentGroup. * The updated team's parentGroup's id.
* Just has to contain the organisation's id - everything else won't be checked or changed.
*/ */
@IsObject() @IsInt()
@IsNotEmpty() @IsPositive()
parentGroup: RunnerOrganisation; parentGroup: number;
/** /**
* Loads the updated teams's parentGroup based on it's id. * Loads the updated teams's parentGroup based on it's id.
@ -33,13 +32,9 @@ export class UpdateRunnerTeam extends CreateRunnerGroup {
if (this.parentGroup === undefined || this.parentGroup === null) { if (this.parentGroup === undefined || this.parentGroup === null) {
throw new RunnerTeamNeedsParentError(); throw new RunnerTeamNeedsParentError();
} }
if (!isNaN(this.parentGroup.id)) { let parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentGroup });
let parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentGroup.id }); if (!parentGroup) { throw new RunnerOrganisationNotFoundError();; }
if (!parentGroup) { throw new RunnerOrganisationNotFoundError();; } return parentGroup;
return parentGroup;
}
throw new RunnerOrganisationWrongTypeError;
} }
/** /**

View File

@ -16,7 +16,7 @@ export abstract class UpdateScan {
id: number; id: number;
/** /**
* The updated scan's associated runner. * The updated scan's associated runner's id.
* This is important to link ran distances to runners. * This is important to link ran distances to runners.
*/ */
@IsInt() @IsInt()

View File

@ -1,4 +1,4 @@
import { IsBoolean, IsInt, IsOptional } from 'class-validator'; import { IsBoolean, IsInt, IsOptional, IsPositive } from 'class-validator';
import { getConnection } from 'typeorm'; import { getConnection } from 'typeorm';
import { RunnerNotFoundError } from '../../../errors/RunnerErrors'; import { RunnerNotFoundError } from '../../../errors/RunnerErrors';
import { ScanStationNotFoundError } from '../../../errors/ScanStationErrors'; import { ScanStationNotFoundError } from '../../../errors/ScanStationErrors';
@ -18,12 +18,12 @@ export abstract class UpdateTrackScan {
id: number; id: number;
/** /**
* The updated scan's associated runner. * The updated scan's associated runner's id.
* This is important to link ran distances to runners. * This is important to link ran distances to runners.
*/ */
@IsInt() @IsInt()
@IsOptional() @IsPositive()
runner?: number; runner: number;
/** /**
* Is the updated scan valid (for fraud reasons). * Is the updated scan valid (for fraud reasons).
@ -33,12 +33,12 @@ export abstract class UpdateTrackScan {
valid?: boolean = true; valid?: boolean = true;
/** /**
* The updated scan's associated station. * The updated scan's associated station's id.
* This is important to link ran distances to runners. * This is important to link ran distances to runners.
*/ */
@IsInt() @IsInt()
@IsOptional() @IsPositive()
public station?: number; public station: number;
/** /**
* Update a TrackScan entity based on this. * Update a TrackScan entity based on this.
@ -46,12 +46,8 @@ export abstract class UpdateTrackScan {
*/ */
public async update(scan: TrackScan): Promise<TrackScan> { public async update(scan: TrackScan): Promise<TrackScan> {
scan.valid = this.valid; scan.valid = this.valid;
if (this.runner) { scan.runner = await this.getRunner();
scan.runner = await this.getRunner(); scan.station = await this.getStation();
}
if (this.station) {
scan.station = await this.getStation();
}
scan.track = scan.station.track; scan.track = scan.station.track;
return scan; return scan;
@ -72,7 +68,7 @@ export abstract class UpdateTrackScan {
* Gets a runner based on the runner id provided via this.runner. * Gets a runner based on the runner id provided via this.runner.
*/ */
public async getStation(): Promise<ScanStation> { public async getStation(): Promise<ScanStation> {
const station = await getConnection().getRepository(ScanStation).findOne({ id: this.station }); const station = await getConnection().getRepository(ScanStation).findOne({ id: this.station }, { relations: ['track'] });
if (!station) { if (!station) {
throw new ScanStationNotFoundError(); throw new ScanStationNotFoundError();
} }

View File

@ -79,11 +79,10 @@ export class UpdateUser {
enabled: boolean = true; enabled: boolean = true;
/** /**
* The updated user's groups. * The updated user's groups' ids.
* This just has to contain the group's id - everything else won't be changed.
*/ */
@IsOptional() @IsOptional()
groups?: UserGroup[] groups?: number | number[]
/** /**
* The user's profile pic (or rather a url pointing to it). * The user's profile pic (or rather a url pointing to it).
@ -124,7 +123,7 @@ export class UpdateUser {
} }
/** /**
* Loads the updated user's groups based on their ids. * Get's all groups for this user by their id's;
*/ */
public async getGroups() { public async getGroups() {
if (!this.groups) { return null; } if (!this.groups) { return null; }
@ -133,7 +132,7 @@ export class UpdateUser {
this.groups = [this.groups] this.groups = [this.groups]
} }
for (let group of this.groups) { for (let group of this.groups) {
let found = await getConnectionManager().get().getRepository(UserGroup).findOne({ id: group.id }); let found = await getConnectionManager().get().getRepository(UserGroup).findOne({ id: group });
if (!found) { throw new UserGroupNotFoundError(); } if (!found) { throw new UserGroupNotFoundError(); }
groups.push(found); groups.push(found);
} }

View File

@ -34,7 +34,7 @@ describe('POST /api/donors with errors', () => {
"firstname": "first", "firstname": "first",
"middlename": "middle", "middlename": "middle",
"lastname": "last", "lastname": "last",
"address": 0 "address": 99999999999999999999999999
}, axios_config); }, axios_config);
expect(res2.status).toEqual(404); expect(res2.status).toEqual(404);
expect(res2.headers['content-type']).toContain("application/json") expect(res2.headers['content-type']).toContain("application/json")

View File

@ -43,7 +43,7 @@ describe('adding + updating name', () => {
"id": added_team_id, "id": added_team_id,
"name": "testlelele", "name": "testlelele",
"contact": null, "contact": null,
"parentGroup": added_org "parentGroup": added_org.id
}, axios_config); }, axios_config);
expect(res3.status).toEqual(200); expect(res3.status).toEqual(200);
expect(res3.headers['content-type']).toContain("application/json") expect(res3.headers['content-type']).toContain("application/json")
@ -79,6 +79,7 @@ describe('adding + try updating id (should return 406)', () => {
}); });
it('update team', async () => { it('update team', async () => {
added_team.id = added_team.id + 1; added_team.id = added_team.id + 1;
added_team.parentGroup = added_team.parentGroup.id;
const res3 = await axios.put(base + '/api/teams/' + added_team_id, added_team, axios_config); const res3 = await axios.put(base + '/api/teams/' + added_team_id, added_team, axios_config);
expect(res3.status).toEqual(406); expect(res3.status).toEqual(406);
expect(res3.headers['content-type']).toContain("application/json") expect(res3.headers['content-type']).toContain("application/json")
@ -117,7 +118,7 @@ describe('add+update parent org (valid)', () => {
expect(res3.headers['content-type']).toContain("application/json") expect(res3.headers['content-type']).toContain("application/json")
}); });
it('update team', async () => { it('update team', async () => {
added_team.parentGroup = added_org2; added_team.parentGroup = added_org2.id;
const res4 = await axios.put(base + '/api/teams/' + added_team_id, added_team, axios_config); const res4 = await axios.put(base + '/api/teams/' + added_team_id, added_team, axios_config);
let updated_team = res4.data; let updated_team = res4.data;
expect(res4.status).toEqual(200); expect(res4.status).toEqual(200);
@ -125,6 +126,6 @@ describe('add+update parent org (valid)', () => {
delete added_org2.address; delete added_org2.address;
delete added_org2.contact; delete added_org2.contact;
delete added_org2.teams; delete added_org2.teams;
expect(updated_team).toEqual(added_team) expect(updated_team.parentGroup).toEqual(added_org2)
}); });
}); });

View File

@ -15,15 +15,14 @@ beforeAll(async () => {
}); });
describe('Update runner name after adding', () => { describe('Update runner name after adding', () => {
let added_org_id; let added_org;
let added_runner; let added_runner;
let updated_runner; let updated_runner;
it('creating a new org with just a name should return 200', async () => { it('creating a new org with just a name should return 200', async () => {
const res1 = await axios.post(base + '/api/organisations', { const res1 = await axios.post(base + '/api/organisations', {
"name": "test123" "name": "test123"
}, axios_config); }, axios_config);
let added_org = res1.data added_org = res1.data
added_org_id = added_org.id;
expect(res1.status).toEqual(200); expect(res1.status).toEqual(200);
expect(res1.headers['content-type']).toContain("application/json") expect(res1.headers['content-type']).toContain("application/json")
}); });
@ -31,7 +30,7 @@ describe('Update runner name after adding', () => {
const res2 = await axios.post(base + '/api/runners', { const res2 = await axios.post(base + '/api/runners', {
"firstname": "first", "firstname": "first",
"lastname": "last", "lastname": "last",
"group": added_org_id "group": added_org.id
}, axios_config); }, axios_config);
added_runner = res2.data; added_runner = res2.data;
expect(res2.status).toEqual(200); expect(res2.status).toEqual(200);
@ -40,10 +39,15 @@ describe('Update runner name after adding', () => {
it('valid update should return 200', async () => { it('valid update should return 200', async () => {
let runnercopy = added_runner let runnercopy = added_runner
runnercopy.firstname = "second" runnercopy.firstname = "second"
runnercopy.group = added_runner.group.id;
const res3 = await axios.put(base + '/api/runners/' + added_runner.id, runnercopy, axios_config); const res3 = await axios.put(base + '/api/runners/' + added_runner.id, runnercopy, axios_config);
expect(res3.status).toEqual(200); expect(res3.status).toEqual(200);
expect(res3.headers['content-type']).toContain("application/json") expect(res3.headers['content-type']).toContain("application/json")
updated_runner = res3.data updated_runner = res3.data;
delete added_org.address;
delete added_org.contact;
delete added_org.teams;
runnercopy.group = added_org;
expect(updated_runner).toEqual(runnercopy); expect(updated_runner).toEqual(runnercopy);
}); });
}); });
@ -83,13 +87,13 @@ describe('Update runner group after adding', () => {
expect(res3.status).toEqual(200); expect(res3.status).toEqual(200);
expect(res3.headers['content-type']).toContain("application/json") expect(res3.headers['content-type']).toContain("application/json")
}); });
it('valid update should return 200', async () => { it('valid group update should return 200', async () => {
added_runner.group = added_org_2; added_runner.group = added_org_2.id;
const res3 = await axios.put(base + '/api/runners/' + added_runner.id, added_runner, axios_config); const res3 = await axios.put(base + '/api/runners/' + added_runner.id, added_runner, axios_config);
expect(res3.status).toEqual(200); expect(res3.status).toEqual(200);
expect(res3.headers['content-type']).toContain("application/json") expect(res3.headers['content-type']).toContain("application/json")
updated_runner = res3.data updated_runner = res3.data
expect(updated_runner).toEqual(added_runner); expect(updated_runner.group).toEqual(added_org_2);
}); });
}); });
// --------------- // ---------------
@ -119,6 +123,7 @@ describe('Update runner id after adding(should fail)', () => {
}); });
it('invalid update should return 406', async () => { it('invalid update should return 406', async () => {
added_runner.id++; added_runner.id++;
added_runner.group = added_runner.group.id;
const res3 = await axios.put(base + '/api/runners/' + added_runner_id, added_runner, axios_config); const res3 = await axios.put(base + '/api/runners/' + added_runner_id, added_runner, axios_config);
expect(res3.status).toEqual(406); expect(res3.status).toEqual(406);
expect(res3.headers['content-type']).toContain("application/json") expect(res3.headers['content-type']).toContain("application/json")
@ -146,9 +151,8 @@ describe('Update runner group with invalid group after adding', () => {
expect(res2.status).toEqual(200); expect(res2.status).toEqual(200);
expect(res2.headers['content-type']).toContain("application/json") expect(res2.headers['content-type']).toContain("application/json")
}); });
it('invalid update should return 404', async () => { it('invalid group update should return 404', async () => {
added_org.id = 0; added_runner.group = 99999999999999999;
added_runner.group = added_org;
const res3 = await axios.put(base + '/api/runners/' + added_runner.id, added_runner, axios_config); const res3 = await axios.put(base + '/api/runners/' + added_runner.id, added_runner, axios_config);
expect(res3.status).toEqual(404); expect(res3.status).toEqual(404);
expect(res3.headers['content-type']).toContain("application/json") expect(res3.headers['content-type']).toContain("application/json")