📏 fit to new structure

This commit is contained in:
2020-12-04 17:10:00 +01:00
parent acd9a691f8
commit ae24c3394b

View File

@@ -0,0 +1,59 @@
import * as argon2 from "argon2";
import { IsInt, IsOptional, IsPhoneNumber, IsString, IsUUID } from 'class-validator';
import * as uuid from 'uuid';
import { UserGroupNotFoundError, UsernameOrEmailNeededError } from '../../errors/CreateUserErrors';
import { User } from '../entities/User';
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
@IsUUID("5")
uuid: string;
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();
}
const new_uuid = uuid.v4()
newUser.email = this.email
newUser.username = this.username
newUser.firstname = this.firstname
newUser.middlename = this.middlename
newUser.lastname = this.lastname
newUser.uuid = new_uuid
// TODO: hash password here or in controller/ in User model via setter?
this.password = await argon2.hash(this.password);
newUser.password = this.password
console.log(newUser)
return newUser;
}
}