mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
fixed decay and missing user data bugs
This commit is contained in:
parent
f72164be13
commit
db028d1d78
@ -46,6 +46,7 @@ export class TransactionResolver {
|
||||
)
|
||||
transactions.decayDate = now.toString()
|
||||
}
|
||||
|
||||
return transactions
|
||||
}
|
||||
|
||||
|
||||
@ -18,12 +18,7 @@ async function calculateAndAddDecayTransactions(
|
||||
|
||||
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 = await dbUser.getUsersIndiced(involvedUsersUnique)
|
||||
|
||||
const transactions = await dbTransaction
|
||||
.createQueryBuilder('transaction')
|
||||
@ -43,15 +38,24 @@ async function calculateAndAddDecayTransactions(
|
||||
const transactionIndiced: dbTransaction[] = []
|
||||
transactions.forEach((transaction: dbTransaction) => {
|
||||
transactionIndiced[transaction.id] = transaction
|
||||
if (transaction.transactionTypeId === 2) {
|
||||
involvedUserIds.push(transaction.transactionSendCoin.userId)
|
||||
involvedUserIds.push(transaction.transactionSendCoin.recipiantUserId)
|
||||
}
|
||||
})
|
||||
// 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 = await dbUser.getUsersIndiced(involvedUsersUnique)
|
||||
|
||||
const decayStartTransaction = await Decay.getDecayStartBlock()
|
||||
|
||||
await userTransactions.forEach(async (userTransaction: dbUserTransaction, i: number) => {
|
||||
for (let i = 0; i < userTransactions.length; i++) {
|
||||
const userTransaction = userTransactions[i]
|
||||
const transaction = transactionIndiced[userTransaction.transactionId]
|
||||
const finalTransaction = new Transaction()
|
||||
finalTransaction.transactionId = transaction.id
|
||||
finalTransaction.date = transaction.received.toString()
|
||||
finalTransaction.date = transaction.received.toISOString()
|
||||
finalTransaction.memo = transaction.memo
|
||||
finalTransaction.totalBalance = roundFloorFrom4(userTransaction.balance)
|
||||
const prev = i > 0 ? userTransactions[i - 1] : null
|
||||
@ -137,7 +141,7 @@ async function calculateAndAddDecayTransactions(
|
||||
finalTransactions.push(decayTransaction)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return finalTransactions
|
||||
}
|
||||
|
||||
@ -44,8 +44,8 @@ export class User extends BaseEntity {
|
||||
.where('user.id IN (:...users)', { users: userIds })
|
||||
.getMany()
|
||||
const usersIndiced: User[] = []
|
||||
users.forEach((value, index) => {
|
||||
usersIndiced[index] = value
|
||||
users.forEach((value) => {
|
||||
usersIndiced[value.id] = value
|
||||
})
|
||||
return usersIndiced
|
||||
}
|
||||
|
||||
@ -23,6 +23,7 @@ async function calculateDecayWithInterval(
|
||||
to: number | Date,
|
||||
): Promise<Decay> {
|
||||
const decayStartBlock = await Decay.getDecayStartBlock()
|
||||
|
||||
const result = new Decay(undefined)
|
||||
result.balance = amount
|
||||
const fromMillis = typeof from === 'number' ? from : from.getTime()
|
||||
@ -36,14 +37,16 @@ async function calculateDecayWithInterval(
|
||||
if (decayStartBlock === undefined || decayStartBlock.received.getTime() > toMillis) {
|
||||
return result
|
||||
}
|
||||
const decayStartBlockMillis = decayStartBlock.received.getTime()
|
||||
|
||||
// if decay start date is before start date we calculate decay for full duration
|
||||
if (decayStartBlock.received.getTime() < fromMillis) {
|
||||
if (decayStartBlockMillis < fromMillis) {
|
||||
result.decayDuration = toMillis - fromMillis
|
||||
}
|
||||
// if decay start in between start date and end date we caculcate decay from decay start time to end date
|
||||
else {
|
||||
result.decayDuration = toMillis - decayStartBlock.received.getTime()
|
||||
result.decayDuration = toMillis - decayStartBlockMillis
|
||||
result.decayStart = (decayStartBlockMillis / 1000).toString()
|
||||
}
|
||||
// js use timestamp in milliseconds but we calculate with seconds
|
||||
result.decayDuration /= 1000
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user