Pulled out some linguini-esc code

ref #6
This commit is contained in:
Nicolai Ort 2020-12-18 14:40:19 +01:00
parent eb9473e230
commit 5dc9edfe40
1 changed files with 26 additions and 24 deletions

View File

@ -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<UserGroup>();
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();
}
}
}