Implementation of the Response classes, and logic for the update user prozess.

This commit is contained in:
Hannes Heine 2021-08-03 10:28:49 +02:00
parent aa22d9154d
commit 99a37ddb0a
12 changed files with 99 additions and 59 deletions

View File

@ -82,22 +82,22 @@ export class UpdateUserInfosArgs {
@Field(() => String) @Field(() => String)
email!: string email!: string
@Field(() => String) @Field({ nullable: true })
firstName?: string firstName?: string
@Field(() => String) @Field({ nullable: true })
lastName?: string lastName?: string
@Field(() => String) @Field({ nullable: true })
username?: string username?: string
@Field(() => String) @Field({ nullable: true })
language?: string language?: string
@Field(() => String) @Field({ nullable: true })
password?: string password?: string
@Field(() => String) @Field({ nullable: true })
passwordNew?: string passwordNew?: string
} }

View File

@ -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
}

View File

@ -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
}

View File

@ -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 { Entity, BaseEntity } from 'typeorm'
import { ObjectType, Field } from 'type-graphql' import { ObjectType, Field } from 'type-graphql'
@Entity() @Entity()
@ObjectType() @ObjectType()
export class CheckUsernameResponse extends BaseEntity { export class CheckUsernameResponse extends BaseEntity {
constructor(json: any) {
super()
this.state = json.state
this.msg = json.msg
this.groupId = json.group_id
}
@Field(() => String) @Field(() => String)
state: string state: string

View File

@ -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 { Entity, BaseEntity } from 'typeorm'
import { ObjectType, Field } from 'type-graphql' import { ObjectType, Field } from 'type-graphql'

View File

@ -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
}

View File

@ -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 { Entity, BaseEntity } from 'typeorm'
import { ObjectType, Field } from 'type-graphql' import { ObjectType, Field } from 'type-graphql'
@Entity() @Entity()
@ObjectType() @ObjectType()
export class SendEmailResponse extends BaseEntity { export class SendPasswordResetEmailResponse extends BaseEntity {
constructor(json: any) { constructor(json: any) {
super() super()
this.state = json.state this.state = json.state

View File

@ -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 { Entity, BaseEntity } from 'typeorm'
import { ObjectType, Field } from 'type-graphql' import { ObjectType, Field } from 'type-graphql'

View File

@ -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 { Entity, BaseEntity } from 'typeorm'
import { ObjectType, Field } from 'type-graphql' import { ObjectType, Field } from 'type-graphql'
@Entity() @Entity()
@ObjectType() @ObjectType()
export class UpdateUserInfosResponse extends BaseEntity { export class UpdateUserInfosResponse extends BaseEntity {
constructor(json: any) {
super()
this.state = json.state
this.validValues = json.valid_values
this.errors = json.errors
}
@Field(() => String) @Field(() => String)
state: string state: string

View File

@ -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 { Entity, BaseEntity } from 'typeorm'
import { ObjectType, Field } from 'type-graphql' import { ObjectType, Field } from 'type-graphql'
import { Server } from './Server' import { Server } from './Server'

View File

@ -1,13 +1,13 @@
// import jwt from 'jsonwebtoken' // 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 CONFIG from '../../config'
import { ChangePasswordResponse } from '../models/ChangePasswordResponse' import { ResetPasswordResponse } from '../models/ResetPasswordResponse'
import { CheckUsernameResponse } from '../models/CheckUsernameResponse' import { CheckUsernameResponse } from '../models/CheckUsernameResponse'
import { CreateResponse } from '../models/CreateResponse' import { CreateResponse } from '../models/CreateResponse'
import { GetUserInfoResponse } from '../models/UserInfoData' import { GetUserInfoResponse } from '../models/UserInfoData'
import { LoginResponse } from '../models/LoginResponse' import { LoginResponse } from '../models/LoginResponse'
import { LoginViaVerificationCode } from '../models/LoginViaVerificationCode' import { LoginViaVerificationCode } from '../models/LoginViaVerificationCode'
import { SendEmailResponse } from '../models/SendEmailResponse' import { SendPasswordResetEmailResponse } from '../models/SendPasswordResetEmailResponse'
import { UpdateUserInfosResponse } from '../models/UpdateUserInfosResponse' import { UpdateUserInfosResponse } from '../models/UpdateUserInfosResponse'
import { import {
ChangePasswordArgs, ChangePasswordArgs,
@ -66,17 +66,16 @@ export class UserResolver {
return new LoginViaVerificationCode(result.data) return new LoginViaVerificationCode(result.data)
} }
/* @Query(() => LogoutResponse) @Query(() => String)
// eslint-disable-next-line @typescript-eslint/no-explicit-any async logout(@Arg('sessionId') sessionId: number): Promise<string> {
async logout(@Arg('sessionId') sessionId: number): Promise<LogoutResponse> {
const payload = { session_id: sessionId } 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) { if (!result.success) {
throw new Error(result.data) throw new Error(result.data)
} }
return result.data return 'success'
} }
*/
@Query(() => CreateResponse) @Query(() => CreateResponse)
async create( async create(
@Args() { email, firstName, lastName, password }: CreateUserArgs, @Args() { email, firstName, lastName, password }: CreateUserArgs,
@ -96,17 +95,20 @@ export class UserResolver {
return new CreateResponse(result.data) return new CreateResponse(result.data)
} }
@Query(() => SendEmailResponse) // TODO
async sendEmail( @Query(() => SendPasswordResetEmailResponse)
async sendResetPasswordEmail(
@Args() @Args()
{ email, emailText = 7, emailVerificationCodeType = 'resetPassword' }: SendEmailArgs, { email, emailText = 7, emailVerificationCodeType = 'resetPassword' }: SendEmailArgs,
): Promise<any> { ): Promise<SendPasswordResetEmailResponse> {
const payload = { const payload = {
email, email,
email_text: emailText, email_text: emailText,
email_verification_code_type: emailVerificationCodeType, 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) @Query(() => GetUserInfoResponse)
@ -119,16 +121,19 @@ export class UserResolver {
return apiPost(CONFIG.LOGIN_API_URL + 'getUserInfos', payload) return apiPost(CONFIG.LOGIN_API_URL + 'getUserInfos', payload)
} }
@Query(() => ChangePasswordResponse) @Query(() => ResetPasswordResponse)
async changePassword(@Args() { sessionId, email, password }: ChangePasswordArgs): Promise<any> { async resetPassword(
@Args()
{ sessionId, email, password }: ChangePasswordArgs,
): Promise<ResetPasswordResponse> {
const payload = { const payload = {
session_id: sessionId, session_id: sessionId,
email, email,
password, password,
} }
const result = await apiPost(CONFIG.LOGIN_API_URL + 'resetPassword', payload) const result = await apiPost(CONFIG.LOGIN_API_URL + 'resetPassword', payload)
if (result.success) return result.result.data.state if (!result.success) throw new Error(result.data)
return result.result return new ResetPasswordResponse(result.data)
} }
@Query(() => UpdateUserInfosResponse) @Query(() => UpdateUserInfosResponse)
@ -137,32 +142,40 @@ export class UserResolver {
{ {
sessionId, sessionId,
email, email,
firstName, firstName = '',
lastName, lastName = '',
username, username = '',
language, language = '',
password, password = '',
passwordNew, passwordNew = '',
}: UpdateUserInfosArgs, }: UpdateUserInfosArgs,
): Promise<any> { ): Promise<UpdateUserInfosResponse> {
const payload = { const payload = {
session_id: sessionId, session_id: sessionId,
email, email,
update: { update: {
'User.first_name': firstName, 'User.first_name': firstName.length > 0 ? firstName : undefined,
'User.last_name': lastName, 'User.last_name': lastName.length > 0 ? lastName : undefined,
// 'User.description': data.description, 'User.username': username.length > 0 ? username : undefined,
'User.username': username, 'User.language': language.length > 0 ? language : undefined,
'User.language': language, 'User.password': passwordNew.length > 0 ? passwordNew : undefined,
'User.password_old': password, 'User.password_old': password.length > 0 ? password : undefined,
'User.password': passwordNew,
}, },
} }
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) @Query(() => CheckUsernameResponse)
async checkUsername(@Args() { username, groupId = 1 }: CheckUsernameArgs): Promise<any> { async checkUsername(
return apiGet(CONFIG.LOGIN_API_URL + `checkUsername?username=${username}&group_id=${groupId}`) @Args() { username, groupId = 1 }: CheckUsernameArgs,
): Promise<CheckUsernameResponse> {
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)
} }
} }

View File

@ -0,0 +1,3 @@
export const MapVariableToColumnName = (object) => {
return null
}