From 8bb6754fd1329dd788eb1ebede139e7f62d9db4a Mon Sep 17 00:00:00 2001 From: nochill Date: Sun, 26 Feb 2023 22:00:26 +0700 Subject: [PATCH] fix error --- src/libs/api/api-error.response.ts | 2 ++ src/libs/application/context/ContextInterceptor.ts | 3 ++- src/libs/utils/correlationId.ts | 12 ++++++++++++ .../user/commands/create-user.http.controller.ts | 4 +--- src/modules/user/commands/create-user.service.ts | 5 +++-- src/modules/user/dtos/user.response.dto.ts | 1 + 6 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 src/libs/utils/correlationId.ts diff --git a/src/libs/api/api-error.response.ts b/src/libs/api/api-error.response.ts index c8b9481..7127784 100644 --- a/src/libs/api/api-error.response.ts +++ b/src/libs/api/api-error.response.ts @@ -3,6 +3,7 @@ export class ApiErrorResponse { readonly message: string; readonly error: string readonly correlationId: string; + readonly metadata?: any; readonly subErrors?: string[]; constructor(body: ApiErrorResponse) { @@ -10,6 +11,7 @@ export class ApiErrorResponse { this.message = body.message; this.error = body.error; this.correlationId = body.correlationId; + this.metadata = body.metadata; this.subErrors = body.subErrors; } } \ No newline at end of file diff --git a/src/libs/application/context/ContextInterceptor.ts b/src/libs/application/context/ContextInterceptor.ts index 8564011..d605aaa 100644 --- a/src/libs/application/context/ContextInterceptor.ts +++ b/src/libs/application/context/ContextInterceptor.ts @@ -1,4 +1,5 @@ import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from "@nestjs/common"; +import { createCorrelationId } from "@src/libs/utils/correlationId"; import { Observable, tap } from "rxjs"; import { RequestContextService } from './AppRequestContext'; @@ -7,7 +8,7 @@ export class ContextInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable | Promise> { const request = context.switchToHttp().getRequest(); - const requestId = request?.body?.requestId; + const requestId = request?.body?.requestId ?? createCorrelationId(6); RequestContextService.setRequestId(requestId); diff --git a/src/libs/utils/correlationId.ts b/src/libs/utils/correlationId.ts new file mode 100644 index 0000000..8b73c3d --- /dev/null +++ b/src/libs/utils/correlationId.ts @@ -0,0 +1,12 @@ +export function createCorrelationId(length: number): string { + const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + let counter = 0; + let result: string = ''; + const charactersLength = characters.length; + while (counter < length) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + counter += 1; + } + return result; + +} \ No newline at end of file diff --git a/src/modules/user/commands/create-user.http.controller.ts b/src/modules/user/commands/create-user.http.controller.ts index fd9259b..a73939c 100644 --- a/src/modules/user/commands/create-user.http.controller.ts +++ b/src/modules/user/commands/create-user.http.controller.ts @@ -1,4 +1,4 @@ -import { Post, ConflictException as ConflictHttpException, Body, Controller, InternalServerErrorException } from "@nestjs/common"; +import { Post, ConflictException as ConflictHttpException, Body, Controller } from "@nestjs/common"; import { match, Result} from 'oxide.ts'; import { routesV1 } from "@src/config/app.routes"; import { UserAlreadyExistsError } from "../domain/user.error"; @@ -18,8 +18,6 @@ export class CreateUserHttpController { const result: Result = await this.commandBus.execute(command); - console.log(result); - return match(result, { Ok: (res: any) => res, Err: (error: Error) => { diff --git a/src/modules/user/commands/create-user.service.ts b/src/modules/user/commands/create-user.service.ts index 8d2f4ea..1d7d0e5 100644 --- a/src/modules/user/commands/create-user.service.ts +++ b/src/modules/user/commands/create-user.service.ts @@ -1,6 +1,6 @@ -import { ConflictException, Inject } from "@nestjs/common"; +import { Inject } from "@nestjs/common"; import { CommandHandler, ICommandHandler } from "@nestjs/cqrs"; -import { AggregateID } from "@src/libs/ddd"; +import { ConflictException } from "@src/libs/exceptions"; import { Err, Ok, Result } from "oxide.ts"; import { UserRepositoryPort } from "../database/user.repository.port"; import { UserEntity } from "../domain/user.entity"; @@ -34,6 +34,7 @@ export class CreateUserService implements ICommandHandler { userResponse.email = command.email; userResponse.fullname = command.fullname; userResponse.phone_number = command.phone_number; + userResponse.role_id = command.role_id; return Ok(userResponse) } catch (error: any) { diff --git a/src/modules/user/dtos/user.response.dto.ts b/src/modules/user/dtos/user.response.dto.ts index 862641b..25aa9c4 100644 --- a/src/modules/user/dtos/user.response.dto.ts +++ b/src/modules/user/dtos/user.response.dto.ts @@ -4,4 +4,5 @@ export class UserResponseDto extends ResponseBase { email: string; phone_number: string; fullname: string; + role_id: number; } \ No newline at end of file