Merge branch 'refactorCommonModules' into 'main'

refactor common modules

See merge request v3/service/matcher!7
This commit is contained in:
Sylvain Briat 2023-05-25 07:11:31 +00:00
commit 8ea3fb0f0a
7 changed files with 57 additions and 63 deletions

View File

@ -9,9 +9,7 @@ export class RedisConfigurationRepository extends IConfigurationRepository {
super();
}
get = async (key: string): Promise<string> => {
return await this.redis.get(key);
};
get = async (key: string): Promise<string> => await this.redis.get(key);
set = async (key: string, value: string): Promise<void> => {
await this.redis.set(key, value);

View File

@ -12,22 +12,22 @@ import { PrismaService } from './prisma-service';
export abstract class PrismaRepository<T> implements IRepository<T> {
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<ICollection<T>> {
const [data, total] = await this._prisma.$transaction([
this._prisma[this.model].findMany({
): Promise<ICollection<T>> => {
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<T> implements IRepository<T> {
data,
total,
});
}
};
async findOneByUuid(uuid: string): Promise<T> {
findOneByUuid = async (uuid: string): Promise<T> => {
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<T> implements IRepository<T> {
throw new DatabaseException();
}
}
}
};
async findOne(where: any, include?: any): Promise<T> {
findOne = async (where: any, include?: any): Promise<T> => {
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<T> implements IRepository<T> {
throw new DatabaseException();
}
}
}
};
// TODO : using any is not good, but needed for nested entities
// TODO : Refactor for good clean architecture ?
async create(entity: Partial<T> | any, include?: any): Promise<T> {
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<T> implements IRepository<T> {
}
}
async update(uuid: string, entity: Partial<T>): Promise<T> {
update = async (uuid: string, entity: Partial<T>): Promise<T> => {
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<T> implements IRepository<T> {
throw new DatabaseException();
}
}
}
};
async updateWhere(
updateWhere = async (
where: any,
entity: Partial<T> | any,
include?: any,
): Promise<T> {
): Promise<T> => {
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<T> implements IRepository<T> {
throw new DatabaseException();
}
}
}
};
async delete(uuid: string): Promise<T> {
delete = async (uuid: string): Promise<T> => {
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<T> implements IRepository<T> {
throw new DatabaseException();
}
}
}
};
async deleteMany(where: any): Promise<void> {
deleteMany = async (where: any): Promise<void> => {
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<T> implements IRepository<T> {
throw new DatabaseException();
}
}
}
};
async findAllByQuery(
findAllByQuery = async (
include: string[],
where: string[],
): Promise<ICollection<T>> {
): Promise<ICollection<T>> => {
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<number> {
createWithFields = async (fields: object): Promise<number> => {
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<T> implements IRepository<T> {
throw new DatabaseException();
}
}
}
};
async updateWithFields(uuid: string, entity: object): Promise<number> {
updateWithFields = async (uuid: string, entity: object): Promise<number> => {
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<T> implements IRepository<T> {
throw new DatabaseException();
}
}
}
};
async healthCheck(): Promise<boolean> {
healthCheck = async (): Promise<boolean> => {
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<T> implements IRepository<T> {
throw new DatabaseException();
}
}
}
};
}

View File

@ -1,4 +0,0 @@
export type Point = {
lon: number;
lat: number;
};

View File

@ -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<HealthCheckResponse> {
const healthCheck = await this._prismaHealthIndicatorUseCase.isHealthy(
const healthCheck = await this.prismaHealthIndicatorUseCase.isHealthy(
'prisma',
);
return {

View File

@ -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),
);

View File

@ -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<string>('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);
};
}

View File

@ -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<HealthIndicatorResult> {
isHealthy = async (key: string): Promise<HealthIndicatorResult> => {
try {
await this._repository.healthCheck();
await this.repository.healthCheck();
return this.getStatus(key, true);
} catch (e) {
throw new HealthCheckError('Prisma', {
prisma: e.message,
});
}
}
};
}