From 5b7f3ae12f3f8749aa637d18763cf5f5df8001eb Mon Sep 17 00:00:00 2001 From: Philipp Dormann Date: Fri, 4 Dec 2020 18:02:28 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20CreateUser=20group=20search=20+?= =?UTF-8?q?=20adding?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ref #14 --- src/models/creation/CreateUser.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/models/creation/CreateUser.ts b/src/models/creation/CreateUser.ts index 102919a..9595039 100644 --- a/src/models/creation/CreateUser.ts +++ b/src/models/creation/CreateUser.ts @@ -2,7 +2,7 @@ import * as argon2 from "argon2"; import { IsEmail, IsOptional, IsPhoneNumber, IsString, IsUUID } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import * as uuid from 'uuid'; -import { UsernameOrEmailNeededError } from '../../errors/UserErrors'; +import { UserGroupNotFoundError, UsernameOrEmailNeededError } from '../../errors/UserErrors'; import { User } from '../entities/User'; import { UserGroup } from '../entities/UserGroup'; @@ -24,8 +24,6 @@ export class CreateUser { @IsEmail() @IsString() email?: string; - // @IsArray() - // @IsInt() @IsOptional() groupId?: number[] | number @IsUUID("4") @@ -39,14 +37,20 @@ export class CreateUser { } if (this.groupId) { - // TODO: link user groups if (Array.isArray(this.groupId)) { - + let found_groups = [] + this.groupId.forEach(async (g) => { + const foundGroup = await getConnectionManager().get().getRepository(UserGroup).find({ id: g }); + if (foundGroup) { + found_groups.push(foundGroup) + } else { + throw new UserGroupNotFoundError(); + } + }); + newUser.groups = found_groups } else { newUser.groups = await getConnectionManager().get().getRepository(UserGroup).find({ id: this.groupId }); } - } else { - // throw new UserGroupNotFoundError(); } const new_uuid = uuid.v4()