diff --git a/backend/src/graphql/inputs/LoginUserInput.ts b/backend/src/graphql/inputs/LoginUserInput.ts index 66f35980f..68ddad628 100644 --- a/backend/src/graphql/inputs/LoginUserInput.ts +++ b/backend/src/graphql/inputs/LoginUserInput.ts @@ -82,23 +82,23 @@ export class UpdateUserInfosArgs { @Field(() => String) email!: string - @Field({ nullable: true }) - firstName?: string | null + @Field(() => String) + firstName?: string - @Field({ nullable: true }) - lastName?: string | null + @Field(() => String) + lastName?: string - @Field({ nullable: true }) - username?: string | null + @Field(() => String) + username?: string - @Field({ nullable: true }) - language?: string | null + @Field(() => String) + language?: string - @Field({ nullable: true }) - password?: string | null + @Field(() => String) + password?: string - @Field({ nullable: true }) - passwordNew?: string | null + @Field(() => String) + passwordNew?: string } @ArgsType() diff --git a/backend/src/graphql/inputs/TransactionInput.ts b/backend/src/graphql/inputs/TransactionInput.ts index db010390a..049a9b2a9 100644 --- a/backend/src/graphql/inputs/TransactionInput.ts +++ b/backend/src/graphql/inputs/TransactionInput.ts @@ -1,7 +1,7 @@ import { ArgsType, Field } from 'type-graphql' @ArgsType() -export class TransactionInput { +export class TransactionListInput { @Field(() => Number) sessionId: number diff --git a/backend/src/graphql/models/Balance.ts b/backend/src/graphql/models/Balance.ts index e54bb3478..4897f3265 100644 --- a/backend/src/graphql/models/Balance.ts +++ b/backend/src/graphql/models/Balance.ts @@ -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' diff --git a/backend/src/graphql/models/Decay.ts b/backend/src/graphql/models/Decay.ts index af13ac193..3d918ce7a 100644 --- a/backend/src/graphql/models/Decay.ts +++ b/backend/src/graphql/models/Decay.ts @@ -1,11 +1,10 @@ -import { Entity, BaseEntity, Column } from 'typeorm' +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { ObjectType, Field } from 'type-graphql' -@Entity() @ObjectType() -export class Decay extends BaseEntity { +export class Decay { constructor(json: any) { - super() this.balance = Number(json.balance) this.decayStart = json.decay_start this.decayEnd = json.decay_end @@ -13,18 +12,14 @@ export class Decay extends BaseEntity { } @Field(() => Number) - @Column() balance: number - @Field(() => Number) - @Column() - decayStart: number + @Field({ nullable: true }) + decayStart?: number - @Field(() => Number) - @Column() - decayEnd: number + @Field({ nullable: true }) + decayEnd?: number @Field(() => String) - @Column() decayDuration: string } diff --git a/backend/src/graphql/models/Transaction.ts b/backend/src/graphql/models/Transaction.ts index 732dd9ec4..61670b06a 100644 --- a/backend/src/graphql/models/Transaction.ts +++ b/backend/src/graphql/models/Transaction.ts @@ -1,11 +1,66 @@ -import { Entity, BaseEntity, Column } from 'typeorm' +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { ObjectType, Field } from 'type-graphql' +import { Decay } from './Decay' + +// we need a better solution for the decay block: +// the first transaction on the first page shows the decay since the last transaction +// the format is actually a Decay and not a Transaction. +// Therefore we have a lot of nullable fields, which should be always present -@Entity() @ObjectType() -export class TransactionList extends BaseEntity { +export class Transaction { + constructor(json: any) { + this.type = json.type + this.balance = Number(json.balance) + this.decayStart = json.decay_start + this.decayEnd = json.decay_end + this.decayDuration = json.decay_duration + this.memo = json.memo + this.transactionId = json.transaction_id + this.name = json.name + this.email = json.email + this.date = json.date + this.decay = json.decay + } + + @Field(() => String) + type: string + + @Field(() => Number) + balance: number + + @Field({ nullable: true }) + decayStart?: number + + @Field({ nullable: true }) + decayEnd?: number + + @Field({ nullable: true }) + decayDuration?: string + + @Field(() => String) + memo: string + + @Field(() => Number) + transactionId: number + + @Field({ nullable: true }) + name?: string + + @Field({ nullable: true }) + email?: string + + @Field({ nullable: true }) + date?: string + + @Field({ nullable: true }) + decay?: Decay +} + +@ObjectType() +export class TransactionList { constructor(json: any) { - super() this.gdtSum = Number(json.gdtSum) this.count = json.count this.balance = Number(json.balance) @@ -15,88 +70,20 @@ export class TransactionList extends BaseEntity { } @Field(() => Number) - @Column() gdtSum: number @Field(() => Number) - @Column() count: number @Field(() => Number) - @Column() balance: number @Field(() => Number) - @Column() decay: number @Field(() => String) - @Column() decayDate: string @Field(() => [Transaction]) transactions: Transaction[] } - -@Entity() -@ObjectType() -export class Transaction extends BaseEntity { - constructor(json: any) { - super() - this.type = json.type - this.balance = Number(json.balance) - this.decayStart = json.decay_start - this.decayEnd = json.decay_end - this.decayDuration = json.decay_duration - this.meno = json.memo - this.transactionId = json.transaction_id - this.name = json.name - this.email = json.email - this.date = json.date - this.decay = json.decay - } - - @Field(() => String) - @Column() - type: string - - @Field(() => Number) - @Column() - balance: number - - @Field(() => Number) - @Column() - decayStart: number - - @Field(() => Number) - @Column() - decayEnd: number - - @Field(() => String) - @Column() - decayDuration: string - - @Field(() => String) - @Column() - memo: string - - @Field(() => Number) - @Column() - transactionId: number - - @Field(() => String) - @Column() - name: string - - @Field(() => String) - @Column() - email: string - - @Field(() => String) - @Column() - date: string - - @Field(() => Decay) - @Column() - decay: Decay -} diff --git a/backend/src/graphql/resolvers/BalanceResolver.ts b/backend/src/graphql/resolvers/BalanceResolver.ts index 31bed076c..c483ad5c9 100644 --- a/backend/src/graphql/resolvers/BalanceResolver.ts +++ b/backend/src/graphql/resolvers/BalanceResolver.ts @@ -1,4 +1,4 @@ -import { Resolver, Query, /* Mutation, */ Args, Arg } from 'type-graphql' +import { Resolver, Query, /* Mutation, */ Arg } from 'type-graphql' import CONFIG from '../../config' import { Balance } from '../models/Balance' import { apiGet } from '../../apis/loginAPI' @@ -8,9 +8,7 @@ export class BalanceResolver { @Query(() => Balance) async balance(@Arg('sessionId') sessionId: number): Promise { const result = await apiGet(CONFIG.COMMUNITY_API_URL + 'getBalance/' + sessionId) - console.log(result) - if (!result.success) - throw new Error(result.data) + if (!result.success) throw new Error(result.data) return new Balance(result.data) } } diff --git a/backend/src/graphql/resolvers/CommunityTransactionResolver.ts b/backend/src/graphql/resolvers/CommunityTransactionResolver.ts deleted file mode 100644 index 1fcc214e7..000000000 --- a/backend/src/graphql/resolvers/CommunityTransactionResolver.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { Resolver, Query, /* Mutation, */ Args, Arg } from 'type-graphql' -import CONFIG from '../../config' -import {} from '../models/Transaction' -import { Balance } from '../models/Balance' -import { - TransactionCreateArgs, - TransactionInput, - TransactionSendArgs, -} from '../inputs/TransactionInput' -import { apiPost, apiGet } from '../../apis/loginAPI' - -@Resolver() -export class CommunityTransactionResolver { - @Query(() => Balance) - async balance(@Arg('sessionId') sessionId: number): Promise { - // eslint-disable-next-line no-console - console.log('IN BALANCE: URL: ' + CONFIG.COMMUNITY_API_URL + 'getBalance/' + sessionId) - const result = await apiGet(CONFIG.COMMUNITY_API_URL + 'getBalance/' + sessionId) - return new Balance(result.result.data) - } - - @Query(() => String) - async transactions( - @Args() { sessionId, firstPage = 1, items = 5, order = 'DESC' }: TransactionInput, - ): Promise { - return apiGet( - `${CONFIG.COMMUNITY_API_URL}listTransactions/${firstPage}/${items}/${order}/${sessionId}`, - ) - } - - @Query(() => String) - async send(@Args() { sessionId, email, amount, memo }: TransactionSendArgs): Promise { - const payload = { - session_id: sessionId, - auto_sign: true, - email: email, - amount: amount, - memo: memo, - } - return apiPost(CONFIG.COMMUNITY_API_URL + 'sendCoins/', payload) - } - - @Query(() => String) - async createCoins( - @Args() { sessionId, email, amount, memo, targetDate = new Date() }: TransactionCreateArgs, - ): Promise { - const payload = { - sessionId, - email, - amount, - targetDate, - memo, - auto_sign: true, - } - return apiPost(CONFIG.COMMUNITY_API_URL + 'createCoins/', payload) - } -} diff --git a/backend/src/graphql/resolvers/TransactionResolver.ts b/backend/src/graphql/resolvers/TransactionResolver.ts new file mode 100644 index 000000000..9834b62f8 --- /dev/null +++ b/backend/src/graphql/resolvers/TransactionResolver.ts @@ -0,0 +1,19 @@ +import { Resolver, Query, /* Mutation, */ Args } from 'type-graphql' +import CONFIG from '../../config' +import { TransactionList } from '../models/Transaction' +import { TransactionListInput } from '../inputs/TransactionInput' +import { apiGet } from '../../apis/loginAPI' + +@Resolver() +export class TransactionResolver { + @Query(() => TransactionList) + async transactionList( + @Args() { sessionId, firstPage = 1, items = 25, order = 'DESC' }: TransactionListInput, + ): Promise { + const result = await apiGet( + `${CONFIG.COMMUNITY_API_URL}listTransactions/${firstPage}/${items}/${order}/${sessionId}`, + ) + if (!result.success) throw new Error(result.data) + return new TransactionList(result.data) + } +} diff --git a/backend/src/graphql/resolvers/UserResolver.ts b/backend/src/graphql/resolvers/UserResolver.ts index 43d83938c..5791ccd15 100644 --- a/backend/src/graphql/resolvers/UserResolver.ts +++ b/backend/src/graphql/resolvers/UserResolver.ts @@ -23,7 +23,6 @@ import { apiPost, apiGet } from '../../apis/loginAPI' @Resolver() export class UserResolver { @Query(() => LoginResponse) - // eslint-disable-next-line @typescript-eslint/no-explicit-any async login(@Args() { email, password }: UnsecureLoginArgs): Promise { email = email.trim().toLowerCase() const result = await apiPost(CONFIG.LOGIN_API_URL + 'unsecureLogin', { email, password }) @@ -51,7 +50,6 @@ export class UserResolver { } @Query(() => LoginViaVerificationCode) - // eslint-disable-next-line @typescript-eslint/no-explicit-any async loginViaEmailVerificationCode( @Arg('optin') optin: string, ): Promise { @@ -112,6 +110,7 @@ export class UserResolver { } @Query(() => GetUserInfoResponse) + // eslint-disable-next-line @typescript-eslint/no-explicit-any async getUserInfos(@Args() { sessionId, email }: GetUserInfoArgs): Promise { const payload = { session_id: sessionId, @@ -142,24 +141,24 @@ export class UserResolver { { sessionId, email, - firstName = null, - lastName = null, - username = null, - language = null, - password = null, - passwordNew = null, + firstName = '', + lastName = '', + username = '', + language = '', + password = '', + passwordNew = '', }: UpdateUserInfosArgs, ): Promise { const payload = { session_id: sessionId, email, update: { - 'User.first_name': firstName !== null ? firstName : undefined, - 'User.last_name': lastName !== null ? lastName : undefined, - 'User.username': username !== null ? username : undefined, - 'User.language': language !== null ? language : undefined, - 'User.password': passwordNew !== null ? passwordNew : undefined, - 'User.password_old': password !== null ? password : undefined, + 'User.first_name': firstName !== '' ? firstName : undefined, + 'User.last_name': lastName !== '' ? lastName : undefined, + 'User.username': username !== '' ? username : undefined, + 'User.language': language !== '' ? language : undefined, + 'User.password': passwordNew !== '' ? passwordNew : undefined, + 'User.password_old': password !== '' ? password : undefined, }, } const result = await apiPost(CONFIG.LOGIN_API_URL + 'updateUserInfos', payload) diff --git a/backend/src/index.ts b/backend/src/index.ts index d9a35fa40..8bf6dbebc 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -9,14 +9,15 @@ import CONFIG from './config' // import { BookResolver } from './graphql/resolvers/BookResolver' import { UserResolver } from './graphql/resolvers/UserResolver' import { BalanceResolver } from './graphql/resolvers/BalanceResolver' -// import { GroupResolver } from './graphql/resolvers/GroupResolver' +import { TransactionResolver } from './graphql/resolvers/TransactionResolver' + // TODO implement // import queryComplexity, { simpleEstimator, fieldConfigEstimator } from "graphql-query-complexity"; async function main() { // const connection = await createConnection() const schema = await buildSchema({ - resolvers: [UserResolver, BalanceResolver], + resolvers: [UserResolver, BalanceResolver, TransactionResolver], }) // Graphiql interface