From 5dc9edfe40fe0898946861430bda15769e67fdb8 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Fri, 18 Dec 2020 14:40:19 +0100 Subject: [PATCH] Pulled out some linguini-esc code ref #6 --- src/models/actions/CreateUser.ts | 50 +++++++++++++++++--------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/src/models/actions/CreateUser.ts b/src/models/actions/CreateUser.ts index d51db4e..78fee8b 100644 --- a/src/models/actions/CreateUser.ts +++ b/src/models/actions/CreateUser.ts @@ -81,30 +81,6 @@ export class CreateUser { throw new UsernameOrEmailNeededError(); } - if (this.group) { - if (!Array.isArray(this.group)) { - this.group = [this.group] - } - const groupIDs: number[] = this.group - let errors = 0 - const validateusergroups = async () => { - let foundgroups = [] - for (const g of groupIDs) { - const found = await getConnectionManager().get().getRepository(UserGroup).find({ id: g }); - if (found.length === 0) { - errors++ - } else { - foundgroups.push(found[0]) - } - } - newUser.groups = foundgroups - } - await validateusergroups() - if (errors !== 0) { - throw new UserGroupNotFoundError(); - } - } - newUser.email = this.email newUser.username = this.username newUser.firstname = this.firstname @@ -113,8 +89,34 @@ export class CreateUser { newUser.uuid = uuid.v4() newUser.phone = this.phone newUser.password = await argon2.hash(this.password + newUser.uuid); + newUser.groups = await this.getGroups(); //TODO: ProfilePics return newUser; } + + public async getGroups() { + if (!this.group) { return null; } + let groups = new Array(); + if (!Array.isArray(this.group)) { + this.group = [this.group] + } + const groupIDs: number[] = this.group + let errors = 0 + const validateusergroups = async () => { + for (const g of groupIDs) { + const found = await getConnectionManager().get().getRepository(UserGroup).find({ id: g }); + if (found.length === 0) { + errors++ + } else { + groups.push(found[0]) + } + } + return groups; + } + await validateusergroups() + if (errors !== 0) { + throw new UserGroupNotFoundError(); + } + } } \ No newline at end of file