Skip to content

Commit

Permalink
Fes 54 get application fee (#66)
Browse files Browse the repository at this point in the history
* add logic, implete the general rpc error response

* clear unneeded code

---------

Co-authored-by: NHT <[email protected]>
  • Loading branch information
nfesta2023 and hoangtuan910 authored Mar 9, 2024
1 parent 78b4f95 commit 37d3c30
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 13 deletions.
8 changes: 8 additions & 0 deletions src/exceptions/custom-rpc.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class CustomRpcException extends Error {
constructor(
public readonly error_code: number,
public readonly detail: any,
) {
super();
}
}
4 changes: 4 additions & 0 deletions src/feature/order/dto/get-application-fee-request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class GetApplicationFeeRequest {
public items_total: number;
public exchange_rate: number;
}
3 changes: 3 additions & 0 deletions src/feature/order/dto/get-application-fee-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class GetApplicationFeeResponse {
application_fee: number;
}
22 changes: 20 additions & 2 deletions src/feature/order/order.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Controller } from '@nestjs/common';
import { Controller, UseFilters } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';
import { InjectRepository } from '@nestjs/typeorm';
import { Order } from 'src/entity/order.entity';
import { Repository } from 'typeorm';
import { OrderService } from './order.service';
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';

@Controller('order')
export class OrderController {
Expand All @@ -18,6 +21,21 @@ export class OrderController {

@MessagePattern({ cmd: 'update_order_status_by_webhook' })
async updateOrderStatusByWebhook({ delivery_order_id, webhookData }) {
return this.orderService.updateOrderStatusFromAhamoveWebhook(delivery_order_id, webhookData);
return this.orderService.updateOrderStatusFromAhamoveWebhook(
delivery_order_id,
webhookData,
);
}

@MessagePattern({ cmd: 'get_application_fee' })
@UseFilters(new CustomRpcExceptionFilter())
async getApplicationFee(
data: GetApplicationFeeRequest,
): Promise<GetApplicationFeeResponse> {
const { items_total, exchange_rate } = data;
return await this.orderService.getApplicationFeeFromEndPoint(
items_total,
exchange_rate,
);
}
}
104 changes: 93 additions & 11 deletions src/feature/order/order.service.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,97 @@
import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common';
import {
Injectable,
InternalServerErrorException,
Logger,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Order } from 'src/entity/order.entity';
import { OrderStatus } from 'src/enum';
import { Repository } from 'typeorm';
import { GetApplicationFeeResponse } from './dto/get-application-fee-response.dto';

@Injectable()
export class OrderService {
private readonly logger = new Logger(OrderService.name);
constructor(@InjectRepository(Order) private orderRepo: Repository<Order>) {}

async updateOrderStatusFromAhamoveWebhook(orderId, webhookData): Promise<any> {
async updateOrderStatusFromAhamoveWebhook(
orderId,
webhookData,
): Promise<any> {
try {
this.logger.log(`receiving data from webhook to ${orderId} with ${webhookData.status}`);
const currentOrder = await this.orderRepo.findOne({ where: { delivery_order_id: orderId } });
this.logger.log(
`receiving data from webhook to ${orderId} with ${webhookData.status}`,
);
const currentOrder = await this.orderRepo.findOne({
where: { delivery_order_id: orderId },
});
if (currentOrder) {
const { status, cancel_time, sub_status, pickup_time, complete_time, fail_time, return_time, processing_time, accept_time } = webhookData;
const ahavemoveData = { status, accept_time, cancel_time, pickup_time, complete_time, fail_time, return_time, sub_status, processing_time, path_status: '' };
const {
status,
cancel_time,
sub_status,
pickup_time,
complete_time,
fail_time,
return_time,
processing_time,
accept_time,
} = webhookData;
const ahavemoveData = {
status,
accept_time,
cancel_time,
pickup_time,
complete_time,
fail_time,
return_time,
sub_status,
processing_time,
path_status: '',
};
ahavemoveData.path_status = webhookData?.path?.status;
const orderData = {
order_pickup_time: currentOrder.pickup_time,
is_preorder: currentOrder.is_preorder,
ready_time: currentOrder.ready_time,
};
const updateData = this.getOrderStatusBaseOnAhahaStatus(orderData, ahavemoveData);
this.logger.log(`Updating data from webhook to ${orderId} with ${JSON.stringify(updateData)}`);
await this.orderRepo.update(currentOrder.order_id, { ...currentOrder, ...updateData });
const updateData = this.getOrderStatusBaseOnAhahaStatus(
orderData,
ahavemoveData,
);
this.logger.log(
`Updating data from webhook to ${orderId} with ${JSON.stringify(
updateData,
)}`,
);
await this.orderRepo.update(currentOrder.order_id, {
...currentOrder,
...updateData,
});
return { message: 'Order updated successfully' };
}
return { message: 'Order not existed' };
} catch (error) {
this.logger.error(`An error occurred while updating order: ${error.message}`);
this.logger.error(
`An error occurred while updating order: ${error.message}`,
);
throw new InternalServerErrorException();
}
}
private getOrderStatusBaseOnAhahaStatus(
{ order_pickup_time, is_preorder, ready_time },
{ status, sub_status, path_status, accept_time, cancel_time, pickup_time, complete_time, fail_time, return_time, processing_time },
{
status,
sub_status,
path_status,
accept_time,
cancel_time,
pickup_time,
complete_time,
fail_time,
return_time,
processing_time,
},
) {
let data = {};
switch (status) {
Expand Down Expand Up @@ -108,4 +164,30 @@ export class OrderService {
}
return data;
}

async getApplicationFeeFromEndPoint(
items_total: number,
exchange_rate: number, //Exchange rate to VND
): Promise<GetApplicationFeeResponse> {
const FEE_RATE = 0.03;
const MAXIMUM_FEE = 75000; //VND
const MINIMUM_FEE = 1000; //VND

const preApplicationFee = items_total * FEE_RATE;
let applicationFee = 0;
if (preApplicationFee >= MAXIMUM_FEE) {
applicationFee = MAXIMUM_FEE / exchange_rate;
} else if (preApplicationFee <= MINIMUM_FEE) {
applicationFee = MINIMUM_FEE / exchange_rate;
} else if (
preApplicationFee < MAXIMUM_FEE &&
preApplicationFee > MINIMUM_FEE
) {
applicationFee = preApplicationFee / exchange_rate;
}

return {
application_fee: applicationFee,
};
}
}
10 changes: 10 additions & 0 deletions src/filters/custom-rpc-exception.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { CustomRpcException } from 'src/exceptions/custom-rpc.exception';

@Catch(CustomRpcException)
export class CustomRpcExceptionFilter implements ExceptionFilter {
catch(exception: CustomRpcException, host: ArgumentsHost): Observable<any> {
return throwError(() => exception);
}
}

0 comments on commit 37d3c30

Please sign in to comment.