Skip to content

Commit

Permalink
get delivery fee (#81)
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 20, 2024
1 parent 5a640ae commit 0de1d44
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/feature/order/dto/get-delivery-fee-request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class GetDeliveryFeeRequest {
restaurant_id: number; //REQUIRED

delivery_latitude: number; //REQUIRED

delivery_longitude: number; //REQUIRED
}
6 changes: 6 additions & 0 deletions src/feature/order/dto/get-delivery-fee-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class GetDeliveryFeeResonse {
delivery_fee: number;
currency: string;
duration_s: number;
distance_km: number;
}
16 changes: 16 additions & 0 deletions src/feature/order/order.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { ApplyPromotionCodeResponse } from './dto/apply-promotion-code-response.
import { CreateOrderRequest } from './dto/create-order-request.dto';
import { CreateOrderResponse } from './dto/create-order-response.dto';
import { OrderDetailResponse } from './dto/order-detail-response.dto';
import { GetDeliveryFeeRequest } from './dto/get-delivery-fee-request.dto';
import { GetDeliveryFeeResonse } from './dto/get-delivery-fee-response.dto';

@Controller('order')
export class OrderController {
Expand Down Expand Up @@ -232,4 +234,18 @@ export class OrderController {

return order;
}

@MessagePattern({ cmd: 'get_delivery_fee' })
@UseFilters(new CustomRpcExceptionFilter())
async getDeliveryFee(
data: GetDeliveryFeeRequest,
): Promise<GetDeliveryFeeResonse> {
const { restaurant_id, delivery_latitude, delivery_longitude } = data;

return await this.orderService.getDeliveryFeeFromEndPoint(
restaurant_id,
delivery_latitude,
delivery_longitude,
);
}
}
42 changes: 42 additions & 0 deletions src/feature/order/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import { PostAhaOrderRequest } from 'src/dependency/ahamove/dto/ahamove.dto';
import { Customer } from 'src/entity/customer.entity';
import { RestaurantExt } from 'src/entity/restaurant-ext.entity';
import { Packaging } from 'src/entity/packaging.entity';
import { GetDeliveryFeeResonse } from './dto/get-delivery-fee-response.dto';
import { VND } from 'src/constant/unit.constant';

@Injectable()
export class OrderService {
Expand Down Expand Up @@ -1366,4 +1368,44 @@ export class OrderService {

return orderDetail;
}

async getDeliveryFeeFromEndPoint(
restaurant_id: number,
delivery_latitude: number,
delivery_longitude: number,
): Promise<GetDeliveryFeeResonse> {
//validate restaurant exist
const restaurant =
await this.commonService.getRestaurantById(restaurant_id);
if (!restaurant) {
throw new CustomRpcException(2, 'Restaurant doesnot exist');
}

//calculate delivery fee
const restaurantAddress = await this.entityManager
.createQueryBuilder(Address, 'address')
.where('address.address_id = :address_id', {
address_id: restaurant.address_id,
})
.getOne();
const deliveryEstimation = (
await this.ahamoveService.estimatePrice([
{
lat: restaurantAddress.latitude,
long: restaurantAddress.longitude,
},
{
lat: delivery_latitude,
long: delivery_longitude,
},
])
)[0].data;

return {
delivery_fee: deliveryEstimation?.total_price,
currency: VND,
duration_s: deliveryEstimation?.duration,
distance_km: deliveryEstimation?.distance,
};
}
}

0 comments on commit 0de1d44

Please sign in to comment.