mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Merge pull request #2792 from gradido/events-transaction-link
feat(backend): events for transaction links
This commit is contained in:
commit
6e96f9ee7c
27
backend/src/event/EVENT_CONTRIBUTION_LINK_REDEEM.ts
Normal file
27
backend/src/event/EVENT_CONTRIBUTION_LINK_REDEEM.ts
Normal 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()
|
||||
23
backend/src/event/EVENT_TRANSACTION_LINK_CREATE.ts
Normal file
23
backend/src/event/EVENT_TRANSACTION_LINK_CREATE.ts
Normal 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()
|
||||
19
backend/src/event/EVENT_TRANSACTION_LINK_DELETE.ts
Normal file
19
backend/src/event/EVENT_TRANSACTION_LINK_DELETE.ts
Normal 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()
|
||||
24
backend/src/event/EVENT_TRANSACTION_LINK_REDEEM.ts
Normal file
24
backend/src/event/EVENT_TRANSACTION_LINK_REDEEM.ts
Normal 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()
|
||||
@ -51,9 +51,13 @@ export { EVENT_CONTRIBUTION_CREATE } from './EVENT_CONTRIBUTION_CREATE'
|
||||
export { EVENT_CONTRIBUTION_DELETE } from './EVENT_CONTRIBUTION_DELETE'
|
||||
export { EVENT_CONTRIBUTION_UPDATE } from './EVENT_CONTRIBUTION_UPDATE'
|
||||
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_REGISTER } from './EVENT_REGISTER'
|
||||
export { EVENT_SEND_ACCOUNT_MULTIREGISTRATION_EMAIL } from './EVENT_SEND_ACCOUNT_MULTIREGISTRATION_EMAIL'
|
||||
export { EVENT_SEND_CONFIRMATION_EMAIL } from './EVENT_SEND_CONFIRMATION_EMAIL'
|
||||
export { EVENT_TRANSACTION_SEND } from './EVENT_TRANSACTION_SEND'
|
||||
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'
|
||||
|
||||
@ -15,6 +15,7 @@ export enum EventType {
|
||||
CONTRIBUTION_DELETE = 'CONTRIBUTION_DELETE',
|
||||
CONTRIBUTION_UPDATE = 'CONTRIBUTION_UPDATE',
|
||||
CONTRIBUTION_MESSAGE_CREATE = 'CONTRIBUTION_MESSAGE_CREATE',
|
||||
CONTRIBUTION_LINK_REDEEM = 'CONTRIBUTION_LINK_REDEEM',
|
||||
LOGIN = 'LOGIN',
|
||||
REGISTER = 'REGISTER',
|
||||
REDEEM_REGISTER = 'REDEEM_REGISTER',
|
||||
@ -22,6 +23,9 @@ export enum EventType {
|
||||
SEND_CONFIRMATION_EMAIL = 'SEND_CONFIRMATION_EMAIL',
|
||||
TRANSACTION_SEND = 'TRANSACTION_SEND',
|
||||
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',
|
||||
// VERIFY_REDEEM = 'VERIFY_REDEEM',
|
||||
// INACTIVE_ACCOUNT = 'INACTIVE_ACCOUNT',
|
||||
|
||||
@ -33,6 +33,9 @@ import Decimal from 'decimal.js-light'
|
||||
import { GraphQLError } from 'graphql'
|
||||
import { TRANSACTIONS_LOCK } from '@/util/TRANSACTIONS_LOCK'
|
||||
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
|
||||
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 () => {
|
||||
jest.clearAllMocks()
|
||||
await expect(
|
||||
|
||||
@ -35,6 +35,12 @@ import LogError from '@/server/LogError'
|
||||
|
||||
import { getLastTransaction } from './util/getLastTransaction'
|
||||
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
|
||||
export const transactionLinkCode = (date: Date): string => {
|
||||
@ -89,6 +95,7 @@ export class TransactionLinkResolver {
|
||||
await DbTransactionLink.save(transactionLink).catch((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))
|
||||
}
|
||||
@ -122,6 +129,8 @@ export class TransactionLinkResolver {
|
||||
throw new LogError('Transaction link could not be deleted', e)
|
||||
})
|
||||
|
||||
await EVENT_TRANSACTION_LINK_DELETE(user, transactionLink)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@ -272,7 +281,13 @@ export class TransactionLinkResolver {
|
||||
await queryRunner.manager.update(DbContribution, { id: contribution.id }, contribution)
|
||||
|
||||
await queryRunner.commitTransaction()
|
||||
logger.info('creation from contribution link commited successfuly.')
|
||||
await EVENT_CONTRIBUTION_LINK_REDEEM(
|
||||
user,
|
||||
transaction,
|
||||
contribution,
|
||||
contributionLink,
|
||||
contributionLink.amount,
|
||||
)
|
||||
} catch (e) {
|
||||
await queryRunner.rollbackTransaction()
|
||||
throw new LogError('Creation from contribution link was not successful', e)
|
||||
@ -321,6 +336,12 @@ export class TransactionLinkResolver {
|
||||
user,
|
||||
transactionLink,
|
||||
)
|
||||
await EVENT_TRANSACTION_LINK_REDEEM(
|
||||
user,
|
||||
{ id: transactionLink.userId } as DbUser,
|
||||
transactionLink,
|
||||
transactionLink.amount,
|
||||
)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user