Merge pull request 'Fix: fix uniuni api error' (#2) from longbot/API:Feature-add-shipment into main
Reviewed-on: #2
This commit is contained in:
commit
5e5ed3b309
|
|
@ -162,6 +162,27 @@ export class LogisticsController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOkResponse(
|
||||||
|
{type: BooleanRes}
|
||||||
|
)
|
||||||
|
@Post('/getShipmentLabel/:shipmentId')
|
||||||
|
async getShipmentLabel(
|
||||||
|
@Param('shipmentId') shipmentId: string
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const res = await this.logisticsService.getShipmentLabel(shipmentId);
|
||||||
|
|
||||||
|
if (res.data.data[0].status === 'Success') {
|
||||||
|
return successResponse({ content: res.data.data[0].labelContent });
|
||||||
|
} else {
|
||||||
|
return errorResponse(res.data.data[0].errors);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
return errorResponse(error?.message || '创建失败');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@ApiOkResponse()
|
@ApiOkResponse()
|
||||||
@Post('/getPaymentMethods')
|
@Post('/getPaymentMethods')
|
||||||
async getpaymentmethods() {
|
async getpaymentmethods() {
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,12 @@ export class StockPoint extends BaseEntity {
|
||||||
@Column({ default: false })
|
@Column({ default: false })
|
||||||
isB: boolean;
|
isB: boolean;
|
||||||
|
|
||||||
|
@Column({ default: 'uniuni' })
|
||||||
|
upStreamName: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
upStreamStockPointId: number;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
example: '2022-12-12 11:11:11',
|
example: '2022-12-12 11:11:11',
|
||||||
description: '创建时间',
|
description: '创建时间',
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ import { CanadaPostService } from './canadaPost.service';
|
||||||
import { OrderItem } from '../entity/order_item.entity';
|
import { OrderItem } from '../entity/order_item.entity';
|
||||||
import { OrderSale } from '../entity/order_sale.entity';
|
import { OrderSale } from '../entity/order_sale.entity';
|
||||||
import { UniExpressService } from './uni_express.service';
|
import { UniExpressService } from './uni_express.service';
|
||||||
|
import { StockPoint } from '../entity/stock_point.entity';
|
||||||
|
|
||||||
@Provide()
|
@Provide()
|
||||||
export class LogisticsService {
|
export class LogisticsService {
|
||||||
|
|
@ -43,6 +44,9 @@ export class LogisticsService {
|
||||||
@InjectEntityModel(Order)
|
@InjectEntityModel(Order)
|
||||||
orderModel: Repository<Order>;
|
orderModel: Repository<Order>;
|
||||||
|
|
||||||
|
@InjectEntityModel(StockPoint)
|
||||||
|
stockPointModel: Repository<StockPoint>
|
||||||
|
|
||||||
@InjectEntityModel(OrderSale)
|
@InjectEntityModel(OrderSale)
|
||||||
orderSaleModel: Repository<OrderSale>;
|
orderSaleModel: Repository<OrderSale>;
|
||||||
|
|
||||||
|
|
@ -188,6 +192,18 @@ export class LogisticsService {
|
||||||
return [...rates, ...canadaPostRates];
|
return [...rates, ...canadaPostRates];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getShipmentLabel(shipmentId) {
|
||||||
|
try {
|
||||||
|
const shipment:Shipment = await this.shipmentModel.findOneBy({id: shipmentId});
|
||||||
|
if (!shipment) {
|
||||||
|
throw new Error('运单不存在');
|
||||||
|
}
|
||||||
|
return await this.uniExpressService.getLabel(shipment.return_tracking_number);
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error('获取运单失败');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async createShipment(orderId: number, data: ShipmentBookDTO, userId: number) {
|
async createShipment(orderId: number, data: ShipmentBookDTO, userId: number) {
|
||||||
const order = await this.orderModel.findOneBy({ id: orderId });
|
const order = await this.orderModel.findOneBy({ id: orderId });
|
||||||
if (!order) {
|
if (!order) {
|
||||||
|
|
@ -200,12 +216,13 @@ export class LogisticsService {
|
||||||
throw new Error('订单状态不正确 ');
|
throw new Error('订单状态不正确 ');
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
const stock_point = await this.stockPointModel.findOneBy({ id: data.stockPointId});
|
||||||
const reqBody = {
|
const reqBody = {
|
||||||
sender: data.details.origin.contact_name,
|
sender: data.details.origin.contact_name,
|
||||||
start_phone: data.details.origin.phone_number,
|
start_phone: data.details.origin.phone_number,
|
||||||
start_postal_code: data.details.origin.address.postal_code.replace(/\s/g, ''),
|
start_postal_code: data.details.origin.address.postal_code.replace(/\s/g, ''),
|
||||||
pickup_address: data.details.origin.address.address_line_1,
|
pickup_address: data.details.origin.address.address_line_1,
|
||||||
pickup_warehouse: 1, // todo, 可能需要添加
|
pickup_warehouse: stock_point.upStreamStockPointId,
|
||||||
shipper_country_code: data.details.origin.address.country,
|
shipper_country_code: data.details.origin.address.country,
|
||||||
receiver: data.details.destination.contact_name,
|
receiver: data.details.destination.contact_name,
|
||||||
city: data.details.destination.address.city,
|
city: data.details.destination.address.city,
|
||||||
|
|
@ -224,6 +241,9 @@ export class LogisticsService {
|
||||||
weight_uom: `${data.details.packaging_properties.packages[0].measurements.weight.unit}S`, // todo,换成KGS和LBS
|
weight_uom: `${data.details.packaging_properties.packages[0].measurements.weight.unit}S`, // todo,换成KGS和LBS
|
||||||
currency: 'CAD',
|
currency: 'CAD',
|
||||||
}
|
}
|
||||||
|
console.log('body', reqBody);
|
||||||
|
|
||||||
|
throw new Error('test ');
|
||||||
|
|
||||||
// todo: 两个请求做异步 参考promise.all()方法
|
// todo: 两个请求做异步 参考promise.all()方法
|
||||||
// 获取预估费率
|
// 获取预估费率
|
||||||
|
|
@ -246,9 +266,17 @@ export class LogisticsService {
|
||||||
unique_id: resShipmentOrder.data.uni_order_sn,
|
unique_id: resShipmentOrder.data.uni_order_sn,
|
||||||
stockPointId: '1', // todo
|
stockPointId: '1', // todo
|
||||||
state: resShipmentOrder.data.uni_status_code,
|
state: resShipmentOrder.data.uni_status_code,
|
||||||
|
return_tracking_number: resShipmentOrder.data.tno,
|
||||||
order_id: order.id
|
order_id: order.id
|
||||||
});
|
});
|
||||||
order.shipmentId = shipment.id;
|
order.shipmentId = shipment.id;
|
||||||
|
|
||||||
|
// 同步物流信息到woocommerce
|
||||||
|
const site = this.geSite(order.siteId);
|
||||||
|
await this.wpService.createShipment(site, order.externalOrderId, {
|
||||||
|
tracking_number: shipment.primary_tracking_number,
|
||||||
|
tracking_provider: shipment?.rate?.carrier_name,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
await orderRepo.save(order);
|
await orderRepo.save(order);
|
||||||
|
|
|
||||||
|
|
@ -84,6 +84,22 @@ export class UniExpressService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getLabel(tracking_number: string) {
|
||||||
|
const body = {
|
||||||
|
packageId: tracking_number
|
||||||
|
};
|
||||||
|
const token = await this.getToken();
|
||||||
|
const config: AxiosRequestConfig= {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
},
|
||||||
|
url: `${this.url}/orders/printlabel`,
|
||||||
|
data: body
|
||||||
|
};
|
||||||
|
return await axios.request(config);
|
||||||
|
}
|
||||||
|
|
||||||
async getOrdersByDate(from: string, to: string, page: number = 1, perPage: number = 100) {
|
async getOrdersByDate(from: string, to: string, page: number = 1, perPage: number = 100) {
|
||||||
try {
|
try {
|
||||||
const token = await this.getToken();
|
const token = await this.getToken();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue