Skip to content

Commit

Permalink
adjust send contact form api (#60)
Browse files Browse the repository at this point in the history
Co-authored-by: NHT <[email protected]>
  • Loading branch information
nfesta2023 and hoangtuan910 authored Feb 23, 2024
1 parent f0373c2 commit 119f85f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 19 deletions.
9 changes: 9 additions & 0 deletions src/dto/general-error-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class GeneralErrorResponse {
constructor(_error_code: number = 0, _detail: any = null) {
this.error_code = _error_code;
this.detail = _detail;
return this;
}
error_code: number;
detail: any;
}
8 changes: 8 additions & 0 deletions src/dto/general-service-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class GeneralServiceResponse {
constructor() {
this.statusCode = null;
this.data = null;
}
statusCode: number;
data: any;
}
8 changes: 8 additions & 0 deletions src/exceptions/custom-bad-request.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { BadRequestException } from '@nestjs/common';
import { GeneralErrorResponse } from 'src/dto/general-error-response.dto';

export class CustomBadRequestException extends BadRequestException {
constructor(public readonly error_response: GeneralErrorResponse) {
super(error_response);
}
}
8 changes: 8 additions & 0 deletions src/exceptions/custom-not-found.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NotFoundException } from '@nestjs/common';
import { GeneralErrorResponse } from 'src/dto/general-error-response.dto';

export class CustomNotFoundException extends NotFoundException {
constructor(public readonly error_response: GeneralErrorResponse) {
super(error_response);
}
}
9 changes: 6 additions & 3 deletions src/feature/restaurant/dto/send-contact-form-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { GeneralResponse } from 'src/dto/general-response.dto';

export class SendContactFormResponse extends GeneralResponse {}
export class SendContactFormResponse {
constructor(_message: string = '') {
this.message = _message;
}
message: string;
}
40 changes: 24 additions & 16 deletions src/feature/restaurant/restaurant.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { GetRestaurantDetailResponse } from './dto/get-restaurant-detail-respons
import { SendContactFormRequest } from './dto/send-contact-form-request.dto';
import { SendContactFormResponse } from './dto/send-contact-form-response.dto';
import { CommonService } from '../common/common.service';
import { GeneralServiceResponse } from 'src/dto/general-service-response.dto';
import { GeneralErrorResponse } from 'src/dto/general-error-response.dto';
import { CustomBadRequestException } from 'src/exceptions/custom-bad-request.exception';

@Controller()
export class RestaurantController {
Expand Down Expand Up @@ -50,40 +53,45 @@ export class RestaurantController {
@MessagePattern({ cmd: 'send_contact_form' })
async sendContactForm(
data: SendContactFormRequest,
): Promise<SendContactFormResponse> {
const res = new SendContactFormResponse(200, '');
): Promise<GeneralServiceResponse> {
const res = new GeneralServiceResponse();
const { email, message } = data;
try {
if (!email) {
throw new HttpException('Email is required', 400);
// throw new HttpException('Email is required', 400);
throw new CustomBadRequestException(
new GeneralErrorResponse(1, 'Email is required'),
);
}
if (!message) {
throw new HttpException('Message is required', 400);
// throw new HttpException('Message is required', 400);
throw new CustomBadRequestException(
new GeneralErrorResponse(1, 'Message is required'),
);
}

//Validate Email Format
if (!this.commonService.validateEmail(email)) {
throw new HttpException('Invalid Email Format', 400);
// throw new HttpException('Invalid Email Format', 400);
throw new CustomBadRequestException(
new GeneralErrorResponse(1, 'Invalid Email Format'),
);
}

//Process data
const conact_id =
await this.restaurantService.sendContactFormFromEndPoint(
email,
message,
);
await this.restaurantService.sendContactFormFromEndPoint(email, message);

res.statusCode = 200;
res.message = 'Sending Contact Form Successfully';
res.data = conact_id;
res.data = new SendContactFormResponse(
'Sending Contact Form Successfully',
);
} catch (error) {
if (error instanceof HttpException) {
res.statusCode = error.getStatus();
res.message = error.getResponse();
res.data = null;
res.data = error.getResponse();
} else {
res.statusCode = 500;
res.message = error.toString();
res.data = null;
res.data = new GeneralErrorResponse(9, error.toString());
}
}

Expand Down

0 comments on commit 119f85f

Please sign in to comment.