fix error

This commit is contained in:
nochill 2023-02-26 22:00:26 +07:00
parent ca73c44dfa
commit 8bb6754fd1
6 changed files with 21 additions and 6 deletions

View File

@ -3,6 +3,7 @@ export class ApiErrorResponse {
readonly message: string; readonly message: string;
readonly error: string readonly error: string
readonly correlationId: string; readonly correlationId: string;
readonly metadata?: any;
readonly subErrors?: string[]; readonly subErrors?: string[];
constructor(body: ApiErrorResponse) { constructor(body: ApiErrorResponse) {
@ -10,6 +11,7 @@ export class ApiErrorResponse {
this.message = body.message; this.message = body.message;
this.error = body.error; this.error = body.error;
this.correlationId = body.correlationId; this.correlationId = body.correlationId;
this.metadata = body.metadata;
this.subErrors = body.subErrors; this.subErrors = body.subErrors;
} }
} }

View File

@ -1,4 +1,5 @@
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from "@nestjs/common"; import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from "@nestjs/common";
import { createCorrelationId } from "@src/libs/utils/correlationId";
import { Observable, tap } from "rxjs"; import { Observable, tap } from "rxjs";
import { RequestContextService } from './AppRequestContext'; import { RequestContextService } from './AppRequestContext';
@ -7,7 +8,7 @@ export class ContextInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler<any>): Observable<any> | Promise<Observable<any>> { intercept(context: ExecutionContext, next: CallHandler<any>): Observable<any> | Promise<Observable<any>> {
const request = context.switchToHttp().getRequest(); const request = context.switchToHttp().getRequest();
const requestId = request?.body?.requestId; const requestId = request?.body?.requestId ?? createCorrelationId(6);
RequestContextService.setRequestId(requestId); RequestContextService.setRequestId(requestId);

View File

@ -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;
}

View File

@ -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 { match, Result} from 'oxide.ts';
import { routesV1 } from "@src/config/app.routes"; import { routesV1 } from "@src/config/app.routes";
import { UserAlreadyExistsError } from "../domain/user.error"; import { UserAlreadyExistsError } from "../domain/user.error";
@ -18,8 +18,6 @@ export class CreateUserHttpController {
const result: Result<UserResponseDto, UserAlreadyExistsError> = const result: Result<UserResponseDto, UserAlreadyExistsError> =
await this.commandBus.execute(command); await this.commandBus.execute(command);
console.log(result);
return match(result, { return match(result, {
Ok: (res: any) => res, Ok: (res: any) => res,
Err: (error: Error) => { Err: (error: Error) => {

View File

@ -1,6 +1,6 @@
import { ConflictException, Inject } from "@nestjs/common"; import { Inject } from "@nestjs/common";
import { CommandHandler, ICommandHandler } from "@nestjs/cqrs"; 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 { Err, Ok, Result } from "oxide.ts";
import { UserRepositoryPort } from "../database/user.repository.port"; import { UserRepositoryPort } from "../database/user.repository.port";
import { UserEntity } from "../domain/user.entity"; import { UserEntity } from "../domain/user.entity";
@ -34,6 +34,7 @@ export class CreateUserService implements ICommandHandler {
userResponse.email = command.email; userResponse.email = command.email;
userResponse.fullname = command.fullname; userResponse.fullname = command.fullname;
userResponse.phone_number = command.phone_number; userResponse.phone_number = command.phone_number;
userResponse.role_id = command.role_id;
return Ok(userResponse) return Ok(userResponse)
} catch (error: any) { } catch (error: any) {

View File

@ -4,4 +4,5 @@ export class UserResponseDto extends ResponseBase {
email: string; email: string;
phone_number: string; phone_number: string;
fullname: string; fullname: string;
role_id: number;
} }