From b5cca82914cf0653484b49cdcde3ac0d400dc05e Mon Sep 17 00:00:00 2001 From: nfesta2023 <142601504+nfesta2023@users.noreply.github.com> Date: Tue, 12 Mar 2024 21:15:17 +0700 Subject: [PATCH] get cutlery fee (#69) Co-authored-by: NHT --- src/entity/restaurant.entity.ts | 3 ++ .../order/dto/get-cutlery-fee-request.dto.ts | 4 +++ .../order/dto/get-cutlery-fee-response.dto.ts | 4 +++ src/feature/order/order.controller.ts | 27 +++++++++++++++++- src/feature/order/order.service.ts | 28 +++++++++++++++++++ src/type/index.ts | 5 ++++ 6 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/feature/order/dto/get-cutlery-fee-request.dto.ts create mode 100644 src/feature/order/dto/get-cutlery-fee-response.dto.ts diff --git a/src/entity/restaurant.entity.ts b/src/entity/restaurant.entity.ts index 3a533f6..82cdaa8 100644 --- a/src/entity/restaurant.entity.ts +++ b/src/entity/restaurant.entity.ts @@ -111,6 +111,9 @@ export class Restaurant { @Column({ type: 'int', nullable: false, unique: false, default: 0 }) public cutoff_time_m: number; + @Column({ type: 'int', nullable: true, unique: false }) + public cutlery_price: number; + @CreateDateColumn({ type: 'datetime', nullable: false, diff --git a/src/feature/order/dto/get-cutlery-fee-request.dto.ts b/src/feature/order/dto/get-cutlery-fee-request.dto.ts new file mode 100644 index 0000000..647188f --- /dev/null +++ b/src/feature/order/dto/get-cutlery-fee-request.dto.ts @@ -0,0 +1,4 @@ +export class GetCutleryFeeRequest { + restaurant_id: number; + item_quantity: number; +} diff --git a/src/feature/order/dto/get-cutlery-fee-response.dto.ts b/src/feature/order/dto/get-cutlery-fee-response.dto.ts new file mode 100644 index 0000000..9e7e006 --- /dev/null +++ b/src/feature/order/dto/get-cutlery-fee-response.dto.ts @@ -0,0 +1,4 @@ +export class GetCutleryFeeResponse { + cutlery_fee: number; + currency: string; +} diff --git a/src/feature/order/order.controller.ts b/src/feature/order/order.controller.ts index ff87409..5d1e5a1 100644 --- a/src/feature/order/order.controller.ts +++ b/src/feature/order/order.controller.ts @@ -1,4 +1,4 @@ -import { Controller, UseFilters } from '@nestjs/common'; +import { Controller, Get, UseFilters } from '@nestjs/common'; import { MessagePattern } from '@nestjs/microservices'; import { InjectRepository } from '@nestjs/typeorm'; import { Order } from 'src/entity/order.entity'; @@ -8,12 +8,17 @@ import { GetApplicationFeeRequest } from './dto/get-application-fee-request.dto' import { CustomRpcExceptionFilter } from 'src/filters/custom-rpc-exception.filter'; import { GetApplicationFeeResponse } from './dto/get-application-fee-response.dto'; import { GetPaymentMethodResponse } from './dto/get-payment-method-response.dto'; +import { GetCutleryFeeRequest } from './dto/get-cutlery-fee-request.dto'; +import { GetCutleryFeeResponse } from './dto/get-cutlery-fee-response.dto'; +import { CommonService } from '../common/common.service'; +import { MoneyType } from 'src/type'; @Controller('order') export class OrderController { constructor( @InjectRepository(Order) private orderRepo: Repository, private readonly orderService: OrderService, + private readonly commonService: CommonService, ) {} @MessagePattern({ cmd: 'get_order_by_id' }) async getOrderByOrderId(order_id) { @@ -56,4 +61,24 @@ export class OrderController { }); return result; } + + @MessagePattern({ cmd: 'get_cutlery_fee' }) + @UseFilters(new CustomRpcExceptionFilter()) + async getCutleryFee( + data: GetCutleryFeeRequest, + ): Promise { + const result = new GetCutleryFeeResponse(); + + const { restaurant_id, item_quantity } = data; + + const fee: MoneyType = await this.orderService.getCutleryFee( + restaurant_id, + item_quantity, + ); + + result.cutlery_fee = fee.amount; + result.currency = fee.currency; + + return result; + } } diff --git a/src/feature/order/order.service.ts b/src/feature/order/order.service.ts index ad8bdab..0558c7d 100644 --- a/src/feature/order/order.service.ts +++ b/src/feature/order/order.service.ts @@ -9,6 +9,9 @@ import { OrderStatus } from 'src/enum'; import { EntityManager, Repository } from 'typeorm'; import { GetApplicationFeeResponse } from './dto/get-application-fee-response.dto'; import { PaymentOption } from 'src/entity/payment-option.entity'; +import { MoneyType } from 'src/type'; +import { Restaurant } from 'src/entity/restaurant.entity'; +import { CustomRpcException } from 'src/exceptions/custom-rpc.exception'; @Injectable() export class OrderService { @@ -201,4 +204,29 @@ export class OrderService { .where('payment.is_active = 1') .getMany(); } + + async getCutleryFee( + restaurant_id: number, + quantity: number, + ): Promise { + const restaurant = await this.entityManager + .createQueryBuilder(Restaurant, 'restaurant') + .leftJoinAndSelect('restaurant.unit_obj', 'unit') + .where('restaurant.restaurant_id = :restaurant_id', { restaurant_id }) + .getOne(); + + if (!restaurant) { + throw new CustomRpcException(2, 'Restaurant doesnot exist'); + } + if (!restaurant.cutlery_price) { + return { + amount: 0, + currency: restaurant.unit_obj.symbol, + }; + } + return { + amount: restaurant.cutlery_price * quantity, + currency: restaurant.unit_obj.symbol, + }; + } } diff --git a/src/type/index.ts b/src/type/index.ts index dcde28a..6a395e3 100644 --- a/src/type/index.ts +++ b/src/type/index.ts @@ -192,3 +192,8 @@ export interface PackagingInfo { } export type NumbericBoolean = 0 | 1; + +export interface MoneyType { + amount: number; + currency: string; +}