mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
work on implementing
This commit is contained in:
parent
e80cea5fc4
commit
e2f6d1a006
@ -10,18 +10,22 @@ import { Decay } from './Decay'
|
||||
|
||||
@ObjectType()
|
||||
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 ? new Decay(json.decay) : undefined
|
||||
constructor()
|
||||
constructor(json: any)
|
||||
constructor(json?: any) {
|
||||
if(json) {
|
||||
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 ? new Decay(json.decay) : undefined
|
||||
}
|
||||
}
|
||||
|
||||
@Field(() => String)
|
||||
|
||||
@ -1,16 +1,64 @@
|
||||
import { User as dbUser } from '../../typeorm/entity/User'
|
||||
import { TransactionList, Transaction } from '../models/Transaction'
|
||||
import { UserTransaction } from '../../typeorm/entity/UserTransaction'
|
||||
import { UserTransaction as dbUserTransaction } from '../../typeorm/entity/UserTransaction'
|
||||
import { Transaction as dbTransaction } from '../../typeorm/entity/Transaction'
|
||||
import { TransactionSendCoin as dbTransactionSendCoin} from '../../typeorm/entity/TransactionSendCoin'
|
||||
import { TransactionCreation as dbTransactionCreation} from '../../typeorm/entity/TransactionCreation'
|
||||
import calculateDecay from '../../util/decay'
|
||||
import { roundFloorFrom4 } from '../../util/round'
|
||||
|
||||
function calculateAndAddDecayTransactions(
|
||||
userTransactions: UserTransaction[],
|
||||
async function calculateAndAddDecayTransactions(
|
||||
userTransactions: dbUserTransaction[],
|
||||
user: dbUser,
|
||||
decay: boolean,
|
||||
skipFirstTransaction: boolean,
|
||||
): Transaction[] {
|
||||
const transactions: Transaction[] = []
|
||||
): Promise<Transaction[]> {
|
||||
let finalTransactions: Transaction[] = []
|
||||
let transactionIds: number[] = []
|
||||
let involvedUserIds: number[] = []
|
||||
|
||||
return transactions
|
||||
userTransactions.forEach((userTransaction: dbUserTransaction) => {
|
||||
transactionIds.push(userTransaction.transactionId)
|
||||
involvedUserIds.push(userTransaction.userId)
|
||||
})
|
||||
// remove duplicates
|
||||
// https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates
|
||||
const involvedUsersUnique = involvedUserIds.filter((v, i, a) => a.indexOf(v) === i)
|
||||
const userIndiced = dbUser.getUsersIndiced(involvedUsersUnique)
|
||||
|
||||
const transactions = await dbTransaction
|
||||
.createQueryBuilder('transaction')
|
||||
.where('transaction.id IN (:...transactions)', { transactions: transactionIds})
|
||||
.leftJoinAndSelect('transaction.sendCoin', 'transactionSendCoin', 'transactionSendCoin.transactionid = transaction.id')
|
||||
.leftJoinAndSelect('transaction.creation', 'transactionCreation', 'transactionSendCoin.transactionid = transaction.id')
|
||||
.getMany()
|
||||
|
||||
let transactionIndiced: dbTransaction[] = []
|
||||
transactions.forEach((transaction: dbTransaction) => {
|
||||
transactionIndiced[transaction.id] = transaction
|
||||
})
|
||||
|
||||
const decayStartTransaction = await dbTransaction.createQueryBuilder('transaction')
|
||||
.where('transaction.transactionTypeId = :transactionTypeId', { transactionTypeId: 9})
|
||||
.orderBy('received', 'ASC')
|
||||
.getOne()
|
||||
|
||||
userTransactions.forEach((userTransaction: dbUserTransaction, i:number) => {
|
||||
const transaction = transactionIndiced[userTransaction.transactionId]
|
||||
let finalTransaction = new Transaction
|
||||
finalTransaction.transactionId = transaction.id
|
||||
finalTransaction.date = transaction.received.toString()
|
||||
finalTransaction.memo = transaction.memo
|
||||
|
||||
let prev = i > 0 ? userTransactions[i-1] : null
|
||||
if(prev && prev.balance > 0) {
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
|
||||
return finalTransactions
|
||||
}
|
||||
|
||||
export default async function listTransactions(
|
||||
|
||||
@ -20,4 +20,11 @@ export class Transaction extends BaseEntity {
|
||||
@Column({ name: 'blockchain_type_id' })
|
||||
blockchainTypeId: number
|
||||
|
||||
static async findByTransactionTypeId(transactionTypeId: number): Promise<Transaction[]> {
|
||||
return this.createQueryBuilder('transaction')
|
||||
.where('transaction.transactionTypeId = :transactionTypeId', { transactionTypeId: transactionTypeId})
|
||||
.getMany()
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -32,4 +32,16 @@ export class User extends BaseEntity {
|
||||
.where('hex(user.pubkey) = :pubkeyHex', { pubkeyHex })
|
||||
.getOneOrFail()
|
||||
}
|
||||
|
||||
static async getUsersIndiced(userIds: number[]): Promise<User[]> {
|
||||
const users = await this.createQueryBuilder('user')
|
||||
.select(['user.id', 'user.firstName', 'user.lastName', 'user.email'])
|
||||
.where('user.id IN (:...users)', { users: userIds})
|
||||
.getMany()
|
||||
let usersIndiced: User[] = []
|
||||
users.forEach((value, index) => {
|
||||
usersIndiced[index] = value
|
||||
})
|
||||
return usersIndiced
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user