forked from yoone/API
parent
0048d2ffec
commit
94767d5120
|
|
@ -145,6 +145,19 @@ export class LogisticsController {
|
|||
}
|
||||
}
|
||||
|
||||
@ApiOkResponse({
|
||||
type: BooleanRes,
|
||||
})
|
||||
@Post('/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 +175,7 @@ export class LogisticsController {
|
|||
}
|
||||
}
|
||||
|
||||
@ApiOkResponse(
|
||||
@ApiOkResponse(
|
||||
{type: BooleanRes}
|
||||
)
|
||||
@Post('/getShipmentFee')
|
||||
|
|
|
|||
|
|
@ -21,6 +21,10 @@ export class Shipment {
|
|||
@Expose()
|
||||
id: string;
|
||||
|
||||
@ApiProperty()
|
||||
@Column({ nullable: true })
|
||||
order_id: number;
|
||||
|
||||
@ApiProperty()
|
||||
@OneToOne(() => Order)
|
||||
@JoinColumn({ name: 'order_id' })
|
||||
|
|
@ -39,6 +43,11 @@ export class Shipment {
|
|||
@Expose()
|
||||
fee: number;
|
||||
|
||||
@ApiProperty()
|
||||
@Column({ nullable: true })
|
||||
@Expose()
|
||||
tracking_id: string;
|
||||
|
||||
@ApiProperty()
|
||||
@Column({ nullable: true })
|
||||
@Expose()
|
||||
|
|
|
|||
|
|
@ -204,6 +204,53 @@ export class LogisticsService {
|
|||
}
|
||||
}
|
||||
|
||||
async removeShipment(shipmentId) {
|
||||
try {
|
||||
const shipment:Shipment = await this.shipmentModel.findOneBy({id: shipmentId});
|
||||
const order:Order = await this.orderModel.findOneBy({id: shipment.order_id});
|
||||
// todo 同步到wooccommerce删除运单信息
|
||||
const site = await this.geSite(order.siteId);
|
||||
await this.wpService.deleteShipment(site, order.externalOrderId, shipment.tracking_id);
|
||||
|
||||
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);
|
||||
|
||||
// 同步订单状态到woocommerce
|
||||
if (order.status === OrderStatus.COMPLETED) {
|
||||
await this.wpService.updateOrder(site, order.externalOrderId, {
|
||||
status: OrderStatus.PROCESSING,
|
||||
});
|
||||
order.status = OrderStatus.PROCESSING;
|
||||
}
|
||||
order.orderStatus = ErpOrderStatus.PROCESSING;
|
||||
|
||||
await orderRepo.save(order);
|
||||
|
||||
}).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 +316,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
|
||||
}
|
||||
}
|
||||
|
||||
// 添加运单
|
||||
|
|
@ -289,30 +339,44 @@ export class LogisticsService {
|
|||
}
|
||||
const dataSource = this.dataSourceManager.getDataSource('default');
|
||||
let transactionError = undefined;
|
||||
let shipmentId = undefined;
|
||||
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,
|
||||
});
|
||||
shipmentId = shipment.id;
|
||||
}
|
||||
|
||||
await orderRepo.save(order);
|
||||
// 同步订单状态到woocommerce
|
||||
if (order.status !== OrderStatus.COMPLETED) {
|
||||
await this.wpService.updateOrder(site, order.externalOrderId, {
|
||||
status: OrderStatus.COMPLETED,
|
||||
});
|
||||
order.status = OrderStatus.COMPLETED;
|
||||
}
|
||||
order.orderStatus = ErpOrderStatus.COMPLETED;
|
||||
|
||||
await orderRepo.save(order);
|
||||
}).catch(error => {
|
||||
transactionError = error
|
||||
});
|
||||
|
|
@ -323,7 +387,7 @@ export class LogisticsService {
|
|||
}
|
||||
|
||||
return { data: {
|
||||
resShipmentOrder
|
||||
shipmentId
|
||||
} };
|
||||
} catch(error) {
|
||||
if (resShipmentOrder.status === 'SUCCESS') {
|
||||
|
|
@ -415,6 +479,7 @@ export class LogisticsService {
|
|||
}
|
||||
|
||||
async delShipment(id: string, userId: number) {
|
||||
|
||||
const shipment = await this.shipmentModel.findOneBy({ id });
|
||||
if (!shipment) throw new Error('物流不存在');
|
||||
if (shipment.type === ShipmentType.FREIGHTCOM) {
|
||||
|
|
|
|||
|
|
@ -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,26 @@ export class WPService {
|
|||
};
|
||||
return await axios.request(config);
|
||||
}
|
||||
}
|
||||
|
||||
async deleteShipment(
|
||||
site: WpSite,
|
||||
orderId: string,
|
||||
trackingId: string,
|
||||
): Promise<Boolean> {
|
||||
const { wpApiUrl, consumerKey, consumerSecret } = site;
|
||||
const auth = Buffer.from(`${consumerKey}:${consumerSecret}`).toString(
|
||||
'base64'
|
||||
);
|
||||
|
||||
console.log('del', orderId, trackingId);
|
||||
// 删除接口: 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}`,
|
||||
},
|
||||
};
|
||||
return await axios.request(config);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue