Merge pull request #2792 from gradido/events-transaction-link

feat(backend): events for transaction links
This commit is contained in:
Ulf Gebhardt 2023-03-28 00:20:55 +02:00 committed by GitHub
commit 6e96f9ee7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 144 additions and 1 deletions

View File

@ -0,0 +1,27 @@
import Decimal from 'decimal.js-light'
import { User as DbUser } from '@entity/User'
import { Transaction as DbTransaction } from '@entity/Transaction'
import { Contribution as DbContribution } from '@entity/Contribution'
import { ContributionLink as DbContributionLink } from '@entity/ContributionLink'
import { Event as DbEvent } from '@entity/Event'
import { Event, EventType } from './Event'
export const EVENT_CONTRIBUTION_LINK_REDEEM = async (
user: DbUser,
transaction: DbTransaction,
contribution: DbContribution,
contributionLink: DbContributionLink,
amount: Decimal,
): Promise<DbEvent> =>
Event(
EventType.CONTRIBUTION_LINK_REDEEM,
user,
user,
null,
transaction,
contribution,
null,
null,
contributionLink,
amount,
).save()

View File

@ -0,0 +1,23 @@
import Decimal from 'decimal.js-light'
import { User as DbUser } from '@entity/User'
import { TransactionLink as DbTransactionLink } from '@entity/TransactionLink'
import { Event as DbEvent } from '@entity/Event'
import { Event, EventType } from './Event'
export const EVENT_TRANSACTION_LINK_CREATE = async (
user: DbUser,
transactionLink: DbTransactionLink,
amount: Decimal,
): Promise<DbEvent> =>
Event(
EventType.TRANSACTION_LINK_CREATE,
user,
user,
null,
null,
null,
null,
transactionLink,
null,
amount,
).save()

View File

@ -0,0 +1,19 @@
import { User as DbUser } from '@entity/User'
import { TransactionLink as DbTransactionLink } from '@entity/TransactionLink'
import { Event as DbEvent } from '@entity/Event'
import { Event, EventType } from './Event'
export const EVENT_TRANSACTION_LINK_DELETE = async (
user: DbUser,
transactionLink: DbTransactionLink,
): Promise<DbEvent> =>
Event(
EventType.TRANSACTION_LINK_DELETE,
user,
user,
null,
null,
null,
null,
transactionLink,
).save()

View File

@ -0,0 +1,24 @@
import Decimal from 'decimal.js-light'
import { User as DbUser } from '@entity/User'
import { TransactionLink as DbTransactionLink } from '@entity/TransactionLink'
import { Event as DbEvent } from '@entity/Event'
import { Event, EventType } from './Event'
export const EVENT_TRANSACTION_LINK_REDEEM = async (
user: DbUser,
senderUser: DbUser,
transactionLink: DbTransactionLink,
amount: Decimal,
): Promise<DbEvent> =>
Event(
EventType.TRANSACTION_LINK_REDEEM,
user,
user,
senderUser,
null,
null,
null,
transactionLink,
null,
amount,
).save()

View File

@ -51,9 +51,13 @@ export { EVENT_CONTRIBUTION_CREATE } from './EVENT_CONTRIBUTION_CREATE'
export { EVENT_CONTRIBUTION_DELETE } from './EVENT_CONTRIBUTION_DELETE' export { EVENT_CONTRIBUTION_DELETE } from './EVENT_CONTRIBUTION_DELETE'
export { EVENT_CONTRIBUTION_UPDATE } from './EVENT_CONTRIBUTION_UPDATE' export { EVENT_CONTRIBUTION_UPDATE } from './EVENT_CONTRIBUTION_UPDATE'
export { EVENT_CONTRIBUTION_MESSAGE_CREATE } from './EVENT_CONTRIBUTION_MESSAGE_CREATE' export { EVENT_CONTRIBUTION_MESSAGE_CREATE } from './EVENT_CONTRIBUTION_MESSAGE_CREATE'
export { EVENT_CONTRIBUTION_LINK_REDEEM } from './EVENT_CONTRIBUTION_LINK_REDEEM'
export { EVENT_LOGIN } from './EVENT_LOGIN' export { EVENT_LOGIN } from './EVENT_LOGIN'
export { EVENT_REGISTER } from './EVENT_REGISTER' export { EVENT_REGISTER } from './EVENT_REGISTER'
export { EVENT_SEND_ACCOUNT_MULTIREGISTRATION_EMAIL } from './EVENT_SEND_ACCOUNT_MULTIREGISTRATION_EMAIL' export { EVENT_SEND_ACCOUNT_MULTIREGISTRATION_EMAIL } from './EVENT_SEND_ACCOUNT_MULTIREGISTRATION_EMAIL'
export { EVENT_SEND_CONFIRMATION_EMAIL } from './EVENT_SEND_CONFIRMATION_EMAIL' export { EVENT_SEND_CONFIRMATION_EMAIL } from './EVENT_SEND_CONFIRMATION_EMAIL'
export { EVENT_TRANSACTION_SEND } from './EVENT_TRANSACTION_SEND' export { EVENT_TRANSACTION_SEND } from './EVENT_TRANSACTION_SEND'
export { EVENT_TRANSACTION_RECEIVE } from './EVENT_TRANSACTION_RECEIVE' export { EVENT_TRANSACTION_RECEIVE } from './EVENT_TRANSACTION_RECEIVE'
export { EVENT_TRANSACTION_LINK_CREATE } from './EVENT_TRANSACTION_LINK_CREATE'
export { EVENT_TRANSACTION_LINK_DELETE } from './EVENT_TRANSACTION_LINK_DELETE'
export { EVENT_TRANSACTION_LINK_REDEEM } from './EVENT_TRANSACTION_LINK_REDEEM'

View File

