Skip to content

Commit

Permalink
get cutlery fee (#69)
Browse files Browse the repository at this point in the history
Co-authored-by: NHT <[email protected]>
  • Loading branch information
nfesta2023 and hoangtuan910 authored Mar 12, 2024
1 parent 774ef81 commit b5cca82
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/entity/restaurant.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/feature/order/dto/get-cutlery-fee-request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class GetCutleryFeeRequest {
restaurant_id: number;
item_quantity: number;
}
4 changes: 4 additions & 0 deletions src/feature/order/dto/get-cutlery-fee-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class GetCutleryFeeResponse {
cutlery_fee: number;
currency: string;
}
27 changes: 26 additions & 1 deletion src/feature/order/order.controller.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<Order>,
private readonly orderService: OrderService,
private readonly commonService: CommonService,
) {}
@MessagePattern({ cmd: 'get_order_by_id' })
async getOrderByOrderId(order_id) {
Expand Down Expand Up @@ -56,4 +61,24 @@ export class OrderController {
});
return result;
}

@MessagePattern({ cmd: 'get_cutlery_fee' })
@UseFilters(new CustomRpcExceptionFilter())
async getCutleryFee(
data: GetCutleryFeeRequest,
): Promise<GetCutleryFeeResponse> {
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;
}
}
28 changes: 28 additions & 0 deletions src/feature/order/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -201,4 +204,29 @@ export class OrderService {
.where('payment.is_active = 1')
.getMany();
}

async getCutleryFee(
restaurant_id: number,
quantity: number,
): Promise<MoneyType> {
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,
};
}
}
5 changes: 5 additions & 0 deletions src/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,8 @@ export interface PackagingInfo {
}

export type NumbericBoolean = 0 | 1;

export interface MoneyType {
amount: number;
currency: string;
}

0 comments on commit b5cca82

Please sign in to comment.