diff --git a/backend/src/event/Event.ts b/backend/src/event/Event.ts index 09a31d4e0..6a1233224 100644 --- a/backend/src/event/Event.ts +++ b/backend/src/event/Event.ts @@ -1,4 +1,3 @@ -import { EventProtocol } from '@entity/EventProtocol' import decimal from 'decimal.js-light' import { EventProtocolType } from './EventProtocolType' @@ -87,21 +86,6 @@ export class EventDeleteContributionLink extends EventBasicCt {} export class EventUpdateContributionLink extends EventBasicCt {} export class Event { - constructor() - constructor(event?: EventProtocol) { - if (event) { - this.id = event.id - this.type = event.type - this.createdAt = event.createdAt - this.userId = event.userId - this.xUserId = event.xUserId - this.xCommunityId = event.xCommunityId - this.transactionId = event.transactionId - this.contributionId = event.contributionId - this.amount = event.amount - } - } - public setEventBasic(): Event { this.type = EventProtocolType.BASIC this.createdAt = new Date() diff --git a/backend/src/event/EventProtocolEmitter.ts b/backend/src/event/EventProtocolEmitter.ts index b4b22bce1..9cde4939f 100644 --- a/backend/src/event/EventProtocolEmitter.ts +++ b/backend/src/event/EventProtocolEmitter.ts @@ -3,39 +3,21 @@ import { backendLogger as logger } from '@/server/logger' import { EventProtocol } from '@entity/EventProtocol' import CONFIG from '@/config' -class EventProtocolEmitter { - /* }extends EventEmitter { */ - private events: Event[] - - /* - public addEvent(event: Event) { - this.events.push(event) +export const writeEvent = async (event: Event): Promise => { + if (CONFIG.EVENT_PROTOCOL_DISABLED) { + logger.info('EventProtocol is disabled', CONFIG.EVENT_PROTOCOL_DISABLED) + return null } - public getEvents(): Event[] { - return this.events - } - */ - - public isDisabled() { - logger.info(`EventProtocol - isDisabled=${CONFIG.EVENT_PROTOCOL_DISABLED}`) - return CONFIG.EVENT_PROTOCOL_DISABLED === true - } - - public async writeEvent(event: Event): Promise { - if (!eventProtocol.isDisabled()) { - logger.info(`writeEvent(${JSON.stringify(event)})`) - const dbEvent = new EventProtocol() - dbEvent.type = event.type - dbEvent.createdAt = event.createdAt - dbEvent.userId = event.userId - if (event.xUserId) dbEvent.xUserId = event.xUserId - if (event.xCommunityId) dbEvent.xCommunityId = event.xCommunityId - if (event.contributionId) dbEvent.contributionId = event.contributionId - if (event.transactionId) dbEvent.transactionId = event.transactionId - if (event.amount) dbEvent.amount = event.amount - await dbEvent.save() - } - } + logger.info('writeEvent', event) + const dbEvent = new EventProtocol() + dbEvent.type = event.type + dbEvent.createdAt = event.createdAt + dbEvent.userId = event.userId + dbEvent.xUserId = event.xUserId || null + dbEvent.xCommunityId = event.xCommunityId || null + dbEvent.contributionId = event.contributionId || null + dbEvent.transactionId = event.transactionId || null + dbEvent.amount = event.amount || null + return dbEvent.save() } -export const eventProtocol = new EventProtocolEmitter() diff --git a/backend/src/graphql/resolver/ContributionResolver.ts b/backend/src/graphql/resolver/ContributionResolver.ts index c7946d2c8..bf80bcb4d 100644 --- a/backend/src/graphql/resolver/ContributionResolver.ts +++ b/backend/src/graphql/resolver/ContributionResolver.ts @@ -46,7 +46,7 @@ import { EventAdminContributionDelete, EventAdminContributionUpdate, } from '@/event/Event' -import { eventProtocol } from '@/event/EventProtocolEmitter' +import { writeEvent } from '@/event/EventProtocolEmitter' import { calculateDecay } from '@/util/decay' import { sendContributionConfirmedEmail, @@ -97,7 +97,7 @@ export class ContributionResolver { eventCreateContribution.userId = user.id eventCreateContribution.amount = amount eventCreateContribution.contributionId = contribution.id - await eventProtocol.writeEvent(event.setEventContributionCreate(eventCreateContribution)) + await writeEvent(event.setEventContributionCreate(eventCreateContribution)) return new UnconfirmedContribution(contribution, user, creations) } @@ -133,7 +133,7 @@ export class ContributionResolver { eventDeleteContribution.userId = user.id eventDeleteContribution.contributionId = contribution.id eventDeleteContribution.amount = contribution.amount - await eventProtocol.writeEvent(event.setEventContributionDelete(eventDeleteContribution)) + await writeEvent(event.setEventContributionDelete(eventDeleteContribution)) const res = await contribution.softRemove() return !!res @@ -290,7 +290,7 @@ export class ContributionResolver { eventUpdateContribution.userId = user.id eventUpdateContribution.contributionId = contributionId eventUpdateContribution.amount = amount - await eventProtocol.writeEvent(event.setEventContributionUpdate(eventUpdateContribution)) + await writeEvent(event.setEventContributionUpdate(eventUpdateContribution)) return new UnconfirmedContribution(contributionToUpdate, user, creations) } @@ -357,9 +357,7 @@ export class ContributionResolver { eventAdminCreateContribution.userId = moderator.id eventAdminCreateContribution.amount = amount eventAdminCreateContribution.contributionId = contribution.id - await eventProtocol.writeEvent( - event.setEventAdminContributionCreate(eventAdminCreateContribution), - ) + await writeEvent(event.setEventAdminContributionCreate(eventAdminCreateContribution)) return getUserCreation(emailContact.userId, clientTimezoneOffset) } @@ -469,9 +467,7 @@ export class ContributionResolver { eventAdminContributionUpdate.userId = user.id eventAdminContributionUpdate.amount = amount eventAdminContributionUpdate.contributionId = contributionToUpdate.id - await eventProtocol.writeEvent( - event.setEventAdminContributionUpdate(eventAdminContributionUpdate), - ) + await writeEvent(event.setEventAdminContributionUpdate(eventAdminContributionUpdate)) return result } @@ -549,9 +545,7 @@ export class ContributionResolver { eventAdminContributionDelete.userId = contribution.userId eventAdminContributionDelete.amount = contribution.amount eventAdminContributionDelete.contributionId = contribution.id - await eventProtocol.writeEvent( - event.setEventAdminContributionDelete(eventAdminContributionDelete), - ) + await writeEvent(event.setEventAdminContributionDelete(eventAdminContributionDelete)) sendContributionDeniedEmail({ firstName: user.firstName, lastName: user.lastName, @@ -679,7 +673,7 @@ export class ContributionResolver { eventContributionConfirm.userId = user.id eventContributionConfirm.amount = contribution.amount eventContributionConfirm.contributionId = contribution.id - await eventProtocol.writeEvent(event.setEventContributionConfirm(eventContributionConfirm)) + await writeEvent(event.setEventContributionConfirm(eventContributionConfirm)) } finally { releaseLock() } diff --git a/backend/src/graphql/resolver/TransactionResolver.ts b/backend/src/graphql/resolver/TransactionResolver.ts index 2f97596b2..b75782abf 100644 --- a/backend/src/graphql/resolver/TransactionResolver.ts +++ b/backend/src/graphql/resolver/TransactionResolver.ts @@ -30,7 +30,7 @@ import { sendTransactionReceivedEmail, } from '@/emails/sendEmailVariants' import { Event, EventTransactionReceive, EventTransactionSend } from '@/event/Event' -import { eventProtocol } from '@/event/EventProtocolEmitter' +import { writeEvent } from '@/event/EventProtocolEmitter' import { BalanceResolver } from './BalanceResolver' import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from './const/const' @@ -144,16 +144,14 @@ export const executeTransaction = async ( eventTransactionSend.xUserId = transactionSend.linkedUserId eventTransactionSend.transactionId = transactionSend.id eventTransactionSend.amount = transactionSend.amount.mul(-1) - await eventProtocol.writeEvent(new Event().setEventTransactionSend(eventTransactionSend)) + await writeEvent(new Event().setEventTransactionSend(eventTransactionSend)) const eventTransactionReceive = new EventTransactionReceive() eventTransactionReceive.userId = transactionReceive.userId eventTransactionReceive.xUserId = transactionReceive.linkedUserId eventTransactionReceive.transactionId = transactionReceive.id eventTransactionReceive.amount = transactionReceive.amount - await eventProtocol.writeEvent( - new Event().setEventTransactionReceive(eventTransactionReceive), - ) + await writeEvent(new Event().setEventTransactionReceive(eventTransactionReceive)) } catch (e) { await queryRunner.rollbackTransaction() logger.error(`Transaction was not successful: ${e}`) diff --git a/backend/src/graphql/resolver/UserResolver.ts b/backend/src/graphql/resolver/UserResolver.ts index d92958009..5ebf5b445 100644 --- a/backend/src/graphql/resolver/UserResolver.ts +++ b/backend/src/graphql/resolver/UserResolver.ts @@ -48,7 +48,7 @@ import { klicktippNewsletterStateMiddleware } from '@/middleware/klicktippMiddle import { klicktippSignIn } from '@/apis/KlicktippController' import { RIGHTS } from '@/auth/RIGHTS' import { hasElopageBuys } from '@/util/hasElopageBuys' -import { eventProtocol } from '@/event/EventProtocolEmitter' +import { writeEvent } from '@/event/EventProtocolEmitter' import { Event, EventLogin, @@ -179,7 +179,7 @@ export class UserResolver { }) const ev = new EventLogin() ev.userId = user.id - eventProtocol.writeEvent(new Event().setEventLogin(ev)) + writeEvent(new Event().setEventLogin(ev)) logger.info(`successful Login: ${JSON.stringify(user, null, 2)}`) return user } @@ -251,9 +251,7 @@ export class UserResolver { }) const eventSendAccountMultiRegistrationEmail = new EventSendAccountMultiRegistrationEmail() eventSendAccountMultiRegistrationEmail.userId = foundUser.id - eventProtocol.writeEvent( - event.setEventSendConfirmationEmail(eventSendAccountMultiRegistrationEmail), - ) + writeEvent(event.setEventSendConfirmationEmail(eventSendAccountMultiRegistrationEmail)) logger.info( `sendAccountMultiRegistrationEmail by ${firstName} ${lastName} to ${foundUser.firstName} ${foundUser.lastName} <${email}>`, ) @@ -336,7 +334,7 @@ export class UserResolver { }) logger.info(`sendAccountActivationEmail of ${firstName}.${lastName} to ${email}`) eventSendConfirmEmail.userId = dbUser.id - eventProtocol.writeEvent(event.setEventSendConfirmationEmail(eventSendConfirmEmail)) + writeEvent(event.setEventSendConfirmationEmail(eventSendConfirmEmail)) if (!emailSent) { logger.debug(`Account confirmation link: ${activationLink}`) @@ -354,10 +352,10 @@ export class UserResolver { if (redeemCode) { eventRedeemRegister.userId = dbUser.id - await eventProtocol.writeEvent(event.setEventRedeemRegister(eventRedeemRegister)) + await writeEvent(event.setEventRedeemRegister(eventRedeemRegister)) } else { eventRegister.userId = dbUser.id - await eventProtocol.writeEvent(event.setEventRegister(eventRegister)) + await writeEvent(event.setEventRegister(eventRegister)) } return new User(dbUser) @@ -477,7 +475,7 @@ export class UserResolver { const eventActivateAccount = new EventActivateAccount() eventActivateAccount.userId = user.id - eventProtocol.writeEvent(event.setEventActivateAccount(eventActivateAccount)) + writeEvent(event.setEventActivateAccount(eventActivateAccount)) } catch (e) { await queryRunner.rollbackTransaction() throw new LogError('Error on writing User and User Contact data', e) @@ -824,9 +822,7 @@ export class UserResolver { const event = new Event() const eventSendConfirmationEmail = new EventSendConfirmationEmail() eventSendConfirmationEmail.userId = user.id - await eventProtocol.writeEvent( - event.setEventSendConfirmationEmail(eventSendConfirmationEmail), - ) + await writeEvent(event.setEventSendConfirmationEmail(eventSendConfirmationEmail)) } return true diff --git a/database/entity/0050-add_messageId_to_event_protocol/EventProtocol.ts b/database/entity/0050-add_messageId_to_event_protocol/EventProtocol.ts index d4dbc526f..e457cc0a3 100644 --- a/database/entity/0050-add_messageId_to_event_protocol/EventProtocol.ts +++ b/database/entity/0050-add_messageId_to_event_protocol/EventProtocol.ts @@ -16,17 +16,17 @@ export class EventProtocol extends BaseEntity { @Column({ name: 'user_id', unsigned: true, nullable: false }) userId: number - @Column({ name: 'x_user_id', unsigned: true, nullable: true }) - xUserId: number + @Column({ name: 'x_user_id', type: 'int', unsigned: true, nullable: true }) + xUserId: number | null - @Column({ name: 'x_community_id', unsigned: true, nullable: true }) - xCommunityId: number + @Column({ name: 'x_community_id', type: 'int', unsigned: true, nullable: true }) + xCommunityId: number | null - @Column({ name: 'transaction_id', unsigned: true, nullable: true }) - transactionId: number + @Column({ name: 'transaction_id', type: 'int', unsigned: true, nullable: true }) + transactionId: number | null - @Column({ name: 'contribution_id', unsigned: true, nullable: true }) - contributionId: number + @Column({ name: 'contribution_id', type: 'int', unsigned: true, nullable: true }) + contributionId: number | null @Column({ type: 'decimal', @@ -35,8 +35,8 @@ export class EventProtocol extends BaseEntity { nullable: true, transformer: DecimalTransformer, }) - amount: Decimal + amount: Decimal | null - @Column({ name: 'message_id', unsigned: true, nullable: true }) - messageId: number + @Column({ name: 'message_id', type: 'int', unsigned: true, nullable: true }) + messageId: number | null }