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