From 55236f1f8e9e0443f02db2a1e271501400543724 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 21 Dec 2022 00:08:46 +0100 Subject: [PATCH] fix another order by `id` instead of `balanceDate`. Have the now calculation for contribution links within the semaphore lock --- backend/src/graphql/resolver/TransactionLinkResolver.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/backend/src/graphql/resolver/TransactionLinkResolver.ts b/backend/src/graphql/resolver/TransactionLinkResolver.ts index 5ab23f2b0..983420e2a 100644 --- a/backend/src/graphql/resolver/TransactionLinkResolver.ts +++ b/backend/src/graphql/resolver/TransactionLinkResolver.ts @@ -166,13 +166,13 @@ export class TransactionLinkResolver { ): Promise { const clientTimezoneOffset = getClientTimezoneOffset(context) const user = getUser(context) - const now = new Date() if (code.match(/^CL-/)) { // acquire lock const releaseLock = await TRANSACTIONS_LOCK.acquire() console.log(`locked for redeemTransactionLink ${code}`) logger.info('redeem contribution link...') + const now = new Date() const queryRunner = getConnection().createQueryRunner() await queryRunner.connect() await queryRunner.startTransaction('REPEATABLE READ') @@ -277,7 +277,7 @@ export class TransactionLinkResolver { .select('transaction') .from(DbTransaction, 'transaction') .where('transaction.userId = :id', { id: user.id }) - .orderBy('transaction.balanceDate', 'DESC') + .orderBy('transaction.id', 'DESC') .getOne() let newBalance = new Decimal(0) @@ -318,6 +318,7 @@ export class TransactionLinkResolver { } return true } else { + const now = new Date() const transactionLink = await DbTransactionLink.findOneOrFail({ code }) const linkedUser = await DbUser.findOneOrFail( { id: transactionLink.userId }, @@ -328,6 +329,9 @@ export class TransactionLinkResolver { throw new Error('Cannot redeem own transaction link.') } + // TODO: The now check should be done within the semaphore lock, + // since the program might wait a while till it is ready to proceed + // writing the transaction. if (transactionLink.validUntil.getTime() < now.getTime()) { throw new Error('Transaction Link is not valid anymore.') }