mirror of
https://gitlab.com/mobicoop/v3/service/user.git
synced 2026-03-14 21:35:49 +00:00
update packages, clean code
This commit is contained in:
16
src/app.constants.ts
Normal file
16
src/app.constants.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
// service
|
||||
export const SERVICE_NAME = 'user';
|
||||
|
||||
// grpc
|
||||
export const GRPC_SERVICE_NAME = 'UserService';
|
||||
|
||||
// configuration
|
||||
export const SERVICE_CONFIGURATION_SET_QUEUE = 'user-configuration-set';
|
||||
export const SERVICE_CONFIGURATION_DELETE_QUEUE = 'user-configuration-delete';
|
||||
export const SERVICE_CONFIGURATION_PROPAGATE_QUEUE =
|
||||
'user-configuration-propagate';
|
||||
|
||||
// health
|
||||
export const HEALTH_SERVICE_NAME = 'health';
|
||||
export const HEALTH_USER_REPOSITORY = 'UserRepository';
|
||||
export const HEALTH_CRITICAL_LOGGING_KEY = 'logging.user.health.crit';
|
||||
@@ -15,6 +15,14 @@ import { USER_REPOSITORY } from './modules/user/user.di-tokens';
|
||||
import { MESSAGE_PUBLISHER } from './modules/messager/messager.di-tokens';
|
||||
import { MessagePublisherPort } from '@mobicoop/ddd-library';
|
||||
import { EventEmitterModule } from '@nestjs/event-emitter';
|
||||
import {
|
||||
HEALTH_CRITICAL_LOGGING_KEY,
|
||||
HEALTH_USER_REPOSITORY,
|
||||
SERVICE_CONFIGURATION_DELETE_QUEUE,
|
||||
SERVICE_CONFIGURATION_PROPAGATE_QUEUE,
|
||||
SERVICE_CONFIGURATION_SET_QUEUE,
|
||||
SERVICE_NAME,
|
||||
} from './app.constants';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -26,19 +34,23 @@ import { EventEmitterModule } from '@nestjs/event-emitter';
|
||||
useFactory: async (
|
||||
configService: ConfigService,
|
||||
): Promise<ConfigurationModuleOptions> => ({
|
||||
domain: configService.get<string>('SERVICE_CONFIGURATION_DOMAIN'),
|
||||
domain: configService.get<string>(
|
||||
'SERVICE_CONFIGURATION_DOMAIN',
|
||||
) as string,
|
||||
messageBroker: {
|
||||
uri: configService.get<string>('MESSAGE_BROKER_URI'),
|
||||
exchange: configService.get<string>('MESSAGE_BROKER_EXCHANGE'),
|
||||
uri: configService.get<string>('MESSAGE_BROKER_URI') as string,
|
||||
exchange: configService.get<string>(
|
||||
'MESSAGE_BROKER_EXCHANGE',
|
||||
) as string,
|
||||
},
|
||||
redis: {
|
||||
host: configService.get<string>('REDIS_HOST'),
|
||||
host: configService.get<string>('REDIS_HOST') as string,
|
||||
password: configService.get<string>('REDIS_PASSWORD'),
|
||||
port: configService.get<number>('REDIS_PORT'),
|
||||
port: configService.get<number>('REDIS_PORT') as number,
|
||||
},
|
||||
setConfigurationBrokerQueue: 'user-configuration-create-update',
|
||||
deleteConfigurationQueue: 'user-configuration-delete',
|
||||
propagateConfigurationQueue: 'user-configuration-propagate',
|
||||
setConfigurationBrokerQueue: SERVICE_CONFIGURATION_SET_QUEUE,
|
||||
deleteConfigurationQueue: SERVICE_CONFIGURATION_DELETE_QUEUE,
|
||||
propagateConfigurationQueue: SERVICE_CONFIGURATION_PROPAGATE_QUEUE,
|
||||
}),
|
||||
}),
|
||||
HealthModule.forRootAsync({
|
||||
@@ -48,11 +60,11 @@ import { EventEmitterModule } from '@nestjs/event-emitter';
|
||||
userRepository: HealthRepositoryPort,
|
||||
messagePublisher: MessagePublisherPort,
|
||||
): Promise<HealthModuleOptions> => ({
|
||||
serviceName: 'user',
|
||||
criticalLoggingKey: 'logging.user.health.crit',
|
||||
serviceName: SERVICE_NAME,
|
||||
criticalLoggingKey: HEALTH_CRITICAL_LOGGING_KEY,
|
||||
checkRepositories: [
|
||||
{
|
||||
name: 'UserRepository',
|
||||
name: HEALTH_USER_REPOSITORY,
|
||||
repository: userRepository,
|
||||
},
|
||||
],
|
||||
|
||||
@@ -2,6 +2,7 @@ import { NestFactory } from '@nestjs/core';
|
||||
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
|
||||
import { join } from 'path';
|
||||
import { AppModule } from './app.module';
|
||||
import { HEALTH_SERVICE_NAME, SERVICE_NAME } from './app.constants';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
@@ -11,17 +12,17 @@ async function bootstrap() {
|
||||
app.connectMicroservice<MicroserviceOptions>({
|
||||
transport: Transport.GRPC,
|
||||
options: {
|
||||
package: ['user', 'health'],
|
||||
package: [SERVICE_NAME, HEALTH_SERVICE_NAME],
|
||||
protoPath: [
|
||||
join(__dirname, 'modules/user/interface/grpc-controllers/user.proto'),
|
||||
join(__dirname, 'health.proto'),
|
||||
],
|
||||
url: process.env.SERVICE_URL + ':' + process.env.SERVICE_PORT,
|
||||
loader: { keepCase: true },
|
||||
url: `${process.env.SERVICE_URL}:${process.env.SERVICE_PORT}`,
|
||||
loader: { keepCase: true, enums: String },
|
||||
},
|
||||
});
|
||||
|
||||
await app.startAllMicroservices();
|
||||
await app.listen(process.env.HEALTH_SERVICE_PORT);
|
||||
await app.listen(process.env.HEALTH_SERVICE_PORT as unknown as number);
|
||||
}
|
||||
bootstrap();
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
MessageBrokerPublisher,
|
||||
} from '@mobicoop/message-broker-module';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { SERVICE_NAME } from '@src/app.constants';
|
||||
|
||||
const imports = [
|
||||
MessageBrokerModule.forRootAsync({
|
||||
@@ -14,9 +15,14 @@ const imports = [
|
||||
useFactory: async (
|
||||
configService: ConfigService,
|
||||
): Promise<MessageBrokerModuleOptions> => ({
|
||||
uri: configService.get<string>('MESSAGE_BROKER_URI'),
|
||||
exchange: configService.get<string>('MESSAGE_BROKER_EXCHANGE'),
|
||||
name: 'user',
|
||||
uri: configService.get<string>('MESSAGE_BROKER_URI') as string,
|
||||
exchange: {
|
||||
name: configService.get<string>('MESSAGE_BROKER_EXCHANGE') as string,
|
||||
durable: configService.get<boolean>(
|
||||
'MESSAGE_BROKER_EXCHANGE_DURABILITY',
|
||||
) as boolean,
|
||||
},
|
||||
name: SERVICE_NAME,
|
||||
}),
|
||||
}),
|
||||
];
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { IntegrationEvent, IntegrationEventProps } from '@mobicoop/ddd-library';
|
||||
|
||||
export class UserCreatedIntegrationEvent extends IntegrationEvent {
|
||||
readonly firstName: string;
|
||||
readonly lastName: string;
|
||||
readonly email: string;
|
||||
readonly phone: string;
|
||||
readonly firstName?: string;
|
||||
readonly lastName?: string;
|
||||
readonly email?: string;
|
||||
readonly phone?: string;
|
||||
|
||||
constructor(props: IntegrationEventProps<UserCreatedIntegrationEvent>) {
|
||||
super(props);
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { IntegrationEvent, IntegrationEventProps } from '@mobicoop/ddd-library';
|
||||
|
||||
export class UserUpdatedIntegrationEvent extends IntegrationEvent {
|
||||
readonly firstName: string;
|
||||
readonly lastName: string;
|
||||
readonly email: string;
|
||||
readonly phone: string;
|
||||
readonly firstName?: string;
|
||||
readonly lastName?: string;
|
||||
readonly email?: string;
|
||||
readonly phone?: string;
|
||||
|
||||
constructor(props: IntegrationEventProps<UserUpdatedIntegrationEvent>) {
|
||||
super(props);
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { DomainEvent, DomainEventProps } from '@mobicoop/ddd-library';
|
||||
|
||||
export class UserCreatedDomainEvent extends DomainEvent {
|
||||
readonly firstName: string;
|
||||
readonly lastName: string;
|
||||
readonly email: string;
|
||||
readonly phone: string;
|
||||
readonly firstName?: string;
|
||||
readonly lastName?: string;
|
||||
readonly email?: string;
|
||||
readonly phone?: string;
|
||||
|
||||
constructor(props: DomainEventProps<UserCreatedDomainEvent>) {
|
||||
super(props);
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { DomainEvent, DomainEventProps } from '@mobicoop/ddd-library';
|
||||
|
||||
export class UserUpdatedDomainEvent extends DomainEvent {
|
||||
readonly firstName: string;
|
||||
readonly lastName: string;
|
||||
readonly email: string;
|
||||
readonly phone: string;
|
||||
readonly firstName?: string;
|
||||
readonly lastName?: string;
|
||||
readonly email?: string;
|
||||
readonly phone?: string;
|
||||
|
||||
constructor(props: DomainEventProps<UserUpdatedDomainEvent>) {
|
||||
super(props);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
@Injectable()
|
||||
@@ -6,10 +6,4 @@ export class PrismaService extends PrismaClient implements OnModuleInit {
|
||||
async onModuleInit() {
|
||||
await this.$connect();
|
||||
}
|
||||
|
||||
async enableShutdownHooks(app: INestApplication) {
|
||||
this.$on('beforeExit', async () => {
|
||||
await app.close();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,18 +10,19 @@ import { UserEntity } from '../core/domain/user.entity';
|
||||
import { UserRepositoryPort } from '../core/application/ports/user.repository.port';
|
||||
import { UserMapper } from '../user.mapper';
|
||||
import { USER_MESSAGE_PUBLISHER } from '../user.di-tokens';
|
||||
import { SERVICE_NAME } from '@src/app.constants';
|
||||
|
||||
export type UserBaseModel = {
|
||||
uuid: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
email: string;
|
||||
phone: string;
|
||||
firstName?: string;
|
||||
lastName?: string;
|
||||
email?: string;
|
||||
phone?: string;
|
||||
};
|
||||
|
||||
export type UserReadModel = UserBaseModel & {
|
||||
createdAt?: Date;
|
||||
updatedAt?: Date;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
};
|
||||
|
||||
export type UserWriteModel = UserBaseModel;
|
||||
@@ -48,7 +49,7 @@ export class UserRepository
|
||||
eventEmitter,
|
||||
new LoggerBase({
|
||||
logger: new Logger(UserRepository.name),
|
||||
domain: 'user',
|
||||
domain: SERVICE_NAME,
|
||||
messagePublisher,
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { ResponseBase } from '@mobicoop/ddd-library';
|
||||
|
||||
export class UserResponseDto extends ResponseBase {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
email: string;
|
||||
phone: string;
|
||||
firstName?: string;
|
||||
lastName?: string;
|
||||
email?: string;
|
||||
phone?: string;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
PhoneAlreadyExistsException,
|
||||
UserAlreadyExistsException,
|
||||
} from '@modules/user/core/domain/user.errors';
|
||||
import { GRPC_SERVICE_NAME } from '@src/app.constants';
|
||||
|
||||
@UsePipes(
|
||||
new RpcValidationPipe({
|
||||
@@ -23,7 +24,7 @@ import {
|
||||
export class CreateUserGrpcController {
|
||||
constructor(private readonly commandBus: CommandBus) {}
|
||||
|
||||
@GrpcMethod('UserService', 'Create')
|
||||
@GrpcMethod(GRPC_SERVICE_NAME, 'Create')
|
||||
async create(
|
||||
createUserRequestDto: CreateUserRequestDto,
|
||||
): Promise<IdResponse> {
|
||||
|
||||
@@ -9,6 +9,7 @@ import { CommandBus } from '@nestjs/cqrs';
|
||||
import { GrpcMethod, RpcException } from '@nestjs/microservices';
|
||||
import { DeleteUserRequestDto } from './dtos/delete-user.request.dto';
|
||||
import { DeleteUserCommand } from '@modules/user/core/application/commands/delete-user/delete-user.command';
|
||||
import { GRPC_SERVICE_NAME } from '@src/app.constants';
|
||||
|
||||
@UsePipes(
|
||||
new RpcValidationPipe({
|
||||
@@ -20,7 +21,7 @@ import { DeleteUserCommand } from '@modules/user/core/application/commands/delet
|
||||
export class DeleteUserGrpcController {
|
||||
constructor(private readonly commandBus: CommandBus) {}
|
||||
|
||||
@GrpcMethod('UserService', 'Delete')
|
||||
@GrpcMethod(GRPC_SERVICE_NAME, 'Delete')
|
||||
async delete(data: DeleteUserRequestDto): Promise<void> {
|
||||
try {
|
||||
await this.commandBus.execute(new DeleteUserCommand(data));
|
||||
|
||||
@@ -9,6 +9,7 @@ import { FindUserByIdRequestDto } from './dtos/find-user-by-id.request.dto';
|
||||
import { UserResponseDto } from '../dtos/user.response.dto';
|
||||
import { UserEntity } from '@modules/user/core/domain/user.entity';
|
||||
import { FindUserByIdQuery } from '@modules/user/core/application/queries/find-user-by-id/find-user-by-id.query';
|
||||
import { GRPC_SERVICE_NAME } from '@src/app.constants';
|
||||
|
||||
@UsePipes(
|
||||
new RpcValidationPipe({
|
||||
@@ -23,7 +24,7 @@ export class FindUserByIdGrpcController {
|
||||
private readonly queryBus: QueryBus,
|
||||
) {}
|
||||
|
||||
@GrpcMethod('UserService', 'FindOneById')
|
||||
@GrpcMethod(GRPC_SERVICE_NAME, 'FindOneById')
|
||||
async findOnebyId(data: FindUserByIdRequestDto): Promise<UserResponseDto> {
|
||||
try {
|
||||
const user: UserEntity = await this.queryBus.execute(
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
PhoneAlreadyExistsException,
|
||||
UserAlreadyExistsException,
|
||||
} from '@modules/user/core/domain/user.errors';
|
||||
import { GRPC_SERVICE_NAME } from '@src/app.constants';
|
||||
|
||||
@UsePipes(
|
||||
new RpcValidationPipe({
|
||||
@@ -25,7 +26,7 @@ import {
|
||||
export class UpdateUserGrpcController {
|
||||
constructor(private readonly commandBus: CommandBus) {}
|
||||
|
||||
@GrpcMethod('UserService', 'Update')
|
||||
@GrpcMethod(GRPC_SERVICE_NAME, 'Update')
|
||||
async updateUser(data: UpdateUserRequestDto): Promise<IdResponse> {
|
||||
try {
|
||||
const aggregateID: AggregateID = await this.commandBus.execute(
|
||||
|
||||
@@ -64,9 +64,8 @@ describe('create-user.service', () => {
|
||||
UserEntity.create = jest.fn().mockReturnValue({
|
||||
id: '047a6ecf-23d4-4d3e-877c-3225d560a8da',
|
||||
});
|
||||
const result: AggregateID = await createUserService.execute(
|
||||
createUserCommand,
|
||||
);
|
||||
const result: AggregateID =
|
||||
await createUserService.execute(createUserCommand);
|
||||
expect(result).toBe('047a6ecf-23d4-4d3e-877c-3225d560a8da');
|
||||
});
|
||||
it('should throw an error if something bad happens', async () => {
|
||||
|
||||
@@ -41,9 +41,8 @@ describe('Delete User Service', () => {
|
||||
describe('execution', () => {
|
||||
const deleteUserCommand = new DeleteUserCommand(deleteUserRequest);
|
||||
it('should delete a user', async () => {
|
||||
const result: boolean = await deleteUserService.execute(
|
||||
deleteUserCommand,
|
||||
);
|
||||
const result: boolean =
|
||||
await deleteUserService.execute(deleteUserCommand);
|
||||
expect(result).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -49,9 +49,8 @@ describe('find-user-by-id.query-handler', () => {
|
||||
const findUserbyIdQuery = new FindUserByIdQuery(
|
||||
'dd264806-13b4-4226-9b18-87adf0ad5dd1',
|
||||
);
|
||||
const user: UserEntity = await findUserByIdQueryHandler.execute(
|
||||
findUserbyIdQuery,
|
||||
);
|
||||
const user: UserEntity =
|
||||
await findUserByIdQueryHandler.execute(findUserbyIdQuery);
|
||||
expect(user.getProps().lastName).toBe('Doe');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -98,9 +98,8 @@ describe('update-user.service', () => {
|
||||
const updateUserCommand = new UpdateUserCommand(
|
||||
updateFirstNameUserRequest,
|
||||
);
|
||||
const result: AggregateID = await updateUserService.execute(
|
||||
updateUserCommand,
|
||||
);
|
||||
const result: AggregateID =
|
||||
await updateUserService.execute(updateUserCommand);
|
||||
expect(result).toBe('c97b1783-76cf-4840-b298-b90b13c58894');
|
||||
expect(userToUpdate.update).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
@@ -65,9 +65,8 @@ describe('Create User Grpc Controller', () => {
|
||||
|
||||
it('should create a new user', async () => {
|
||||
jest.spyOn(mockCommandBus, 'execute');
|
||||
const result: IdResponse = await createUserGrpcController.create(
|
||||
createUserRequest,
|
||||
);
|
||||
const result: IdResponse =
|
||||
await createUserGrpcController.create(createUserRequest);
|
||||
expect(result).toBeInstanceOf(IdResponse);
|
||||
expect(result.id).toBe('200d61a8-d878-4378-a609-c19ea71633d2');
|
||||
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { RedisClientOptions } from '@liaoliaots/nestjs-redis';
|
||||
import { Module, Provider } from '@nestjs/common';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { CqrsModule } from '@nestjs/cqrs';
|
||||
@@ -20,6 +19,7 @@ import { FindUserByIdQueryHandler } from './core/application/queries/find-user-b
|
||||
import { PublishMessageWhenUserIsCreatedDomainEventHandler } from './core/application/event-handlers/publish-message-when-user-is-created.domain-event-handler';
|
||||
import { PublishMessageWhenUserIsUpdatedDomainEventHandler } from './core/application/event-handlers/publish-message-when-user-is-updated.domain-event-handler';
|
||||
import { PublishMessageWhenUserIsDeletedDomainEventHandler } from './core/application/event-handlers/publish-message-when-user-is-deleted.domain-event-handler';
|
||||
import { RedisClientOptions } from '@songkeys/nestjs-redis';
|
||||
|
||||
const imports = [
|
||||
CqrsModule,
|
||||
|
||||
Reference in New Issue
Block a user