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
WEBHOOK_ELOPAGE_SECRET=secret WEBHOOK_ELOPAGE_SECRET=secret
# EventProtocol
EVENT_PROTOCOL_ENABLED=true
# SET LOG LEVEL AS NEEDED IN YOUR .ENV # SET LOG LEVEL AS NEEDED IN YOUR .ENV
# POSSIBLE VALUES: all | trace | debug | info | warn | error | fatal # POSSIBLE VALUES: all | trace | debug | info | warn | error | fatal
# LOG_LEVEL=info # LOG_LEVEL=info

View File

@ -94,6 +94,11 @@ const webhook = {
WEBHOOK_ELOPAGE_SECRET: process.env.WEBHOOK_ELOPAGE_SECRET || 'secret', 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 // This is needed by graphql-directive-auth
process.env.APP_SECRET = server.JWT_SECRET process.env.APP_SECRET = server.JWT_SECRET
@ -118,6 +123,7 @@ const CONFIG = {
...email, ...email,
...loginServer, ...loginServer,
...webhook, ...webhook,
...eventProtocol,
} }
export default CONFIG export default CONFIG

View File

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

View File

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