Merge branch 'master' into fix_decay

This commit is contained in:
Moriz Wahl 2022-03-31 22:30:52 +02:00 committed by GitHub
commit 447fdb6057
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 15 deletions

View File

@ -57,7 +57,12 @@ export const executeTransaction = async (
// validate amount // validate amount
const receivedCallDate = new Date() const receivedCallDate = new Date()
const sendBalance = await calculateBalance(sender.id, amount.mul(-1), receivedCallDate) const sendBalance = await calculateBalance(
sender.id,
amount.mul(-1),
receivedCallDate,
transactionLink,
)
if (!sendBalance) { if (!sendBalance) {
throw new Error("user hasn't enough GDD or amount is < 0") throw new Error("user hasn't enough GDD or amount is < 0")
} }

View File

@ -4,4 +4,6 @@ export interface CreationInterface {
memo: string memo: string
creationDate: string creationDate: string
confirmed?: boolean confirmed?: boolean
// number of months to move the confirmed creation to the past
moveCreationDate?: number
} }

View File

@ -1,29 +1,28 @@
import { CreationInterface } from './CreationInterface' import { CreationInterface } from './CreationInterface'
import { nMonthsBefore } from '../factory/creation'
const lastMonth = (date: Date): string => {
return new Date(date.getFullYear(), date.getMonth() - 1, 1).toISOString()
}
export const creations: CreationInterface[] = [ export const creations: CreationInterface[] = [
{ {
email: 'bibi@bloxberg.de', email: 'bibi@bloxberg.de',
amount: 1000, amount: 1000,
memo: 'Herzlich Willkommen bei Gradido!', memo: 'Herzlich Willkommen bei Gradido!',
creationDate: lastMonth(new Date()), creationDate: nMonthsBefore(new Date()),
confirmed: true, confirmed: true,
moveCreationDate: 12,
}, },
{ {
email: 'bob@baumeister.de', email: 'bob@baumeister.de',
amount: 1000, amount: 1000,
memo: 'Herzlich Willkommen bei Gradido!', memo: 'Herzlich Willkommen bei Gradido!',
creationDate: lastMonth(new Date()), creationDate: nMonthsBefore(new Date()),
confirmed: true, confirmed: true,
moveCreationDate: 8,
}, },
{ {
email: 'raeuber@hotzenplotz.de', email: 'raeuber@hotzenplotz.de',
amount: 1000, amount: 1000,
memo: 'Herzlich Willkommen bei Gradido!', memo: 'Herzlich Willkommen bei Gradido!',
creationDate: lastMonth(new Date()), creationDate: nMonthsBefore(new Date()),
confirmed: true, confirmed: true,
}, },
] ]

View File

@ -6,9 +6,14 @@ import { login } from '@/seeds/graphql/queries'
import { CreationInterface } from '@/seeds/creation/CreationInterface' import { CreationInterface } from '@/seeds/creation/CreationInterface'
import { ApolloServerTestClient } from 'apollo-server-testing' import { ApolloServerTestClient } from 'apollo-server-testing'
import { User } from '@entity/User' import { User } from '@entity/User'
import { Transaction } from '@entity/Transaction'
import { AdminPendingCreation } from '@entity/AdminPendingCreation' import { AdminPendingCreation } from '@entity/AdminPendingCreation'
// import CONFIG from '@/config/index' // import CONFIG from '@/config/index'
export const nMonthsBefore = (date: Date, months = 1): string => {
return new Date(date.getFullYear(), date.getMonth() - months, 1).toISOString()
}
export const creationFactory = async ( export const creationFactory = async (
client: ApolloServerTestClient, client: ApolloServerTestClient,
creation: CreationInterface, creation: CreationInterface,
@ -34,5 +39,21 @@ export const creationFactory = async (
}) })
await mutate({ mutation: confirmPendingCreation, variables: { id: pendingCreation.id } }) await mutate({ mutation: confirmPendingCreation, variables: { id: pendingCreation.id } })
if (creation.moveCreationDate) {
const transaction = await Transaction.findOneOrFail({
where: { userId: user.id, creationDate: new Date(creation.creationDate) },
order: { balanceDate: 'DESC' },
})
if (transaction.decay.equals(0) && transaction.creationDate) {
transaction.creationDate = new Date(
nMonthsBefore(transaction.creationDate, creation.moveCreationDate),
)
transaction.balanceDate = new Date(
nMonthsBefore(transaction.balanceDate, creation.moveCreationDate),
)
await transaction.save()
}
}
} }
} }

