Merge branch 'master' into contribution-resolver-logs-and-events

This commit is contained in:
jjimenezgarcia 2022-10-17 23:31:19 +02:00 committed by GitHub
commit 49ea82783d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 8 deletions

View File

@ -73,8 +73,8 @@ describe('TransactionLinkResolver', () => {
deletedAt: null, deletedAt: null,
code: expect.stringMatching(/^[0-9a-f]{24,24}$/), code: expect.stringMatching(/^[0-9a-f]{24,24}$/),
linkEnabled: true, linkEnabled: true,
// amount: '200', amount: expect.decimalEqual(5),
// maxAmountPerMonth: '200', maxAmountPerMonth: expect.decimalEqual(200),
}), }),
) )
}) })
@ -111,6 +111,56 @@ describe('TransactionLinkResolver', () => {
], ],
}) })
}) })
describe('after one day', () => {
beforeAll(async () => {
jest.useFakeTimers()
/* eslint-disable-next-line @typescript-eslint/no-empty-function */
setTimeout(() => {}, 1000 * 60 * 60 * 24)
jest.runAllTimers()
await mutate({
mutation: login,
variables: { email: 'peter@lustig.de', password: 'Aa12345_' },
})
})
afterAll(() => {
jest.useRealTimers()
})
it('allows the user to redeem the contribution link again', async () => {
await expect(
mutate({
mutation: redeemTransactionLink,
variables: {
code: 'CL-' + (contributionLink ? contributionLink.code : ''),
},
}),
).resolves.toMatchObject({
data: {
redeemTransactionLink: true,
},
errors: undefined,
})
})
it('does not allow the user to redeem the contribution link a second time on the same day', async () => {
await expect(
mutate({
mutation: redeemTransactionLink,
variables: {
code: 'CL-' + (contributionLink ? contributionLink.code : ''),
},
}),
).resolves.toMatchObject({
errors: [
new GraphQLError(
'Creation from contribution link was not successful. Error: Contribution link already redeemed today',
),
],
})
})
})
}) })
}) })

View File

@ -1,6 +1,6 @@
import { backendLogger as logger } from '@/server/logger' import { backendLogger as logger } from '@/server/logger'
import { Context, getUser } from '@/server/context' import { Context, getUser } from '@/server/context'
import { getConnection, Between } from '@dbTools/typeorm' import { getConnection } from '@dbTools/typeorm'
import { import {
Resolver, Resolver,
Args, Args,
@ -235,11 +235,16 @@ export class TransactionLinkResolver {
.createQueryBuilder() .createQueryBuilder()
.select('contribution') .select('contribution')
.from(DbContribution, 'contribution') .from(DbContribution, 'contribution')
.where('contribution.contributionLinkId = :linkId AND contribution.userId = :id', { .where(
`contribution.contributionLinkId = :linkId AND contribution.userId = :id
AND Date(contribution.confirmedAt) BETWEEN :start AND :end`,
{
linkId: contributionLink.id, linkId: contributionLink.id,
id: user.id, id: user.id,
contributionDate: Between(start, end), start,
}) end,
},
)
.getOne() .getOne()
if (alreadyRedeemed) { if (alreadyRedeemed) {
logger.error( logger.error(