parent
34d4ebc7cb
commit
d2c826c7c9
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue