From 1d6675fcf10f0a1b9d19cd59bc821bfeb04b959b Mon Sep 17 00:00:00 2001 From: sbriat Date: Wed, 24 May 2023 16:21:59 +0200 Subject: [PATCH] publish errors for invalid ads --- .../primaries/ad-messager.controller.ts | 42 ++++++++++++++++--- .../configuration-messager.controller.ts | 16 +++---- .../redis-configuration.repository.ts | 20 ++++----- .../configuration/configuration.module.ts | 3 ++ .../interfaces/configuration.repository.ts | 4 +- .../usecases/delete-configuration.usecase.ts | 16 +++---- .../usecases/get-configuration.usecase.ts | 11 ++--- .../usecases/set-configuration.usecase.ts | 16 +++---- 8 files changed, 84 insertions(+), 44 deletions(-) diff --git a/src/modules/ad/adapters/primaries/ad-messager.controller.ts b/src/modules/ad/adapters/primaries/ad-messager.controller.ts index ce10434..9744705 100644 --- a/src/modules/ad/adapters/primaries/ad-messager.controller.ts +++ b/src/modules/ad/adapters/primaries/ad-messager.controller.ts @@ -6,6 +6,8 @@ import { CreateAdRequest } from '../../domain/dtos/create-ad.request'; import { validateOrReject } from 'class-validator'; import { Messager } from '../secondaries/messager'; import { plainToInstance } from 'class-transformer'; +import { DatabaseException } from 'src/modules/database/exceptions/database.exception'; +import { ExceptionCode } from 'src/modules/utils/exception-code.enum'; @Controller() export class AdMessagerController { @@ -18,12 +20,10 @@ export class AdMessagerController { name: 'adCreated', }) async adCreatedHandler(message: string): Promise { + let createAdRequest: CreateAdRequest; + // parse message to request instance try { - // parse message to request instance - const createAdRequest: CreateAdRequest = plainToInstance( - CreateAdRequest, - JSON.parse(message), - ); + createAdRequest = plainToInstance(CreateAdRequest, JSON.parse(message)); // validate instance await validateOrReject(createAdRequest); // validate nested objects (fixes direct nested validation bug) @@ -34,8 +34,40 @@ export class AdMessagerController { throw e; } } + } catch (e) { + this.messager.publish( + 'matcher.ad.crit', + JSON.stringify({ + message: `Can't validate message : ${message}`, + error: e, + }), + ); + } + try { await this.commandBus.execute(new CreateAdCommand(createAdRequest)); } catch (e) { + if (e instanceof DatabaseException) { + if (e.message.includes('already exists')) { + this.messager.publish( + 'matcher.ad.crit', + JSON.stringify({ + code: ExceptionCode.ALREADY_EXISTS, + message: 'Already exists', + uuid: createAdRequest.uuid, + }), + ); + } + if (e.message.includes("Can't reach database server")) { + this.messager.publish( + 'matcher.ad.crit', + JSON.stringify({ + code: ExceptionCode.UNAVAILABLE, + message: 'Database server unavailable', + uuid: createAdRequest.uuid, + }), + ); + } + } this.messager.publish( 'logging.matcher.ad.crit', JSON.stringify({ diff --git a/src/modules/configuration/adapters/primaries/configuration-messager.controller.ts b/src/modules/configuration/adapters/primaries/configuration-messager.controller.ts index d41e817..e53688a 100644 --- a/src/modules/configuration/adapters/primaries/configuration-messager.controller.ts +++ b/src/modules/configuration/adapters/primaries/configuration-messager.controller.ts @@ -11,8 +11,8 @@ import { Configuration } from '../../domain/entities/configuration'; @Controller() export class ConfigurationMessagerController { constructor( - private readonly _commandBus: CommandBus, - private readonly _configService: ConfigService, + private readonly commandBus: CommandBus, + private readonly configService: ConfigService, ) {} @RabbitSubscribe({ @@ -22,14 +22,14 @@ export class ConfigurationMessagerController { const configuration: Configuration = JSON.parse(message); if ( configuration.domain == - this._configService.get('SERVICE_CONFIGURATION_DOMAIN') + this.configService.get('SERVICE_CONFIGURATION_DOMAIN') ) { const setConfigurationRequest: SetConfigurationRequest = new SetConfigurationRequest(); setConfigurationRequest.domain = configuration.domain; setConfigurationRequest.key = configuration.key; setConfigurationRequest.value = configuration.value; - await this._commandBus.execute( + await this.commandBus.execute( new SetConfigurationCommand(setConfigurationRequest), ); } @@ -42,12 +42,12 @@ export class ConfigurationMessagerController { const deletedConfiguration: Configuration = JSON.parse(message); if ( deletedConfiguration.domain == - this._configService.get('SERVICE_CONFIGURATION_DOMAIN') + this.configService.get('SERVICE_CONFIGURATION_DOMAIN') ) { const deleteConfigurationRequest = new DeleteConfigurationRequest(); deleteConfigurationRequest.domain = deletedConfiguration.domain; deleteConfigurationRequest.key = deletedConfiguration.key; - await this._commandBus.execute( + await this.commandBus.execute( new DeleteConfigurationCommand(deleteConfigurationRequest), ); } @@ -61,14 +61,14 @@ export class ConfigurationMessagerController { configurations.forEach(async (configuration) => { if ( configuration.domain == - this._configService.get('SERVICE_CONFIGURATION_DOMAIN') + this.configService.get('SERVICE_CONFIGURATION_DOMAIN') ) { const setConfigurationRequest: SetConfigurationRequest = new SetConfigurationRequest(); setConfigurationRequest.domain = configuration.domain; setConfigurationRequest.key = configuration.key; setConfigurationRequest.value = configuration.value; - await this._commandBus.execute( + await this.commandBus.execute( new SetConfigurationCommand(setConfigurationRequest), ); } diff --git a/src/modules/configuration/adapters/secondaries/redis-configuration.repository.ts b/src/modules/configuration/adapters/secondaries/redis-configuration.repository.ts index 2de14f0..0ef29f2 100644 --- a/src/modules/configuration/adapters/secondaries/redis-configuration.repository.ts +++ b/src/modules/configuration/adapters/secondaries/redis-configuration.repository.ts @@ -5,19 +5,19 @@ import { IConfigurationRepository } from '../../domain/interfaces/configuration. @Injectable() export class RedisConfigurationRepository extends IConfigurationRepository { - constructor(@InjectRedis() private readonly _redis: Redis) { + constructor(@InjectRedis() private readonly redis: Redis) { super(); } - async get(key: string): Promise { - return await this._redis.get(key); - } + get = async (key: string): Promise => { + return await this.redis.get(key); + }; - async set(key: string, value: string) { - await this._redis.set(key, value); - } + set = async (key: string, value: string): Promise => { + await this.redis.set(key, value); + }; - async del(key: string) { - await this._redis.del(key); - } + del = async (key: string): Promise => { + await this.redis.del(key); + }; } diff --git a/src/modules/configuration/configuration.module.ts b/src/modules/configuration/configuration.module.ts index fab6fe0..31693e5 100644 --- a/src/modules/configuration/configuration.module.ts +++ b/src/modules/configuration/configuration.module.ts @@ -41,14 +41,17 @@ import { SetConfigurationUseCase } from './domain/usecases/set-configuration.use setConfiguration: { exchange: configService.get('RMQ_EXCHANGE'), routingKey: ['configuration.create', 'configuration.update'], + queue: 'matcher-configuration-create-update', }, deleteConfiguration: { exchange: configService.get('RMQ_EXCHANGE'), routingKey: 'configuration.delete', + queue: 'matcher-configuration-delete', }, propagateConfiguration: { exchange: configService.get('RMQ_EXCHANGE'), routingKey: 'configuration.propagate', + queue: 'matcher-configuration-propagate', }, }, uri: configService.get('RMQ_URI'), diff --git a/src/modules/configuration/domain/interfaces/configuration.repository.ts b/src/modules/configuration/domain/interfaces/configuration.repository.ts index 657e5fd..4ea21c2 100644 --- a/src/modules/configuration/domain/interfaces/configuration.repository.ts +++ b/src/modules/configuration/domain/interfaces/configuration.repository.ts @@ -3,6 +3,6 @@ import { Injectable } from '@nestjs/common'; @Injectable() export abstract class IConfigurationRepository { abstract get(key: string): Promise; - abstract set(key: string, value: string): void; - abstract del(key: string): void; + abstract set(key: string, value: string): Promise; + abstract del(key: string): Promise; } diff --git a/src/modules/configuration/domain/usecases/delete-configuration.usecase.ts b/src/modules/configuration/domain/usecases/delete-configuration.usecase.ts index 14ab3cb..15b39ce 100644 --- a/src/modules/configuration/domain/usecases/delete-configuration.usecase.ts +++ b/src/modules/configuration/domain/usecases/delete-configuration.usecase.ts @@ -4,13 +4,15 @@ import { DeleteConfigurationCommand } from '../../commands/delete-configuration. @CommandHandler(DeleteConfigurationCommand) export class DeleteConfigurationUseCase { - constructor(private _configurationRepository: RedisConfigurationRepository) {} + constructor(private configurationRepository: RedisConfigurationRepository) {} - async execute(deleteConfigurationCommand: DeleteConfigurationCommand) { - await this._configurationRepository.del( - deleteConfigurationCommand.deleteConfigurationRequest.domain + - ':' + - deleteConfigurationCommand.deleteConfigurationRequest.key, + execute = async ( + deleteConfigurationCommand: DeleteConfigurationCommand, + ): Promise => { + await this.configurationRepository.del( + `${deleteConfigurationCommand.deleteConfigurationRequest.domain} + : + ${deleteConfigurationCommand.deleteConfigurationRequest.key}`, ); - } + }; } diff --git a/src/modules/configuration/domain/usecases/get-configuration.usecase.ts b/src/modules/configuration/domain/usecases/get-configuration.usecase.ts index 38036ff..77742e0 100644 --- a/src/modules/configuration/domain/usecases/get-configuration.usecase.ts +++ b/src/modules/configuration/domain/usecases/get-configuration.usecase.ts @@ -4,11 +4,12 @@ import { GetConfigurationQuery } from '../../queries/get-configuration.query'; @QueryHandler(GetConfigurationQuery) export class GetConfigurationUseCase { - constructor(private _configurationRepository: RedisConfigurationRepository) {} + constructor(private configurationRepository: RedisConfigurationRepository) {} - async execute(getConfigurationQuery: GetConfigurationQuery): Promise { - return this._configurationRepository.get( - getConfigurationQuery.domain + ':' + getConfigurationQuery.key, + execute = async ( + getConfigurationQuery: GetConfigurationQuery, + ): Promise => + this.configurationRepository.get( + `${getConfigurationQuery.domain}:${getConfigurationQuery.key}`, ); - } } diff --git a/src/modules/configuration/domain/usecases/set-configuration.usecase.ts b/src/modules/configuration/domain/usecases/set-configuration.usecase.ts index 408340a..8a8ad41 100644 --- a/src/modules/configuration/domain/usecases/set-configuration.usecase.ts +++ b/src/modules/configuration/domain/usecases/set-configuration.usecase.ts @@ -4,14 +4,16 @@ import { SetConfigurationCommand } from '../../commands/set-configuration.comman @CommandHandler(SetConfigurationCommand) export class SetConfigurationUseCase { - constructor(private _configurationRepository: RedisConfigurationRepository) {} + constructor(private configurationRepository: RedisConfigurationRepository) {} - async execute(setConfigurationCommand: SetConfigurationCommand) { - await this._configurationRepository.set( - setConfigurationCommand.setConfigurationRequest.domain + - ':' + - setConfigurationCommand.setConfigurationRequest.key, + execute = async ( + setConfigurationCommand: SetConfigurationCommand, + ): Promise => { + await this.configurationRepository.set( + `${setConfigurationCommand.setConfigurationRequest.domain} + : + ${setConfigurationCommand.setConfigurationRequest.key}`, setConfigurationCommand.setConfigurationRequest.value, ); - } + }; }