Changed structure of the models, now they are not all in one file.

This commit is contained in:
Hannes Heine 2021-08-02 16:03:33 +02:00
parent f77364e053
commit aa22d9154d
17 changed files with 233 additions and 158 deletions

View File

@ -11,10 +11,10 @@ export const apiPost = async (url: string, payload: unknown): Promise<any> => {
if (result.data.state !== 'success') {
throw new Error(result.data.msg)
}
return { success: true, result }
return { success: true, data: result.data }
})
.catch((error) => {
return { success: false, result: error }
return { success: false, data: error.message }
})
}
@ -29,11 +29,11 @@ export const apiGet = async (url: string): Promise<any> => {
if (!['success', 'warning'].includes(result.data.state)) {
throw new Error(result.data.msg)
}
return { success: true, result: result }
return { success: true, data: result.data }
})
.catch((error) => {
// eslint-disable-next-line no-console
console.log('IN apiGet.ERROR: ' + JSON.stringify({ success: false, result: error }))
return { success: false, result: error }
return { success: false, data: error.message }
})
}

View File

@ -1,5 +1,4 @@
import { ObjectType, ArgsType, Field, Int, Float } from 'type-graphql'
import { Entity, BaseEntity, Column, Double } from 'typeorm'
import { ArgsType, Field } from 'type-graphql'
@ArgsType()
export class TransactionInput {

View File

@ -1,10 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { Entity, BaseEntity, Column } from 'typeorm'
import { ObjectType, Field } from 'type-graphql'
@Entity()
@ObjectType()
export class Balance extends BaseEntity {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(json: any) {
super()
this.balance = json.balance

View File

@ -0,0 +1,9 @@
import { Entity, BaseEntity } from 'typeorm'
import { ObjectType, Field } from 'type-graphql'
@Entity()
@ObjectType()
export class BaseResponse extends BaseEntity {
@Field(() => Boolean)
success: boolean
}

View File

@ -0,0 +1,9 @@
import { Entity, BaseEntity } from 'typeorm'
import { ObjectType, Field } from 'type-graphql'
@Entity()
@ObjectType()
export class ChangePasswordResponse extends BaseEntity {
@Field(() => String)
state: string
}

View File

@ -0,0 +1,15 @@
import { Entity, BaseEntity } from 'typeorm'
import { ObjectType, Field } from 'type-graphql'
@Entity()
@ObjectType()
export class CheckUsernameResponse extends BaseEntity {
@Field(() => String)
state: string
@Field(() => String)
msg?: string
@Field(() => Number)
groupId?: number
}

View File

@ -0,0 +1,14 @@
import { Entity, BaseEntity } from 'typeorm'
import { ObjectType, Field } from 'type-graphql'
@Entity()
@ObjectType()
export class CreateResponse extends BaseEntity {
constructor(json: any) {
super()
this.state = json.state
}
@Field(() => String)
state: string
}

View File

@ -0,0 +1,25 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { Entity, BaseEntity, Column } from 'typeorm'
import { ObjectType, Field } from 'type-graphql'
import { User } from './User'
// temporaray solution until we have JWT implemented
@Entity()
@ObjectType()
export class LoginResponse extends BaseEntity {
constructor(json: any) {
super()
this.sessionId = json.session_id
this.user = new User(json.user)
}
@Field(() => Number)
@Column()
sessionId: number
@Field(() => User)
@Column()
user: User
}

View File

@ -0,0 +1,22 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { Entity, BaseEntity, Column } from 'typeorm'
import { ObjectType, Field } from 'type-graphql'
@Entity()
@ObjectType()
export class LoginViaVerificationCode extends BaseEntity {
constructor(json: any) {
super()
this.sessionId = json.session_id
this.email = json.user.email
}
@Field(() => Number)
@Column()
sessionId: number
@Field(() => String)
@Column()
email: string
}

View File

@ -0,0 +1,18 @@
import { Entity, BaseEntity } from 'typeorm'
import { ObjectType, Field } from 'type-graphql'
@Entity()
@ObjectType()
export class SendEmailResponse extends BaseEntity {
constructor(json: any) {
super()
this.state = json.state
this.msg = json.msg
}
@Field(() => String)
state: string
@Field(() => String)
msg?: string
}

View File

@ -0,0 +1,14 @@
import { Entity, BaseEntity } from 'typeorm'
import { ObjectType, Field } from 'type-graphql'
@Entity()
@ObjectType()
export class Server extends BaseEntity {
constructor(json: any) {
super()
this.loginServerPath = json.login_server_path
}
@Field(() => String)
loginServerPath: string
}

View File

@ -0,0 +1,15 @@
import { Entity, BaseEntity } from 'typeorm'
import { ObjectType, Field } from 'type-graphql'
@Entity()
@ObjectType()
export class UpdateUserInfosResponse extends BaseEntity {
@Field(() => String)
state: string
@Field(() => Number)
validValues: number
@Field(() => [String])
errors: [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, Column } from 'typeorm'
import { ObjectType, Field } from 'type-graphql'
@ -9,6 +11,15 @@ export class User extends BaseEntity {
@PrimaryGeneratedColumn()
id: number
*/
constructor(json: any) {
super()
this.email = json.email
this.firstName = json.first_name
this.lastName = json.last_name
this.username = json.username
this.description = json.description
this.language = json.language
}
@Field(() => String)
@Column({ length: 191 })
@ -70,115 +81,3 @@ export class User extends BaseEntity {
publisherId: number
*/
}
// temporaray solution until we have JWT implemented
@Entity()
@ObjectType()
export class LoginResponse extends BaseEntity {
@Field(() => Number)
@Column()
sessionId: number
@Field(() => User)
@Column()
user: User
}
@Entity()
@ObjectType()
export class LoginViaVerificationCode extends BaseEntity {
@Field(() => Number)
@Column()
sessionId: number
@Field(() => String)
@Column()
email: string
}
@Entity()
@ObjectType()
export class LogoutResponse extends BaseEntity {
@Field(() => String)
state: string
}
@Entity()
@ObjectType()
export class CreateResponse extends BaseEntity {
@Field(() => String)
state: string
}
@Entity()
@ObjectType()
export class SendEmailResponse extends BaseEntity {
@Field(() => String)
state: string
@Field(() => String)
msg?: string
}
@Entity()
@ObjectType()
export class Server extends BaseEntity {
@Field(() => String)
loginServerPath: string
}
@Entity()
@ObjectType()
export class ErrorData extends BaseEntity {
@Field(() => String)
messages: string
}
@Entity()
@ObjectType()
export class GetUserInfoResponse extends BaseEntity {
@Field(() => String)
state: string
@Field(() => User)
userData: User
@Field(() => Server)
server: Server
@Field(() => [ErrorData])
errors: [ErrorData]
}
@Entity()
@ObjectType()
export class ChangePasswordResponse extends BaseEntity {
@Field(() => String)
state: string
}
@Entity()
@ObjectType()
export class UpdateUserInfosResponse extends BaseEntity {
@Field(() => String)
state: string
@Field(() => Number)
validValues: number
@Field(() => [ErrorData])
errors: [ErrorData]
}
@Entity()
@ObjectType()
export class CheckUsernameResponse extends BaseEntity {
@Field(() => String)
state: string
@Field(() => String)
msg?: string
@Field(() => Number)
groupId?: number
}

View File

@ -0,0 +1,38 @@
import { Entity, BaseEntity } from 'typeorm'
import { ObjectType, Field } from 'type-graphql'
import { Server } from './Server'
import { User } from './User'
@Entity()
@ObjectType()
export class UserInfoData extends BaseEntity {
constructor(json: any) {
super()
this.state = json.state
this.userData = new User(json.user_data)
this.server = new Server(json.server)
this.errors = json.errors
}
@Field(() => String)
state: string
@Field(() => User)
userData: User
@Field(() => Server)
server: Server
@Field(() => [String])
errors: [string]
}
@Entity()
@ObjectType()
export class GetUserInfoResponse extends BaseEntity {
@Field(() => Boolean)
success: boolean
@Field(() => UserInfoData)
data: UserInfoData
}

View File

@ -41,7 +41,7 @@ export class CommunityTransactionResolver {
}
@Query(() => String)
async create(
async createCoins(
@Args() { sessionId, email, amount, memo, targetDate = new Date() }: TransactionCreateArgs,
): Promise<any> {
const payload = {

View File

@ -1,17 +1,14 @@
// import jwt from 'jsonwebtoken'
import { Resolver, Query, /* Mutation, */ Args, Arg } from 'type-graphql'
import CONFIG from '../../config'
import {
ChangePasswordResponse,
CheckUsernameResponse,
CreateResponse,
GetUserInfoResponse,
LoginResponse,
LoginViaVerificationCode,
LogoutResponse,
SendEmailResponse,
UpdateUserInfosResponse,
} from '../models/User'
import { ChangePasswordResponse } from '../models/ChangePasswordResponse'
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 { UpdateUserInfosResponse } from '../models/UpdateUserInfosResponse'
import {
ChangePasswordArgs,
CheckUsernameArgs,
@ -27,27 +24,17 @@ import { apiPost, apiGet } from '../../apis/loginAPI'
export class UserResolver {
@Query(() => LoginResponse)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async login(@Args() { email, password }: UnsecureLoginArgs): Promise<any> {
async login(@Args() { email, password }: UnsecureLoginArgs): Promise<LoginResponse> {
email = email.trim().toLowerCase()
const result = await apiPost(CONFIG.LOGIN_API_URL + 'unsecureLogin', { email, password })
// if there is no user, throw an authentication error
if (!result.success) {
throw new Error(result.result)
throw new Error(result.data)
}
// temporary solution until we have JWT implemented
return {
sessionId: result.result.data.session_id,
user: {
email: result.result.data.user.email,
language: result.result.data.user.language,
username: result.result.data.user.username,
firstName: result.result.data.user.first_name,
lastName: result.result.data.user.last_name,
description: result.result.data.user.description,
},
}
return new LoginResponse(result.data)
// create and return the json web token
// The expire doesn't help us here. The client needs to track when the token expires on its own,
@ -65,29 +52,35 @@ export class UserResolver {
@Query(() => LoginViaVerificationCode)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async loginViaEmailVerificationCode(@Arg('optin') optin: string): Promise<any> {
async loginViaEmailVerificationCode(
@Arg('optin') optin: string,
): Promise<LoginViaVerificationCode> {
// I cannot use number as type here.
// The value received is not the same as sent by the query
const result = await apiGet(
CONFIG.LOGIN_API_URL + 'loginViaEmailVerificationCode?emailVerificationCode=' + optin,
)
if (result.success)
return {
sessionId: result.result.data.session_id,
email: result.result.data.user.email,
}
return result.result
if (!result.success) {
throw new Error(result.data)
}
return new LoginViaVerificationCode(result.data)
}
@Query(() => LogoutResponse)
async logout(@Arg('sessionId') sessionId: number): Promise<any> {
/* @Query(() => LogoutResponse)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async logout(@Arg('sessionId') sessionId: number): Promise<LogoutResponse> {
const payload = { session_id: sessionId }
const result = apiPost(CONFIG.LOGIN_API_URL + 'logout', payload);
return result
const result = apiPost(CONFIG.LOGIN_API_URL + 'logout', payload)
if (!result.success) {
throw new Error(result.data)
}
return result.data
}
*/
@Query(() => CreateResponse)
async create(@Args() { email, firstName, lastName, password }: CreateUserArgs): Promise<any> {
async create(
@Args() { email, firstName, lastName, password }: CreateUserArgs,
): Promise<CreateResponse> {
const payload = {
email,
first_name: firstName,
@ -96,7 +89,11 @@ export class UserResolver {
emailType: 2,
login_after_register: true,
}
return apiPost(CONFIG.LOGIN_API_URL + 'createUser', payload)
const result = await apiPost(CONFIG.LOGIN_API_URL + 'createUser', payload)
if (!result.success) {
throw new Error(result.data)
}
return new CreateResponse(result.data)
}
@Query(() => SendEmailResponse)