From 68d37ba2cca7f5737fcde91ef268dc1c2b2a3aef Mon Sep 17 00:00:00 2001 From: sbriat Date: Fri, 5 May 2023 16:39:28 +0200 Subject: [PATCH] clean using es6 --- Dockerfile | 4 + .../configuration-messager.controller.ts | 16 +- .../redis-configuration.repository.ts | 8 +- .../usecases/delete-configuration.usecase.ts | 10 +- .../usecases/get-configuration.usecase.ts | 9 +- .../usecases/set-configuration.usecase.ts | 10 +- .../secondaries/prisma-repository.abstract.ts | 145 +++++++++--------- .../adapters/secondaries/prisma-service.ts | 0 src/modules/database/database.module.ts | 4 +- .../database/{src => }/domain/point.type.ts | 0 .../{src => }/domain/territory-repository.ts | 19 ++- .../exceptions/database.exception.ts | 0 .../interfaces/collection.interface.ts | 0 .../interfaces/repository.interface.ts | 0 .../tests/unit/prisma-repository.spec.ts | 32 ++-- .../tests/unit/territory-repository.spec.ts | 8 +- .../primaries/health-server.controller.ts | 4 +- .../adapters/primaries/health.controller.ts | 12 +- .../health/adapters/secondaries/messager.ts | 10 +- .../prisma.health-indicator.usecase.ts | 8 +- .../primaries/territory.controller.ts | 4 +- .../adapters/secondaries/messager.ts | 10 +- .../secondaries/territories.repository.ts | 4 +- .../usecases/create-territory.usecase.ts | 24 +-- .../usecases/delete-territory.usecase.ts | 4 +- .../find-all-territories-for-point.usecase.ts | 11 +- ...find-all-territories-for-points.usecase.ts | 13 +- .../usecases/find-all-territories.usecase.ts | 11 +- .../find-territory-by-uuid.usecase.ts | 14 +- .../usecases/update-territory.usecase.ts | 24 +-- .../territories.repository.spec.ts | 4 +- 31 files changed, 214 insertions(+), 208 deletions(-) rename src/modules/database/{src => }/adapters/secondaries/prisma-repository.abstract.ts (52%) rename src/modules/database/{src => }/adapters/secondaries/prisma-service.ts (100%) rename src/modules/database/{src => }/domain/point.type.ts (100%) rename src/modules/database/{src => }/domain/territory-repository.ts (86%) rename src/modules/database/{src => }/exceptions/database.exception.ts (100%) rename src/modules/database/{src => }/interfaces/collection.interface.ts (100%) rename src/modules/database/{src => }/interfaces/repository.interface.ts (100%) diff --git a/Dockerfile b/Dockerfile index 90f8c66..68bb1e8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,8 +12,12 @@ WORKDIR /usr/src/app # Copying this first prevents re-running npm install on every code change. COPY --chown=node:node package*.json ./ +# Copy prisma (needed for prisma error types) +COPY --chown=node:node ./prisma prisma + # Install app dependencies using the `npm ci` command instead of `npm install` RUN npm ci +RUN npx prisma generate # Bundle app source COPY --chown=node:node . . diff --git a/src/modules/configuration/adapters/primaries/configuration-messager.controller.ts b/src/modules/configuration/adapters/primaries/configuration-messager.controller.ts index c9408ca..929605a 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..26c6e16 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); + return await this.redis.get(key); } async set(key: string, value: string) { - await this._redis.set(key, value); + await this.redis.set(key, value); } async del(key: string) { - await this._redis.del(key); + await this.redis.del(key); } } diff --git a/src/modules/configuration/domain/usecases/delete-configuration.usecase.ts b/src/modules/configuration/domain/usecases/delete-configuration.usecase.ts index 14ab3cb..0ff1095 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( + 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..f7a5740 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( + 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..0ed7503 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( + execute = async ( + setConfigurationCommand: SetConfigurationCommand, + ): Promise => { + await this.configurationRepository.set( setConfigurationCommand.setConfigurationRequest.domain + ':' + setConfigurationCommand.setConfigurationRequest.key, setConfigurationCommand.setConfigurationRequest.value, ); - } + }; } diff --git a/src/modules/database/src/adapters/secondaries/prisma-repository.abstract.ts b/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts similarity index 52% rename from src/modules/database/src/adapters/secondaries/prisma-repository.abstract.ts rename to src/modules/database/adapters/secondaries/prisma-repository.abstract.ts index 2827de9..c62eaf2 100644 --- a/src/modules/database/src/adapters/secondaries/prisma-repository.abstract.ts +++ b/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts @@ -1,5 +1,5 @@ import { Injectable } from '@nestjs/common'; -import { PrismaClientKnownRequestError } from '@prisma/client/runtime'; +import { Prisma } from '@prisma/client'; import { DatabaseException } from '../../exceptions/database.exception'; import { ICollection } from '../../interfaces/collection.interface'; import { IRepository } from '../../interfaces/repository.interface'; @@ -10,24 +10,24 @@ import { PrismaService } from './prisma-service'; */ @Injectable() export abstract class PrismaRepository implements IRepository { - protected _model: string; + protected model: string; - constructor(protected readonly _prisma: PrismaService) {} + constructor(protected readonly prisma: PrismaService) {} - async findAll( + findAll = async ( page = 1, perPage = 10, where?: any, include?: any, - ): Promise> { - const [data, total] = await this._prisma.$transaction([ - this._prisma[this._model].findMany({ + ): Promise> => { + const [data, total] = await this.prisma.$transaction([ + this.prisma[this.model].findMany({ where, include, skip: (page - 1) * perPage, take: perPage, }), - this._prisma[this._model].count({ + this.prisma[this.model].count({ where, }), ]); @@ -35,19 +35,19 @@ export abstract class PrismaRepository implements IRepository { data, total, }); - } + }; - async findOneByUuid(uuid: string): Promise { + findOneByUuid = async (uuid: string): Promise => { try { - const entity = await this._prisma[this._model].findUnique({ + const entity = await this.prisma[this.model].findUnique({ where: { uuid }, }); return entity; } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -55,39 +55,42 @@ export abstract class PrismaRepository implements IRepository { throw new DatabaseException(); } } - } + }; - async findOne(where: any, include?: any): Promise { + findOne = async (where: any, include?: any): Promise => { try { - const entity = await this._prisma[this._model].findFirst({ + const entity = await this.prisma[this.model].findFirst({ where: where, include: include, }); return entity; } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { - throw new DatabaseException(PrismaClientKnownRequestError.name, e.code); + if (e instanceof Prisma.PrismaClientKnownRequestError) { + throw new DatabaseException( + Prisma.PrismaClientKnownRequestError.name, + e.code, + ); } else { throw new DatabaseException(); } } - } + }; // TODO : using any is not good, but needed for nested entities // TODO : Refactor for good clean architecture ? - async create(entity: Partial | any, include?: any): Promise { + create = async (entity: Partial | any, include?: any): Promise => { try { - const res = await this._prisma[this._model].create({ + const res = await this.prisma[this.model].create({ data: entity, include: include, }); return res; } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -95,19 +98,19 @@ export abstract class PrismaRepository implements IRepository { throw new DatabaseException(); } } - } + }; - async update(uuid: string, entity: Partial): Promise { + update = async (uuid: string, entity: Partial): Promise => { try { - const updatedEntity = await this._prisma[this._model].update({ + const updatedEntity = await this.prisma[this.model].update({ where: { uuid }, data: entity, }); return updatedEntity; } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -115,15 +118,15 @@ export abstract class PrismaRepository implements IRepository { throw new DatabaseException(); } } - } + }; - async updateWhere( + updateWhere = async ( where: any, entity: Partial | any, include?: any, - ): Promise { + ): Promise => { try { - const updatedEntity = await this._prisma[this._model].update({ + const updatedEntity = await this.prisma[this.model].update({ where: where, data: entity, include: include, @@ -131,9 +134,9 @@ export abstract class PrismaRepository implements IRepository { return updatedEntity; } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -141,19 +144,19 @@ export abstract class PrismaRepository implements IRepository { throw new DatabaseException(); } } - } + }; - async delete(uuid: string): Promise { + delete = async (uuid: string): Promise => { try { - const entity = await this._prisma[this._model].delete({ + const entity = await this.prisma[this.model].delete({ where: { uuid }, }); return entity; } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -161,19 +164,19 @@ export abstract class PrismaRepository implements IRepository { throw new DatabaseException(); } } - } + }; - async deleteMany(where: any): Promise { + deleteMany = async (where: any): Promise => { try { - const entity = await this._prisma[this._model].deleteMany({ + const entity = await this.prisma[this.model].deleteMany({ where: where, }); return entity; } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -181,32 +184,32 @@ export abstract class PrismaRepository implements IRepository { throw new DatabaseException(); } } - } + }; - async findAllByQuery( - include: Array, - where: Array, - ): Promise> { + findAllByQuery = async ( + include: string[], + where: string[], + ): Promise> => { const query = `SELECT ${include.join(',')} FROM ${ - this._model + this.model } WHERE ${where.join(' AND ')}`; - const data: Array = await this._prisma.$queryRawUnsafe(query); + const data: T[] = await this.prisma.$queryRawUnsafe(query); return Promise.resolve({ data, total: data.length, }); - } + }; - async createWithFields(fields: object): Promise { + createWithFields = async (fields: object): Promise => { try { - const command = `INSERT INTO ${this._model} (${Object.keys(fields).join( - ',', - )}) VALUES (${Object.values(fields).join(',')})`; - return await this._prisma.$executeRawUnsafe(command); + const command = `INSERT INTO ${this.model} ("${Object.keys(fields).join( + '","', + )}") VALUES (${Object.values(fields).join(',')})`; + return await this.prisma.$executeRawUnsafe(command); } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -214,20 +217,20 @@ export abstract class PrismaRepository implements IRepository { throw new DatabaseException(); } } - } + }; - async updateWithFields(uuid: string, entity: Partial): Promise { + updateWithFields = async (uuid: string, entity: object): Promise => { entity['"updatedAt"'] = `to_timestamp(${Date.now()} / 1000.0)`; const values = Object.keys(entity).map((key) => `${key} = ${entity[key]}`); try { - const command = `UPDATE ${this._model} SET ${values.join( + const command = `UPDATE ${this.model} SET ${values.join( ', ', )} WHERE uuid = '${uuid}'`; - return await this._prisma.$executeRawUnsafe(command); + return await this.prisma.$executeRawUnsafe(command); } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -235,16 +238,16 @@ export abstract class PrismaRepository implements IRepository { throw new DatabaseException(); } } - } + }; - async healthCheck(): Promise { + healthCheck = async (): Promise => { try { - await this._prisma.$queryRaw`SELECT 1`; + await this.prisma.$queryRaw`SELECT 1`; return true; } catch (e) { - if (e instanceof PrismaClientKnownRequestError) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( - PrismaClientKnownRequestError.name, + Prisma.PrismaClientKnownRequestError.name, e.code, e.message, ); @@ -252,5 +255,5 @@ export abstract class PrismaRepository implements IRepository { throw new DatabaseException(); } } - } + }; } diff --git a/src/modules/database/src/adapters/secondaries/prisma-service.ts b/src/modules/database/adapters/secondaries/prisma-service.ts similarity index 100% rename from src/modules/database/src/adapters/secondaries/prisma-service.ts rename to src/modules/database/adapters/secondaries/prisma-service.ts diff --git a/src/modules/database/database.module.ts b/src/modules/database/database.module.ts index d979954..0b3f797 100644 --- a/src/modules/database/database.module.ts +++ b/src/modules/database/database.module.ts @@ -1,6 +1,6 @@ import { Module } from '@nestjs/common'; -import { PrismaService } from './src/adapters/secondaries/prisma-service'; -import { TerritoryRepository } from './src/domain/territory-repository'; +import { PrismaService } from './adapters/secondaries/prisma-service'; +import { TerritoryRepository } from './domain/territory-repository'; @Module({ providers: [PrismaService, TerritoryRepository], diff --git a/src/modules/database/src/domain/point.type.ts b/src/modules/database/domain/point.type.ts similarity index 100% rename from src/modules/database/src/domain/point.type.ts rename to src/modules/database/domain/point.type.ts diff --git a/src/modules/database/src/domain/territory-repository.ts b/src/modules/database/domain/territory-repository.ts similarity index 86% rename from src/modules/database/src/domain/territory-repository.ts rename to src/modules/database/domain/territory-repository.ts index 43cee35..6adac0f 100644 --- a/src/modules/database/src/domain/territory-repository.ts +++ b/src/modules/database/domain/territory-repository.ts @@ -6,16 +6,15 @@ import { Territory } from 'src/modules/territory/domain/entities/territory'; import { DatabaseException } from '../exceptions/database.exception'; export class TerritoryRepository extends PrismaRepository { - async findForPoint(point: Point): Promise> { - return await this.findAllByQuery( + findForPoint = async (point: Point): Promise> => + await this.findAllByQuery( ['uuid', 'name'], [ `ST_Intersects(ST_GeomFromText('POINT(${point.lon} ${point.lat})',4326),shape) = true`, ], ); - } - async findForPoints(points: Point[]): Promise> { + findForPoints = async (points: Point[]): Promise> => { const multipoint = points .map((point) => '(' + point.lon + ' ' + point.lat + ')') .join(', '); @@ -25,9 +24,9 @@ export class TerritoryRepository extends PrismaRepository { `ST_Intersects(ST_GeomFromText('MULTIPOINT(${multipoint})',4326),shape) = true`, ], ); - } + }; - async createTerritory(territory: Partial): Promise { + createTerritory = async (territory: Partial): Promise => { try { const affectedRowNumber = await this.createWithFields({ uuid: `'${uuidv4()}'`, @@ -43,12 +42,12 @@ export class TerritoryRepository extends PrismaRepository { } catch (e) { throw e; } - } + }; - async updateTerritory( + updateTerritory = async ( uuid: string, territory: Partial, - ): Promise { + ): Promise => { try { const fields = {}; if (territory.name) fields['name'] = `'${territory.name}'`; @@ -66,5 +65,5 @@ export class TerritoryRepository extends PrismaRepository { } catch (e) { throw e; } - } + }; } diff --git a/src/modules/database/src/exceptions/database.exception.ts b/src/modules/database/exceptions/database.exception.ts similarity index 100% rename from src/modules/database/src/exceptions/database.exception.ts rename to src/modules/database/exceptions/database.exception.ts diff --git a/src/modules/database/src/interfaces/collection.interface.ts b/src/modules/database/interfaces/collection.interface.ts similarity index 100% rename from src/modules/database/src/interfaces/collection.interface.ts rename to src/modules/database/interfaces/collection.interface.ts diff --git a/src/modules/database/src/interfaces/repository.interface.ts b/src/modules/database/interfaces/repository.interface.ts similarity index 100% rename from src/modules/database/src/interfaces/repository.interface.ts rename to src/modules/database/interfaces/repository.interface.ts diff --git a/src/modules/database/tests/unit/prisma-repository.spec.ts b/src/modules/database/tests/unit/prisma-repository.spec.ts index bc194c4..eb3bad0 100644 --- a/src/modules/database/tests/unit/prisma-repository.spec.ts +++ b/src/modules/database/tests/unit/prisma-repository.spec.ts @@ -1,9 +1,9 @@ import { Injectable } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; -import { PrismaService } from '../../src/adapters/secondaries/prisma-service'; -import { PrismaRepository } from '../../src/adapters/secondaries/prisma-repository.abstract'; -import { DatabaseException } from '../../src/exceptions/database.exception'; -import { PrismaClientKnownRequestError } from '@prisma/client/runtime'; +import { PrismaService } from '../../adapters/secondaries/prisma-service'; +import { PrismaRepository } from '../../adapters/secondaries/prisma-repository.abstract'; +import { DatabaseException } from '../../exceptions/database.exception'; +import { Prisma } from '@prisma/client'; class FakeEntity { uuid?: string; @@ -41,7 +41,7 @@ Array.from({ length: 10 }).forEach(() => { @Injectable() class FakePrismaRepository extends PrismaRepository { - protected _model = 'fake'; + protected model = 'fake'; } class FakePrismaService extends PrismaService { @@ -66,7 +66,7 @@ const mockPrismaService = { .mockResolvedValueOnce(fakeEntityCreated) // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((fields: object) => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); @@ -78,7 +78,7 @@ const mockPrismaService = { .mockResolvedValueOnce(fakeEntityCreated) // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((fields: object) => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); @@ -90,7 +90,7 @@ const mockPrismaService = { $queryRaw: jest .fn() .mockImplementationOnce(() => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); @@ -99,7 +99,7 @@ const mockPrismaService = { return true; }) .mockImplementation(() => { - throw new PrismaClientKnownRequestError('Database unavailable', { + throw new Prisma.PrismaClientKnownRequestError('Database unavailable', { code: 'code', clientVersion: 'version', }); @@ -110,7 +110,7 @@ const mockPrismaService = { .mockResolvedValueOnce(fakeEntityCreated) // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((params?: any) => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); @@ -139,7 +139,7 @@ const mockPrismaService = { } if (!entity && params?.where?.uuid == 'unknown') { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); @@ -161,7 +161,7 @@ const mockPrismaService = { }) // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((params?: any) => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); @@ -175,14 +175,14 @@ const mockPrismaService = { .fn() // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((params?: any) => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); }) // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((params?: any) => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); @@ -212,7 +212,7 @@ const mockPrismaService = { .fn() // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((params?: any) => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); @@ -236,7 +236,7 @@ const mockPrismaService = { .fn() // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementationOnce((params?: any) => { - throw new PrismaClientKnownRequestError('unknown request', { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); diff --git a/src/modules/database/tests/unit/territory-repository.spec.ts b/src/modules/database/tests/unit/territory-repository.spec.ts index 9476f7b..fef4b7d 100644 --- a/src/modules/database/tests/unit/territory-repository.spec.ts +++ b/src/modules/database/tests/unit/territory-repository.spec.ts @@ -1,10 +1,10 @@ import { Injectable } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; -import { PrismaService } from '../../src/adapters/secondaries/prisma-service'; -import { TerritoryRepository } from '../../src/domain/territory-repository'; +import { PrismaService } from '../../adapters/secondaries/prisma-service'; +import { TerritoryRepository } from '../../domain/territory-repository'; import { Territory } from '../../../territory/domain/entities/territory'; import { Point } from '../../../territory/domain/entities/point'; -import { DatabaseException } from '../../src/exceptions/database.exception'; +import { DatabaseException } from '../../exceptions/database.exception'; const mockTerritories: Array = [ { @@ -33,7 +33,7 @@ const territoryUpdated: Territory = { @Injectable() class TerritoriesRepository extends TerritoryRepository { - protected _model = 'territory'; + protected model = 'territory'; } const mockPrismaService = { diff --git a/src/modules/health/adapters/primaries/health-server.controller.ts b/src/modules/health/adapters/primaries/health-server.controller.ts index b58c761..c0d63c8 100644 --- a/src/modules/health/adapters/primaries/health-server.controller.ts +++ b/src/modules/health/adapters/primaries/health-server.controller.ts @@ -19,7 +19,7 @@ interface HealthCheckResponse { @Controller() export class HealthServerController { constructor( - private readonly _prismaHealthIndicatorUseCase: PrismaHealthIndicatorUseCase, + private readonly prismaHealthIndicatorUseCase: PrismaHealthIndicatorUseCase, ) {} @GrpcMethod('Health', 'Check') @@ -29,7 +29,7 @@ export class HealthServerController { // eslint-disable-next-line @typescript-eslint/no-unused-vars metadata: any, ): Promise { - const healthCheck = await this._prismaHealthIndicatorUseCase.isHealthy( + const healthCheck = await this.prismaHealthIndicatorUseCase.isHealthy( 'prisma', ); return { diff --git a/src/modules/health/adapters/primaries/health.controller.ts b/src/modules/health/adapters/primaries/health.controller.ts index 810652b..8307bd5 100644 --- a/src/modules/health/adapters/primaries/health.controller.ts +++ b/src/modules/health/adapters/primaries/health.controller.ts @@ -10,21 +10,21 @@ import { PrismaHealthIndicatorUseCase } from '../../domain/usecases/prisma.healt @Controller('health') export class HealthController { constructor( - private readonly _prismaHealthIndicatorUseCase: PrismaHealthIndicatorUseCase, - private _healthCheckService: HealthCheckService, - private _messager: Messager, + private readonly prismaHealthIndicatorUseCase: PrismaHealthIndicatorUseCase, + private readonly healthCheckService: HealthCheckService, + private readonly messager: Messager, ) {} @Get() @HealthCheck() async check() { try { - return await this._healthCheckService.check([ - async () => this._prismaHealthIndicatorUseCase.isHealthy('prisma'), + return await this.healthCheckService.check([ + async () => this.prismaHealthIndicatorUseCase.isHealthy('prisma'), ]); } catch (error) { const healthCheckResult: HealthCheckResult = error.response; - this._messager.publish( + this.messager.publish( 'logging.territory.health.crit', JSON.stringify(healthCheckResult.error), ); diff --git a/src/modules/health/adapters/secondaries/messager.ts b/src/modules/health/adapters/secondaries/messager.ts index 0725261..567a23f 100644 --- a/src/modules/health/adapters/secondaries/messager.ts +++ b/src/modules/health/adapters/secondaries/messager.ts @@ -6,13 +6,13 @@ import { IMessageBroker } from './message-broker'; @Injectable() export class Messager extends IMessageBroker { constructor( - private readonly _amqpConnection: AmqpConnection, - configService: ConfigService, + private readonly amqpConnection: AmqpConnection, + private readonly configService: ConfigService, ) { super(configService.get('RMQ_EXCHANGE')); } - publish(routingKey: string, message: string): void { - this._amqpConnection.publish(this.exchange, routingKey, message); - } + publish = (routingKey: string, message: string): void => { + this.amqpConnection.publish(this.exchange, routingKey, message); + }; } diff --git a/src/modules/health/domain/usecases/prisma.health-indicator.usecase.ts b/src/modules/health/domain/usecases/prisma.health-indicator.usecase.ts index 7f30583..e7b3348 100644 --- a/src/modules/health/domain/usecases/prisma.health-indicator.usecase.ts +++ b/src/modules/health/domain/usecases/prisma.health-indicator.usecase.ts @@ -8,18 +8,18 @@ import { TerritoriesRepository } from '../../../territory/adapters/secondaries/t @Injectable() export class PrismaHealthIndicatorUseCase extends HealthIndicator { - constructor(private readonly _repository: TerritoriesRepository) { + constructor(private readonly repository: TerritoriesRepository) { super(); } - async isHealthy(key: string): Promise { + isHealthy = async (key: string): Promise => { try { - await this._repository.healthCheck(); + await this.repository.healthCheck(); return this.getStatus(key, true); } catch (e) { throw new HealthCheckError('Prisma', { prisma: e.message, }); } - } + }; } diff --git a/src/modules/territory/adapters/primaries/territory.controller.ts b/src/modules/territory/adapters/primaries/territory.controller.ts index cd49e76..941b702 100644 --- a/src/modules/territory/adapters/primaries/territory.controller.ts +++ b/src/modules/territory/adapters/primaries/territory.controller.ts @@ -6,7 +6,7 @@ import { GrpcMethod, RpcException } from '@nestjs/microservices'; import { Territory } from '../../domain/entities/territory'; import { FindAllTerritoriesForPointQuery } from '../../queries/find-all-territories-for-point.query'; import { TerritoryPresenter } from './territory.presenter'; -import { ICollection } from '../../../database/src/interfaces/collection.interface'; +import { ICollection } from '../../../database/interfaces/collection.interface'; import { RpcValidationPipe } from '../../../../utils/pipes/rpc.validation-pipe'; import { FindAllTerritoriesForPointRequest } from '../../domain/dtos/find-all-territories-for-point.request'; import { FindAllTerritoriesRequest } from '../../domain/dtos/find-all-territories.request'; @@ -15,7 +15,7 @@ import { FindTerritoryByUuidRequest } from '../../domain/dtos/find-territory-by- import { FindTerritoryByUuidQuery } from '../../queries/find-territory-by-uuid.query'; import { CreateTerritoryRequest } from '../../domain/dtos/create-territory.request'; import { CreateTerritoryCommand } from '../../commands/create-territory.command'; -import { DatabaseException } from 'src/modules/database/src/exceptions/database.exception'; +import { DatabaseException } from 'src/modules/database/exceptions/database.exception'; import { UpdateTerritoryRequest } from '../../domain/dtos/update-territory.request'; import { UpdateTerritoryCommand } from '../../commands/update-territory.command'; import { DeleteTerritoryCommand } from '../../commands/delete-territory.command'; diff --git a/src/modules/territory/adapters/secondaries/messager.ts b/src/modules/territory/adapters/secondaries/messager.ts index 0ee32e3..d8fdac1 100644 --- a/src/modules/territory/adapters/secondaries/messager.ts +++ b/src/modules/territory/adapters/secondaries/messager.ts @@ -6,13 +6,13 @@ import { IMessageBroker } from '../../domain/interfaces/message-broker'; @Injectable() export class Messager extends IMessageBroker { constructor( - private readonly _amqpConnection: AmqpConnection, - configService: ConfigService, + private readonly amqpConnection: AmqpConnection, + private readonly configService: ConfigService, ) { super(configService.get('RMQ_EXCHANGE')); } - publish(routingKey: string, message: string): void { - this._amqpConnection.publish(this.exchange, routingKey, message); - } + publish = (routingKey: string, message: string): void => { + this.amqpConnection.publish(this.exchange, routingKey, message); + }; } diff --git a/src/modules/territory/adapters/secondaries/territories.repository.ts b/src/modules/territory/adapters/secondaries/territories.repository.ts index 6c617c4..fa05f16 100644 --- a/src/modules/territory/adapters/secondaries/territories.repository.ts +++ b/src/modules/territory/adapters/secondaries/territories.repository.ts @@ -1,8 +1,8 @@ import { Injectable } from '@nestjs/common'; -import { TerritoryRepository } from '../../../database/src/domain/territory-repository'; +import { TerritoryRepository } from '../../../database/domain/territory-repository'; import { Territory } from '../../domain/entities/territory'; @Injectable() export class TerritoriesRepository extends TerritoryRepository { - protected _model = 'territory'; + protected model = 'territory'; } diff --git a/src/modules/territory/domain/usecases/create-territory.usecase.ts b/src/modules/territory/domain/usecases/create-territory.usecase.ts index ee7e58e..83760d0 100644 --- a/src/modules/territory/domain/usecases/create-territory.usecase.ts +++ b/src/modules/territory/domain/usecases/create-territory.usecase.ts @@ -12,30 +12,30 @@ import { TerritoryPresenter } from '../../adapters/secondaries/territory.present @CommandHandler(CreateTerritoryCommand) export class CreateTerritoryUseCase { constructor( - private readonly _repository: TerritoriesRepository, - private readonly _messager: Messager, - @InjectMapper() private readonly _mapper: Mapper, + private readonly repository: TerritoriesRepository, + private readonly messager: Messager, + @InjectMapper() private readonly mapper: Mapper, ) {} - async execute(command: CreateTerritoryCommand): Promise { - const entity = this._mapper.map( + execute = async (command: CreateTerritoryCommand): Promise => { + const entity = this.mapper.map( command.createTerritoryRequest, CreateTerritoryRequest, Territory, ); try { - const territory = await this._repository.createTerritory(entity); - this._messager.publish( + const territory = await this.repository.createTerritory(entity); + this.messager.publish( 'territory.create', JSON.stringify( - this._mapper.map(territory, Territory, TerritoryPresenter), + this.mapper.map(territory, Territory, TerritoryPresenter), ), ); - this._messager.publish( + this.messager.publish( 'logging.territory.create.info', JSON.stringify( - this._mapper.map(territory, Territory, TerritoryLoggingPresenter), + this.mapper.map(territory, Territory, TerritoryLoggingPresenter), ), ); return territory; @@ -44,7 +44,7 @@ export class CreateTerritoryUseCase { if (error.message.includes('already exists')) { key = 'logging.territory.create.warning'; } - this._messager.publish( + this.messager.publish( key, JSON.stringify({ command, @@ -53,5 +53,5 @@ export class CreateTerritoryUseCase { ); throw error; } - } + }; } diff --git a/src/modules/territory/domain/usecases/delete-territory.usecase.ts b/src/modules/territory/domain/usecases/delete-territory.usecase.ts index 7c63775..47c0c66 100644 --- a/src/modules/territory/domain/usecases/delete-territory.usecase.ts +++ b/src/modules/territory/domain/usecases/delete-territory.usecase.ts @@ -11,7 +11,7 @@ export class DeleteTerritoryUseCase { private readonly _messager: Messager, ) {} - async execute(command: DeleteTerritoryCommand): Promise { + execute = async (command: DeleteTerritoryCommand): Promise => { try { const territory = await this._repository.delete(command.uuid); this._messager.publish( @@ -33,5 +33,5 @@ export class DeleteTerritoryUseCase { ); throw error; } - } + }; } diff --git a/src/modules/territory/domain/usecases/find-all-territories-for-point.usecase.ts b/src/modules/territory/domain/usecases/find-all-territories-for-point.usecase.ts index c39701d..044c227 100644 --- a/src/modules/territory/domain/usecases/find-all-territories-for-point.usecase.ts +++ b/src/modules/territory/domain/usecases/find-all-territories-for-point.usecase.ts @@ -1,5 +1,5 @@ import { QueryHandler } from '@nestjs/cqrs'; -import { ICollection } from 'src/modules/database/src/interfaces/collection.interface'; +import { ICollection } from 'src/modules/database/interfaces/collection.interface'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { FindAllTerritoriesForPointQuery } from '../../queries/find-all-territories-for-point.query'; import { Territory } from '../entities/territory'; @@ -7,16 +7,15 @@ import { Point } from '../entities/point'; @QueryHandler(FindAllTerritoriesForPointQuery) export class FindAllTerritoriesForPointUseCase { - constructor(private readonly _repository: TerritoriesRepository) {} + constructor(private readonly repository: TerritoriesRepository) {} - async execute( + execute = async ( findAllTerritoriesForPointQuery: FindAllTerritoriesForPointQuery, - ): Promise> { - return this._repository.findForPoint( + ): Promise> => + this.repository.findForPoint( new Point( findAllTerritoriesForPointQuery.point.lon, findAllTerritoriesForPointQuery.point.lat, ), ); - } } diff --git a/src/modules/territory/domain/usecases/find-all-territories-for-points.usecase.ts b/src/modules/territory/domain/usecases/find-all-territories-for-points.usecase.ts index 8b103d7..11595fa 100644 --- a/src/modules/territory/domain/usecases/find-all-territories-for-points.usecase.ts +++ b/src/modules/territory/domain/usecases/find-all-territories-for-points.usecase.ts @@ -1,18 +1,15 @@ import { QueryHandler } from '@nestjs/cqrs'; -import { ICollection } from 'src/modules/database/src/interfaces/collection.interface'; +import { ICollection } from 'src/modules/database/interfaces/collection.interface'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { FindAllTerritoriesForPointsQuery } from '../../queries/find-all-territories-for-points.query'; import { Territory } from '../entities/territory'; @QueryHandler(FindAllTerritoriesForPointsQuery) export class FindAllTerritoriesForPointsUseCase { - constructor(private readonly _repository: TerritoriesRepository) {} + constructor(private readonly repository: TerritoriesRepository) {} - async execute( + execute = async ( findAllTerritoriesForPointsQuery: FindAllTerritoriesForPointsQuery, - ): Promise> { - return this._repository.findForPoints( - findAllTerritoriesForPointsQuery.points, - ); - } + ): Promise> => + this.repository.findForPoints(findAllTerritoriesForPointsQuery.points); } diff --git a/src/modules/territory/domain/usecases/find-all-territories.usecase.ts b/src/modules/territory/domain/usecases/find-all-territories.usecase.ts index dadb3f9..35a97eb 100644 --- a/src/modules/territory/domain/usecases/find-all-territories.usecase.ts +++ b/src/modules/territory/domain/usecases/find-all-territories.usecase.ts @@ -1,19 +1,18 @@ import { QueryHandler } from '@nestjs/cqrs'; -import { ICollection } from 'src/modules/database/src/interfaces/collection.interface'; +import { ICollection } from 'src/modules/database/interfaces/collection.interface'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { FindAllTerritoriesQuery } from '../../queries/find-all-territories.query'; import { Territory } from '../entities/territory'; @QueryHandler(FindAllTerritoriesQuery) export class FindAllTerritoriesUseCase { - constructor(private readonly _repository: TerritoriesRepository) {} + constructor(private readonly repository: TerritoriesRepository) {} - async execute( + execute = async ( findAllTerritoriesQuery: FindAllTerritoriesQuery, - ): Promise> { - return this._repository.findAll( + ): Promise> => + this.repository.findAll( findAllTerritoriesQuery.page, findAllTerritoriesQuery.perPage, ); - } } diff --git a/src/modules/territory/domain/usecases/find-territory-by-uuid.usecase.ts b/src/modules/territory/domain/usecases/find-territory-by-uuid.usecase.ts index 6784871..ff279f0 100644 --- a/src/modules/territory/domain/usecases/find-territory-by-uuid.usecase.ts +++ b/src/modules/territory/domain/usecases/find-territory-by-uuid.usecase.ts @@ -8,21 +8,21 @@ import { Territory } from '../entities/territory'; @QueryHandler(FindTerritoryByUuidQuery) export class FindTerritoryByUuidUseCase { constructor( - private readonly _repository: TerritoriesRepository, - private readonly _messager: Messager, + private readonly repository: TerritoriesRepository, + private readonly messager: Messager, ) {} - async execute( + execute = async ( findTerritoryByUuidQuery: FindTerritoryByUuidQuery, - ): Promise { + ): Promise => { try { - const territory = await this._repository.findOneByUuid( + const territory = await this.repository.findOneByUuid( findTerritoryByUuidQuery.uuid, ); if (!territory) throw new NotFoundException(); return territory; } catch (error) { - this._messager.publish( + this.messager.publish( 'logging.territory.read.warning', JSON.stringify({ query: findTerritoryByUuidQuery, @@ -31,5 +31,5 @@ export class FindTerritoryByUuidUseCase { ); throw error; } - } + }; } diff --git a/src/modules/territory/domain/usecases/update-territory.usecase.ts b/src/modules/territory/domain/usecases/update-territory.usecase.ts index c27dfbc..ec3b8ca 100644 --- a/src/modules/territory/domain/usecases/update-territory.usecase.ts +++ b/src/modules/territory/domain/usecases/update-territory.usecase.ts @@ -12,35 +12,35 @@ import { TerritoryPresenter } from '../../adapters/secondaries/territory.present @CommandHandler(UpdateTerritoryCommand) export class UpdateTerritoryUseCase { constructor( - private readonly _repository: TerritoriesRepository, - private readonly _messager: Messager, - @InjectMapper() private readonly _mapper: Mapper, + private readonly repository: TerritoriesRepository, + private readonly messager: Messager, + @InjectMapper() private readonly mapper: Mapper, ) {} - async execute(command: UpdateTerritoryCommand): Promise { + execute = async (command: UpdateTerritoryCommand): Promise => { try { - const territory = await this._repository.updateTerritory( + const territory = await this.repository.updateTerritory( command.updateTerritoryRequest.uuid, - this._mapper.map( + this.mapper.map( command.updateTerritoryRequest, UpdateTerritoryRequest, Territory, ), ); - this._messager.publish( + this.messager.publish( 'territory.update', JSON.stringify( - this._mapper.map( + this.mapper.map( command.updateTerritoryRequest, UpdateTerritoryRequest, TerritoryPresenter, ), ), ); - this._messager.publish( + this.messager.publish( 'logging.territory.update.info', JSON.stringify( - this._mapper.map( + this.mapper.map( command.updateTerritoryRequest, UpdateTerritoryRequest, TerritoryLoggingPresenter, @@ -49,7 +49,7 @@ export class UpdateTerritoryUseCase { ); return territory; } catch (error) { - this._messager.publish( + this.messager.publish( 'logging.territory.update.crit', JSON.stringify({ command, @@ -58,5 +58,5 @@ export class UpdateTerritoryUseCase { ); throw error; } - } + }; } diff --git a/src/modules/territory/tests/integration/territories.repository.spec.ts b/src/modules/territory/tests/integration/territories.repository.spec.ts index 63e9655..5ec735d 100644 --- a/src/modules/territory/tests/integration/territories.repository.spec.ts +++ b/src/modules/territory/tests/integration/territories.repository.spec.ts @@ -1,7 +1,7 @@ import { TestingModule, Test } from '@nestjs/testing'; import { DatabaseModule } from '../../../database/database.module'; -import { PrismaService } from '../../../database/src/adapters/secondaries/prisma-service'; -import { DatabaseException } from '../../../database/src/exceptions/database.exception'; +import { PrismaService } from '../../../database/adapters/secondaries/prisma-service'; +import { DatabaseException } from '../../../database/exceptions/database.exception'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { v4 as uuidv4 } from 'uuid'; import { Territory } from '../../domain/entities/territory';