diff --git a/backend/src/graphql/inputs/LoginUserInput.ts b/backend/src/graphql/inputs/LoginUserInput.ts index 68ddad628..1f786702f 100644 --- a/backend/src/graphql/inputs/LoginUserInput.ts +++ b/backend/src/graphql/inputs/LoginUserInput.ts @@ -82,22 +82,22 @@ export class UpdateUserInfosArgs { @Field(() => String) email!: string - @Field(() => String) + @Field({ nullable: true }) firstName?: string - @Field(() => String) + @Field({ nullable: true }) lastName?: string - @Field(() => String) + @Field({ nullable: true }) username?: string - @Field(() => String) + @Field({ nullable: true }) language?: string - @Field(() => String) + @Field({ nullable: true }) password?: string - @Field(() => String) + @Field({ nullable: true }) passwordNew?: string } diff --git a/backend/src/graphql/models/BaseResponse.ts b/backend/src/graphql/models/BaseResponse.ts deleted file mode 100644 index 712660bd3..000000000 --- a/backend/src/graphql/models/BaseResponse.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Entity, BaseEntity } from 'typeorm' -import { ObjectType, Field } from 'type-graphql' - -@Entity() -@ObjectType() -export class BaseResponse extends BaseEntity { - @Field(() => Boolean) - success: boolean -} diff --git a/backend/src/graphql/models/ChangePasswordResponse.ts b/backend/src/graphql/models/ChangePasswordResponse.ts deleted file mode 100644 index 1ba3247cd..000000000 --- a/backend/src/graphql/models/ChangePasswordResponse.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Entity, BaseEntity } from 'typeorm' -import { ObjectType, Field } from 'type-graphql' - -@Entity() -@ObjectType() -export class ChangePasswordResponse extends BaseEntity { - @Field(() => String) - state: string -} diff --git a/backend/src/graphql/models/CheckUsernameResponse.ts b/backend/src/graphql/models/CheckUsernameResponse.ts index ac51b64e1..de5724c8d 100644 --- a/backend/src/graphql/models/CheckUsernameResponse.ts +++ b/backend/src/graphql/models/CheckUsernameResponse.ts @@ -1,9 +1,18 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { Entity, BaseEntity } from 'typeorm' import { ObjectType, Field } from 'type-graphql' @Entity() @ObjectType() export class CheckUsernameResponse extends BaseEntity { + constructor(json: any) { + super() + this.state = json.state + this.msg = json.msg + this.groupId = json.group_id + } + @Field(() => String) state: string diff --git a/backend/src/graphql/models/CreateResponse.ts b/backend/src/graphql/models/CreateResponse.ts index 54276b031..144d6456c 100644 --- a/backend/src/graphql/models/CreateResponse.ts +++ b/backend/src/graphql/models/CreateResponse.ts @@ -1,3 +1,5 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { Entity, BaseEntity } from 'typeorm' import { ObjectType, Field } from 'type-graphql' diff --git a/backend/src/graphql/models/ResetPasswordResponse.ts b/backend/src/graphql/models/ResetPasswordResponse.ts new file mode 100644 index 000000000..aae209059 --- /dev/null +++ b/backend/src/graphql/models/ResetPasswordResponse.ts @@ -0,0 +1,16 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ +import { Entity, BaseEntity } from 'typeorm' +import { ObjectType, Field } from 'type-graphql' + +@Entity() +@ObjectType() +export class ResetPasswordResponse extends BaseEntity { + constructor(json: any) { + super() + this.state = json.state + } + + @Field(() => String) + state: string +} diff --git a/backend/src/graphql/models/SendEmailResponse.ts b/backend/src/graphql/models/SendPasswordResetEmailResponse.ts similarity index 60% rename from backend/src/graphql/models/SendEmailResponse.ts rename to backend/src/graphql/models/SendPasswordResetEmailResponse.ts index d8e163478..f426d5245 100644 --- a/backend/src/graphql/models/SendEmailResponse.ts +++ b/backend/src/graphql/models/SendPasswordResetEmailResponse.ts @@ -1,9 +1,11 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { Entity, BaseEntity } from 'typeorm' import { ObjectType, Field } from 'type-graphql' @Entity() @ObjectType() -export class SendEmailResponse extends BaseEntity { +export class SendPasswordResetEmailResponse extends BaseEntity { constructor(json: any) { super() this.state = json.state diff --git a/backend/src/graphql/models/Server.ts b/backend/src/graphql/models/Server.ts index e722a2c97..1b6f01627 100644 --- a/backend/src/graphql/models/Server.ts +++ b/backend/src/graphql/models/Server.ts @@ -1,3 +1,5 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { Entity, BaseEntity } from 'typeorm' import { ObjectType, Field } from 'type-graphql' diff --git a/backend/src/graphql/models/UpdateUserInfosResponse.ts b/backend/src/graphql/models/UpdateUserInfosResponse.ts index 14f834932..820ee0011 100644 --- a/backend/src/graphql/models/UpdateUserInfosResponse.ts +++ b/backend/src/graphql/models/UpdateUserInfosResponse.ts @@ -1,9 +1,18 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { Entity, BaseEntity } from 'typeorm' import { ObjectType, Field } from 'type-graphql' @Entity() @ObjectType() export class UpdateUserInfosResponse extends BaseEntity { + constructor(json: any) { + super() + this.state = json.state + this.validValues = json.valid_values + this.errors = json.errors + } + @Field(() => String) state: string diff --git a/backend/src/graphql/models/UserInfoData.ts b/backend/src/graphql/models/UserInfoData.ts index 46b336d5e..2e65aca82 100644 --- a/backend/src/graphql/models/UserInfoData.ts +++ b/backend/src/graphql/models/UserInfoData.ts @@ -1,3 +1,5 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { Entity, BaseEntity } from 'typeorm' import { ObjectType, Field } from 'type-graphql' import { Server } from './Server' diff --git a/backend/src/graphql/resolvers/UserResolver.ts b/backend/src/graphql/resolvers/UserResolver.ts index d22dd49ac..5dc93f7bd 100644 --- a/backend/src/graphql/resolvers/UserResolver.ts +++ b/backend/src/graphql/resolvers/UserResolver.ts @@ -1,13 +1,13 @@ // import jwt from 'jsonwebtoken' -import { Resolver, Query, /* Mutation, */ Args, Arg } from 'type-graphql' +import { Resolver, Query, /* Mutation, */ Args, Arg, Field } from 'type-graphql' import CONFIG from '../../config' -import { ChangePasswordResponse } from '../models/ChangePasswordResponse' +import { ResetPasswordResponse } from '../models/ResetPasswordResponse' import { CheckUsernameResponse } from '../models/CheckUsernameResponse' import { CreateResponse } from '../models/CreateResponse' import { GetUserInfoResponse } from '../models/UserInfoData' import { LoginResponse } from '../models/LoginResponse' import { LoginViaVerificationCode } from '../models/LoginViaVerificationCode' -import { SendEmailResponse } from '../models/SendEmailResponse' +import { SendPasswordResetEmailResponse } from '../models/SendPasswordResetEmailResponse' import { UpdateUserInfosResponse } from '../models/UpdateUserInfosResponse' import { ChangePasswordArgs, @@ -66,17 +66,16 @@ export class UserResolver { return new LoginViaVerificationCode(result.data) } - /* @Query(() => LogoutResponse) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - async logout(@Arg('sessionId') sessionId: number): Promise { + @Query(() => String) + async logout(@Arg('sessionId') sessionId: number): Promise { const payload = { session_id: sessionId } - const result = apiPost(CONFIG.LOGIN_API_URL + 'logout', payload) + const result = await apiPost(CONFIG.LOGIN_API_URL + 'logout', payload) if (!result.success) { throw new Error(result.data) } - return result.data + return 'success' } -*/ + @Query(() => CreateResponse) async create( @Args() { email, firstName, lastName, password }: CreateUserArgs, @@ -96,17 +95,20 @@ export class UserResolver { return new CreateResponse(result.data) } - @Query(() => SendEmailResponse) - async sendEmail( + // TODO + @Query(() => SendPasswordResetEmailResponse) + async sendResetPasswordEmail( @Args() { email, emailText = 7, emailVerificationCodeType = 'resetPassword' }: SendEmailArgs, - ): Promise { + ): Promise { const payload = { email, email_text: emailText, email_verification_code_type: emailVerificationCodeType, } - return apiPost(CONFIG.LOGIN_API_URL + 'sendEmail', payload) + const response = await apiPost(CONFIG.LOGIN_API_URL + 'sendEmail', payload) + if (!response.success) throw new Error(response.data) + return new SendPasswordResetEmailResponse(response.data) } @Query(() => GetUserInfoResponse) @@ -119,16 +121,19 @@ export class UserResolver { return apiPost(CONFIG.LOGIN_API_URL + 'getUserInfos', payload) } - @Query(() => ChangePasswordResponse) - async changePassword(@Args() { sessionId, email, password }: ChangePasswordArgs): Promise { + @Query(() => ResetPasswordResponse) + async resetPassword( + @Args() + { sessionId, email, password }: ChangePasswordArgs, + ): Promise { const payload = { session_id: sessionId, email, password, } const result = await apiPost(CONFIG.LOGIN_API_URL + 'resetPassword', payload) - if (result.success) return result.result.data.state - return result.result + if (!result.success) throw new Error(result.data) + return new ResetPasswordResponse(result.data) } @Query(() => UpdateUserInfosResponse) @@ -137,32 +142,40 @@ export class UserResolver { { sessionId, email, - firstName, - lastName, - username, - language, - password, - passwordNew, + firstName = '', + lastName = '', + username = '', + language = '', + password = '', + passwordNew = '', }: UpdateUserInfosArgs, - ): Promise { + ): Promise { const payload = { session_id: sessionId, email, update: { - 'User.first_name': firstName, - 'User.last_name': lastName, - // 'User.description': data.description, - 'User.username': username, - 'User.language': language, - 'User.password_old': password, - 'User.password': passwordNew, + 'User.first_name': firstName.length > 0 ? firstName : undefined, + 'User.last_name': lastName.length > 0 ? lastName : undefined, + 'User.username': username.length > 0 ? username : undefined, + 'User.language': language.length > 0 ? language : undefined, + 'User.password': passwordNew.length > 0 ? passwordNew : undefined, + 'User.password_old': password.length > 0 ? password : undefined, }, } - return apiPost(CONFIG.LOGIN_API_URL + 'updateUserInfos', payload) + const result = await apiPost(CONFIG.LOGIN_API_URL + 'updateUserInfos', payload) + if (!result.success) throw new Error(result.data) + return new UpdateUserInfosResponse(result.data) } + // TODO @Query(() => CheckUsernameResponse) - async checkUsername(@Args() { username, groupId = 1 }: CheckUsernameArgs): Promise { - return apiGet(CONFIG.LOGIN_API_URL + `checkUsername?username=${username}&group_id=${groupId}`) + async checkUsername( + @Args() { username, groupId = 1 }: CheckUsernameArgs, + ): Promise { + const response = await apiGet( + CONFIG.LOGIN_API_URL + `checkUsername?username=${username}&group_id=${groupId}`, + ) + if (!response.success) throw new Error(response.data) + return new CheckUsernameResponse(response.data) } } diff --git a/backend/src/graphql/resolvers/helpers/MapVariableToColumnName.ts b/backend/src/graphql/resolvers/helpers/MapVariableToColumnName.ts index e69de29bb..361b35d9d 100644 --- a/backend/src/graphql/resolvers/helpers/MapVariableToColumnName.ts +++ b/backend/src/graphql/resolvers/helpers/MapVariableToColumnName.ts @@ -0,0 +1,3 @@ +export const MapVariableToColumnName = (object) => { + return null +}