44
src/models/actions/update/UpdateDonor.ts
Normal file
44
src/models/actions/update/UpdateDonor.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { IsBoolean, IsInt, IsOptional } from 'class-validator';
|
||||
import { DonorReceiptAddressNeededError } from '../../../errors/DonorErrors';
|
||||
import { Donor } from '../../entities/Donor';
|
||||
import { CreateParticipant } from './CreateParticipant';
|
||||
|
||||
/**
|
||||
* This class is used to update a Donor entity (via put request).
|
||||
*/
|
||||
export class UpdateDonor extends CreateParticipant {
|
||||
|
||||
/**
|
||||
* The updated donor's id.
|
||||
* This shouldn't have changed but it is here in case anyone ever wants to enable id changes (whyever they would want to).
|
||||
*/
|
||||
@IsInt()
|
||||
id: number;
|
||||
|
||||
/**
|
||||
* Does the updated donor need a receipt?
|
||||
*/
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
receiptNeeded?: boolean;
|
||||
|
||||
|
||||
/**
|
||||
* Updates a provided Donor entity based on this.
|
||||
*/
|
||||
public async updateDonor(donor: Donor): Promise<Donor> {
|
||||
donor.firstname = this.firstname;
|
||||
donor.middlename = this.middlename;
|
||||
donor.lastname = this.lastname;
|
||||
donor.phone = this.phone;
|
||||
donor.email = this.email;
|
||||
donor.receiptNeeded = this.receiptNeeded;
|
||||
donor.address = await this.getAddress();
|
||||
|
||||
if (this.receiptNeeded == true && this.address == null) {
|
||||
throw new DonorReceiptAddressNeededError()
|
||||
}
|
||||
|
||||
return donor;
|
||||
}
|
||||
}
|
||||
68
src/models/actions/update/UpdatePermission.ts
Normal file
68
src/models/actions/update/UpdatePermission.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { IsInt, IsNotEmpty, IsObject } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { PermissionNeedsPrincipalError } from '../../errors/PermissionErrors';
|
||||
import { PrincipalNotFoundError, PrincipalWrongTypeError } from '../../errors/PrincipalErrors';
|
||||
import { Permission } from '../entities/Permission';
|
||||
import { Principal } from '../entities/Principal';
|
||||
import { PermissionAction } from '../enums/PermissionAction';
|
||||
import { PermissionTarget } from '../enums/PermissionTargets';
|
||||
|
||||
/**
|
||||
* This class is used to update a Permission entity (via put request).
|
||||
*/
|
||||
export class UpdatePermission {
|
||||
|
||||
/**
|
||||
* The updated permission's id.
|
||||
* This shouldn't have changed but it is here in case anyone ever wants to enable id changes (whyever they would want to).
|
||||
*/
|
||||
@IsInt()
|
||||
id: number;
|
||||
|
||||
/**
|
||||
* The updated permissions's principal.
|
||||
* Just has to contain the principal's id -everything else won't be checked or changed.
|
||||
*/
|
||||
@IsObject()
|
||||
@IsNotEmpty()
|
||||
principal: Principal;
|
||||
|
||||
/**
|
||||
* The permissions's target.
|
||||
*/
|
||||
@IsNotEmpty()
|
||||
target: PermissionTarget;
|
||||
|
||||
/**
|
||||
* The permissions's action.
|
||||
*/
|
||||
@IsNotEmpty()
|
||||
action: PermissionAction;
|
||||
|
||||
/**
|
||||
* Updates a provided Permission entity based on this.
|
||||
*/
|
||||
public async updatePermission(permission: Permission): Promise<Permission> {
|
||||
permission.principal = await this.getPrincipal();
|
||||
permission.target = this.target;
|
||||
permission.action = this.action;
|
||||
|
||||
return permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the updated permission's principal based on it's id.
|
||||
*/
|
||||
public async getPrincipal(): Promise<Principal> {
|
||||
if (this.principal === undefined || this.principal === null) {
|
||||
throw new PermissionNeedsPrincipalError();
|
||||
}
|
||||
if (!isNaN(this.principal.id)) {
|
||||
let principal = await getConnectionManager().get().getRepository(Principal).findOne({ id: this.principal.id });
|
||||
if (!principal) { throw new PrincipalNotFoundError(); }
|
||||
return principal;
|
||||
}
|
||||
|
||||
throw new PrincipalWrongTypeError();
|
||||
}
|
||||
}
|
||||
59
src/models/actions/update/UpdateRunner.ts
Normal file
59
src/models/actions/update/UpdateRunner.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
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';
|
||||
|
||||
/**
|
||||
* This class is used to update a Runner entity (via put request).
|
||||
*/
|
||||
export class UpdateRunner extends CreateParticipant {
|
||||
|
||||
/**
|
||||
* The updated runner's id.
|
||||
* This shouldn't have changed but it is here in case anyone ever wants to enable id changes (whyever they would want to).
|
||||
*/
|
||||
@IsInt()
|
||||
id: number;
|
||||
|
||||
/**
|
||||
* The updated runner's new team/org.
|
||||
* Just has to contain the group's id -everything else won't be checked or changed.
|
||||
*/
|
||||
@IsObject()
|
||||
group: RunnerGroup;
|
||||
|
||||
/**
|
||||
* Updates a provided Runner entity based on this.
|
||||
*/
|
||||
public async updateRunner(runner: Runner): Promise<Runner> {
|
||||
runner.firstname = this.firstname;
|
||||
runner.middlename = this.middlename;
|
||||
runner.lastname = this.lastname;
|
||||
runner.phone = this.phone;
|
||||
runner.email = this.email;
|
||||
runner.group = await this.getGroup();
|
||||
runner.address = await this.getAddress();
|
||||
|
||||
return runner;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the updated runner's group based on it's id.
|
||||
*/
|
||||
public async getGroup(): Promise<RunnerGroup> {
|
||||
if (this.group === undefined || this.group === null) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
51
src/models/actions/update/UpdateRunnerCard.ts
Normal file
51
src/models/actions/update/UpdateRunnerCard.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { IsBoolean, IsInt, IsOptional, IsPositive } from 'class-validator';
|
||||
import { getConnection } from 'typeorm';
|
||||
import { RunnerNotFoundError } from '../../errors/RunnerErrors';
|
||||
import { Runner } from '../entities/Runner';
|
||||
import { RunnerCard } from '../entities/RunnerCard';
|
||||
|
||||
/**
|
||||
* This class is used to update a RunnerCard entity (via put request).
|
||||
*/
|
||||
export class UpdateRunnerCard {
|
||||
/**
|
||||
* The updated card's id.
|
||||
* This shouldn't have changed but it is here in case anyone ever wants to enable id changes (whyever they would want to).
|
||||
*/
|
||||
@IsInt()
|
||||
@IsPositive()
|
||||
id?: number;
|
||||
|
||||
/**
|
||||
* The updated card's associated runner.
|
||||
*/
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
runner?: number;
|
||||
|
||||
/**
|
||||
* Is the updated card enabled (for fraud reasons)?
|
||||
* Default: true
|
||||
*/
|
||||
@IsBoolean()
|
||||
enabled: boolean = true;
|
||||
|
||||
/**
|
||||
* Creates a new RunnerCard entity from this.
|
||||
*/
|
||||
public async update(card: RunnerCard): Promise<RunnerCard> {
|
||||
card.enabled = this.enabled;
|
||||
card.runner = await this.getRunner();
|
||||
|
||||
return card;
|
||||
}
|
||||
|
||||
public async getRunner(): Promise<Runner> {
|
||||
if (!this.runner) { return null; }
|
||||
const runner = await getConnection().getRepository(Runner).findOne({ id: this.runner });
|
||||
if (!runner) {
|
||||
throw new RunnerNotFoundError();
|
||||
}
|
||||
return runner;
|
||||
}
|
||||
}
|
||||
52
src/models/actions/update/UpdateRunnerOrganisation.ts
Normal file
52
src/models/actions/update/UpdateRunnerOrganisation.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { IsInt, IsOptional } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { AddressNotFoundError } from '../../errors/AddressErrors';
|
||||
import { Address } from '../entities/Address';
|
||||
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
||||
import { CreateRunnerGroup } from './CreateRunnerGroup';
|
||||
|
||||
/**
|
||||
* This class is used to update a RunnerOrganisation entity (via put request).
|
||||
*/
|
||||
export class UpdateRunnerOrganisation extends CreateRunnerGroup {
|
||||
|
||||
/**
|
||||
* The updated orgs's id.
|
||||
* This shouldn't have changed but it is here in case anyone ever wants to enable id changes (whyever they would want to).
|
||||
*/
|
||||
@IsInt()
|
||||
id: number;
|
||||
|
||||
/**
|
||||
* The updated organisation's address.
|
||||
* Just has to contain the address's id - everything else won't be checked or changed.
|
||||
* Optional.
|
||||
*/
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
address?: Address;
|
||||
|
||||
/**
|
||||
* Loads the organisation's address based on it's id.
|
||||
*/
|
||||
public async getAddress(): Promise<Address> {
|
||||
if (this.address === undefined || this.address === null) {
|
||||
return null;
|
||||
}
|
||||
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address.id });
|
||||
if (!address) { throw new AddressNotFoundError; }
|
||||
return address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a provided RunnerOrganisation entity based on this.
|
||||
*/
|
||||
public async updateRunnerOrganisation(organisation: RunnerOrganisation): Promise<RunnerOrganisation> {
|
||||
|
||||
organisation.name = this.name;
|
||||
organisation.contact = await this.getContact();
|
||||
// organisation.address = await this.getAddress();
|
||||
|
||||
return organisation;
|
||||
}
|
||||
}
|
||||
56
src/models/actions/update/UpdateRunnerTeam.ts
Normal file
56
src/models/actions/update/UpdateRunnerTeam.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { IsInt, IsNotEmpty, IsObject } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { RunnerOrganisationNotFoundError, RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors';
|
||||
import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors';
|
||||
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
||||
import { RunnerTeam } from '../entities/RunnerTeam';
|
||||
import { CreateRunnerGroup } from './CreateRunnerGroup';
|
||||
|
||||
/**
|
||||
* This class is used to update a RunnerTeam entity (via put request).
|
||||
*/
|
||||
export class UpdateRunnerTeam extends CreateRunnerGroup {
|
||||
|
||||
/**
|
||||
* The updated team's id.
|
||||
* This shouldn't have changed but it is here in case anyone ever wants to enable id changes (whyever they would want to).
|
||||
*/
|
||||
@IsInt()
|
||||
id: number;
|
||||
|
||||
/**
|
||||
* The updated team's parentGroup.
|
||||
* Just has to contain the organisation's id - everything else won't be checked or changed.
|
||||
*/
|
||||
@IsObject()
|
||||
@IsNotEmpty()
|
||||
parentGroup: RunnerOrganisation;
|
||||
|
||||
/**
|
||||
* Loads the updated teams's parentGroup based on it's id.
|
||||
*/
|
||||
public async getParent(): Promise<RunnerOrganisation> {
|
||||
if (this.parentGroup === undefined || this.parentGroup === null) {
|
||||
throw new RunnerTeamNeedsParentError();
|
||||
}
|
||||
if (!isNaN(this.parentGroup.id)) {
|
||||
let parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentGroup.id });
|
||||
if (!parentGroup) { throw new RunnerOrganisationNotFoundError();; }
|
||||
return parentGroup;
|
||||
}
|
||||
|
||||
throw new RunnerOrganisationWrongTypeError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a provided RunnerTeam entity based on this.
|
||||
*/
|
||||
public async updateRunnerTeam(team: RunnerTeam): Promise<RunnerTeam> {
|
||||
|
||||
team.name = this.name;
|
||||
team.parentGroup = await this.getParent();
|
||||
team.contact = await this.getContact()
|
||||
|
||||
return team;
|
||||
}
|
||||
}
|
||||
62
src/models/actions/update/UpdateScan.ts
Normal file
62
src/models/actions/update/UpdateScan.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { IsBoolean, IsInt, IsOptional, IsPositive } from 'class-validator';
|
||||
import { getConnection } from 'typeorm';
|
||||
import { RunnerNotFoundError } from '../../errors/RunnerErrors';
|
||||
import { Runner } from '../entities/Runner';
|
||||
import { Scan } from '../entities/Scan';
|
||||
|
||||
/**
|
||||
* This class is used to update a Scan entity (via put request)
|
||||
*/
|
||||
export abstract class UpdateScan {
|
||||
/**
|
||||
* The updated scan's id.
|
||||
* This shouldn't have changed but it is here in case anyone ever wants to enable id changes (whyever they would want to).
|
||||
*/
|
||||
@IsInt()
|
||||
id: number;
|
||||
|
||||
/**
|
||||
* The updated scan's associated runner.
|
||||
* This is important to link ran distances to runners.
|
||||
*/
|
||||
@IsInt()
|
||||
@IsPositive()
|
||||
runner: number;
|
||||
|
||||
/**
|
||||
* Is the updated scan valid (for fraud reasons).
|
||||
*/
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
valid?: boolean = true;
|
||||
|
||||
/**
|
||||
* The updated scan's distance in meters.
|
||||
*/
|
||||
@IsInt()
|
||||
@IsPositive()
|
||||
public distance: number;
|
||||
|
||||
/**
|
||||
* Update a Scan entity based on this.
|
||||
* @param scan The scan that shall be updated.
|
||||
*/
|
||||
public async updateScan(scan: Scan): Promise<Scan> {
|
||||
scan.distance = this.distance;
|
||||
scan.valid = this.valid;
|
||||
scan.runner = await this.getRunner();
|
||||
|
||||
return scan;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a runner based on the runner id provided via this.runner.
|
||||
*/
|
||||
public async getRunner(): Promise<Runner> {
|
||||
const runner = await getConnection().getRepository(Runner).findOne({ id: this.runner });
|
||||
if (!runner) {
|
||||
throw new RunnerNotFoundError();
|
||||
}
|
||||
return runner;
|
||||
}
|
||||
}
|
||||
39
src/models/actions/update/UpdateScanStation.ts
Normal file
39
src/models/actions/update/UpdateScanStation.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { IsBoolean, IsInt, IsOptional, IsString } from 'class-validator';
|
||||
import { ScanStation } from '../entities/ScanStation';
|
||||
|
||||
/**
|
||||
* This class is used to update a ScanStation entity (via put request)
|
||||
*/
|
||||
export class UpdateScanStation {
|
||||
/**
|
||||
* The updated station's id.
|
||||
* This shouldn't have changed but it is here in case anyone ever wants to enable id changes (whyever they would want to).
|
||||
*/
|
||||
@IsInt()
|
||||
id: number;
|
||||
|
||||
/**
|
||||
* The updated station's description.
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
description?: string;
|
||||
|
||||
/**
|
||||
* Is this station enabled?
|
||||
*/
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
enabled?: boolean = true;
|
||||
|
||||
/**
|
||||
* Update a ScanStation entity based on this.
|
||||
* @param station The station that shall be updated.
|
||||
*/
|
||||
public async updateStation(station: ScanStation): Promise<ScanStation> {
|
||||
station.description = this.description;
|
||||
station.enabled = this.enabled;
|
||||
|
||||
return station;
|
||||
}
|
||||
}
|
||||
50
src/models/actions/update/UpdateTrack.ts
Normal file
50
src/models/actions/update/UpdateTrack.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { IsInt, IsNotEmpty, IsOptional, IsPositive, IsString } from 'class-validator';
|
||||
import { TrackLapTimeCantBeNegativeError } from '../../errors/TrackErrors';
|
||||
import { Track } from '../entities/Track';
|
||||
|
||||
/**
|
||||
* This class is used to update a Track entity (via put request).
|
||||
*/
|
||||
export class UpdateTrack {
|
||||
/**
|
||||
* The updated track's id.
|
||||
* This shouldn't have changed but it is here in case anyone ever wants to enable id changes (whyever they would want to).
|
||||
*/
|
||||
@IsInt()
|
||||
id: number;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* The updated track's distance in meters (must be greater than 0).
|
||||
*/
|
||||
@IsInt()
|
||||
@IsPositive()
|
||||
distance: number;
|
||||
|
||||
/**
|
||||
* The minimum time a runner should take to run a lap on this track (in seconds).
|
||||
* Will be used for fraud detection.
|
||||
*/
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
minimumLapTime: number;
|
||||
|
||||
|
||||
/**
|
||||
* Update a Track entity based on this.
|
||||
* @param track The track that shall be updated.
|
||||
*/
|
||||
public updateTrack(track: Track): Track {
|
||||
track.name = this.name;
|
||||
track.distance = this.distance;
|
||||
track.minimumLapTime = this.minimumLapTime;
|
||||
if (this.minimumLapTime < 0) {
|
||||
throw new TrackLapTimeCantBeNegativeError();
|
||||
}
|
||||
|
||||
return track;
|
||||
}
|
||||
}
|
||||
81
src/models/actions/update/UpdateTrackScan.ts
Normal file
81
src/models/actions/update/UpdateTrackScan.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { IsBoolean, IsInt, IsOptional } from 'class-validator';
|
||||
import { getConnection } from 'typeorm';
|
||||
import { RunnerNotFoundError } from '../../errors/RunnerErrors';
|
||||
import { ScanStationNotFoundError } from '../../errors/ScanStationErrors';
|
||||
import { Runner } from '../entities/Runner';
|
||||
import { ScanStation } from '../entities/ScanStation';
|
||||
import { TrackScan } from '../entities/TrackScan';
|
||||
|
||||
/**
|
||||
* This class is used to update a TrackScan entity (via put request)
|
||||
*/
|
||||
export abstract class UpdateTrackScan {
|
||||
/**
|
||||
* The updated scan's id.
|
||||
* This shouldn't have changed but it is here in case anyone ever wants to enable id changes (whyever they would want to).
|
||||
*/
|
||||
@IsInt()
|
||||
id: number;
|
||||
|
||||
/**
|
||||
* The updated scan's associated runner.
|
||||
* This is important to link ran distances to runners.
|
||||
*/
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
runner?: number;
|
||||
|
||||
/**
|
||||
* Is the updated scan valid (for fraud reasons).
|
||||
*/
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
valid?: boolean = true;
|
||||
|
||||
/**
|
||||
* The updated scan's associated station.
|
||||
* This is important to link ran distances to runners.
|
||||
*/
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
public station?: number;
|
||||
|
||||
/**
|
||||
* Update a TrackScan entity based on this.
|
||||
* @param scan The scan that shall be updated.
|
||||
*/
|
||||
public async updateScan(scan: TrackScan): Promise<TrackScan> {
|
||||
scan.valid = this.valid;
|
||||
if (this.runner) {
|
||||
scan.runner = await this.getRunner();
|
||||
}
|
||||
if (this.station) {
|
||||
scan.station = await this.getStation();
|
||||
}
|
||||
scan.track = scan.station.track;
|
||||
|
||||
return scan;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a runner based on the runner id provided via this.runner.
|
||||
*/
|
||||
public async getRunner(): Promise<Runner> {
|
||||
const runner = await getConnection().getRepository(Runner).findOne({ id: this.runner });
|
||||
if (!runner) {
|
||||
throw new RunnerNotFoundError();
|
||||
}
|
||||
return runner;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a runner based on the runner id provided via this.runner.
|
||||
*/
|
||||
public async getStation(): Promise<ScanStation> {
|
||||
const station = await getConnection().getRepository(ScanStation).findOne({ id: this.station });
|
||||
if (!station) {
|
||||
throw new ScanStationNotFoundError();
|
||||
}
|
||||
return station;
|
||||
}
|
||||
}
|
||||
141
src/models/actions/update/UpdateUser.ts
Normal file
141
src/models/actions/update/UpdateUser.ts
Normal file
@@ -0,0 +1,141 @@
|
||||
import * as argon2 from "argon2";
|
||||
import { IsBoolean, IsEmail, IsInt, IsOptional, IsPhoneNumber, IsString, IsUrl } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { config } from '../../config';
|
||||
import { UsernameOrEmailNeededError } from '../../errors/AuthError';
|
||||
import { UserGroupNotFoundError } from '../../errors/UserGroupErrors';
|
||||
import { User } from '../entities/User';
|
||||
import { UserGroup } from '../entities/UserGroup';
|
||||
|
||||
/**
|
||||
* This class is used to update a User entity (via put request).
|
||||
*/
|
||||
export class UpdateUser {
|
||||
|
||||
/**
|
||||
* The updated user's id.
|
||||
* This shouldn't have changed but it is here in case anyone ever wants to enable id changes (whyever they would want to).
|
||||
*/
|
||||
@IsInt()
|
||||
id: number;
|
||||
|
||||
/**
|
||||
* The updated user's first name.
|
||||
*/
|
||||
@IsString()
|
||||
firstname: string;
|
||||
|
||||
/**
|
||||
* The updated user's middle name.
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
middlename?: string;
|
||||
|
||||
/**
|
||||
* The updated user's last name.
|
||||
*/
|
||||
@IsString()
|
||||
lastname: string;
|
||||
|
||||
/**
|
||||
* The updated user's username.
|
||||
* You have to provide at least one of: {email, username}.
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
username?: string;
|
||||
|
||||
/**
|
||||
* The updated user's email address.
|
||||
* You have to provide at least one of: {email, username}.
|
||||
*/
|
||||
@IsEmail()
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
email?: string;
|
||||
|
||||
/**
|
||||
* The updated user's phone number.
|
||||
* This will be validated against the configured country phone numer syntax (default: international).
|
||||
*/
|
||||
@IsPhoneNumber(config.phone_validation_countrycode)
|
||||
@IsOptional()
|
||||
phone?: string;
|
||||
|
||||
/**
|
||||
* The new updated's password.
|
||||
* Only provide it if you want it updated.
|
||||
* Changeing the password will invalidate all of the user's jwts.
|
||||
* This will of course not be saved in plaintext :)
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
password?: string;
|
||||
|
||||
/**
|
||||
* Should the user be enabled?
|
||||
*/
|
||||
@IsBoolean()
|
||||
enabled: boolean = true;
|
||||
|
||||
/**
|
||||
* The updated user's groups.
|
||||
* This just has to contain the group's id - everything else won't be changed.
|
||||
*/
|
||||
@IsOptional()
|
||||
groups?: UserGroup[]
|
||||
|
||||
/**
|
||||
* The user's profile pic (or rather a url pointing to it).
|
||||
*/
|
||||
@IsString()
|
||||
@IsUrl()
|
||||
@IsOptional()
|
||||
profilePic?: string;
|
||||
|
||||
/**
|
||||
* Updates a user entity based on this.
|
||||
* @param user The user that shall be updated.
|
||||
*/
|
||||
public async updateUser(user: User): Promise<User> {
|
||||
user.email = this.email;
|
||||
user.username = this.username;
|
||||
if ((user.email === undefined || user.email === null) && (user.username === undefined || user.username === null)) {
|
||||
throw new UsernameOrEmailNeededError();
|
||||
}
|
||||
if (this.password) {
|
||||
user.password = await argon2.hash(this.password + user.uuid);
|
||||
user.refreshTokenCount = user.refreshTokenCount + 1;
|
||||
}
|
||||
|
||||
user.enabled = this.enabled;
|
||||
user.firstname = this.firstname
|
||||
user.middlename = this.middlename
|
||||
user.lastname = this.lastname
|
||||
user.phone = this.phone;
|
||||
user.groups = await this.getGroups();
|
||||
|
||||
if (!this.profilePic) { user.profilePic = `https://dev.lauf-fuer-kaya.de/lfk-logo.png`; }
|
||||
else { user.profilePic = this.profilePic; }
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the updated user's groups based on their ids.
|
||||
*/
|
||||
public async getGroups() {
|
||||
if (!this.groups) { return null; }
|
||||
let groups = new Array<UserGroup>();
|
||||
if (!Array.isArray(this.groups)) {
|
||||
this.groups = [this.groups]
|
||||
}
|
||||
for (let group of this.groups) {
|
||||
let found = await getConnectionManager().get().getRepository(UserGroup).findOne({ id: group.id });
|
||||
if (!found) { throw new UserGroupNotFoundError(); }
|
||||
groups.push(found);
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user