Add mutations setUsername and setHandle

Closes #70
pull/4/head
trivernis 5 years ago
parent 5f15d4b3ba
commit 26ecb3b0cf

@ -0,0 +1,10 @@
import {BaseError} from "./BaseError";
/**
* An error that is thrown when the user changes the handle to a value that is already used by a different user
*/
export class HandleInUseError extends BaseError {
constructor(handle: string) {
super(`A different user is already using the handle '${handle}'!`);
}
}

@ -4,6 +4,7 @@ import isEmail from "validator/lib/isEmail";
import dataAccess from "../../lib/dataAccess";
import {BlacklistedError} from "../../lib/errors/BlacklistedError";
import {GroupNotFoundError} from "../../lib/errors/GroupNotFoundError";
import {HandleInUseError} from "../../lib/errors/HandleInUseError";
import {InvalidEmailError} from "../../lib/errors/InvalidEmailError";
import {NotAGroupAdminError} from "../../lib/errors/NotAGroupAdminError";
import {NotAnAdminError} from "../../lib/errors/NotAnAdminError";
@ -98,6 +99,36 @@ export class MutationResolver extends BaseResolver {
return user;
}
/**
* Sets a new username for a user
* @param username
* @param request
*/
public async setUsername({username}: {username: string}, request: any): Promise<User> {
this.ensureLoggedIn(request);
const user = await User.findByPk(request.session.userId);
user.username = username;
await user.save();
return user;
}
/**
* Sets a new handle for the user. If the handle is alredy used by a different user, an error is returned
* @param handle
* @param request
*/
public async setHandle({handle}: {handle: string}, request: any): Promise<User> {
this.ensureLoggedIn(request);
const user = await User.findByPk(request.session.userId);
const handleAvailable = !(await User.findOne({where: {handle}}));
if (!handleAvailable) {
throw new HandleInUseError(handle);
}
user.handle = handle;
await user.save();
return user;
}
/**
* Sets the frontend settings for the logged in user
* @param settings

@ -60,6 +60,12 @@ type Mutation {
"Sets the user settings to the specified settings string. The settings parameter should be a valid yaml."
setUserSettings(settings: String!): String!
"Sets a new username"
setUsername(username: String!): Profile!
"Sets a new handle"
setHandle(handle: String!): Profile!
"Logout of the user."
logout: Boolean

Loading…
Cancel
Save