add enable-check to EventProtocol configurable as CONFIG-Property

This commit is contained in:
Claus-Peter Hübner 2022-07-01 00:40:10 +02:00
parent 38fcb0e9cf
commit 7f5995d2fa
4 changed files with 39 additions and 19 deletions

View File

@ -52,6 +52,9 @@ EMAIL_CODE_REQUEST_TIME=10
# Webhook
WEBHOOK_ELOPAGE_SECRET=secret
# EventProtocol
EVENT_PROTOCOL_ENABLED=true
# SET LOG LEVEL AS NEEDED IN YOUR .ENV
# POSSIBLE VALUES: all | trace | debug | info | warn | error | fatal
# LOG_LEVEL=info

View File

@ -94,6 +94,11 @@ const webhook = {
WEBHOOK_ELOPAGE_SECRET: process.env.WEBHOOK_ELOPAGE_SECRET || 'secret',
}
const eventProtocol = {
// global switch to enable writing of EventProtocol-Entries
EVENT_PROTOCOL_ENABLED: process.env.EVENT_PROTOCOL_ENABLED === 'true' || false,
}
// This is needed by graphql-directive-auth
process.env.APP_SECRET = server.JWT_SECRET
@ -118,6 +123,7 @@ const CONFIG = {
...email,
...loginServer,
...webhook,
...eventProtocol,
}
export default CONFIG

View File

@ -4,8 +4,14 @@ import { EventProtocolType } from './EventProtocolType'
import { EventProtocol } from '@entity/EventProtocol'
import { getConnection } from '@dbTools/typeorm'
import Decimal from 'decimal.js-light'
import CONFIG from '@/config'
class EventProtocolEmitter extends EventEmitter { }
class EventProtocolEmitter extends EventEmitter {
public isEnabled() {
logger.info(`EventProtocol - isEnabled=${CONFIG.EVENT_PROTOCOL_ENABLED}`)
return CONFIG.EVENT_PROTOCOL_ENABLED
}
}
export const eventProtocol = new EventProtocolEmitter()
eventProtocol.on('error', (err) => {
@ -329,11 +335,11 @@ async function writeEvent(
// eslint-disable-next-line no-unused-expressions
amount ? (dbEvent.amount = amount) : null
// set event values here when having the result ...
// await dbEvent.save()
// dbEvent.save()
const queryRunner = getConnection().createQueryRunner('master')
await queryRunner.connect()
await queryRunner.startTransaction('READ UNCOMMITTED')
await queryRunner.startTransaction('REPEATABLE READ')
try {
await queryRunner.manager.save(dbEvent).catch((error) => {
logger.error('Error while saving dbEvent', error)
@ -348,4 +354,3 @@ async function writeEvent(
await queryRunner.release()
}
}

View File

@ -387,26 +387,30 @@ export class UserResolver {
logger.info('redeemCode found contributionLink=' + contributionLink)
if (contributionLink) {
dbUser.contributionLinkId = contributionLink.id
eventProtocol.emit(
EventProtocolType.REDEEM_REGISTER,
new Date(Date.now()),
dbUser.id,
null,
contributionLink.id,
)
if (eventProtocol.isEnabled()) {
eventProtocol.emit(
EventProtocolType.REDEEM_REGISTER,
new Date(Date.now()),
dbUser.id,
null,
contributionLink.id,
)
}
}
} else {
const transactionLink = await dbTransactionLink.findOne({ code: redeemCode })
logger.info('redeemCode found transactionLink=' + transactionLink)
if (transactionLink) {
dbUser.referrerId = transactionLink.userId
eventProtocol.emit(
EventProtocolType.REDEEM_REGISTER,
new Date(Date.now()),
dbUser.id,
transactionLink.id,
null,
)
if (eventProtocol.isEnabled()) {
eventProtocol.emit(
EventProtocolType.REDEEM_REGISTER,
new Date(Date.now()),
dbUser.id,
transactionLink.id,
null,
)
}
}
}
}
@ -461,8 +465,10 @@ export class UserResolver {
await queryRunner.release()
}
logger.info('createUser() successful...')
eventProtocol.emit(EventProtocolType.REGISTER, new Date(Date.now()), dbUser.id)
if (eventProtocol.isEnabled()) {
eventProtocol.emit(EventProtocolType.REGISTER, new Date(Date.now()), dbUser.id)
}
return new User(dbUser)
}