From da9d1ba1ca2649b866c5d071f20a4015aca06135 Mon Sep 17 00:00:00 2001 From: sbriat Date: Thu, 25 May 2023 09:07:31 +0200 Subject: [PATCH] refactor common modules --- .../redis-configuration.repository.ts | 4 +- .../secondaries/prisma-repository.abstract.ts | 80 +++++++++---------- src/modules/database/domain/point.type.ts | 4 - .../primaries/health-server.controller.ts | 4 +- .../adapters/primaries/health.controller.ts | 12 +-- .../health/adapters/secondaries/messager.ts | 8 +- .../prisma.health-indicator.usecase.ts | 8 +- 7 files changed, 57 insertions(+), 63 deletions(-) delete mode 100644 src/modules/database/domain/point.type.ts diff --git a/src/modules/configuration/adapters/secondaries/redis-configuration.repository.ts b/src/modules/configuration/adapters/secondaries/redis-configuration.repository.ts index 0ef29f2..1ceaebe 100644 --- a/src/modules/configuration/adapters/secondaries/redis-configuration.repository.ts +++ b/src/modules/configuration/adapters/secondaries/redis-configuration.repository.ts @@ -9,9 +9,7 @@ export class RedisConfigurationRepository extends IConfigurationRepository { super(); } - get = async (key: string): Promise => { - return await this.redis.get(key); - }; + get = async (key: string): Promise => await this.redis.get(key); set = async (key: string, value: string): Promise => { await this.redis.set(key, value); diff --git a/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts b/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts index 635e966..02c5cf9 100644 --- a/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts +++ b/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts @@ -12,22 +12,22 @@ import { PrismaService } from './prisma-service'; export abstract class PrismaRepository implements IRepository { 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,11 +35,11 @@ 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 }, }); @@ -55,11 +55,11 @@ 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, }); @@ -75,13 +75,13 @@ export abstract class PrismaRepository implements IRepository { 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 { try { - const res = await this._prisma[this.model].create({ + const res = await this.prisma[this.model].create({ data: entity, include: include, }); @@ -100,9 +100,9 @@ export abstract class PrismaRepository implements IRepository { } } - 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, }); @@ -118,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, @@ -144,11 +144,11 @@ 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 }, }); @@ -164,11 +164,11 @@ 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, }); @@ -184,28 +184,28 @@ export abstract class PrismaRepository implements IRepository { throw new DatabaseException(); } } - } + }; - async findAllByQuery( + findAllByQuery = async ( include: string[], where: string[], - ): Promise> { + ): Promise> => { const query = `SELECT ${include.join(',')} FROM ${ this.model } WHERE ${where.join(' AND ')}`; - const data: T[] = 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); + return await this.prisma.$executeRawUnsafe(command); } catch (e) { if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( @@ -217,16 +217,16 @@ export abstract class PrismaRepository implements IRepository { throw new DatabaseException(); } } - } + }; - async updateWithFields(uuid: string, entity: object): 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( ', ', )} WHERE uuid = '${uuid}'`; - return await this._prisma.$executeRawUnsafe(command); + return await this.prisma.$executeRawUnsafe(command); } catch (e) { if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseException( @@ -238,11 +238,11 @@ 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 Prisma.PrismaClientKnownRequestError) { @@ -255,5 +255,5 @@ export abstract class PrismaRepository implements IRepository { throw new DatabaseException(); } } - } + }; } diff --git a/src/modules/database/domain/point.type.ts b/src/modules/database/domain/point.type.ts deleted file mode 100644 index 9bb160e..0000000 --- a/src/modules/database/domain/point.type.ts +++ /dev/null @@ -1,4 +0,0 @@ -export type Point = { - lon: number; - lat: number; -}; 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 e5c5ac3..f9c59ed 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 healthCheckService: HealthCheckService, + private 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.matcher.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..cd7e7ef 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, + private readonly amqpConnection: AmqpConnection, 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 cb04b3d..be8520e 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 { AdRepository } from '../../../ad/adapters/secondaries/ad.repository'; @Injectable() export class PrismaHealthIndicatorUseCase extends HealthIndicator { - constructor(private readonly _repository: AdRepository) { + constructor(private readonly repository: AdRepository) { 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, }); } - } + }; }