View File

@ -4,6 +4,7 @@ import { Transaction } from '@entity/Transaction'
import { Decay } from '@model/Decay' import { Decay } from '@model/Decay'
import { getCustomRepository } from '@dbTools/typeorm' import { getCustomRepository } from '@dbTools/typeorm'
import { TransactionLinkRepository } from '@repository/TransactionLink' import { TransactionLinkRepository } from '@repository/TransactionLink'
import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink'
function isStringBoolean(value: string): boolean { function isStringBoolean(value: string): boolean {
const lowerValue = value.toLowerCase() const lowerValue = value.toLowerCase()
@ -21,6 +22,7 @@ async function calculateBalance(
userId: number, userId: number,
amount: Decimal, amount: Decimal,
time: Date, time: Date,
transactionLink?: dbTransactionLink | null,
): Promise<{ balance: Decimal; decay: Decay; lastTransactionId: number } | null> { ): Promise<{ balance: Decimal; decay: Decay; lastTransactionId: number } | null> {
const lastTransaction = await Transaction.findOne({ userId }, { order: { balanceDate: 'DESC' } }) const lastTransaction = await Transaction.findOne({ userId }, { order: { balanceDate: 'DESC' } })
if (!lastTransaction) return null if (!lastTransaction) return null
@ -32,7 +34,13 @@ async function calculateBalance(
const transactionLinkRepository = getCustomRepository(TransactionLinkRepository) const transactionLinkRepository = getCustomRepository(TransactionLinkRepository)
const { sumHoldAvailableAmount } = await transactionLinkRepository.summary(userId, time) const { sumHoldAvailableAmount } = await transactionLinkRepository.summary(userId, time)
if (balance.minus(sumHoldAvailableAmount.toString()).lessThan(0)) { // If we want to redeem a link we need to make sure that the link amount is not considered as blocked
// else we cannot redeem links which are more or equal to half of what an account actually owns
const releasedLinkAmount = transactionLink ? transactionLink.holdAvailableAmount : new Decimal(0)
if (
balance.minus(sumHoldAvailableAmount.toString()).plus(releasedLinkAmount.toString()).lessThan(0)
) {
return null return null
} }
return { balance, lastTransactionId: lastTransaction.id, decay } return { balance, lastTransactionId: lastTransaction.id, decay }

View File

@ -155,8 +155,6 @@ describe('Send', () => {
}) })
}) })
/* LINK */
describe('transaction form link', () => { describe('transaction form link', () => {
beforeEach(async () => { beforeEach(async () => {
apolloMutationMock.mockResolvedValue({ apolloMutationMock.mockResolvedValue({
@ -261,7 +259,7 @@ describe('Send', () => {
}) })
it('toasts an error message', () => { it('toasts an error message', () => {
expect(toastErrorSpy).toBeCalledWith({ message: 'OUCH!' }) expect(toastErrorSpy).toBeCalledWith('OUCH!')
}) })
}) })
}) })

View File

@ -133,8 +133,8 @@ export default {
this.transactionData = { ...EMPTY_TRANSACTION_DATA } this.transactionData = { ...EMPTY_TRANSACTION_DATA }
this.currentTransactionStep = TRANSACTION_STEPS.transactionResultSendSuccess this.currentTransactionStep = TRANSACTION_STEPS.transactionResultSendSuccess
}) })
.catch((err) => { .catch((error) => {
this.errorResult = err.message this.errorResult = error.message
this.error = true this.error = true
this.currentTransactionStep = TRANSACTION_STEPS.transactionResultSendError this.currentTransactionStep = TRANSACTION_STEPS.transactionResultSendError
}) })
@ -153,7 +153,7 @@ export default {
this.updateTransactions({}) this.updateTransactions({})
}) })
.catch((error) => { .catch((error) => {
this.toastError(error) this.toastError(error.message)
}) })
break break
default: default: