clean using es6
This commit is contained in:
parent
15e0e32e2e
commit
68d37ba2cc
|
@ -12,8 +12,12 @@ WORKDIR /usr/src/app
|
||||||
# Copying this first prevents re-running npm install on every code change.
|
# Copying this first prevents re-running npm install on every code change.
|
||||||
COPY --chown=node:node package*.json ./
|
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`
|
# Install app dependencies using the `npm ci` command instead of `npm install`
|
||||||
RUN npm ci
|
RUN npm ci
|
||||||
|
RUN npx prisma generate
|
||||||
|
|
||||||
# Bundle app source
|
# Bundle app source
|
||||||
COPY --chown=node:node . .
|
COPY --chown=node:node . .
|
||||||
|
|
|
@ -11,8 +11,8 @@ import { Configuration } from '../../domain/entities/configuration';
|
||||||
@Controller()
|
@Controller()
|
||||||
export class ConfigurationMessagerController {
|
export class ConfigurationMessagerController {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly _commandBus: CommandBus,
|
private readonly commandBus: CommandBus,
|
||||||
private readonly _configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@RabbitSubscribe({
|
@RabbitSubscribe({
|
||||||
|
@ -22,14 +22,14 @@ export class ConfigurationMessagerController {
|
||||||
const configuration: Configuration = JSON.parse(message);
|
const configuration: Configuration = JSON.parse(message);
|
||||||
if (
|
if (
|
||||||
configuration.domain ==
|
configuration.domain ==
|
||||||
this._configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
|
this.configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
|
||||||
) {
|
) {
|
||||||
const setConfigurationRequest: SetConfigurationRequest =
|
const setConfigurationRequest: SetConfigurationRequest =
|
||||||
new SetConfigurationRequest();
|
new SetConfigurationRequest();
|
||||||
setConfigurationRequest.domain = configuration.domain;
|
setConfigurationRequest.domain = configuration.domain;
|
||||||
setConfigurationRequest.key = configuration.key;
|
setConfigurationRequest.key = configuration.key;
|
||||||
setConfigurationRequest.value = configuration.value;
|
setConfigurationRequest.value = configuration.value;
|
||||||
await this._commandBus.execute(
|
await this.commandBus.execute(
|
||||||
new SetConfigurationCommand(setConfigurationRequest),
|
new SetConfigurationCommand(setConfigurationRequest),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -42,12 +42,12 @@ export class ConfigurationMessagerController {
|
||||||
const deletedConfiguration: Configuration = JSON.parse(message);
|
const deletedConfiguration: Configuration = JSON.parse(message);
|
||||||
if (
|
if (
|
||||||
deletedConfiguration.domain ==
|
deletedConfiguration.domain ==
|
||||||
this._configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
|
this.configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
|
||||||
) {
|
) {
|
||||||
const deleteConfigurationRequest = new DeleteConfigurationRequest();
|
const deleteConfigurationRequest = new DeleteConfigurationRequest();
|
||||||
deleteConfigurationRequest.domain = deletedConfiguration.domain;
|
deleteConfigurationRequest.domain = deletedConfiguration.domain;
|
||||||
deleteConfigurationRequest.key = deletedConfiguration.key;
|
deleteConfigurationRequest.key = deletedConfiguration.key;
|
||||||
await this._commandBus.execute(
|
await this.commandBus.execute(
|
||||||
new DeleteConfigurationCommand(deleteConfigurationRequest),
|
new DeleteConfigurationCommand(deleteConfigurationRequest),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -61,14 +61,14 @@ export class ConfigurationMessagerController {
|
||||||
configurations.forEach(async (configuration) => {
|
configurations.forEach(async (configuration) => {
|
||||||
if (
|
if (
|
||||||
configuration.domain ==
|
configuration.domain ==
|
||||||
this._configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
|
this.configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
|
||||||
) {
|
) {
|
||||||
const setConfigurationRequest: SetConfigurationRequest =
|
const setConfigurationRequest: SetConfigurationRequest =
|
||||||
new SetConfigurationRequest();
|
new SetConfigurationRequest();
|
||||||
setConfigurationRequest.domain = configuration.domain;
|
setConfigurationRequest.domain = configuration.domain;
|
||||||
setConfigurationRequest.key = configuration.key;
|
setConfigurationRequest.key = configuration.key;
|
||||||
setConfigurationRequest.value = configuration.value;
|
setConfigurationRequest.value = configuration.value;
|
||||||
await this._commandBus.execute(
|
await this.commandBus.execute(
|
||||||
new SetConfigurationCommand(setConfigurationRequest),
|
new SetConfigurationCommand(setConfigurationRequest),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,19 +5,19 @@ import { IConfigurationRepository } from '../../domain/interfaces/configuration.
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class RedisConfigurationRepository extends IConfigurationRepository {
|
export class RedisConfigurationRepository extends IConfigurationRepository {
|
||||||
constructor(@InjectRedis() private readonly _redis: Redis) {
|
constructor(@InjectRedis() private readonly redis: Redis) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(key: string): Promise<string> {
|
async get(key: string): Promise<string> {
|
||||||
return await this._redis.get(key);
|
return await this.redis.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
async set(key: string, value: string) {
|
async set(key: string, value: string) {
|
||||||
await this._redis.set(key, value);
|
await this.redis.set(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
async del(key: string) {
|
async del(key: string) {
|
||||||
await this._redis.del(key);
|
await this.redis.del(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,13 +4,15 @@ import { DeleteConfigurationCommand } from '../../commands/delete-configuration.
|
||||||
|
|
||||||
@CommandHandler(DeleteConfigurationCommand)
|
@CommandHandler(DeleteConfigurationCommand)
|
||||||
export class DeleteConfigurationUseCase {
|
export class DeleteConfigurationUseCase {
|
||||||
constructor(private _configurationRepository: RedisConfigurationRepository) {}
|
constructor(private configurationRepository: RedisConfigurationRepository) {}
|
||||||
|
|
||||||
async execute(deleteConfigurationCommand: DeleteConfigurationCommand) {
|
execute = async (
|
||||||
await this._configurationRepository.del(
|
deleteConfigurationCommand: DeleteConfigurationCommand,
|
||||||
|
): Promise<void> => {
|
||||||
|
await this.configurationRepository.del(
|
||||||
deleteConfigurationCommand.deleteConfigurationRequest.domain +
|
deleteConfigurationCommand.deleteConfigurationRequest.domain +
|
||||||
':' +
|
':' +
|
||||||
deleteConfigurationCommand.deleteConfigurationRequest.key,
|
deleteConfigurationCommand.deleteConfigurationRequest.key,
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,12 @@ import { GetConfigurationQuery } from '../../queries/get-configuration.query';
|
||||||
|
|
||||||
@QueryHandler(GetConfigurationQuery)
|
@QueryHandler(GetConfigurationQuery)
|
||||||
export class GetConfigurationUseCase {
|
export class GetConfigurationUseCase {
|
||||||
constructor(private _configurationRepository: RedisConfigurationRepository) {}
|
constructor(private configurationRepository: RedisConfigurationRepository) {}
|
||||||
|
|
||||||
async execute(getConfigurationQuery: GetConfigurationQuery): Promise<string> {
|
execute = async (
|
||||||
return this._configurationRepository.get(
|
getConfigurationQuery: GetConfigurationQuery,
|
||||||
|
): Promise<string> =>
|
||||||
|
this.configurationRepository.get(
|
||||||
getConfigurationQuery.domain + ':' + getConfigurationQuery.key,
|
getConfigurationQuery.domain + ':' + getConfigurationQuery.key,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -4,14 +4,16 @@ import { SetConfigurationCommand } from '../../commands/set-configuration.comman
|
||||||
|
|
||||||
@CommandHandler(SetConfigurationCommand)
|
@CommandHandler(SetConfigurationCommand)
|
||||||
export class SetConfigurationUseCase {
|
export class SetConfigurationUseCase {
|
||||||
constructor(private _configurationRepository: RedisConfigurationRepository) {}
|
constructor(private configurationRepository: RedisConfigurationRepository) {}
|
||||||
|
|
||||||
async execute(setConfigurationCommand: SetConfigurationCommand) {
|
execute = async (
|
||||||
await this._configurationRepository.set(
|
setConfigurationCommand: SetConfigurationCommand,
|
||||||
|
): Promise<void> => {
|
||||||
|
await this.configurationRepository.set(
|
||||||
setConfigurationCommand.setConfigurationRequest.domain +
|
setConfigurationCommand.setConfigurationRequest.domain +
|
||||||
':' +
|
':' +
|
||||||
setConfigurationCommand.setConfigurationRequest.key,
|
setConfigurationCommand.setConfigurationRequest.key,
|
||||||
setConfigurationCommand.setConfigurationRequest.value,
|
setConfigurationCommand.setConfigurationRequest.value,
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { PrismaClientKnownRequestError } from '@prisma/client/runtime';
|
import { Prisma } from '@prisma/client';
|
||||||
import { DatabaseException } from '../../exceptions/database.exception';
|
import { DatabaseException } from '../../exceptions/database.exception';
|
||||||
import { ICollection } from '../../interfaces/collection.interface';
|
import { ICollection } from '../../interfaces/collection.interface';
|
||||||
import { IRepository } from '../../interfaces/repository.interface';
|
import { IRepository } from '../../interfaces/repository.interface';
|
||||||
|
@ -10,24 +10,24 @@ import { PrismaService } from './prisma-service';
|
||||||
*/
|
*/
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export abstract class PrismaRepository<T> implements IRepository<T> {
|
export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
protected _model: string;
|
protected model: string;
|
||||||
|
|
||||||
constructor(protected readonly _prisma: PrismaService) {}
|
constructor(protected readonly prisma: PrismaService) {}
|
||||||
|
|
||||||
async findAll(
|
findAll = async (
|
||||||
page = 1,
|
page = 1,
|
||||||
perPage = 10,
|
perPage = 10,
|
||||||
where?: any,
|
where?: any,
|
||||||
include?: any,
|
include?: any,
|
||||||
): Promise<ICollection<T>> {
|
): Promise<ICollection<T>> => {
|
||||||
const [data, total] = await this._prisma.$transaction([
|
const [data, total] = await this.prisma.$transaction([
|
||||||
this._prisma[this._model].findMany({
|
this.prisma[this.model].findMany({
|
||||||
where,
|
where,
|
||||||
include,
|
include,
|
||||||
skip: (page - 1) * perPage,
|
skip: (page - 1) * perPage,
|
||||||
take: perPage,
|
take: perPage,
|
||||||
}),
|
}),
|
||||||
this._prisma[this._model].count({
|
this.prisma[this.model].count({
|
||||||
where,
|
where,
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
|
@ -35,19 +35,19 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
data,
|
data,
|
||||||
total,
|
total,
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
async findOneByUuid(uuid: string): Promise<T> {
|
findOneByUuid = async (uuid: string): Promise<T> => {
|
||||||
try {
|
try {
|
||||||
const entity = await this._prisma[this._model].findUnique({
|
const entity = await this.prisma[this.model].findUnique({
|
||||||
where: { uuid },
|
where: { uuid },
|
||||||
});
|
});
|
||||||
|
|
||||||
return entity;
|
return entity;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -55,39 +55,42 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
throw new DatabaseException();
|
throw new DatabaseException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
async findOne(where: any, include?: any): Promise<T> {
|
findOne = async (where: any, include?: any): Promise<T> => {
|
||||||
try {
|
try {
|
||||||
const entity = await this._prisma[this._model].findFirst({
|
const entity = await this.prisma[this.model].findFirst({
|
||||||
where: where,
|
where: where,
|
||||||
include: include,
|
include: include,
|
||||||
});
|
});
|
||||||
|
|
||||||
return entity;
|
return entity;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(PrismaClientKnownRequestError.name, e.code);
|
throw new DatabaseException(
|
||||||
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
|
e.code,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
throw new DatabaseException();
|
throw new DatabaseException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
// TODO : using any is not good, but needed for nested entities
|
// TODO : using any is not good, but needed for nested entities
|
||||||
// TODO : Refactor for good clean architecture ?
|
// TODO : Refactor for good clean architecture ?
|
||||||
async create(entity: Partial<T> | any, include?: any): Promise<T> {
|
create = async (entity: Partial<T> | any, include?: any): Promise<T> => {
|
||||||
try {
|
try {
|
||||||
const res = await this._prisma[this._model].create({
|
const res = await this.prisma[this.model].create({
|
||||||
data: entity,
|
data: entity,
|
||||||
include: include,
|
include: include,
|
||||||
});
|
});
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -95,19 +98,19 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
throw new DatabaseException();
|
throw new DatabaseException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
async update(uuid: string, entity: Partial<T>): Promise<T> {
|
update = async (uuid: string, entity: Partial<T>): Promise<T> => {
|
||||||
try {
|
try {
|
||||||
const updatedEntity = await this._prisma[this._model].update({
|
const updatedEntity = await this.prisma[this.model].update({
|
||||||
where: { uuid },
|
where: { uuid },
|
||||||
data: entity,
|
data: entity,
|
||||||
});
|
});
|
||||||
return updatedEntity;
|
return updatedEntity;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -115,15 +118,15 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
throw new DatabaseException();
|
throw new DatabaseException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
async updateWhere(
|
updateWhere = async (
|
||||||
where: any,
|
where: any,
|
||||||
entity: Partial<T> | any,
|
entity: Partial<T> | any,
|
||||||
include?: any,
|
include?: any,
|
||||||
): Promise<T> {
|
): Promise<T> => {
|
||||||
try {
|
try {
|
||||||
const updatedEntity = await this._prisma[this._model].update({
|
const updatedEntity = await this.prisma[this.model].update({
|
||||||
where: where,
|
where: where,
|
||||||
data: entity,
|
data: entity,
|
||||||
include: include,
|
include: include,
|
||||||
|
@ -131,9 +134,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
|
|
||||||
return updatedEntity;
|
return updatedEntity;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -141,19 +144,19 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
throw new DatabaseException();
|
throw new DatabaseException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
async delete(uuid: string): Promise<T> {
|
delete = async (uuid: string): Promise<T> => {
|
||||||
try {
|
try {
|
||||||
const entity = await this._prisma[this._model].delete({
|
const entity = await this.prisma[this.model].delete({
|
||||||
where: { uuid },
|
where: { uuid },
|
||||||
});
|
});
|
||||||
|
|
||||||
return entity;
|
return entity;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -161,19 +164,19 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
throw new DatabaseException();
|
throw new DatabaseException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
async deleteMany(where: any): Promise<void> {
|
deleteMany = async (where: any): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const entity = await this._prisma[this._model].deleteMany({
|
const entity = await this.prisma[this.model].deleteMany({
|
||||||
where: where,
|
where: where,
|
||||||
});
|
});
|
||||||
|
|
||||||
return entity;
|
return entity;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -181,32 +184,32 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
throw new DatabaseException();
|
throw new DatabaseException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
async findAllByQuery(
|
findAllByQuery = async (
|
||||||
include: Array<string>,
|
include: string[],
|
||||||
where: Array<string>,
|
where: string[],
|
||||||
): Promise<ICollection<T>> {
|
): Promise<ICollection<T>> => {
|
||||||
const query = `SELECT ${include.join(',')} FROM ${
|
const query = `SELECT ${include.join(',')} FROM ${
|
||||||
this._model
|
this.model
|
||||||
} WHERE ${where.join(' AND ')}`;
|
} WHERE ${where.join(' AND ')}`;
|
||||||
const data: Array<T> = await this._prisma.$queryRawUnsafe(query);
|
const data: T[] = await this.prisma.$queryRawUnsafe(query);
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
data,
|
data,
|
||||||
total: data.length,
|
total: data.length,
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
async createWithFields(fields: object): Promise<number> {
|
createWithFields = async (fields: object): Promise<number> => {
|
||||||
try {
|
try {
|
||||||
const command = `INSERT INTO ${this._model} (${Object.keys(fields).join(
|
const command = `INSERT INTO ${this.model} ("${Object.keys(fields).join(
|
||||||
',',
|
'","',
|
||||||
)}) VALUES (${Object.values(fields).join(',')})`;
|
)}") VALUES (${Object.values(fields).join(',')})`;
|
||||||
return await this._prisma.$executeRawUnsafe(command);
|
return await this.prisma.$executeRawUnsafe(command);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -214,20 +217,20 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
throw new DatabaseException();
|
throw new DatabaseException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
async updateWithFields(uuid: string, entity: Partial<T>): Promise<number> {
|
updateWithFields = async (uuid: string, entity: object): Promise<number> => {
|
||||||
entity['"updatedAt"'] = `to_timestamp(${Date.now()} / 1000.0)`;
|
entity['"updatedAt"'] = `to_timestamp(${Date.now()} / 1000.0)`;
|
||||||
const values = Object.keys(entity).map((key) => `${key} = ${entity[key]}`);
|
const values = Object.keys(entity).map((key) => `${key} = ${entity[key]}`);
|
||||||
try {
|
try {
|
||||||
const command = `UPDATE ${this._model} SET ${values.join(
|
const command = `UPDATE ${this.model} SET ${values.join(
|
||||||
', ',
|
', ',
|
||||||
)} WHERE uuid = '${uuid}'`;
|
)} WHERE uuid = '${uuid}'`;
|
||||||
return await this._prisma.$executeRawUnsafe(command);
|
return await this.prisma.$executeRawUnsafe(command);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -235,16 +238,16 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
throw new DatabaseException();
|
throw new DatabaseException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
async healthCheck(): Promise<boolean> {
|
healthCheck = async (): Promise<boolean> => {
|
||||||
try {
|
try {
|
||||||
await this._prisma.$queryRaw`SELECT 1`;
|
await this.prisma.$queryRaw`SELECT 1`;
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -252,5 +255,5 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
throw new DatabaseException();
|
throw new DatabaseException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { PrismaService } from './src/adapters/secondaries/prisma-service';
|
import { PrismaService } from './adapters/secondaries/prisma-service';
|
||||||
import { TerritoryRepository } from './src/domain/territory-repository';
|
import { TerritoryRepository } from './domain/territory-repository';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
providers: [PrismaService, TerritoryRepository],
|
providers: [PrismaService, TerritoryRepository],
|
||||||
|
|
|
@ -6,16 +6,15 @@ import { Territory } from 'src/modules/territory/domain/entities/territory';
|
||||||
import { DatabaseException } from '../exceptions/database.exception';
|
import { DatabaseException } from '../exceptions/database.exception';
|
||||||
|
|
||||||
export class TerritoryRepository<T> extends PrismaRepository<T> {
|
export class TerritoryRepository<T> extends PrismaRepository<T> {
|
||||||
async findForPoint(point: Point): Promise<ICollection<T>> {
|
findForPoint = async (point: Point): Promise<ICollection<T>> =>
|
||||||
return await this.findAllByQuery(
|
await this.findAllByQuery(
|
||||||
['uuid', 'name'],
|
['uuid', 'name'],
|
||||||
[
|
[
|
||||||
`ST_Intersects(ST_GeomFromText('POINT(${point.lon} ${point.lat})',4326),shape) = true`,
|
`ST_Intersects(ST_GeomFromText('POINT(${point.lon} ${point.lat})',4326),shape) = true`,
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
async findForPoints(points: Point[]): Promise<ICollection<T>> {
|
findForPoints = async (points: Point[]): Promise<ICollection<T>> => {
|
||||||
const multipoint = points
|
const multipoint = points
|
||||||
.map((point) => '(' + point.lon + ' ' + point.lat + ')')
|
.map((point) => '(' + point.lon + ' ' + point.lat + ')')
|
||||||
.join(', ');
|
.join(', ');
|
||||||
|
@ -25,9 +24,9 @@ export class TerritoryRepository<T> extends PrismaRepository<T> {
|
||||||
`ST_Intersects(ST_GeomFromText('MULTIPOINT(${multipoint})',4326),shape) = true`,
|
`ST_Intersects(ST_GeomFromText('MULTIPOINT(${multipoint})',4326),shape) = true`,
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
async createTerritory(territory: Partial<Territory>): Promise<T> {
|
createTerritory = async (territory: Partial<Territory>): Promise<T> => {
|
||||||
try {
|
try {
|
||||||
const affectedRowNumber = await this.createWithFields({
|
const affectedRowNumber = await this.createWithFields({
|
||||||
uuid: `'${uuidv4()}'`,
|
uuid: `'${uuidv4()}'`,
|
||||||
|
@ -43,12 +42,12 @@ export class TerritoryRepository<T> extends PrismaRepository<T> {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
async updateTerritory(
|
updateTerritory = async (
|
||||||
uuid: string,
|
uuid: string,
|
||||||
territory: Partial<Territory>,
|
territory: Partial<Territory>,
|
||||||
): Promise<T> {
|
): Promise<T> => {
|
||||||
try {
|
try {
|
||||||
const fields = {};
|
const fields = {};
|
||||||
if (territory.name) fields['name'] = `'${territory.name}'`;
|
if (territory.name) fields['name'] = `'${territory.name}'`;
|
||||||
|
@ -66,5 +65,5 @@ export class TerritoryRepository<T> extends PrismaRepository<T> {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
|
@ -1,9 +1,9 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
import { PrismaService } from '../../src/adapters/secondaries/prisma-service';
|
import { PrismaService } from '../../adapters/secondaries/prisma-service';
|
||||||
import { PrismaRepository } from '../../src/adapters/secondaries/prisma-repository.abstract';
|
import { PrismaRepository } from '../../adapters/secondaries/prisma-repository.abstract';
|
||||||
import { DatabaseException } from '../../src/exceptions/database.exception';
|
import { DatabaseException } from '../../exceptions/database.exception';
|
||||||
import { PrismaClientKnownRequestError } from '@prisma/client/runtime';
|
import { Prisma } from '@prisma/client';
|
||||||
|
|
||||||
class FakeEntity {
|
class FakeEntity {
|
||||||
uuid?: string;
|
uuid?: string;
|
||||||
|
@ -41,7 +41,7 @@ Array.from({ length: 10 }).forEach(() => {
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
class FakePrismaRepository extends PrismaRepository<FakeEntity> {
|
class FakePrismaRepository extends PrismaRepository<FakeEntity> {
|
||||||
protected _model = 'fake';
|
protected model = 'fake';
|
||||||
}
|
}
|
||||||
|
|
||||||
class FakePrismaService extends PrismaService {
|
class FakePrismaService extends PrismaService {
|
||||||
|
@ -66,7 +66,7 @@ const mockPrismaService = {
|
||||||
.mockResolvedValueOnce(fakeEntityCreated)
|
.mockResolvedValueOnce(fakeEntityCreated)
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
.mockImplementationOnce((fields: object) => {
|
.mockImplementationOnce((fields: object) => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -78,7 +78,7 @@ const mockPrismaService = {
|
||||||
.mockResolvedValueOnce(fakeEntityCreated)
|
.mockResolvedValueOnce(fakeEntityCreated)
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
.mockImplementationOnce((fields: object) => {
|
.mockImplementationOnce((fields: object) => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -90,7 +90,7 @@ const mockPrismaService = {
|
||||||
$queryRaw: jest
|
$queryRaw: jest
|
||||||
.fn()
|
.fn()
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -99,7 +99,7 @@ const mockPrismaService = {
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
.mockImplementation(() => {
|
.mockImplementation(() => {
|
||||||
throw new PrismaClientKnownRequestError('Database unavailable', {
|
throw new Prisma.PrismaClientKnownRequestError('Database unavailable', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -110,7 +110,7 @@ const mockPrismaService = {
|
||||||
.mockResolvedValueOnce(fakeEntityCreated)
|
.mockResolvedValueOnce(fakeEntityCreated)
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
.mockImplementationOnce((params?: any) => {
|
.mockImplementationOnce((params?: any) => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -139,7 +139,7 @@ const mockPrismaService = {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!entity && params?.where?.uuid == 'unknown') {
|
if (!entity && params?.where?.uuid == 'unknown') {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -161,7 +161,7 @@ const mockPrismaService = {
|
||||||
})
|
})
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
.mockImplementationOnce((params?: any) => {
|
.mockImplementationOnce((params?: any) => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -175,14 +175,14 @@ const mockPrismaService = {
|
||||||
.fn()
|
.fn()
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
.mockImplementationOnce((params?: any) => {
|
.mockImplementationOnce((params?: any) => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
.mockImplementationOnce((params?: any) => {
|
.mockImplementationOnce((params?: any) => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -212,7 +212,7 @@ const mockPrismaService = {
|
||||||
.fn()
|
.fn()
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
.mockImplementationOnce((params?: any) => {
|
.mockImplementationOnce((params?: any) => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -236,7 +236,7 @@ const mockPrismaService = {
|
||||||
.fn()
|
.fn()
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
.mockImplementationOnce((params?: any) => {
|
.mockImplementationOnce((params?: any) => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
import { PrismaService } from '../../src/adapters/secondaries/prisma-service';
|
import { PrismaService } from '../../adapters/secondaries/prisma-service';
|
||||||
import { TerritoryRepository } from '../../src/domain/territory-repository';
|
import { TerritoryRepository } from '../../domain/territory-repository';
|
||||||
import { Territory } from '../../../territory/domain/entities/territory';
|
import { Territory } from '../../../territory/domain/entities/territory';
|
||||||
import { Point } from '../../../territory/domain/entities/point';
|
import { Point } from '../../../territory/domain/entities/point';
|
||||||
import { DatabaseException } from '../../src/exceptions/database.exception';
|
import { DatabaseException } from '../../exceptions/database.exception';
|
||||||
|
|
||||||
const mockTerritories: Array<Territory> = [
|
const mockTerritories: Array<Territory> = [
|
||||||
{
|
{
|
||||||
|
@ -33,7 +33,7 @@ const territoryUpdated: Territory = {
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
class TerritoriesRepository extends TerritoryRepository<Territory> {
|
class TerritoriesRepository extends TerritoryRepository<Territory> {
|
||||||
protected _model = 'territory';
|
protected model = 'territory';
|
||||||
}
|
}
|
||||||
|
|
||||||
const mockPrismaService = {
|
const mockPrismaService = {
|
||||||
|
|
|
@ -19,7 +19,7 @@ interface HealthCheckResponse {
|
||||||
@Controller()
|
@Controller()
|
||||||
export class HealthServerController {
|
export class HealthServerController {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly _prismaHealthIndicatorUseCase: PrismaHealthIndicatorUseCase,
|
private readonly prismaHealthIndicatorUseCase: PrismaHealthIndicatorUseCase,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@GrpcMethod('Health', 'Check')
|
@GrpcMethod('Health', 'Check')
|
||||||
|
@ -29,7 +29,7 @@ export class HealthServerController {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
metadata: any,
|
metadata: any,
|
||||||
): Promise<HealthCheckResponse> {
|
): Promise<HealthCheckResponse> {
|
||||||
const healthCheck = await this._prismaHealthIndicatorUseCase.isHealthy(
|
const healthCheck = await this.prismaHealthIndicatorUseCase.isHealthy(
|
||||||
'prisma',
|
'prisma',
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -10,21 +10,21 @@ import { PrismaHealthIndicatorUseCase } from '../../domain/usecases/prisma.healt
|
||||||
@Controller('health')
|
@Controller('health')
|
||||||
export class HealthController {
|
export class HealthController {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly _prismaHealthIndicatorUseCase: PrismaHealthIndicatorUseCase,
|
private readonly prismaHealthIndicatorUseCase: PrismaHealthIndicatorUseCase,
|
||||||
private _healthCheckService: HealthCheckService,
|
private readonly healthCheckService: HealthCheckService,
|
||||||
private _messager: Messager,
|
private readonly messager: Messager,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
@HealthCheck()
|
@HealthCheck()
|
||||||
async check() {
|
async check() {
|
||||||
try {
|
try {
|
||||||
return await this._healthCheckService.check([
|
return await this.healthCheckService.check([
|
||||||
async () => this._prismaHealthIndicatorUseCase.isHealthy('prisma'),
|
async () => this.prismaHealthIndicatorUseCase.isHealthy('prisma'),
|
||||||
]);
|
]);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const healthCheckResult: HealthCheckResult = error.response;
|
const healthCheckResult: HealthCheckResult = error.response;
|
||||||
this._messager.publish(
|
this.messager.publish(
|
||||||
'logging.territory.health.crit',
|
'logging.territory.health.crit',
|
||||||
JSON.stringify(healthCheckResult.error),
|
JSON.stringify(healthCheckResult.error),
|
||||||
);
|
);
|
||||||
|
|
|
@ -6,13 +6,13 @@ import { IMessageBroker } from './message-broker';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class Messager extends IMessageBroker {
|
export class Messager extends IMessageBroker {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly _amqpConnection: AmqpConnection,
|
private readonly amqpConnection: AmqpConnection,
|
||||||
configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
) {
|
) {
|
||||||
super(configService.get<string>('RMQ_EXCHANGE'));
|
super(configService.get<string>('RMQ_EXCHANGE'));
|
||||||
}
|
}
|
||||||
|
|
||||||
publish(routingKey: string, message: string): void {
|
publish = (routingKey: string, message: string): void => {
|
||||||
this._amqpConnection.publish(this.exchange, routingKey, message);
|
this.amqpConnection.publish(this.exchange, routingKey, message);
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,18 +8,18 @@ import { TerritoriesRepository } from '../../../territory/adapters/secondaries/t
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PrismaHealthIndicatorUseCase extends HealthIndicator {
|
export class PrismaHealthIndicatorUseCase extends HealthIndicator {
|
||||||
constructor(private readonly _repository: TerritoriesRepository) {
|
constructor(private readonly repository: TerritoriesRepository) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
async isHealthy(key: string): Promise<HealthIndicatorResult> {
|
isHealthy = async (key: string): Promise<HealthIndicatorResult> => {
|
||||||
try {
|
try {
|
||||||
await this._repository.healthCheck();
|
await this.repository.healthCheck();
|
||||||
return this.getStatus(key, true);
|
return this.getStatus(key, true);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new HealthCheckError('Prisma', {
|
throw new HealthCheckError('Prisma', {
|
||||||
prisma: e.message,
|
prisma: e.message,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import { GrpcMethod, RpcException } from '@nestjs/microservices';
|
||||||
import { Territory } from '../../domain/entities/territory';
|
import { Territory } from '../../domain/entities/territory';
|
||||||
import { FindAllTerritoriesForPointQuery } from '../../queries/find-all-territories-for-point.query';
|
import { FindAllTerritoriesForPointQuery } from '../../queries/find-all-territories-for-point.query';
|
||||||
import { TerritoryPresenter } from './territory.presenter';
|
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 { RpcValidationPipe } from '../../../../utils/pipes/rpc.validation-pipe';
|
||||||
import { FindAllTerritoriesForPointRequest } from '../../domain/dtos/find-all-territories-for-point.request';
|
import { FindAllTerritoriesForPointRequest } from '../../domain/dtos/find-all-territories-for-point.request';
|
||||||
import { FindAllTerritoriesRequest } from '../../domain/dtos/find-all-territories.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 { FindTerritoryByUuidQuery } from '../../queries/find-territory-by-uuid.query';
|
||||||
import { CreateTerritoryRequest } from '../../domain/dtos/create-territory.request';
|
import { CreateTerritoryRequest } from '../../domain/dtos/create-territory.request';
|
||||||
import { CreateTerritoryCommand } from '../../commands/create-territory.command';
|
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 { UpdateTerritoryRequest } from '../../domain/dtos/update-territory.request';
|
||||||
import { UpdateTerritoryCommand } from '../../commands/update-territory.command';
|
import { UpdateTerritoryCommand } from '../../commands/update-territory.command';
|
||||||
import { DeleteTerritoryCommand } from '../../commands/delete-territory.command';
|
import { DeleteTerritoryCommand } from '../../commands/delete-territory.command';
|
||||||
|
|
|
@ -6,13 +6,13 @@ import { IMessageBroker } from '../../domain/interfaces/message-broker';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class Messager extends IMessageBroker {
|
export class Messager extends IMessageBroker {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly _amqpConnection: AmqpConnection,
|
private readonly amqpConnection: AmqpConnection,
|
||||||
configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
) {
|
) {
|
||||||
super(configService.get<string>('RMQ_EXCHANGE'));
|
super(configService.get<string>('RMQ_EXCHANGE'));
|
||||||
}
|
}
|
||||||
|
|
||||||
publish(routingKey: string, message: string): void {
|
publish = (routingKey: string, message: string): void => {
|
||||||
this._amqpConnection.publish(this.exchange, routingKey, message);
|
this.amqpConnection.publish(this.exchange, routingKey, message);
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
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';
|
import { Territory } from '../../domain/entities/territory';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TerritoriesRepository extends TerritoryRepository<Territory> {
|
export class TerritoriesRepository extends TerritoryRepository<Territory> {
|
||||||
protected _model = 'territory';
|
protected model = 'territory';
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,30 +12,30 @@ import { TerritoryPresenter } from '../../adapters/secondaries/territory.present
|
||||||
@CommandHandler(CreateTerritoryCommand)
|
@CommandHandler(CreateTerritoryCommand)
|
||||||
export class CreateTerritoryUseCase {
|
export class CreateTerritoryUseCase {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly _repository: TerritoriesRepository,
|
private readonly repository: TerritoriesRepository,
|
||||||
private readonly _messager: Messager,
|
private readonly messager: Messager,
|
||||||
@InjectMapper() private readonly _mapper: Mapper,
|
@InjectMapper() private readonly mapper: Mapper,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async execute(command: CreateTerritoryCommand): Promise<Territory> {
|
execute = async (command: CreateTerritoryCommand): Promise<Territory> => {
|
||||||
const entity = this._mapper.map(
|
const entity = this.mapper.map(
|
||||||
command.createTerritoryRequest,
|
command.createTerritoryRequest,
|
||||||
CreateTerritoryRequest,
|
CreateTerritoryRequest,
|
||||||
Territory,
|
Territory,
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const territory = await this._repository.createTerritory(entity);
|
const territory = await this.repository.createTerritory(entity);
|
||||||
this._messager.publish(
|
this.messager.publish(
|
||||||
'territory.create',
|
'territory.create',
|
||||||
JSON.stringify(
|
JSON.stringify(
|
||||||
this._mapper.map(territory, Territory, TerritoryPresenter),
|
this.mapper.map(territory, Territory, TerritoryPresenter),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
this._messager.publish(
|
this.messager.publish(
|
||||||
'logging.territory.create.info',
|
'logging.territory.create.info',
|
||||||
JSON.stringify(
|
JSON.stringify(
|
||||||
this._mapper.map(territory, Territory, TerritoryLoggingPresenter),
|
this.mapper.map(territory, Territory, TerritoryLoggingPresenter),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
return territory;
|
return territory;
|
||||||
|
@ -44,7 +44,7 @@ export class CreateTerritoryUseCase {
|
||||||
if (error.message.includes('already exists')) {
|
if (error.message.includes('already exists')) {
|
||||||
key = 'logging.territory.create.warning';
|
key = 'logging.territory.create.warning';
|
||||||
}
|
}
|
||||||
this._messager.publish(
|
this.messager.publish(
|
||||||
key,
|
key,
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
command,
|
command,
|
||||||
|
@ -53,5 +53,5 @@ export class CreateTerritoryUseCase {
|
||||||
);
|
);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ export class DeleteTerritoryUseCase {
|
||||||
private readonly _messager: Messager,
|
private readonly _messager: Messager,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async execute(command: DeleteTerritoryCommand): Promise<Territory> {
|
execute = async (command: DeleteTerritoryCommand): Promise<Territory> => {
|
||||||
try {
|
try {
|
||||||
const territory = await this._repository.delete(command.uuid);
|
const territory = await this._repository.delete(command.uuid);
|
||||||
this._messager.publish(
|
this._messager.publish(
|
||||||
|
@ -33,5 +33,5 @@ export class DeleteTerritoryUseCase {
|
||||||
);
|
);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { QueryHandler } from '@nestjs/cqrs';
|
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 { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
|
||||||
import { FindAllTerritoriesForPointQuery } from '../../queries/find-all-territories-for-point.query';
|
import { FindAllTerritoriesForPointQuery } from '../../queries/find-all-territories-for-point.query';
|
||||||
import { Territory } from '../entities/territory';
|
import { Territory } from '../entities/territory';
|
||||||
|
@ -7,16 +7,15 @@ import { Point } from '../entities/point';
|
||||||
|
|
||||||
@QueryHandler(FindAllTerritoriesForPointQuery)
|
@QueryHandler(FindAllTerritoriesForPointQuery)
|
||||||
export class FindAllTerritoriesForPointUseCase {
|
export class FindAllTerritoriesForPointUseCase {
|
||||||
constructor(private readonly _repository: TerritoriesRepository) {}
|
constructor(private readonly repository: TerritoriesRepository) {}
|
||||||
|
|
||||||
async execute(
|
execute = async (
|
||||||
findAllTerritoriesForPointQuery: FindAllTerritoriesForPointQuery,
|
findAllTerritoriesForPointQuery: FindAllTerritoriesForPointQuery,
|
||||||
): Promise<ICollection<Territory>> {
|
): Promise<ICollection<Territory>> =>
|
||||||
return this._repository.findForPoint(
|
this.repository.findForPoint(
|
||||||
new Point(
|
new Point(
|
||||||
findAllTerritoriesForPointQuery.point.lon,
|
findAllTerritoriesForPointQuery.point.lon,
|
||||||
findAllTerritoriesForPointQuery.point.lat,
|
findAllTerritoriesForPointQuery.point.lat,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -1,18 +1,15 @@
|
||||||
import { QueryHandler } from '@nestjs/cqrs';
|
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 { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
|
||||||
import { FindAllTerritoriesForPointsQuery } from '../../queries/find-all-territories-for-points.query';
|
import { FindAllTerritoriesForPointsQuery } from '../../queries/find-all-territories-for-points.query';
|
||||||
import { Territory } from '../entities/territory';
|
import { Territory } from '../entities/territory';
|
||||||
|
|
||||||
@QueryHandler(FindAllTerritoriesForPointsQuery)
|
@QueryHandler(FindAllTerritoriesForPointsQuery)
|
||||||
export class FindAllTerritoriesForPointsUseCase {
|
export class FindAllTerritoriesForPointsUseCase {
|
||||||
constructor(private readonly _repository: TerritoriesRepository) {}
|
constructor(private readonly repository: TerritoriesRepository) {}
|
||||||
|
|
||||||
async execute(
|
execute = async (
|
||||||
findAllTerritoriesForPointsQuery: FindAllTerritoriesForPointsQuery,
|
findAllTerritoriesForPointsQuery: FindAllTerritoriesForPointsQuery,
|
||||||
): Promise<ICollection<Territory>> {
|
): Promise<ICollection<Territory>> =>
|
||||||
return this._repository.findForPoints(
|
this.repository.findForPoints(findAllTerritoriesForPointsQuery.points);
|
||||||
findAllTerritoriesForPointsQuery.points,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,18 @@
|
||||||
import { QueryHandler } from '@nestjs/cqrs';
|
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 { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
|
||||||
import { FindAllTerritoriesQuery } from '../../queries/find-all-territories.query';
|
import { FindAllTerritoriesQuery } from '../../queries/find-all-territories.query';
|
||||||
import { Territory } from '../entities/territory';
|
import { Territory } from '../entities/territory';
|
||||||
|
|
||||||
@QueryHandler(FindAllTerritoriesQuery)
|
@QueryHandler(FindAllTerritoriesQuery)
|
||||||
export class FindAllTerritoriesUseCase {
|
export class FindAllTerritoriesUseCase {
|
||||||
constructor(private readonly _repository: TerritoriesRepository) {}
|
constructor(private readonly repository: TerritoriesRepository) {}
|
||||||
|
|
||||||
async execute(
|
execute = async (
|
||||||
findAllTerritoriesQuery: FindAllTerritoriesQuery,
|
findAllTerritoriesQuery: FindAllTerritoriesQuery,
|
||||||
): Promise<ICollection<Territory>> {
|
): Promise<ICollection<Territory>> =>
|
||||||
return this._repository.findAll(
|
this.repository.findAll(
|
||||||
findAllTerritoriesQuery.page,
|
findAllTerritoriesQuery.page,
|
||||||
findAllTerritoriesQuery.perPage,
|
findAllTerritoriesQuery.perPage,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -8,21 +8,21 @@ import { Territory } from '../entities/territory';
|
||||||
@QueryHandler(FindTerritoryByUuidQuery)
|
@QueryHandler(FindTerritoryByUuidQuery)
|
||||||
export class FindTerritoryByUuidUseCase {
|
export class FindTerritoryByUuidUseCase {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly _repository: TerritoriesRepository,
|
private readonly repository: TerritoriesRepository,
|
||||||
private readonly _messager: Messager,
|
private readonly messager: Messager,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async execute(
|
execute = async (
|
||||||
findTerritoryByUuidQuery: FindTerritoryByUuidQuery,
|
findTerritoryByUuidQuery: FindTerritoryByUuidQuery,
|
||||||
): Promise<Territory> {
|
): Promise<Territory> => {
|
||||||
try {
|
try {
|
||||||
const territory = await this._repository.findOneByUuid(
|
const territory = await this.repository.findOneByUuid(
|
||||||
findTerritoryByUuidQuery.uuid,
|
findTerritoryByUuidQuery.uuid,
|
||||||
);
|
);
|
||||||
if (!territory) throw new NotFoundException();
|
if (!territory) throw new NotFoundException();
|
||||||
return territory;
|
return territory;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this._messager.publish(
|
this.messager.publish(
|
||||||
'logging.territory.read.warning',
|
'logging.territory.read.warning',
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
query: findTerritoryByUuidQuery,
|
query: findTerritoryByUuidQuery,
|
||||||
|
@ -31,5 +31,5 @@ export class FindTerritoryByUuidUseCase {
|
||||||
);
|
);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,35 +12,35 @@ import { TerritoryPresenter } from '../../adapters/secondaries/territory.present
|
||||||
@CommandHandler(UpdateTerritoryCommand)
|
@CommandHandler(UpdateTerritoryCommand)
|
||||||
export class UpdateTerritoryUseCase {
|
export class UpdateTerritoryUseCase {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly _repository: TerritoriesRepository,
|
private readonly repository: TerritoriesRepository,
|
||||||
private readonly _messager: Messager,
|
private readonly messager: Messager,
|
||||||
@InjectMapper() private readonly _mapper: Mapper,
|
@InjectMapper() private readonly mapper: Mapper,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async execute(command: UpdateTerritoryCommand): Promise<Territory> {
|
execute = async (command: UpdateTerritoryCommand): Promise<Territory> => {
|
||||||
try {
|
try {
|
||||||
const territory = await this._repository.updateTerritory(
|
const territory = await this.repository.updateTerritory(
|
||||||
command.updateTerritoryRequest.uuid,
|
command.updateTerritoryRequest.uuid,
|
||||||
this._mapper.map(
|
this.mapper.map(
|
||||||
command.updateTerritoryRequest,
|
command.updateTerritoryRequest,
|
||||||
UpdateTerritoryRequest,
|
UpdateTerritoryRequest,
|
||||||
Territory,
|
Territory,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
this._messager.publish(
|
this.messager.publish(
|
||||||
'territory.update',
|
'territory.update',
|
||||||
JSON.stringify(
|
JSON.stringify(
|
||||||
this._mapper.map(
|
this.mapper.map(
|
||||||
command.updateTerritoryRequest,
|
command.updateTerritoryRequest,
|
||||||
UpdateTerritoryRequest,
|
UpdateTerritoryRequest,
|
||||||
TerritoryPresenter,
|
TerritoryPresenter,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
this._messager.publish(
|
this.messager.publish(
|
||||||
'logging.territory.update.info',
|
'logging.territory.update.info',
|
||||||
JSON.stringify(
|
JSON.stringify(
|
||||||
this._mapper.map(
|
this.mapper.map(
|
||||||
command.updateTerritoryRequest,
|
command.updateTerritoryRequest,
|
||||||
UpdateTerritoryRequest,
|
UpdateTerritoryRequest,
|
||||||
TerritoryLoggingPresenter,
|
TerritoryLoggingPresenter,
|
||||||
|
@ -49,7 +49,7 @@ export class UpdateTerritoryUseCase {
|
||||||
);
|
);
|
||||||
return territory;
|
return territory;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this._messager.publish(
|
this.messager.publish(
|
||||||
'logging.territory.update.crit',
|
'logging.territory.update.crit',
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
command,
|
command,
|
||||||
|
@ -58,5 +58,5 @@ export class UpdateTerritoryUseCase {
|
||||||
);
|
);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { TestingModule, Test } from '@nestjs/testing';
|
import { TestingModule, Test } from '@nestjs/testing';
|
||||||
import { DatabaseModule } from '../../../database/database.module';
|
import { DatabaseModule } from '../../../database/database.module';
|
||||||
import { PrismaService } from '../../../database/src/adapters/secondaries/prisma-service';
|
import { PrismaService } from '../../../database/adapters/secondaries/prisma-service';
|
||||||
import { DatabaseException } from '../../../database/src/exceptions/database.exception';
|
import { DatabaseException } from '../../../database/exceptions/database.exception';
|
||||||
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
|
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
import { Territory } from '../../domain/entities/territory';
|
import { Territory } from '../../domain/entities/territory';
|
||||||
|
|
Loading…
Reference in New Issue