@ -15,6 +15,7 @@ export enum EventType {
CONTRIBUTION_DELETE = 'CONTRIBUTION_DELETE', CONTRIBUTION_DELETE = 'CONTRIBUTION_DELETE',
CONTRIBUTION_UPDATE = 'CONTRIBUTION_UPDATE', CONTRIBUTION_UPDATE = 'CONTRIBUTION_UPDATE',
CONTRIBUTION_MESSAGE_CREATE = 'CONTRIBUTION_MESSAGE_CREATE', CONTRIBUTION_MESSAGE_CREATE = 'CONTRIBUTION_MESSAGE_CREATE',
CONTRIBUTION_LINK_REDEEM = 'CONTRIBUTION_LINK_REDEEM',
LOGIN = 'LOGIN', LOGIN = 'LOGIN',
REGISTER = 'REGISTER', REGISTER = 'REGISTER',
REDEEM_REGISTER = 'REDEEM_REGISTER', REDEEM_REGISTER = 'REDEEM_REGISTER',
@ -22,6 +23,9 @@ export enum EventType {
SEND_CONFIRMATION_EMAIL = 'SEND_CONFIRMATION_EMAIL', SEND_CONFIRMATION_EMAIL = 'SEND_CONFIRMATION_EMAIL',
TRANSACTION_SEND = 'TRANSACTION_SEND', TRANSACTION_SEND = 'TRANSACTION_SEND',
TRANSACTION_RECEIVE = 'TRANSACTION_RECEIVE', TRANSACTION_RECEIVE = 'TRANSACTION_RECEIVE',
TRANSACTION_LINK_CREATE = 'TRANSACTION_LINK_CREATE',
TRANSACTION_LINK_DELETE = 'TRANSACTION_LINK_DELETE',
TRANSACTION_LINK_REDEEM = 'TRANSACTION_LINK_REDEEM',
// VISIT_GRADIDO = 'VISIT_GRADIDO', // VISIT_GRADIDO = 'VISIT_GRADIDO',
// VERIFY_REDEEM = 'VERIFY_REDEEM', // VERIFY_REDEEM = 'VERIFY_REDEEM',
// INACTIVE_ACCOUNT = 'INACTIVE_ACCOUNT', // INACTIVE_ACCOUNT = 'INACTIVE_ACCOUNT',

View File

@ -33,6 +33,9 @@ import Decimal from 'decimal.js-light'
import { GraphQLError } from 'graphql' import { GraphQLError } from 'graphql'
import { TRANSACTIONS_LOCK } from '@/util/TRANSACTIONS_LOCK' import { TRANSACTIONS_LOCK } from '@/util/TRANSACTIONS_LOCK'
import { logger } from '@test/testSetup' import { logger } from '@test/testSetup'
import { EventType } from '@/event/Event'
import { Event as DbEvent } from '@entity/Event'
import { UserContact } from '@entity/UserContact'
// mock semaphore to allow use fake timers // mock semaphore to allow use fake timers
jest.mock('@/util/TRANSACTIONS_LOCK') jest.mock('@/util/TRANSACTIONS_LOCK')
@ -445,6 +448,24 @@ describe('TransactionLinkResolver', () => {
}) })
}) })
it('stores the CONTRIBUTION_LINK_REDEEM event in the database', async () => {
const userConatct = await UserContact.findOneOrFail(
{ email: 'bibi@bloxberg.de' },
{ relations: ['user'] },
)
await expect(DbEvent.find()).resolves.toContainEqual(
expect.objectContaining({
type: EventType.CONTRIBUTION_LINK_REDEEM,
affectedUserId: userConatct.user.id,
actingUserId: userConatct.user.id,
involvedTransactionId: expect.any(Number),
involvedContributionId: expect.any(Number),
involvedContributionLinkId: contributionLink?.id,
amount: contributionLink?.amount,
}),
)
})
it('does not allow the user to redeem the contribution link a second time on the same day', async () => { it('does not allow the user to redeem the contribution link a second time on the same day', async () => {
jest.clearAllMocks() jest.clearAllMocks()
await expect( await expect(

View File

@ -35,6 +35,12 @@ import LogError from '@/server/LogError'
import { getLastTransaction } from './util/getLastTransaction' import { getLastTransaction } from './util/getLastTransaction'
import transactionLinkList from './util/transactionLinkList' import transactionLinkList from './util/transactionLinkList'
import {
EVENT_CONTRIBUTION_LINK_REDEEM,
EVENT_TRANSACTION_LINK_CREATE,
EVENT_TRANSACTION_LINK_DELETE,
EVENT_TRANSACTION_LINK_REDEEM,
} from '@/event/Event'
// TODO: do not export, test it inside the resolver // TODO: do not export, test it inside the resolver
export const transactionLinkCode = (date: Date): string => { export const transactionLinkCode = (date: Date): string => {
@ -89,6 +95,7 @@ export class TransactionLinkResolver {
await DbTransactionLink.save(transactionLink).catch((e) => { await DbTransactionLink.save(transactionLink).catch((e) => {
throw new LogError('Unable to save transaction link', e) throw new LogError('Unable to save transaction link', e)
}) })
await EVENT_TRANSACTION_LINK_CREATE(user, transactionLink, amount)
return new TransactionLink(transactionLink, new User(user)) return new TransactionLink(transactionLink, new User(user))
} }
@ -122,6 +129,8 @@ export class TransactionLinkResolver {
throw new LogError('Transaction link could not be deleted', e) throw new LogError('Transaction link could not be deleted', e)
}) })
await EVENT_TRANSACTION_LINK_DELETE(user, transactionLink)
return true return true
} }
@ -272,7 +281,13 @@ export class TransactionLinkResolver {
await queryRunner.manager.update(DbContribution, { id: contribution.id }, contribution) await queryRunner.manager.update(DbContribution, { id: contribution.id }, contribution)
await queryRunner.commitTransaction() await queryRunner.commitTransaction()
logger.info('creation from contribution link commited successfuly.') await EVENT_CONTRIBUTION_LINK_REDEEM(
user,
transaction,
contribution,
contributionLink,
contributionLink.amount,
)
} catch (e) { } catch (e) {
await queryRunner.rollbackTransaction() await queryRunner.rollbackTransaction()
throw new LogError('Creation from contribution link was not successful', e) throw new LogError('Creation from contribution link was not successful', e)
@ -321,6 +336,12 @@ export class TransactionLinkResolver {
user, user,
transactionLink, transactionLink,
) )
await EVENT_TRANSACTION_LINK_REDEEM(
user,
{ id: transactionLink.userId } as DbUser,
transactionLink,
transactionLink.amount,
)
return true return true
} }