From d2c826c7c9a8b79ff5ab6268865a26791b36c2c2 Mon Sep 17 00:00:00 2001 From: Philipp Dormann Date: Thu, 3 Dec 2020 18:33:20 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20CreateUser=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ref #14 --- src/models/CreateUser.ts | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/models/CreateUser.ts diff --git a/src/models/CreateUser.ts b/src/models/CreateUser.ts new file mode 100644 index 0000000..23cd934 --- /dev/null +++ b/src/models/CreateUser.ts @@ -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 { + 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; + } +} \ No newline at end of file