From 0bf3048d0e7841efbabc76c7ac1c56589ef16e5e Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 30 May 2023 11:41:30 +0200 Subject: [PATCH] replace configuration module by dedicated package --- package-lock.json | 111 ++++++++++++++++-- package.json | 1 + src/app.module.ts | 14 ++- .../primaries/ad-messager.controller.ts | 3 +- .../configuration-messager.controller.ts | 77 ------------ .../redis-configuration.repository.ts | 21 ---- .../commands/delete-configuration.command.ts | 9 -- .../commands/set-configuration.command.ts | 9 -- .../configuration/configuration.module.ts | 71 ----------- .../dtos/delete-configuration.request.ts | 11 -- .../domain/dtos/set-configuration.request.ts | 15 --- .../domain/entities/configuration.ts | 12 -- .../interfaces/configuration.repository.ts | 8 -- .../usecases/delete-configuration.usecase.ts | 18 --- .../usecases/get-configuration.usecase.ts | 15 --- .../usecases/set-configuration.usecase.ts | 19 --- .../queries/get-configuration.query.ts | 9 -- .../unit/delete-configuration.usecase.spec.ts | 49 -------- .../unit/get-configuration.usecase.spec.ts | 43 ------- .../redis-configuration.repository.spec.ts | 47 -------- .../unit/set-configuration.usecase.spec.ts | 50 -------- 21 files changed, 113 insertions(+), 499 deletions(-) delete mode 100644 src/modules/configuration/adapters/primaries/configuration-messager.controller.ts delete mode 100644 src/modules/configuration/adapters/secondaries/redis-configuration.repository.ts delete mode 100644 src/modules/configuration/commands/delete-configuration.command.ts delete mode 100644 src/modules/configuration/commands/set-configuration.command.ts delete mode 100644 src/modules/configuration/configuration.module.ts delete mode 100644 src/modules/configuration/domain/dtos/delete-configuration.request.ts delete mode 100644 src/modules/configuration/domain/dtos/set-configuration.request.ts delete mode 100644 src/modules/configuration/domain/entities/configuration.ts delete mode 100644 src/modules/configuration/domain/interfaces/configuration.repository.ts delete mode 100644 src/modules/configuration/domain/usecases/delete-configuration.usecase.ts delete mode 100644 src/modules/configuration/domain/usecases/get-configuration.usecase.ts delete mode 100644 src/modules/configuration/domain/usecases/set-configuration.usecase.ts delete mode 100644 src/modules/configuration/queries/get-configuration.query.ts delete mode 100644 src/modules/configuration/tests/unit/delete-configuration.usecase.spec.ts delete mode 100644 src/modules/configuration/tests/unit/get-configuration.usecase.spec.ts delete mode 100644 src/modules/configuration/tests/unit/redis-configuration.repository.spec.ts delete mode 100644 src/modules/configuration/tests/unit/set-configuration.usecase.spec.ts diff --git a/package-lock.json b/package-lock.json index e565461..b354985 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "@grpc/grpc-js": "^1.8.13", "@grpc/proto-loader": "^0.7.6", "@liaoliaots/nestjs-redis": "^9.0.5", + "@mobicoop/configuration-module": "^1.0.0", "@nestjs/axios": "^2.0.0", "@nestjs/cache-manager": "^1.0.0", "@nestjs/common": "^9.0.0", @@ -65,6 +66,24 @@ "typescript": "^4.7.4" } }, + "node_modules/@acuminous/bitsyntax": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/@acuminous/bitsyntax/-/bitsyntax-0.1.2.tgz", + "integrity": "sha512-29lUK80d1muEQqiUsSo+3A0yP6CdspgC95EnKBMi22Xlwt79i/En4Vr67+cXhU+cZjbti3TgGGC5wy1stIywVQ==", + "dependencies": { + "buffer-more-ints": "~1.0.0", + "debug": "^4.3.4", + "safe-buffer": "~5.1.2" + }, + "engines": { + "node": ">=0.8" + } + }, + "node_modules/@acuminous/bitsyntax/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, "node_modules/@ampproject/remapping": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.2.0.tgz", @@ -1578,6 +1597,59 @@ "node": ">=8" } }, + "node_modules/@mobicoop/configuration-module": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@mobicoop/configuration-module/-/configuration-module-1.0.0.tgz", + "integrity": "sha512-ufpJl3YupHhpcGS26h/kEGSplmJ5C3UcE5UkX5MUHbJzJBATN6FmikU3JgKU9bdN/YxHrxylmnknQVKcr3HT1w==", + "dependencies": { + "@golevelup/nestjs-rabbitmq": "^3.6.0", + "@liaoliaots/nestjs-redis": "^9.0.5", + "@nestjs/config": "^2.3.2", + "@nestjs/cqrs": "^9.0.4", + "@types/amqplib": "^0.10.1", + "amqplib": "^0.10.3", + "class-validator": "^0.14.0", + "ioredis": "^5.3.2" + }, + "peerDependencies": { + "@nestjs/common": "^9.4.2" + } + }, + "node_modules/@mobicoop/configuration-module/node_modules/amqplib": { + "version": "0.10.3", + "resolved": "https://registry.npmjs.org/amqplib/-/amqplib-0.10.3.tgz", + "integrity": "sha512-UHmuSa7n8vVW/a5HGh2nFPqAEr8+cD4dEZ6u9GjP91nHfr1a54RyAKyra7Sb5NH7NBKOUlyQSMXIp0qAixKexw==", + "dependencies": { + "@acuminous/bitsyntax": "^0.1.2", + "buffer-more-ints": "~1.0.0", + "readable-stream": "1.x >=1.1.9", + "url-parse": "~1.5.10" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@mobicoop/configuration-module/node_modules/isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha512-D2S+3GLxWH+uhrNEcoh/fnmYeP8E8/zHl644d/jdA0g2uyXvy3sb0qxotE+ne0LtccHknQzWwZEzhak7oJ0COQ==" + }, + "node_modules/@mobicoop/configuration-module/node_modules/readable-stream": { + "version": "1.1.14", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", + "integrity": "sha512-+MeVjFf4L44XUkhM1eYbD8fyEsxcV81pqMSR5gblfcLCHfZvbrqy4/qYHE+/R5HoBUT11WV5O08Cr1n3YXkWVQ==", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.1", + "isarray": "0.0.1", + "string_decoder": "~0.10.x" + } + }, + "node_modules/@mobicoop/configuration-module/node_modules/string_decoder": { + "version": "0.10.31", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "integrity": "sha512-ev2QzSzWPYmy9GuqfIVildA4OdcGLeFZQrq5ys6RtiuF+RQQiZWr8TZNyAcuVXyQRYfEO+MsoB/1BuQVhOJuoQ==" + }, "node_modules/@nestjs/axios": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/@nestjs/axios/-/axios-2.0.0.tgz", @@ -1707,12 +1779,12 @@ } }, "node_modules/@nestjs/common": { - "version": "9.4.0", - "resolved": "https://registry.npmjs.org/@nestjs/common/-/common-9.4.0.tgz", - "integrity": "sha512-RUcVAQsEF4WPrmzFXEOUfZnPwrLTe1UVlzXTlSyfqfqbdWDPKDGlIPVelBLfc5/+RRUQ0I5iE4+CQvpCmkqldw==", + "version": "9.4.2", + "resolved": "https://registry.npmjs.org/@nestjs/common/-/common-9.4.2.tgz", + "integrity": "sha512-sea+qZnbD5x3YWZDVQT/wbVJ2NiABaM1tyZTLuW9hpkcM2KFA96xKtK3VaCxyz49zoXIgSOefsyK7HuUMCe27Q==", "dependencies": { "iterare": "1.2.1", - "tslib": "2.5.0", + "tslib": "2.5.2", "uid": "2.0.2" }, "funding": { @@ -1738,10 +1810,15 @@ } } }, + "node_modules/@nestjs/common/node_modules/tslib": { + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.5.2.tgz", + "integrity": "sha512-5svOrSA2w3iGFDs1HibEVBGbDrAY82bFQ3HZ3ixB+88nsbsWQoKqDRb5UBYAUPEzbBn6dAp5gRNXglySbx1MlA==" + }, "node_modules/@nestjs/config": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/@nestjs/config/-/config-2.3.1.tgz", - "integrity": "sha512-Ckzel0NZ9CWhNsLfE1hxfDuxJuEbhQvGxSlmZ1/X8awjRmAA/g3kT6M1+MO1SHj1wMtPyUfd9WpwkiqFbiwQgA==", + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/@nestjs/config/-/config-2.3.2.tgz", + "integrity": "sha512-VtGV8PBpxzMzz68kdxTWqPm9v7SYCSZXQ0tC72AMNnjdmU+CVjUSLpEpdnm0XcWHxE1nV6wSI3HZxsATIV4ZxA==", "dependencies": { "dotenv": "16.0.3", "dotenv-expand": "10.0.0", @@ -1792,9 +1869,9 @@ } }, "node_modules/@nestjs/cqrs": { - "version": "9.0.3", - "resolved": "https://registry.npmjs.org/@nestjs/cqrs/-/cqrs-9.0.3.tgz", - "integrity": "sha512-hmbrqf51BVdgmnnxErnLVXfPNTEqr4Hz8DyLa9dKLIW3BuOyI5RDwJ/9sKbJ47UDBhumC5nQlNK9qk27mhqHfw==", + "version": "9.0.4", + "resolved": "https://registry.npmjs.org/@nestjs/cqrs/-/cqrs-9.0.4.tgz", + "integrity": "sha512-nWDF+xs4jqs6OjxFg/wVSd0NiIV9+EFCJrJNTo4VRWe78CcAaitbp56CBspUh4gKyfkci95i+EhHdEqRXKFptg==", "dependencies": { "uuid": "9.0.0" }, @@ -2228,6 +2305,14 @@ "url": "https://opencollective.com/turf" } }, + "node_modules/@types/amqplib": { + "version": "0.10.1", + "resolved": "https://registry.npmjs.org/@types/amqplib/-/amqplib-0.10.1.tgz", + "integrity": "sha512-j6ANKT79ncUDnAs/+9r9eDujxbeJoTjoVu33gHHcaPfmLQaMhvfbH2GqSe8KUM444epAp1Vl3peVOQfZk3UIqA==", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/babel__core": { "version": "7.20.0", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.0.tgz", @@ -5547,9 +5632,9 @@ } }, "node_modules/ioredis": { - "version": "5.3.1", - "resolved": "https://registry.npmjs.org/ioredis/-/ioredis-5.3.1.tgz", - "integrity": "sha512-C+IBcMysM6v52pTLItYMeV4Hz7uriGtoJdz7SSBDX6u+zwSYGirLdQh3L7t/OItWITcw3gTFMjJReYUwS4zihg==", + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/ioredis/-/ioredis-5.3.2.tgz", + "integrity": "sha512-1DKMMzlIHM02eBBVOFQ1+AolGjs6+xEcM4PDL7NqOS6szq7H9jSaEkIUH6/a5Hl241LzW6JLSiAbNvTQjUupUA==", "dependencies": { "@ioredis/commands": "^1.1.1", "cluster-key-slot": "^1.1.0", diff --git a/package.json b/package.json index 9380c4c..90d5e14 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "@grpc/grpc-js": "^1.8.13", "@grpc/proto-loader": "^0.7.6", "@liaoliaots/nestjs-redis": "^9.0.5", + "@mobicoop/configuration-module": "^1.0.0", "@nestjs/axios": "^2.0.0", "@nestjs/cache-manager": "^1.0.0", "@nestjs/common": "^9.0.0", diff --git a/src/app.module.ts b/src/app.module.ts index 30da99c..f88e28c 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -2,16 +2,26 @@ import { classes } from '@automapper/classes'; import { AutomapperModule } from '@automapper/nestjs'; import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; -import { ConfigurationModule } from './modules/configuration/configuration.module'; import { HealthModule } from './modules/health/health.module'; import { MatcherModule } from './modules/matcher/matcher.module'; import { AdModule } from './modules/ad/ad.module'; +import { ConfigurationModule } from '@mobicoop/configuration-module'; @Module({ imports: [ ConfigModule.forRoot({ isGlobal: true }), + ConfigurationModule.forRootAsync({ + setConfigurationBrokerRoutingKeys: [ + 'configuration.create', + 'configuration.update', + ], + deleteConfigurationRoutingKey: 'configuration.delete', + propagateConfigurationRoutingKey: 'configuration.propagate', + setConfigurationBrokerQueue: 'matcher-configuration-create-update', + deleteConfigurationQueue: 'matcher-configuration-delete', + propagateConfigurationQueue: 'matcher-configuration-propagate', + }), AutomapperModule.forRoot({ strategyInitializer: classes() }), - ConfigurationModule, HealthModule, MatcherModule, AdModule, diff --git a/src/modules/ad/adapters/primaries/ad-messager.controller.ts b/src/modules/ad/adapters/primaries/ad-messager.controller.ts index 9744705..99f2d87 100644 --- a/src/modules/ad/adapters/primaries/ad-messager.controller.ts +++ b/src/modules/ad/adapters/primaries/ad-messager.controller.ts @@ -1,6 +1,6 @@ import { RabbitSubscribe } from '@golevelup/nestjs-rabbitmq'; import { Controller } from '@nestjs/common'; -import { CommandBus } from '@nestjs/cqrs'; +import { CommandBus, QueryBus } from '@nestjs/cqrs'; import { CreateAdCommand } from '../../commands/create-ad.command'; import { CreateAdRequest } from '../../domain/dtos/create-ad.request'; import { validateOrReject } from 'class-validator'; @@ -14,6 +14,7 @@ export class AdMessagerController { constructor( private readonly messager: Messager, private readonly commandBus: CommandBus, + private readonly queryBus: QueryBus, ) {} @RabbitSubscribe({ diff --git a/src/modules/configuration/adapters/primaries/configuration-messager.controller.ts b/src/modules/configuration/adapters/primaries/configuration-messager.controller.ts deleted file mode 100644 index e53688a..0000000 --- a/src/modules/configuration/adapters/primaries/configuration-messager.controller.ts +++ /dev/null @@ -1,77 +0,0 @@ -import { RabbitSubscribe } from '@golevelup/nestjs-rabbitmq'; -import { Controller } from '@nestjs/common'; -import { ConfigService } from '@nestjs/config'; -import { CommandBus } from '@nestjs/cqrs'; -import { DeleteConfigurationCommand } from '../../commands/delete-configuration.command'; -import { SetConfigurationCommand } from '../../commands/set-configuration.command'; -import { DeleteConfigurationRequest } from '../../domain/dtos/delete-configuration.request'; -import { SetConfigurationRequest } from '../../domain/dtos/set-configuration.request'; -import { Configuration } from '../../domain/entities/configuration'; - -@Controller() -export class ConfigurationMessagerController { - constructor( - private readonly commandBus: CommandBus, - private readonly configService: ConfigService, - ) {} - - @RabbitSubscribe({ - name: 'setConfiguration', - }) - public async setConfigurationHandler(message: string) { - const configuration: Configuration = JSON.parse(message); - if ( - 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( - new SetConfigurationCommand(setConfigurationRequest), - ); - } - } - - @RabbitSubscribe({ - name: 'deleteConfiguration', - }) - public async configurationDeletedHandler(message: string) { - const deletedConfiguration: Configuration = JSON.parse(message); - if ( - deletedConfiguration.domain == - this.configService.get('SERVICE_CONFIGURATION_DOMAIN') - ) { - const deleteConfigurationRequest = new DeleteConfigurationRequest(); - deleteConfigurationRequest.domain = deletedConfiguration.domain; - deleteConfigurationRequest.key = deletedConfiguration.key; - await this.commandBus.execute( - new DeleteConfigurationCommand(deleteConfigurationRequest), - ); - } - } - - @RabbitSubscribe({ - name: 'propagateConfiguration', - }) - public async propagateConfigurationsHandler(message: string) { - const configurations: Configuration[] = JSON.parse(message); - configurations.forEach(async (configuration) => { - if ( - 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( - 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 deleted file mode 100644 index 1ceaebe..0000000 --- a/src/modules/configuration/adapters/secondaries/redis-configuration.repository.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { InjectRedis } from '@liaoliaots/nestjs-redis'; -import { Injectable } from '@nestjs/common'; -import { Redis } from 'ioredis'; -import { IConfigurationRepository } from '../../domain/interfaces/configuration.repository'; - -@Injectable() -export class RedisConfigurationRepository extends IConfigurationRepository { - constructor(@InjectRedis() private readonly redis: Redis) { - super(); - } - - get = async (key: string): Promise => await this.redis.get(key); - - set = async (key: string, value: string): Promise => { - await this.redis.set(key, value); - }; - - del = async (key: string): Promise => { - await this.redis.del(key); - }; -} diff --git a/src/modules/configuration/commands/delete-configuration.command.ts b/src/modules/configuration/commands/delete-configuration.command.ts deleted file mode 100644 index 8a6753e..0000000 --- a/src/modules/configuration/commands/delete-configuration.command.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { DeleteConfigurationRequest } from '../domain/dtos/delete-configuration.request'; - -export class DeleteConfigurationCommand { - readonly deleteConfigurationRequest: DeleteConfigurationRequest; - - constructor(deleteConfigurationRequest: DeleteConfigurationRequest) { - this.deleteConfigurationRequest = deleteConfigurationRequest; - } -} diff --git a/src/modules/configuration/commands/set-configuration.command.ts b/src/modules/configuration/commands/set-configuration.command.ts deleted file mode 100644 index 52f54ee..0000000 --- a/src/modules/configuration/commands/set-configuration.command.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { SetConfigurationRequest } from '../domain/dtos/set-configuration.request'; - -export class SetConfigurationCommand { - readonly setConfigurationRequest: SetConfigurationRequest; - - constructor(setConfigurationRequest: SetConfigurationRequest) { - this.setConfigurationRequest = setConfigurationRequest; - } -} diff --git a/src/modules/configuration/configuration.module.ts b/src/modules/configuration/configuration.module.ts deleted file mode 100644 index 31693e5..0000000 --- a/src/modules/configuration/configuration.module.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { RabbitMQConfig, RabbitMQModule } from '@golevelup/nestjs-rabbitmq'; -import { RedisModule, RedisModuleOptions } from '@liaoliaots/nestjs-redis'; -import { Module } from '@nestjs/common'; -import { ConfigModule, ConfigService } from '@nestjs/config'; -import { CqrsModule } from '@nestjs/cqrs'; -import { ConfigurationMessagerController } from './adapters/primaries/configuration-messager.controller'; -import { RedisConfigurationRepository } from './adapters/secondaries/redis-configuration.repository'; -import { DeleteConfigurationUseCase } from './domain/usecases/delete-configuration.usecase'; -import { GetConfigurationUseCase } from './domain/usecases/get-configuration.usecase'; -import { SetConfigurationUseCase } from './domain/usecases/set-configuration.usecase'; - -@Module({ - imports: [ - CqrsModule, - RedisModule.forRootAsync({ - imports: [ConfigModule], - inject: [ConfigService], - useFactory: async ( - configService: ConfigService, - ): Promise => ({ - config: { - host: configService.get('REDIS_HOST'), - port: configService.get('REDIS_PORT'), - password: configService.get('REDIS_PASSWORD'), - }, - }), - }), - RabbitMQModule.forRootAsync(RabbitMQModule, { - imports: [ConfigModule], - inject: [ConfigService], - useFactory: async ( - configService: ConfigService, - ): Promise => ({ - exchanges: [ - { - name: configService.get('RMQ_EXCHANGE'), - type: 'topic', - }, - ], - handlers: { - 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'), - connectionInitOptions: { wait: false }, - enableControllerDiscovery: true, - }), - }), - ], - controllers: [ConfigurationMessagerController], - providers: [ - GetConfigurationUseCase, - SetConfigurationUseCase, - DeleteConfigurationUseCase, - RedisConfigurationRepository, - ], -}) -export class ConfigurationModule {} diff --git a/src/modules/configuration/domain/dtos/delete-configuration.request.ts b/src/modules/configuration/domain/dtos/delete-configuration.request.ts deleted file mode 100644 index 3430832..0000000 --- a/src/modules/configuration/domain/dtos/delete-configuration.request.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { IsNotEmpty, IsString } from 'class-validator'; - -export class DeleteConfigurationRequest { - @IsString() - @IsNotEmpty() - domain: string; - - @IsString() - @IsNotEmpty() - key: string; -} diff --git a/src/modules/configuration/domain/dtos/set-configuration.request.ts b/src/modules/configuration/domain/dtos/set-configuration.request.ts deleted file mode 100644 index 3ed3fff..0000000 --- a/src/modules/configuration/domain/dtos/set-configuration.request.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { IsNotEmpty, IsString } from 'class-validator'; - -export class SetConfigurationRequest { - @IsString() - @IsNotEmpty() - domain: string; - - @IsString() - @IsNotEmpty() - key: string; - - @IsString() - @IsNotEmpty() - value: string; -} diff --git a/src/modules/configuration/domain/entities/configuration.ts b/src/modules/configuration/domain/entities/configuration.ts deleted file mode 100644 index 2008403..0000000 --- a/src/modules/configuration/domain/entities/configuration.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { AutoMap } from '@automapper/classes'; - -export class Configuration { - @AutoMap() - domain: string; - - @AutoMap() - key: string; - - @AutoMap() - value: string; -} diff --git a/src/modules/configuration/domain/interfaces/configuration.repository.ts b/src/modules/configuration/domain/interfaces/configuration.repository.ts deleted file mode 100644 index 4ea21c2..0000000 --- a/src/modules/configuration/domain/interfaces/configuration.repository.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Injectable } from '@nestjs/common'; - -@Injectable() -export abstract class IConfigurationRepository { - abstract get(key: string): Promise; - 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 deleted file mode 100644 index 15b39ce..0000000 --- a/src/modules/configuration/domain/usecases/delete-configuration.usecase.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { CommandHandler } from '@nestjs/cqrs'; -import { RedisConfigurationRepository } from '../../adapters/secondaries/redis-configuration.repository'; -import { DeleteConfigurationCommand } from '../../commands/delete-configuration.command'; - -@CommandHandler(DeleteConfigurationCommand) -export class DeleteConfigurationUseCase { - constructor(private configurationRepository: RedisConfigurationRepository) {} - - 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 deleted file mode 100644 index 77742e0..0000000 --- a/src/modules/configuration/domain/usecases/get-configuration.usecase.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { QueryHandler } from '@nestjs/cqrs'; -import { RedisConfigurationRepository } from '../../adapters/secondaries/redis-configuration.repository'; -import { GetConfigurationQuery } from '../../queries/get-configuration.query'; - -@QueryHandler(GetConfigurationQuery) -export class GetConfigurationUseCase { - constructor(private configurationRepository: RedisConfigurationRepository) {} - - 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 deleted file mode 100644 index 8a8ad41..0000000 --- a/src/modules/configuration/domain/usecases/set-configuration.usecase.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { CommandHandler } from '@nestjs/cqrs'; -import { RedisConfigurationRepository } from '../../adapters/secondaries/redis-configuration.repository'; -import { SetConfigurationCommand } from '../../commands/set-configuration.command'; - -@CommandHandler(SetConfigurationCommand) -export class SetConfigurationUseCase { - constructor(private configurationRepository: RedisConfigurationRepository) {} - - execute = async ( - setConfigurationCommand: SetConfigurationCommand, - ): Promise => { - await this.configurationRepository.set( - `${setConfigurationCommand.setConfigurationRequest.domain} - : - ${setConfigurationCommand.setConfigurationRequest.key}`, - setConfigurationCommand.setConfigurationRequest.value, - ); - }; -} diff --git a/src/modules/configuration/queries/get-configuration.query.ts b/src/modules/configuration/queries/get-configuration.query.ts deleted file mode 100644 index 62211e7..0000000 --- a/src/modules/configuration/queries/get-configuration.query.ts +++ /dev/null @@ -1,9 +0,0 @@ -export class GetConfigurationQuery { - readonly domain: string; - readonly key: string; - - constructor(domain: string, key: string) { - this.domain = domain; - this.key = key; - } -} diff --git a/src/modules/configuration/tests/unit/delete-configuration.usecase.spec.ts b/src/modules/configuration/tests/unit/delete-configuration.usecase.spec.ts deleted file mode 100644 index a28ae3c..0000000 --- a/src/modules/configuration/tests/unit/delete-configuration.usecase.spec.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { RedisConfigurationRepository } from '../../adapters/secondaries/redis-configuration.repository'; -import { DeleteConfigurationCommand } from '../../commands/delete-configuration.command'; -import { DeleteConfigurationRequest } from '../../domain/dtos/delete-configuration.request'; -import { DeleteConfigurationUseCase } from '../../domain/usecases/delete-configuration.usecase'; - -const mockRedisConfigurationRepository = { - del: jest.fn().mockResolvedValue(undefined), -}; - -describe('DeleteConfigurationUseCase', () => { - let deleteConfigurationUseCase: DeleteConfigurationUseCase; - - beforeAll(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [ - { - provide: RedisConfigurationRepository, - useValue: mockRedisConfigurationRepository, - }, - - DeleteConfigurationUseCase, - ], - }).compile(); - - deleteConfigurationUseCase = module.get( - DeleteConfigurationUseCase, - ); - }); - - it('should be defined', () => { - expect(deleteConfigurationUseCase).toBeDefined(); - }); - - describe('execute', () => { - it('should delete a key', async () => { - jest.spyOn(mockRedisConfigurationRepository, 'del'); - const deleteConfigurationRequest: DeleteConfigurationRequest = { - domain: 'my-domain', - key: 'my-key', - }; - await deleteConfigurationUseCase.execute( - new DeleteConfigurationCommand(deleteConfigurationRequest), - ); - - expect(mockRedisConfigurationRepository.del).toHaveBeenCalledTimes(1); - }); - }); -}); diff --git a/src/modules/configuration/tests/unit/get-configuration.usecase.spec.ts b/src/modules/configuration/tests/unit/get-configuration.usecase.spec.ts deleted file mode 100644 index a94fc70..0000000 --- a/src/modules/configuration/tests/unit/get-configuration.usecase.spec.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { RedisConfigurationRepository } from '../../adapters/secondaries/redis-configuration.repository'; -import { GetConfigurationUseCase } from '../../domain/usecases/get-configuration.usecase'; -import { GetConfigurationQuery } from '../../queries/get-configuration.query'; - -const mockRedisConfigurationRepository = { - get: jest.fn().mockResolvedValue('my-value'), -}; - -describe('GetConfigurationUseCase', () => { - let getConfigurationUseCase: GetConfigurationUseCase; - - beforeAll(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [ - { - provide: RedisConfigurationRepository, - useValue: mockRedisConfigurationRepository, - }, - - GetConfigurationUseCase, - ], - }).compile(); - - getConfigurationUseCase = module.get( - GetConfigurationUseCase, - ); - }); - - it('should be defined', () => { - expect(getConfigurationUseCase).toBeDefined(); - }); - - describe('execute', () => { - it('should get a value for a key', async () => { - const value: string = await getConfigurationUseCase.execute( - new GetConfigurationQuery('my-domain', 'my-key'), - ); - - expect(value).toBe('my-value'); - }); - }); -}); diff --git a/src/modules/configuration/tests/unit/redis-configuration.repository.spec.ts b/src/modules/configuration/tests/unit/redis-configuration.repository.spec.ts deleted file mode 100644 index 8efa436..0000000 --- a/src/modules/configuration/tests/unit/redis-configuration.repository.spec.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { RedisConfigurationRepository } from '../../adapters/secondaries/redis-configuration.repository'; -import { getRedisToken } from '@liaoliaots/nestjs-redis'; - -const mockRedis = { - get: jest.fn().mockResolvedValue('myValue'), - set: jest.fn().mockImplementation(), - del: jest.fn().mockImplementation(), -}; - -describe('RedisConfigurationRepository', () => { - let redisConfigurationRepository: RedisConfigurationRepository; - - beforeAll(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [ - { - provide: getRedisToken('default'), - useValue: mockRedis, - }, - RedisConfigurationRepository, - ], - }).compile(); - - redisConfigurationRepository = module.get( - RedisConfigurationRepository, - ); - }); - - it('should be defined', () => { - expect(redisConfigurationRepository).toBeDefined(); - }); - - describe('interact', () => { - it('should get a value', async () => { - expect(await redisConfigurationRepository.get('myKey')).toBe('myValue'); - }); - it('should set a value', async () => { - expect( - await redisConfigurationRepository.set('myKey', 'myValue'), - ).toBeUndefined(); - }); - it('should delete a value', async () => { - expect(await redisConfigurationRepository.del('myKey')).toBeUndefined(); - }); - }); -}); diff --git a/src/modules/configuration/tests/unit/set-configuration.usecase.spec.ts b/src/modules/configuration/tests/unit/set-configuration.usecase.spec.ts deleted file mode 100644 index f6e25d1..0000000 --- a/src/modules/configuration/tests/unit/set-configuration.usecase.spec.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { RedisConfigurationRepository } from '../../adapters/secondaries/redis-configuration.repository'; -import { SetConfigurationCommand } from '../../commands/set-configuration.command'; -import { SetConfigurationRequest } from '../../domain/dtos/set-configuration.request'; -import { SetConfigurationUseCase } from '../../domain/usecases/set-configuration.usecase'; - -const mockRedisConfigurationRepository = { - set: jest.fn().mockResolvedValue(undefined), -}; - -describe('SetConfigurationUseCase', () => { - let setConfigurationUseCase: SetConfigurationUseCase; - - beforeAll(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [ - { - provide: RedisConfigurationRepository, - useValue: mockRedisConfigurationRepository, - }, - - SetConfigurationUseCase, - ], - }).compile(); - - setConfigurationUseCase = module.get( - SetConfigurationUseCase, - ); - }); - - it('should be defined', () => { - expect(setConfigurationUseCase).toBeDefined(); - }); - - describe('execute', () => { - it('should set a value for a key', async () => { - jest.spyOn(mockRedisConfigurationRepository, 'set'); - const setConfigurationRequest: SetConfigurationRequest = { - domain: 'my-domain', - key: 'my-key', - value: 'my-value', - }; - await setConfigurationUseCase.execute( - new SetConfigurationCommand(setConfigurationRequest), - ); - - expect(mockRedisConfigurationRepository.set).toHaveBeenCalledTimes(1); - }); - }); -});