forked from yoone/API
1
0
Fork 0

Fix: 增加订单信息

增加了删除接口,同步了woocommerce订单状态,部分bug修复
This commit is contained in:
longbot 2025-08-09 10:41:13 +08:00
parent 0048d2ffec
commit 676aa47e6f
4 changed files with 95 additions and 12 deletions

View File

@ -145,6 +145,16 @@ export class LogisticsController {
}
}
@Del('deleteShipment/id')
async deleteShipment(@Param('id') id: number) {
try {
const res = await this.logisticsService.removeShipment(id);
return successResponse(res);
} catch (error) {
return errorResponse(error?.message || '删除运单失败' );
}
}
@ApiOkResponse({
type: BooleanRes,
})
@ -162,7 +172,7 @@ export class LogisticsController {
}
}
@ApiOkResponse(
@ApiOkResponse(
{type: BooleanRes}
)
@Post('/getShipmentFee')

View File

@ -39,6 +39,11 @@ export class Shipment {
@Expose()
fee: number;
@ApiProperty()
@Column({ nullable: true })
@Expose()
tracking_id: string;
@ApiProperty()
@Column({ nullable: true })
@Expose()

View File

@ -204,6 +204,44 @@ export class LogisticsService {
}
}
async removeShipment(shipmentId) {
try {
const shipment:Shipment = await this.shipmentModel.findOneBy({id: shipmentId});
const order = shipment.order;
// todo 同步到wooccommerce
const site = await this.geSite(order.siteId);
await this.wpService.deleteShipment(site, order.externalOrderId, shipment.tracking_id, {
tracking_number: shipment.return_tracking_number,
tracking_provider: shipment?.tracking_provider,
});
const dataSource = this.dataSourceManager.getDataSource('default');
let transactionError = undefined;
await dataSource.transaction(async manager => {
const orderRepo = manager.getRepository(Order);
const shipmentRepo = manager.getRepository(Shipment);
order.shipmentId = null;
orderRepo.save(order);
shipmentRepo.remove(shipment);
const res = await this.uniExpressService.deleteShipment(shipment.return_tracking_number);
console.log('res', res.data);
}).catch(error => {
transactionError = error;
});
if (transactionError !== undefined) {
throw new Error(`数据库同步错误: ${transactionError.message}`);
}
return true;
} catch {
throw new Error('删除运单失败');
}
}
async getShipmentFee(data: ShipmentBookDTO) {
try {
const stock_point = await this.stockPointModel.findOneBy({ id: data.stockPointId});
@ -269,13 +307,16 @@ export class LogisticsService {
receiver_phone: data.details.destination.phone_number.number,
receiver_email: data.details.destination.email_addresses,
// item_description: data.sales, // todo: 货品信息
length: data.details.packaging_properties.packages[0].measurements.cuboid.l, // todo, (只能一个包)
length: data.details.packaging_properties.packages[0].measurements.cuboid.l,
width: data.details.packaging_properties.packages[0].measurements.cuboid.w,
height: data.details.packaging_properties.packages[0].measurements.cuboid.h,
dimension_uom: data.details.packaging_properties.packages[0].measurements.cuboid.unit,
weight: data.details.packaging_properties.packages[0].measurements.weight.value,
weight_uom: data.details.packaging_properties.packages[0].measurements.weight.unit,
currency: 'CAD',
custom_field: {
'order_id': order.externalOrderId
}
}
// 添加运单
@ -292,23 +333,27 @@ export class LogisticsService {
await dataSource.transaction(async manager => {
const orderRepo = manager.getRepository(Order);
const shipmentRepo = manager.getRepository(Shipment);
const tracking_provider = 'UniUni'; // todo: id未确定后写进常数
// 同步物流信息到woocommerce
const site = await this.geSite(order.siteId);
const res = await this.wpService.createShipment(site, order.externalOrderId, {
tracking_number: resShipmentOrder.data.tno,
tracking_provider: tracking_provider,
});
if (order.orderStatus === ErpOrderStatus.COMPLETED) {
const shipment = await shipmentRepo.save({
tracking_provider: 'uniuni-express', // todo: id未确定后写进常数
tracking_provider: tracking_provider,
tracking_id: res.data.tracking_id,
unique_id: resShipmentOrder.data.uni_order_sn,
stockPointId: String(data.stockPointId), // todo
state: resShipmentOrder.data.uni_status_code,
return_tracking_number: resShipmentOrder.data.tno,
fee: data.details.shipmentFee,
order_id: orderId
order: order
});
order.shipmentId = shipment.id;
// 同步物流信息到woocommerce
const site = await this.geSite(order.siteId);
await this.wpService.createShipment(site, order.externalOrderId, {
tracking_number: shipment.return_tracking_number,
tracking_provider: shipment?.tracking_provider,
});
}
await orderRepo.save(order);

View File

@ -261,7 +261,7 @@ export class WPService {
site: WpSite,
orderId: string,
data: Record<string, any>
): Promise<Boolean> {
) {
const { wpApiUrl, consumerKey, consumerSecret } = site;
const auth = Buffer.from(`${consumerKey}:${consumerSecret}`).toString(
'base64'
@ -276,4 +276,27 @@ export class WPService {
};
return await axios.request(config);
}
async deleteShipment(
site: WpSite,
orderId: string,
trackingId: string,
data: Record<string, any>
): Promise<Boolean> {
const { wpApiUrl, consumerKey, consumerSecret } = site;
const auth = Buffer.from(`${consumerKey}:${consumerSecret}`).toString(
'base64'
);
// 删除接口: DELETE /wp-json/wc-shipment-tracking/v3/orders/<order_id>/shipment-trackings/<tracking_id>
const config: AxiosRequestConfig = {
method: 'DELETE',
url: `${wpApiUrl}/wp-json/wc-ast/v3/orders/${orderId}/shipment-trackings/${trackingId}`,
headers: {
Authorization: `Basic ${auth}`,
},
data,
};
return await axios.request(config);
}
}