From 928485c336f7b624b414c58ce76690bb8adf6ee4 Mon Sep 17 00:00:00 2001 From: trivernis Date: Fri, 24 Jan 2020 16:17:26 +0100 Subject: [PATCH] Add mutations setUsername and setHandle Closes #70 --- src/lib/errors/HandleInUseError.ts | 10 +++++++++ src/routes/graphql/MutationResolver.ts | 31 ++++++++++++++++++++++++++ src/routes/graphql/schema.graphql | 6 +++++ 3 files changed, 47 insertions(+) create mode 100644 src/lib/errors/HandleInUseError.ts diff --git a/src/lib/errors/HandleInUseError.ts b/src/lib/errors/HandleInUseError.ts new file mode 100644 index 0000000..8eff834 --- /dev/null +++ b/src/lib/errors/HandleInUseError.ts @@ -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}'!`); + } +} diff --git a/src/routes/graphql/MutationResolver.ts b/src/routes/graphql/MutationResolver.ts index aea1be2..85c3823 100644 --- a/src/routes/graphql/MutationResolver.ts +++ b/src/routes/graphql/MutationResolver.ts @@ -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 { + 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 { + 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 diff --git a/src/routes/graphql/schema.graphql b/src/routes/graphql/schema.graphql index 5ed3592..abc7fa1 100644 --- a/src/routes/graphql/schema.graphql +++ b/src/routes/graphql/schema.graphql @@ -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