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

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 { 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

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 { 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 { 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

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 { 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 { 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

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

View File

@ -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<LogoutResponse> {
@Query(() => String)
async logout(@Arg('sessionId') sessionId: number): Promise<string> {
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<any> {
): Promise<SendPasswordResetEmailResponse> {
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<any> {
@Query(() => ResetPasswordResponse)
async resetPassword(
@Args()
{ sessionId, email, password }: ChangePasswordArgs,
): Promise<ResetPasswordResponse> {
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<any> {
): Promise<UpdateUserInfosResponse> {
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<any> {
return apiGet(CONFIG.LOGIN_API_URL + `checkUsername?username=${username}&group_id=${groupId}`)
async checkUsername(
@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
}