@@ -1,5 +1,6 @@
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { CourierConfigService } from '../../integrations/courier/courier.config';
|
||||
import type { XiaofeixiaConfig } from '../../integrations/courier/courier.config';
|
||||
import { CourierApiError } from '../../integrations/courier/courier.error';
|
||||
import { CourierService } from '../../integrations/courier/courier.service';
|
||||
import { CourierPayMode } from '../../integrations/courier/courier.types';
|
||||
@@ -8,6 +9,7 @@ import type {
|
||||
CreateShipmentInput,
|
||||
ShipmentQuery,
|
||||
} from '../../integrations/courier/courier.types';
|
||||
import { FulfillmentProviderService } from '../fulfillment/fulfillment-provider.service';
|
||||
import type {
|
||||
XiaofeixiaBatchShipmentQueryDto,
|
||||
XiaofeixiaCheckCoverageDto,
|
||||
@@ -27,48 +29,61 @@ export class AdminXiaofeixiaService {
|
||||
constructor(
|
||||
private readonly courier: CourierService,
|
||||
private readonly courierConfig: CourierConfigService,
|
||||
private readonly fulfillmentProviderService: FulfillmentProviderService,
|
||||
) {}
|
||||
|
||||
getConfig() {
|
||||
const cfg = this.courierConfig.load();
|
||||
const xfx = cfg.xiaofeixia;
|
||||
async getConfig() {
|
||||
const fromDb = await this.fulfillmentProviderService.resolveDefaultXiaofeixiaConfig();
|
||||
const envCfg = this.courierConfig.load().xiaofeixia;
|
||||
const xfx = fromDb ?? envCfg;
|
||||
return {
|
||||
provider: cfg.provider,
|
||||
provider: this.courierConfig.load().provider,
|
||||
activeProvider: this.courier.activeProvider,
|
||||
source: fromDb ? 'fulfillment_provider' : 'env',
|
||||
apiUrl: xfx.apiUrl,
|
||||
appId: xfx.appId ?? null,
|
||||
mchId: xfx.mchId || null,
|
||||
mchIdMasked: xfx.mchId ? maskSecret(xfx.mchId) : null,
|
||||
hasApiKey: Boolean(xfx.apiKey),
|
||||
signType: xfx.signType,
|
||||
ready: Boolean(xfx.mchId && xfx.apiKey),
|
||||
ready: Boolean(xfx.mchId && xfx.apiKey && xfx.apiUrl),
|
||||
hint: fromDb
|
||||
? '凭证来自仓配管理中启用的小飞侠承运商'
|
||||
: '未在仓配管理配置,回退到环境变量(请迁移至仓配管理)',
|
||||
};
|
||||
}
|
||||
|
||||
async estimateFreight(dto: XiaofeixiaEstimateFreightDto) {
|
||||
return this.wrap(() => this.courier.estimateFreight(dto.weight));
|
||||
const options = await this.callOptions();
|
||||
return this.wrap(() => this.courier.estimateFreight(dto.weight, options));
|
||||
}
|
||||
|
||||
async checkCoverage(dto: XiaofeixiaCheckCoverageDto) {
|
||||
return this.wrap(() => this.courier.checkDeliveryCoverage(dto.toAddress));
|
||||
const options = await this.callOptions();
|
||||
return this.wrap(() => this.courier.checkDeliveryCoverage(dto.toAddress, options));
|
||||
}
|
||||
|
||||
async createShipment(dto: XiaofeixiaCreateShipmentDto) {
|
||||
async createShipment(dto: XiaofeixiaCreateShipmentDto, xfxOverride?: XiaofeixiaConfig) {
|
||||
const input = this.mapCreateInput(dto);
|
||||
return this.wrap(() => this.courier.createShipment(input));
|
||||
const options = xfxOverride
|
||||
? { xiaofeixia: xfxOverride }
|
||||
: await this.callOptions();
|
||||
return this.wrap(() => this.courier.createShipment(input, options));
|
||||
}
|
||||
|
||||
async cancelShipment(dto: XiaofeixiaShipmentQueryDto) {
|
||||
const query = this.mapShipmentQuery(dto);
|
||||
const options = await this.callOptions();
|
||||
return this.wrap(async () => {
|
||||
await this.courier.cancelShipment(query);
|
||||
await this.courier.cancelShipment(query, options);
|
||||
return { cancelled: true };
|
||||
});
|
||||
}
|
||||
|
||||
async getShipment(dto: XiaofeixiaShipmentQueryDto) {
|
||||
const query = this.mapShipmentQuery(dto);
|
||||
return this.wrap(() => this.courier.getShipment(query));
|
||||
const options = await this.callOptions();
|
||||
return this.wrap(() => this.courier.getShipment(query, options));
|
||||
}
|
||||
|
||||
async batchGetShipments(dto: XiaofeixiaBatchShipmentQueryDto) {
|
||||
@@ -76,12 +91,19 @@ export class AdminXiaofeixiaService {
|
||||
trackingNumbers: dto.trackingNumbers?.filter(Boolean),
|
||||
outNumbers: dto.outNumbers?.filter(Boolean),
|
||||
};
|
||||
return this.wrap(() => this.courier.batchGetShipments(query));
|
||||
const options = await this.callOptions();
|
||||
return this.wrap(() => this.courier.batchGetShipments(query, options));
|
||||
}
|
||||
|
||||
async getTrack(dto: XiaofeixiaShipmentQueryDto) {
|
||||
const query = this.mapShipmentQuery(dto);
|
||||
return this.wrap(() => this.courier.getTrack(query));
|
||||
const options = await this.callOptions();
|
||||
return this.wrap(() => this.courier.getTrack(query, options));
|
||||
}
|
||||
|
||||
private async callOptions() {
|
||||
const fromDb = await this.fulfillmentProviderService.resolveDefaultXiaofeixiaConfig();
|
||||
return fromDb ? { xiaofeixia: fromDb } : undefined;
|
||||
}
|
||||
|
||||
private mapShipmentQuery(dto: XiaofeixiaShipmentQueryDto): ShipmentQuery {
|
||||
|
||||
Reference in New Issue
Block a user