mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
linting and use db for db Models
This commit is contained in:
parent
7613db5060
commit
aa0e84fbf6
@ -9,7 +9,7 @@ export class TransactionListInput {
|
||||
items: number
|
||||
|
||||
@Field(() => String)
|
||||
order: "ASC" | "DESC"
|
||||
order: 'ASC' | 'DESC'
|
||||
}
|
||||
|
||||
@ArgsType()
|
||||
|
||||
@ -6,8 +6,8 @@ import CONFIG from '../../config'
|
||||
import { TransactionList } from '../models/Transaction'
|
||||
import { TransactionListInput, TransactionSendArgs } from '../inputs/TransactionInput'
|
||||
import { apiGet, apiPost } from '../../apis/HttpRequest'
|
||||
import { User } from '../../typeorm/entity/User'
|
||||
import { Balance } from '../../typeorm/entity/Balance'
|
||||
import { User as dbUser } from '../../typeorm/entity/User'
|
||||
import { Balance as dbBalance } from '../../typeorm/entity/Balance'
|
||||
import listTransactions from './listTransactions'
|
||||
import { roundFloorFrom4 } from '../../util/round'
|
||||
import calculateDecay from '../../util/decay'
|
||||
@ -25,24 +25,26 @@ export class TransactionResolver {
|
||||
if (!result.success) throw new Error(result.data)
|
||||
|
||||
// load user
|
||||
const userEntity = await User.findByPubkeyHex(result.data.user.public_hex)
|
||||
const userEntity = await dbUser.findByPubkeyHex(result.data.user.public_hex)
|
||||
|
||||
const transactions = await listTransactions(firstPage, items, order, userEntity)
|
||||
|
||||
// get gdt sum
|
||||
const resultGDTSum = await apiPost(
|
||||
`${CONFIG.GDT_API_URL}/GdtEntries/sumPerEmailApi`, {email: userEntity.email}
|
||||
)
|
||||
// get gdt sum
|
||||
const resultGDTSum = await apiPost(`${CONFIG.GDT_API_URL}/GdtEntries/sumPerEmailApi`, {
|
||||
email: userEntity.email,
|
||||
})
|
||||
if (!resultGDTSum.success) throw new Error(resultGDTSum.data)
|
||||
transactions.gdtSum = resultGDTSum.data.sum
|
||||
|
||||
// get balance
|
||||
const balanceEntity = await Balance.findByUser(userEntity.id)
|
||||
const balanceEntity = await dbBalance.findByUser(userEntity.id)
|
||||
const now = new Date()
|
||||
transactions.balance = roundFloorFrom4(balanceEntity.amount)
|
||||
transactions.decay = roundFloorFrom4(calculateDecay(balanceEntity.amount, balanceEntity.recordDate, now))
|
||||
transactions.decay = roundFloorFrom4(
|
||||
calculateDecay(balanceEntity.amount, balanceEntity.recordDate, now),
|
||||
)
|
||||
transactions.decayDate = now.toString()
|
||||
|
||||
|
||||
return transactions
|
||||
}
|
||||
|
||||
|
||||
@ -1,61 +1,67 @@
|
||||
|
||||
import { User } from '../../typeorm/entity/User'
|
||||
import { User as dbUser } from '../../typeorm/entity/User'
|
||||
import { TransactionList, Transaction } from '../models/Transaction'
|
||||
import { UserTransaction } from '../../typeorm/entity/UserTransaction'
|
||||
|
||||
function calculateAndAddDecayTransactions(
|
||||
userTransactions:UserTransaction[],
|
||||
user: User,
|
||||
decay:boolean,
|
||||
skipFirstTransaction:boolean
|
||||
): Transaction[]
|
||||
{
|
||||
let transactions: Transaction[] = []
|
||||
userTransactions: UserTransaction[],
|
||||
user: dbUser,
|
||||
decay: boolean,
|
||||
skipFirstTransaction: boolean,
|
||||
): Transaction[] {
|
||||
const transactions: Transaction[] = []
|
||||
|
||||
return transactions
|
||||
return transactions
|
||||
}
|
||||
|
||||
|
||||
export default async function listTransactions(
|
||||
firstPage:number,
|
||||
items:number,
|
||||
order:"ASC" | "DESC",
|
||||
user:User) : Promise<TransactionList>
|
||||
{
|
||||
firstPage: number,
|
||||
items: number,
|
||||
order: 'ASC' | 'DESC',
|
||||
user: dbUser,
|
||||
): Promise<TransactionList> {
|
||||
let limit = items
|
||||
let offset = 0
|
||||
let skipFirstTransaction = false
|
||||
if (firstPage > 1) {
|
||||
offset = (firstPage - 1) * items - 1
|
||||
limit++
|
||||
}
|
||||
|
||||
let limit = items
|
||||
let offset = 0
|
||||
let skipFirstTransaction = false
|
||||
if(firstPage > 1) {
|
||||
offset = (( firstPage - 1 ) * items) - 1;
|
||||
limit++;
|
||||
if (offset && order === 'ASC') {
|
||||
offset--
|
||||
}
|
||||
let [userTransactions, userTransactionsCount] = await UserTransaction.findByUserPaged(
|
||||
user.id,
|
||||
limit,
|
||||
offset,
|
||||
order,
|
||||
)
|
||||
skipFirstTransaction = userTransactionsCount > offset + limit
|
||||
const decay = !(firstPage > 1)
|
||||
const transactions: Transaction[] = []
|
||||
if (userTransactions.length) {
|
||||
if (order === 'DESC') {
|
||||
userTransactions = userTransactions.reverse()
|
||||
}
|
||||
|
||||
if(offset && order == 'ASC') {
|
||||
offset--;
|
||||
}
|
||||
let [userTransactions, userTransactionsCount] = await UserTransaction.findByUserPaged(user.id, limit, offset, order)
|
||||
skipFirstTransaction = userTransactionsCount > offset + limit
|
||||
const decay = !(firstPage > 1)
|
||||
let transactions: Transaction[] = []
|
||||
if(userTransactions.length) {
|
||||
if(order === 'DESC') {
|
||||
userTransactions = userTransactions.reverse()
|
||||
}
|
||||
let transactions = calculateAndAddDecayTransactions(userTransactions, user, decay, skipFirstTransaction)
|
||||
if(order === 'DESC') {
|
||||
transactions = transactions.reverse()
|
||||
}
|
||||
let transactions = calculateAndAddDecayTransactions(
|
||||
userTransactions,
|
||||
user,
|
||||
decay,
|
||||
skipFirstTransaction,
|
||||
)
|
||||
if (order === 'DESC') {
|
||||
transactions = transactions.reverse()
|
||||
}
|
||||
}
|
||||
|
||||
const transactionList = new TransactionList({
|
||||
gdtSum: 0,
|
||||
count: userTransactionsCount,
|
||||
balance: 0,
|
||||
decay: 0,
|
||||
decay_date: '',
|
||||
transactions: transactions
|
||||
})
|
||||
const transactionList = new TransactionList({
|
||||
gdtSum: 0,
|
||||
count: userTransactionsCount,
|
||||
balance: 0,
|
||||
decay: 0,
|
||||
decay_date: '',
|
||||
transactions: transactions,
|
||||
})
|
||||
|
||||
return transactionList
|
||||
}
|
||||
return transactionList
|
||||
}
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm'
|
||||
import { UserTransaction } from './UserTransaction'
|
||||
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
|
||||
|
||||
// import { Group } from "./Group"
|
||||
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, Timestamp, ManyToOne } from 'typeorm'
|
||||
import { User } from './User'
|
||||
|
||||
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, Timestamp } from 'typeorm'
|
||||
|
||||
@Entity('state_user_transactions')
|
||||
export class UserTransaction extends BaseEntity {
|
||||
@ -21,15 +19,18 @@ export class UserTransaction extends BaseEntity {
|
||||
|
||||
@Column({ name: 'balance_date', type: 'timestamp' })
|
||||
balanceDate: Timestamp
|
||||
|
||||
static findByUserPaged(userId: number, limit: number, offset: number, order: "ASC" | "DESC")
|
||||
:Promise<[UserTransaction[], number]>
|
||||
{
|
||||
|
||||
static findByUserPaged(
|
||||
userId: number,
|
||||
limit: number,
|
||||
offset: number,
|
||||
order: 'ASC' | 'DESC',
|
||||
): Promise<[UserTransaction[], number]> {
|
||||
return this.createQueryBuilder('userTransaction')
|
||||
.where('userTransaction.userId = :userId', { userId })
|
||||
.orderBy('userTransaction.balanceDate', order)
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
.getManyAndCount()
|
||||
.where('userTransaction.userId = :userId', { userId })
|
||||
.orderBy('userTransaction.balanceDate', order)
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
.getManyAndCount()
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user