From d349a46f5d09157736de553f36f6ef2712e9a429 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Thu, 31 Mar 2022 12:08:09 +0200 Subject: [PATCH 01/36] Balance Resolver: calculate Decay on full sum, not including the links --- backend/src/graphql/resolver/BalanceResolver.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/backend/src/graphql/resolver/BalanceResolver.ts b/backend/src/graphql/resolver/BalanceResolver.ts index 672e07b12..f5a9f6371 100644 --- a/backend/src/graphql/resolver/BalanceResolver.ts +++ b/backend/src/graphql/resolver/BalanceResolver.ts @@ -59,14 +59,17 @@ export class BalanceResolver { ? { sumHoldAvailableAmount: context.sumHoldAvailableAmount } : await transactionLinkRepository.summary(user.id, now) + // The decay is always calculated on the last booked transaction const calculatedDecay = calculateDecay( - lastTransaction.balance.minus(sumHoldAvailableAmount.toString()), + lastTransaction.balance, lastTransaction.balanceDate, now, ) return new Balance({ - balance: calculatedDecay.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN), // round towards zero + balance: calculatedDecay.balance + .minus(sumHoldAvailableAmount.toString()) + .toDecimalPlaces(2, Decimal.ROUND_DOWN), // round towards zero decay: calculatedDecay.decay.toDecimalPlaces(2, Decimal.ROUND_FLOOR), // round towards - infinity lastBookedBalance: lastTransaction.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN), balanceGDT, From 592e16e2602cb077aa0313178d8b8fbcb0cc0bef Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Thu, 31 Mar 2022 12:09:00 +0200 Subject: [PATCH 02/36] virtual decay transaction is based on full sum not including the link values --- backend/src/graphql/resolver/TransactionResolver.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/backend/src/graphql/resolver/TransactionResolver.ts b/backend/src/graphql/resolver/TransactionResolver.ts index 461a70a00..14d44dc2e 100644 --- a/backend/src/graphql/resolver/TransactionResolver.ts +++ b/backend/src/graphql/resolver/TransactionResolver.ts @@ -198,13 +198,10 @@ export class TransactionResolver { // decay & link transactions if (currentPage === 1 && order === Order.DESC) { + // The virtual decay is always on the booked amount, not including the generated, not yet booked links, + // since the decay is substantially different when the amount is less transactions.push( - virtualDecayTransaction( - lastTransaction.balance.minus(sumHoldAvailableAmount.toString()), - lastTransaction.balanceDate, - now, - self, - ), + virtualDecayTransaction(lastTransaction.balance, lastTransaction.balanceDate, now, self), ) // virtual transaction for pending transaction-links sum if (sumHoldAvailableAmount.greaterThan(0)) { From 9ed00edc52f6c4c1be4197bf9883543ddff19b24 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Thu, 31 Mar 2022 12:14:38 +0200 Subject: [PATCH 03/36] code order & comment --- backend/src/graphql/resolver/BalanceResolver.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/backend/src/graphql/resolver/BalanceResolver.ts b/backend/src/graphql/resolver/BalanceResolver.ts index f5a9f6371..f30e779e5 100644 --- a/backend/src/graphql/resolver/BalanceResolver.ts +++ b/backend/src/graphql/resolver/BalanceResolver.ts @@ -54,11 +54,6 @@ export class BalanceResolver { }, }) - const transactionLinkRepository = getCustomRepository(TransactionLinkRepository) - const { sumHoldAvailableAmount } = context.sumHoldAvailableAmount - ? { sumHoldAvailableAmount: context.sumHoldAvailableAmount } - : await transactionLinkRepository.summary(user.id, now) - // The decay is always calculated on the last booked transaction const calculatedDecay = calculateDecay( lastTransaction.balance, @@ -66,6 +61,12 @@ export class BalanceResolver { now, ) + // The final balance is reduced by the link amount withheld + const transactionLinkRepository = getCustomRepository(TransactionLinkRepository) + const { sumHoldAvailableAmount } = context.sumHoldAvailableAmount + ? { sumHoldAvailableAmount: context.sumHoldAvailableAmount } + : await transactionLinkRepository.summary(user.id, now) + return new Balance({ balance: calculatedDecay.balance .minus(sumHoldAvailableAmount.toString()) From a9ba92f67bb207bd8ba744861c0ef64add4918b2 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Thu, 31 Mar 2022 12:32:35 +0200 Subject: [PATCH 04/36] Transaction Resolver: transmit Redeem Link to calculateBalance --- backend/src/graphql/resolver/TransactionResolver.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/backend/src/graphql/resolver/TransactionResolver.ts b/backend/src/graphql/resolver/TransactionResolver.ts index 461a70a00..240369006 100644 --- a/backend/src/graphql/resolver/TransactionResolver.ts +++ b/backend/src/graphql/resolver/TransactionResolver.ts @@ -57,7 +57,12 @@ export const executeTransaction = async ( // validate amount 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) { throw new Error("user hasn't enough GDD or amount is < 0") } From fa7f2b9d87581f7c07dee6c64b8a01e3697de355 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Thu, 31 Mar 2022 12:33:29 +0200 Subject: [PATCH 05/36] calculateBalance: If we redeem a link, make sure we do not consider its amount as blocked when calculating the balance --- backend/src/util/validate.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/backend/src/util/validate.ts b/backend/src/util/validate.ts index 95e1bf699..5d2f39aa0 100644 --- a/backend/src/util/validate.ts +++ b/backend/src/util/validate.ts @@ -4,6 +4,7 @@ import { Transaction } from '@entity/Transaction' import { Decay } from '@model/Decay' import { getCustomRepository } from '@dbTools/typeorm' import { TransactionLinkRepository } from '@repository/TransactionLink' +import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink' function isStringBoolean(value: string): boolean { const lowerValue = value.toLowerCase() @@ -21,6 +22,7 @@ async function calculateBalance( userId: number, amount: Decimal, time: Date, + transactionLink?: dbTransactionLink | null, ): Promise<{ balance: Decimal; decay: Decay; lastTransactionId: number } | null> { const lastTransaction = await Transaction.findOne({ userId }, { order: { balanceDate: 'DESC' } }) if (!lastTransaction) return null @@ -32,7 +34,13 @@ async function calculateBalance( const transactionLinkRepository = getCustomRepository(TransactionLinkRepository) 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 calculated as blocked + // else we cannot redeem links which are more or equal to half of what an account actually owns + const sumHoldAvailableAmountMinusTransactionLink = transactionLink + ? sumHoldAvailableAmount.minus(transactionLink.amount.toString()) + : sumHoldAvailableAmount + + if (balance.minus(sumHoldAvailableAmountMinusTransactionLink.toString()).lessThan(0)) { return null } return { balance, lastTransactionId: lastTransaction.id, decay } From 04807bd225603f6c88c60052e51bb90cf24c4e93 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Thu, 31 Mar 2022 12:34:09 +0200 Subject: [PATCH 06/36] corrected comment --- backend/src/util/validate.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/util/validate.ts b/backend/src/util/validate.ts index 5d2f39aa0..1a0ce02ce 100644 --- a/backend/src/util/validate.ts +++ b/backend/src/util/validate.ts @@ -34,7 +34,7 @@ async function calculateBalance( const transactionLinkRepository = getCustomRepository(TransactionLinkRepository) const { sumHoldAvailableAmount } = await transactionLinkRepository.summary(userId, time) - // If we want to redeem a link we need to make sure that the link amount is not calculated as blocked + // 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 sumHoldAvailableAmountMinusTransactionLink = transactionLink ? sumHoldAvailableAmount.minus(transactionLink.amount.toString()) From d0dc8dc0fd653a3ef3cbb158dfc513be37192187 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Thu, 31 Mar 2022 13:16:53 +0200 Subject: [PATCH 07/36] the backend returns an array of errors, we display the first --- frontend/src/pages/Send.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/pages/Send.vue b/frontend/src/pages/Send.vue index 270187bec..903de6e49 100644 --- a/frontend/src/pages/Send.vue +++ b/frontend/src/pages/Send.vue @@ -134,7 +134,7 @@ export default { this.currentTransactionStep = TRANSACTION_STEPS.transactionResultSendSuccess }) .catch((err) => { - this.errorResult = err.message + this.errorResult = err[0].message this.error = true this.currentTransactionStep = TRANSACTION_STEPS.transactionResultSendError }) From 06dcb2380e19892587ac120e4e8420e0162c1be6 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 31 Mar 2022 13:48:15 +0200 Subject: [PATCH 08/36] seed creations months ago from now --- .../src/seeds/creation/CreationInterface.ts | 2 ++ backend/src/seeds/creation/index.ts | 13 ++++++------ backend/src/seeds/factory/creation.ts | 21 +++++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/backend/src/seeds/creation/CreationInterface.ts b/backend/src/seeds/creation/CreationInterface.ts index 8723f441d..3e0f3cf6c 100644 --- a/backend/src/seeds/creation/CreationInterface.ts +++ b/backend/src/seeds/creation/CreationInterface.ts @@ -4,4 +4,6 @@ export interface CreationInterface { memo: string creationDate: string confirmed?: boolean + // number of months to move the confirmed creation to the past + moveCreationDate?: number } diff --git a/backend/src/seeds/creation/index.ts b/backend/src/seeds/creation/index.ts index 7396a7ec8..20d30d94c 100644 --- a/backend/src/seeds/creation/index.ts +++ b/backend/src/seeds/creation/index.ts @@ -1,29 +1,28 @@ import { CreationInterface } from './CreationInterface' - -const lastMonth = (date: Date): string => { - return new Date(date.getFullYear(), date.getMonth() - 1, 1).toISOString() -} +import { nMonthsBefore } from '../factory/creation' export const creations: CreationInterface[] = [ { email: 'bibi@bloxberg.de', amount: 1000, memo: 'Herzlich Willkommen bei Gradido!', - creationDate: lastMonth(new Date()), + creationDate: nMonthsBefore(new Date()), confirmed: true, + moveCreationDate: 12, }, { email: 'bob@baumeister.de', amount: 1000, memo: 'Herzlich Willkommen bei Gradido!', - creationDate: lastMonth(new Date()), + creationDate: nMonthsBefore(new Date()), confirmed: true, + moveCreationDate: 8, }, { email: 'raeuber@hotzenplotz.de', amount: 1000, memo: 'Herzlich Willkommen bei Gradido!', - creationDate: lastMonth(new Date()), + creationDate: nMonthsBefore(new Date()), confirmed: true, }, ] diff --git a/backend/src/seeds/factory/creation.ts b/backend/src/seeds/factory/creation.ts index d4d4c8101..64f693360 100644 --- a/backend/src/seeds/factory/creation.ts +++ b/backend/src/seeds/factory/creation.ts @@ -6,9 +6,14 @@ import { login } from '@/seeds/graphql/queries' import { CreationInterface } from '@/seeds/creation/CreationInterface' import { ApolloServerTestClient } from 'apollo-server-testing' import { User } from '@entity/User' +import { Transaction } from '@entity/Transaction' import { AdminPendingCreation } from '@entity/AdminPendingCreation' // 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 ( client: ApolloServerTestClient, creation: CreationInterface, @@ -34,5 +39,21 @@ export const creationFactory = async ( }) 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() + } + } } } From aee53d17c0e9badfee8d0405094f39e109754355 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 31 Mar 2022 14:11:32 +0200 Subject: [PATCH 09/36] error message from server is toasted correctly --- frontend/src/pages/Send.spec.js | 4 +--- frontend/src/pages/Send.vue | 6 +++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/frontend/src/pages/Send.spec.js b/frontend/src/pages/Send.spec.js index 9fe774e33..802fdf8a7 100644 --- a/frontend/src/pages/Send.spec.js +++ b/frontend/src/pages/Send.spec.js @@ -155,8 +155,6 @@ describe('Send', () => { }) }) - /* LINK */ - describe('transaction form link', () => { beforeEach(async () => { apolloMutationMock.mockResolvedValue({ @@ -261,7 +259,7 @@ describe('Send', () => { }) it('toasts an error message', () => { - expect(toastErrorSpy).toBeCalledWith({ message: 'OUCH!' }) + expect(toastErrorSpy).toBeCalledWith('OUCH!') }) }) }) diff --git a/frontend/src/pages/Send.vue b/frontend/src/pages/Send.vue index 903de6e49..668e24493 100644 --- a/frontend/src/pages/Send.vue +++ b/frontend/src/pages/Send.vue @@ -133,8 +133,8 @@ export default { this.transactionData = { ...EMPTY_TRANSACTION_DATA } this.currentTransactionStep = TRANSACTION_STEPS.transactionResultSendSuccess }) - .catch((err) => { - this.errorResult = err[0].message + .catch((error) => { + this.errorResult = error.message this.error = true this.currentTransactionStep = TRANSACTION_STEPS.transactionResultSendError }) @@ -153,7 +153,7 @@ export default { this.updateTransactions({}) }) .catch((error) => { - this.toastError(error) + this.toastError(error.message) }) break default: From 69b1cf7a06287ea0826ba626a563a9dfd50a8cfc Mon Sep 17 00:00:00 2001 From: ogerly Date: Thu, 31 Mar 2022 14:47:48 +0200 Subject: [PATCH 10/36] add extra disabled variable for send emit, disabled send by emit --- .../GddSend/TransactionConfirmationSend.vue | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/frontend/src/components/GddSend/TransactionConfirmationSend.vue b/frontend/src/components/GddSend/TransactionConfirmationSend.vue index 2d85fa33f..6ac367795 100644 --- a/frontend/src/components/GddSend/TransactionConfirmationSend.vue +++ b/frontend/src/components/GddSend/TransactionConfirmationSend.vue @@ -58,7 +58,11 @@ {{ $t('form.cancel') }} - + {{ $t('form.send_now') }} @@ -76,6 +80,11 @@ export default { loading: { type: Boolean, required: true }, selected: { type: String, required: true }, }, + data() { + return { + disabled: this.loading, + } + }, }