Cleaned up realations regarding response classes
continuous-integration/drone/pr Build is passing
Details
continuous-integration/drone/pr Build is passing
Details
ref #132
This commit is contained in:
parent
8dc2810c0c
commit
ff7406e71a
|
@ -78,7 +78,7 @@ export class DonationController {
|
|||
async postDistance(@Body({ validate: true }) createDonation: CreateDistanceDonation) {
|
||||
let donation = await createDonation.toEntity();
|
||||
donation = await this.distanceDonationRepository.save(donation);
|
||||
return (await this.donationRepository.findOne({ id: donation.id }, { relations: ['runner', 'donor', 'runner.scans', 'runner.scans.track'] })).toResponse();
|
||||
return (await this.distanceDonationRepository.findOne({ id: donation.id }, { relations: ['runner', 'donor', 'runner.scans', 'runner.scans.track'] })).toResponse();
|
||||
}
|
||||
|
||||
@Put('/fixed/:id')
|
||||
|
@ -124,7 +124,7 @@ export class DonationController {
|
|||
}
|
||||
|
||||
await this.distanceDonationRepository.save(await donation.update(oldDonation));
|
||||
return (await this.donationRepository.findOne({ id: donation.id }, { relations: ['runner', 'donor', 'runner.scans', 'runner.scans.track'] })).toResponse();
|
||||
return (await this.distanceDonationRepository.findOne({ id: donation.id }, { relations: ['runner', 'donor', 'runner.scans', 'runner.scans.track'] })).toResponse();
|
||||
}
|
||||
|
||||
@Delete('/:id')
|
||||
|
|
|
@ -36,7 +36,7 @@ export class ScanController {
|
|||
@OpenAPI({ description: 'Lists all scans (normal or track) from all runners. <br> This includes the scan\'s runner\'s distance ran.' })
|
||||
async getAll() {
|
||||
let responseScans: ResponseScan[] = new Array<ResponseScan>();
|
||||
const scans = await this.scanRepository.find({ relations: ['runner', 'track', 'runner.scans', 'runner.scans.track', 'card', 'station'] });
|
||||
const scans = await this.scanRepository.find({ relations: ['runner', 'track', 'runner.scans', 'runner.group', 'runner.scans.track', 'card', 'station'] });
|
||||
scans.forEach(scan => {
|
||||
responseScans.push(scan.toResponse());
|
||||
});
|
||||
|
@ -51,7 +51,7 @@ export class ScanController {
|
|||
@OnUndefined(ScanNotFoundError)
|
||||
@OpenAPI({ description: 'Lists all information about the scan whose id got provided. This includes the scan\'s runner\'s distance ran.' })
|
||||
async getOne(@Param('id') id: number) {
|
||||
let scan = await this.scanRepository.findOne({ id: id }, { relations: ['runner', 'track', 'runner.scans', 'runner.scans.track', 'card', 'station'] })
|
||||
let scan = await this.scanRepository.findOne({ id: id }, { relations: ['runner', 'track', 'runner.scans', 'runner.group', 'runner.scans.track', 'card', 'station'] })
|
||||
if (!scan) { throw new ScanNotFoundError(); }
|
||||
return scan.toResponse();
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ export class ScanController {
|
|||
async post(@Body({ validate: true }) createScan: CreateScan) {
|
||||
let scan = await createScan.toEntity();
|
||||
scan = await this.scanRepository.save(scan);
|
||||
return (await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner', 'track', 'runner.scans', 'runner.scans.track', 'card', 'station'] })).toResponse();
|
||||
return (await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner', 'track', 'runner.scans', 'runner.group', 'runner.scans.track', 'card', 'station'] })).toResponse();
|
||||
}
|
||||
|
||||
@Post("/trackscans")
|
||||
|
@ -75,7 +75,7 @@ export class ScanController {
|
|||
async postTrackScans(@Body({ validate: true }) createScan: CreateTrackScan) {
|
||||
let scan = await createScan.toEntity();
|
||||
scan = await this.trackScanRepository.save(scan);
|
||||
return (await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner', 'track', 'runner.scans', 'runner.scans.track', 'card', 'station'] })).toResponse();
|
||||
return (await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner', 'track', 'runner.scans', 'runner.group', 'runner.scans.track', 'card', 'station'] })).toResponse();
|
||||
}
|
||||
|
||||
@Put('/:id')
|
||||
|
@ -97,7 +97,7 @@ export class ScanController {
|
|||
}
|
||||
|
||||
await this.scanRepository.save(await scan.update(oldScan));
|
||||
return (await this.scanRepository.findOne({ id: id }, { relations: ['runner', 'track', 'runner.scans', 'runner.scans.track', 'card', 'station'] })).toResponse();
|
||||
return (await this.scanRepository.findOne({ id: id }, { relations: ['runner', 'track', 'runner.scans', 'runner.group', 'runner.scans.track', 'card', 'station'] })).toResponse();
|
||||
}
|
||||
|
||||
@Put('/trackscans/:id')
|
||||
|
@ -120,7 +120,7 @@ export class ScanController {
|
|||
}
|
||||
|
||||
await this.trackScanRepository.save(await scan.update(oldScan));
|
||||
return (await this.scanRepository.findOne({ id: id }, { relations: ['runner', 'track', 'runner.scans', 'runner.scans.track', 'card', 'station'] })).toResponse();
|
||||
return (await this.scanRepository.findOne({ id: id }, { relations: ['runner', 'track', 'runner.scans', 'runner.group', 'runner.scans.track', 'card', 'station'] })).toResponse();
|
||||
}
|
||||
|
||||
@Delete('/:id')
|
||||
|
@ -132,7 +132,7 @@ export class ScanController {
|
|||
async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
|
||||
let scan = await this.scanRepository.findOne({ id: id });
|
||||
if (!scan) { return null; }
|
||||
const responseScan = await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner', 'track', 'runner.scans', 'runner.scans.track', 'card', 'station'] });
|
||||
const responseScan = await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner', 'track', 'runner.scans', 'runner.group', 'runner.scans.track', 'card', 'station'] });
|
||||
|
||||
await this.scanRepository.delete(scan);
|
||||
return responseScan.toResponse();
|
||||
|
|
|
@ -4,7 +4,6 @@ import {
|
|||
IsString
|
||||
} from "class-validator";
|
||||
import { ChildEntity, Column } from "typeorm";
|
||||
import { ResponsePrincipal } from '../responses/ResponsePrincipal';
|
||||
import { ResponseUserGroup } from '../responses/ResponseUserGroup';
|
||||
import { Principal } from './Principal';
|
||||
|
||||
|
@ -34,7 +33,7 @@ export class UserGroup extends Principal {
|
|||
/**
|
||||
* Turns this entity into it's response class.
|
||||
*/
|
||||
public toResponse(): ResponsePrincipal {
|
||||
public toResponse(): ResponseUserGroup {
|
||||
return new ResponseUserGroup(this);
|
||||
}
|
||||
}
|
|
@ -36,7 +36,7 @@ export class ResponseDistanceDonation extends ResponseDonation implements IRespo
|
|||
*/
|
||||
public constructor(donation: DistanceDonation) {
|
||||
super(donation);
|
||||
this.runner = donation.runner.toResponse();
|
||||
if (donation.runner) { this.runner = donation.runner.toResponse(); }
|
||||
this.amountPerDistance = donation.amountPerDistance;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,8 +77,10 @@ export class ResponseGroupContact implements IResponse {
|
|||
this.email = contact.email;
|
||||
this.address = contact.address;
|
||||
this.groups = new Array<ResponseRunnerGroup>();
|
||||
for (let group of contact.groups) {
|
||||
this.groups.push(group.toResponse());
|
||||
if (contact.groups) {
|
||||
for (let group of contact.groups) {
|
||||
this.groups.push(group.toResponse());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,10 +3,10 @@ import {
|
|||
IsObject
|
||||
} from "class-validator";
|
||||
import { Runner } from '../entities/Runner';
|
||||
import { RunnerGroup } from '../entities/RunnerGroup';
|
||||
import { ResponseObjectType } from '../enums/ResponseObjectType';
|
||||
import { IResponse } from './IResponse';
|
||||
import { ResponseParticipant } from './ResponseParticipant';
|
||||
import { ResponseRunnerGroup } from './ResponseRunnerGroup';
|
||||
|
||||
/**
|
||||
* Defines the runner response.
|
||||
|
@ -28,7 +28,7 @@ export class ResponseRunner extends ResponseParticipant implements IResponse {
|
|||
* The runner's group.
|
||||
*/
|
||||
@IsObject()
|
||||
group: RunnerGroup;
|
||||
group: ResponseRunnerGroup;
|
||||
|
||||
/**
|
||||
* Creates a ResponseRunner object from a runner.
|
||||
|
@ -38,6 +38,6 @@ export class ResponseRunner extends ResponseParticipant implements IResponse {
|
|||
super(runner);
|
||||
if (!runner.scans) { this.distance = 0 }
|
||||
else { this.distance = runner.validScans.reduce((sum, current) => sum + current.distance, 0); }
|
||||
this.group = runner.group;
|
||||
if (runner.group) { this.group = runner.group.toResponse(); }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { IsInt, IsNotEmpty, IsObject, IsOptional, IsString } from "class-validator";
|
||||
import { GroupContact } from '../entities/GroupContact';
|
||||
import { RunnerGroup } from '../entities/RunnerGroup';
|
||||
import { ResponseObjectType } from '../enums/ResponseObjectType';
|
||||
import { IResponse } from './IResponse';
|
||||
import { ResponseGroupContact } from './ResponseGroupContact';
|
||||
|
||||
/**
|
||||
* Defines the runnerGroup response.
|
||||
|
@ -34,7 +34,7 @@ export abstract class ResponseRunnerGroup implements IResponse {
|
|||
*/
|
||||
@IsObject()
|
||||
@IsOptional()
|
||||
contact?: GroupContact;
|
||||
contact?: ResponseGroupContact;
|
||||
|
||||
/**
|
||||
* Creates a ResponseRunnerGroup object from a runnerGroup.
|
||||
|
@ -43,6 +43,6 @@ export abstract class ResponseRunnerGroup implements IResponse {
|
|||
public constructor(group: RunnerGroup) {
|
||||
this.id = group.id;
|
||||
this.name = group.name;
|
||||
this.contact = group.contact;
|
||||
if (group.contact) { this.contact = group.contact.toResponse(); };
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,10 +11,10 @@ import {
|
|||
} from "class-validator";
|
||||
import { Address } from '../entities/Address';
|
||||
import { RunnerOrganization } from '../entities/RunnerOrganization';
|
||||
import { RunnerTeam } from '../entities/RunnerTeam';
|
||||
import { ResponseObjectType } from '../enums/ResponseObjectType';
|
||||
import { IResponse } from './IResponse';
|
||||
import { ResponseRunnerGroup } from './ResponseRunnerGroup';
|
||||
import { ResponseRunnerTeam } from './ResponseRunnerTeam';
|
||||
|
||||
/**
|
||||
* Defines the runnerOrganization response.
|
||||
|
@ -37,7 +37,7 @@ export class ResponseRunnerOrganization extends ResponseRunnerGroup implements I
|
|||
* The runnerOrganization associated teams.
|
||||
*/
|
||||
@IsArray()
|
||||
teams: RunnerTeam[];
|
||||
teams: ResponseRunnerTeam[];
|
||||
|
||||
/**
|
||||
* The organization's registration key.
|
||||
|
@ -62,7 +62,13 @@ export class ResponseRunnerOrganization extends ResponseRunnerGroup implements I
|
|||
public constructor(org: RunnerOrganization) {
|
||||
super(org);
|
||||
this.address = org.address;
|
||||
this.teams = org.teams;
|
||||
this.teams = new Array<ResponseRunnerTeam>();
|
||||
if (org.teams) {
|
||||
for (let team of org.teams) {
|
||||
this.teams.push(team.toResponse());
|
||||
}
|
||||
}
|
||||
|
||||
if (!org.key) { this.registrationEnabled = false; }
|
||||
else { this.registrationKey = Buffer.from(org.key).toString('base64'); }
|
||||
}
|
||||
|
|
|
@ -28,6 +28,6 @@ export class ResponseRunnerTeam extends ResponseRunnerGroup implements IResponse
|
|||
*/
|
||||
public constructor(team: RunnerTeam) {
|
||||
super(team);
|
||||
this.parentGroup = team.parentGroup.toResponse();
|
||||
if (team.parentGroup) { this.parentGroup = team.parentGroup.toResponse(); }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@ export class ResponseScan implements IResponse {
|
|||
*/
|
||||
public constructor(scan: Scan) {
|
||||
this.id = scan.id;
|
||||
this.runner = scan.runner.toResponse();
|
||||
if (scan.runner) { this.runner = scan.runner.toResponse(); }
|
||||
this.distance = scan.distance;
|
||||
this.valid = scan.valid;
|
||||
}
|
||||
|
|
|
@ -72,7 +72,7 @@ export class ResponseScanStation implements IResponse {
|
|||
this.description = station.description;
|
||||
this.prefix = station.prefix;
|
||||
this.key = "Only visible on creation.";
|
||||
this.track = station.track.toResponse();
|
||||
if (station.track) { this.track = station.track.toResponse(); }
|
||||
this.enabled = station.enabled;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,9 +4,9 @@ import {
|
|||
IsString
|
||||
} from "class-validator";
|
||||
import { Runner } from '../entities/Runner';
|
||||
import { RunnerGroup } from '../entities/RunnerGroup';
|
||||
import { ResponseObjectType } from '../enums/ResponseObjectType';
|
||||
import { IResponse } from './IResponse';
|
||||
import { ResponseRunnerGroup } from './ResponseRunnerGroup';
|
||||
|
||||
/**
|
||||
* Defines the runner stats response.
|
||||
|
@ -59,7 +59,7 @@ export class ResponseStatsRunner implements IResponse {
|
|||
* The runner's group.
|
||||
*/
|
||||
@IsObject()
|
||||
group: RunnerGroup;
|
||||
group: ResponseRunnerGroup;
|
||||
|
||||
/**
|
||||
* Creates a new runner stats response from a runner
|
||||
|
@ -72,6 +72,6 @@ export class ResponseStatsRunner implements IResponse {
|
|||
this.lastname = runner.lastname;
|
||||
this.distance = runner.distance;
|
||||
this.donationAmount = runner.distanceDonationAmount;
|
||||
this.group = runner.group;
|
||||
this.group = runner.group.toResponse();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,10 +3,10 @@ import {
|
|||
IsObject,
|
||||
IsString
|
||||
} from "class-validator";
|
||||
import { RunnerGroup } from '../entities/RunnerGroup';
|
||||
import { RunnerTeam } from '../entities/RunnerTeam';
|
||||
import { ResponseObjectType } from '../enums/ResponseObjectType';
|
||||
import { IResponse } from './IResponse';
|
||||
import { ResponseRunnerGroup } from './ResponseRunnerGroup';
|
||||
|
||||
/**
|
||||
* Defines the team stats response.
|
||||
|
@ -47,7 +47,7 @@ export class ResponseStatsTeam implements IResponse {
|
|||
* The teams's parent group.
|
||||
*/
|
||||
@IsObject()
|
||||
parent: RunnerGroup;
|
||||
parent: ResponseRunnerGroup;
|
||||
|
||||
/**
|
||||
* Creates a new team stats response from a team
|
||||
|
@ -56,7 +56,7 @@ export class ResponseStatsTeam implements IResponse {
|
|||
public constructor(team: RunnerTeam) {
|
||||
this.name = team.name;
|
||||
this.id = team.id;
|
||||
this.parent = team.parentGroup;
|
||||
this.parent = team.parentGroup.toResponse();
|
||||
this.distance = team.distance;
|
||||
this.donationAmount = team.distanceDonationAmount;
|
||||
}
|
||||
|
|
|
@ -49,8 +49,8 @@ export class ResponseTrackScan extends ResponseScan implements IResponse {
|
|||
public constructor(scan: TrackScan) {
|
||||
super(scan);
|
||||
this.track = new ResponseTrack(scan.track);
|
||||
this.card = scan.card.toResponse();
|
||||
this.station = scan.station.toResponse();
|
||||
if (scan.card) { scan.card.toResponse(); }
|
||||
if (scan.station) { scan.station.toResponse(); }
|
||||
this.timestamp = scan.timestamp;
|
||||
this.distance = scan.distance;
|
||||
}
|
||||
|
|
|
@ -6,10 +6,10 @@ import {
|
|||
IsString
|
||||
} from "class-validator";
|
||||
import { User } from '../entities/User';
|
||||
import { UserGroup } from '../entities/UserGroup';
|
||||
import { ResponseObjectType } from '../enums/ResponseObjectType';
|
||||
import { IResponse } from './IResponse';
|
||||
import { ResponsePrincipal } from './ResponsePrincipal';
|
||||
import { ResponseUserGroup } from './ResponseUserGroup';
|
||||
|
||||
/**
|
||||
* Defines the user response.
|
||||
|
@ -74,7 +74,7 @@ export class ResponseUser extends ResponsePrincipal implements IResponse {
|
|||
*/
|
||||
@IsArray()
|
||||
@IsOptional()
|
||||
groups: UserGroup[];
|
||||
groups: ResponseUserGroup[];
|
||||
|
||||
/**
|
||||
* The user's permissions.
|
||||
|
@ -98,10 +98,13 @@ export class ResponseUser extends ResponsePrincipal implements IResponse {
|
|||
this.username = user.username;
|
||||
this.enabled = user.enabled;
|
||||
this.profilePic = user.profilePic;
|
||||
this.groups = user.groups;
|
||||
this.groups = new Array<ResponseUserGroup>();
|
||||
this.permissions = user.allPermissions;
|
||||
if (this.groups) {
|
||||
this.groups.forEach(function (g) { delete g.permissions });
|
||||
if (user.groups) {
|
||||
for (let group of user.groups) {
|
||||
delete group.permissions;
|
||||
this.groups.push(group.toResponse());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { IsArray, IsNotEmpty, IsOptional, IsString } from "class-validator";
|
||||
import { Permission } from '../entities/Permission';
|
||||
import { UserGroup } from '../entities/UserGroup';
|
||||
import { ResponseObjectType } from '../enums/ResponseObjectType';
|
||||
import { IResponse } from './IResponse';
|
||||
import { ResponsePermission } from './ResponsePermission';
|
||||
import { ResponsePrincipal } from './ResponsePrincipal';
|
||||
|
||||
/**
|
||||
|
@ -34,7 +34,7 @@ export class ResponseUserGroup extends ResponsePrincipal implements IResponse {
|
|||
*/
|
||||
@IsArray()
|
||||
@IsOptional()
|
||||
permissions: Permission[];
|
||||
permissions: ResponsePermission[];
|
||||
|
||||
/**
|
||||
* Creates a ResponseUserGroup object from a userGroup.
|
||||
|
@ -44,6 +44,10 @@ export class ResponseUserGroup extends ResponsePrincipal implements IResponse {
|
|||
super(group);
|
||||
this.name = group.name;
|
||||
this.description = group.description;
|
||||
this.permissions = group.permissions;
|
||||
if (group.permissions) {
|
||||
for (let permission of group.permissions) {
|
||||
this.permissions.push(permission.toResponse());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue