🚧 CreateUser model

ref #14
This commit is contained in:
Philipp Dormann 2020-12-03 18:33:20 +01:00
parent 34d4ebc7cb
commit d2c826c7c9
1 changed files with 53 additions and 0 deletions

53
src/models/CreateUser.ts Normal file
View File

@ -0,0 +1,53 @@
import { IsInt, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
import { User } from '../models/User';
import { getConnectionManager } from 'typeorm';
import { UserGroupNotFoundError, UsernameOrEmailNeededError } from '../errors/CreateUserErrors';
import { UserGroup } from './UserGroup';
export class CreateUser {
@IsString()
firstname: string;
@IsString()
middlename?: string;
@IsOptional()
@IsString()
username?: string;
@IsPhoneNumber("ZZ")
@IsOptional()
phone?: string;
@IsString()
password: string;
@IsString()
lastname: string;
@IsString()
email?: string;
@IsInt()
@IsOptional()
groupId?: number[] | number
public async toUser(): Promise<User> {
let newUser: User = new User();
if (this.email === undefined && this.username === undefined) {
throw new UsernameOrEmailNeededError();
}
if (this.groupId) {
// TODO: link user groups
// newUser.groups = await getConnectionManager().get().getRepository(UserGroup).findOne({ id: this.teamId });
} else {
throw new UserGroupNotFoundError();
}
newUser.email = this.email
newUser.username = this.username
newUser.firstname = this.firstname
newUser.middlename = this.middlename
newUser.lastname = this.lastname
// TODO: hash password here or in controller/ in User model via setter?
newUser.password = this.password
console.log(newUser)
return newUser;
}
}