WMS_MAIN/test/app/service/systems/setting/expressInterface.test.js
liufumingren 99367e5043 1
2026-06-06 05:15:46 +08:00

1230 lines
40 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const { Op } = require('sequelize');
const SettingExpressInterfaceService = require('../../../../../app/service/systems/setting/expressInterface');
function buildRecord(row, calls) {
return Object.assign(row, {
async update(updates) {
calls.updatePayload = {
id: Number(this.id),
updates: {
...updates,
},
};
Object.assign(this, updates);
return this;
},
async destroy() {
calls.destroyPayload = {
id: Number(this.id),
};
return 1;
},
get() {
return { ...this };
},
});
}
function createHarness(options = {}) {
const calls = {
findAndCountAllCalls: 0,
findOneCalls: [],
findAllCalls: 0,
expressFindOneCalls: [],
printPlatformFindOneCalls: [],
printPlatformFindAllCalls: 0,
printPlatformFindAllQuery: null,
shopFindOneCalls: [],
createPayload: null,
updatePayload: null,
destroyPayload: null,
cacheWrites: [],
cacheDeletes: [],
};
const cacheStore = new Map();
const defaultRows = [
{
id: 11,
warehouse_id: 100000,
sys_express_code: 'SYS-SF',
name: '顺丰接口',
express_code: 'SF',
platform_code: 'TB',
platform_shop_id: 20001,
template_id: 50001,
brandno: 'BRAND-DEFAULT',
express_platform_id: 1,
weight_type: 2,
express: {
code: 'SF',
name: '顺丰',
polyapi_code: 'SF_API',
polyapi_extra_info: { channel: 'polyapi' },
pdd_code: 'SF-PDD',
},
express_platform: {
id: 1,
interface_url: 'https://api.example.com/express/sf',
platform_code: 'TB',
interface_name: '顺丰接口平台',
platid: 'plat-1',
appkey: 'app-key-1',
app_secret: 'secret-1',
token: 'token-1',
version: 'v1',
},
sender_name: '张三',
sender_tel: '13800000000',
weight: 10,
token: 'token-1',
viewer: '验视员A',
client_id: 'client-1',
is_share: 1,
share_id: 9001,
can_use_count: 3,
province_name: '广东省',
city_name: '深圳市',
district_name: '南山区',
site_code: 'SITE-01',
product_type: 'STD',
shop_name: '旗舰店',
address: '广东省深圳市南山区科技园',
town_name: '粤海街道',
ispickup: 1,
shipperno: 'SHIPPER-001',
template_url: 'https://example.com/template/default',
},
{
id: 12,
warehouse_id: 100000,
name: '圆通接口',
express_code: 'YTO',
platform_code: 'JD',
express_platform_id: 2,
express: {
code: 'YTO',
name: '圆通',
polyapi_code: 'YTO_API',
polyapi_extra_info: null,
pdd_code: 'YTO-PDD',
},
express_platform: {
id: 2,
interface_url: 'https://api.example.com/express/yto',
platform_code: 'JD',
interface_name: '圆通接口平台',
platid: 'plat-2',
appkey: 'app-key-2',
app_secret: 'secret-2',
token: 'token-2',
version: 'v2',
},
},
{
id: 21,
warehouse_id: 200000,
name: '其他仓库接口',
express_code: 'SF',
platform_code: 'TB',
express_platform_id: 1,
},
];
const records = (options.rows || defaultRows).map(row => ({
...row,
express: row.express ? { ...row.express } : row.express,
express_platform: row.express_platform ? { ...row.express_platform } : row.express_platform,
}));
const rawAttributes = {
id: {},
name: {},
express_code: {},
platform_code: {},
sys_express_code: {},
warehouse_id: {},
shop_id: {},
platform_shop_id: {},
template_id: {},
brandno: {},
providertype: {},
sender_name: {},
sender_tel: {},
weight: {},
token: {},
viewer: {},
client_id: {},
is_share: {},
share_id: {},
can_use_count: {},
province_name: {},
city_name: {},
district_name: {},
site_code: {},
product_type: {},
weight_type: {},
shop_name: {},
address: {},
town_name: {},
ispickup: {},
shipperno: {},
template_url: {},
ext_data: {},
};
const expressRows = options.expressRows || [
{ code: 'SF', name: '顺丰', PDD: 'SF-PDD', KS: 'SF-KS', DY: 'SF-DYXD' },
{ code: 'JD', name: '京东', PDD: 'JD-PDD', KS: 'JD-KS', DY: 'JD-DYXD' },
{ code: 'ZTO', name: '中通', PDD: 'ZTO-PDD', KS: 'ZTO-KS', DY: 'ZTO-DYXD' },
];
const shopRows = options.shopRows || [
{
id: 301,
warehouse_id: 100000,
access_token: 'shop-access-token-301',
},
{
id: 302,
warehouse_id: 100000,
access_token: null,
},
{
id: 401,
warehouse_id: 200000,
access_token: 'shop-access-token-401',
},
];
const ctx = {
user: {
store_id: options.storeId || 100000,
},
where: options.where || {},
whereParsed: options.whereParsed || null,
app: {
Sequelize: {
Op,
},
async getCacheAsync(cacheKey) {
return cacheStore.has(cacheKey) ? cacheStore.get(cacheKey) : null;
},
async setCacheAsync(cacheKey, value, ttl) {
calls.cacheWrites.push({
cacheKey,
value: Array.isArray(value)
? value.map(item => (item && typeof item === 'object' ? { ...item } : item))
: value && typeof value === 'object'
? { ...value }
: value,
ttl,
});
cacheStore.set(cacheKey, value);
},
async delCacheAsync(cacheKey) {
calls.cacheDeletes.push(cacheKey);
cacheStore.delete(cacheKey);
},
},
model: {
PrintPlatform: {
async findAll(query) {
calls.printPlatformFindAllCalls += 1;
calls.printPlatformFindAllQuery = query;
const printPlatformRows = Array.isArray(options.printPlatformRows)
? options.printPlatformRows
: [
{ id: 1, platform_code: 'CAINIAO', platform_name: '菜鸟电子面单', platid: '1001', status: 'ACTIVE', deleted: 0, remark: '默认平台', need_callback_url: 'https://callback.example.com/cainiao' },
{ id: 2, platform_code: 'PDD', platform_name: '拼多多电子面单', platid: '1002', status: 'ACTIVE', deleted: 0, remark: null, need_callback_url: null },
{ id: 3, platform_code: 'DY', platform_name: '抖音电子面单', platid: '1003', status: 'DISABLED', deleted: 0, remark: null, need_callback_url: null },
{ id: 4, platform_code: 'KS', platform_name: '快手电子面单', platid: '1004', status: 'ACTIVE', deleted: 1, remark: null, need_callback_url: null },
];
const where = query && query.where ? query.where : {};
return printPlatformRows.filter(row => {
if (where.deleted !== undefined && Number(row.deleted || 0) !== Number(where.deleted)) {
return false;
}
if (where.status !== undefined && String(row.status || '') !== String(where.status || '')) {
return false;
}
return true;
});
},
async findOne(query) {
calls.printPlatformFindOneCalls.push(query);
const where = query.where || {};
const targetCode = String(where.platform_code || '');
const printPlatformRows = Array.isArray(options.printPlatformRows)
? options.printPlatformRows
: [{ platform_code: 'TB' }, { platform_code: 'JD' }, { platform_code: 'PDD' }, { platform_code: 'WX' }];
const found = printPlatformRows.find(row => String(row.platform_code || '') === targetCode);
return found ? { ...found } : null;
},
},
Shop: {
rawAttributes: {
id: {},
warehouse_id: {},
access_token: {},
},
async findOne(query) {
calls.shopFindOneCalls.push(query);
const where = query.where || {};
const found = shopRows.find(row => (
Number(row.id) === Number(where.id)
&& Number(row.warehouse_id) === Number(where.warehouse_id)
));
return found ? {
...found,
get() {
return { ...found };
},
} : null;
},
},
Express: {
async findOne(query) {
calls.expressFindOneCalls.push(query);
const where = query.where || {};
const [ key ] = Object.keys(where);
const value = where[key];
const found = expressRows.find(row => String(row[key] || '') === String(value || ''));
return found ? { ...found } : null;
},
},
ExpressInterface: {
rawAttributes,
async findAndCountAll(query) {
calls.findAndCountAllCalls += 1;
calls.findAndCountAllQuery = query;
return {
rows: options.listRows || records,
count: options.count !== undefined ? options.count : (options.listRows || records).length,
};
},
async findOne(query) {
calls.findOneCalls.push(query);
const where = query.where || {};
const excludedId = where.id && where.id[Op.ne] ? Number(where.id[Op.ne]) : null;
const found = records.find(row => {
const sameWarehouse = Number(row.warehouse_id) === Number(where.warehouse_id);
const isExcluded = excludedId && Number(row.id) === excludedId;
if (Object.prototype.hasOwnProperty.call(where, 'id') && !Object.prototype.hasOwnProperty.call(where, 'express_code') && !Object.prototype.hasOwnProperty.call(where, 'platform_code')) {
return sameWarehouse && Number(row.id) === Number(where.id) && !isExcluded;
}
if (Object.prototype.hasOwnProperty.call(where, 'express_code')) {
const sameCode = String(row.express_code || '') === String(where.express_code || '');
if (!sameCode) {
return false;
}
}
if (Object.prototype.hasOwnProperty.call(where, 'platform_code')) {
const samePlatform = String(row.platform_code || '') === String(where.platform_code || '');
if (!samePlatform) {
return false;
}
}
if (Object.prototype.hasOwnProperty.call(where, 'express_platform_id')) {
if (Number(row.express_platform_id) !== Number(where.express_platform_id)) {
return false;
}
}
return sameWarehouse && !isExcluded;
});
return found ? buildRecord(found, calls) : null;
},
async findAll(query) {
calls.findAllCalls += 1;
calls.findAllQuery = query;
return options.listRows || records;
},
async create(payload) {
calls.createPayload = {
...payload,
};
const createdRow = {
id: options.createdId || 99,
...payload,
...(typeof options.createdRowFactory === 'function' ? options.createdRowFactory(payload) : {}),
};
records.push(createdRow);
return buildRecord(createdRow, calls);
},
},
},
};
return {
ctx,
calls,
cacheStore,
};
}
describe('test/app/service/systems/setting/expressInterface.test.js', () => {
it('should list active print platforms for express interface dropdown', async () => {
const { ctx, calls } = createHarness();
const service = new SettingExpressInterfaceService(ctx);
const result = await service.getPrintPlatformList();
assert.equal(calls.printPlatformFindAllCalls, 1);
assert.deepEqual(calls.printPlatformFindAllQuery.where, {
deleted: 0,
status: 'ACTIVE',
});
assert.deepEqual(calls.printPlatformFindAllQuery.order, [[ 'platform_name', 'ASC' ], [ 'id', 'ASC' ]]);
assert.deepEqual(result, [
{
id: 1,
code: 'CAINIAO',
name: '菜鸟电子面单',
value: 'CAINIAO',
label: '菜鸟电子面单',
platform_code: 'CAINIAO',
platform_name: '菜鸟电子面单',
platid: '1001',
status: 'ACTIVE',
remark: '默认平台',
need_callback_url: 'https://callback.example.com/cainiao',
},
{
id: 2,
code: 'PDD',
name: '拼多多电子面单',
value: 'PDD',
label: '拼多多电子面单',
platform_code: 'PDD',
platform_name: '拼多多电子面单',
platid: '1002',
status: 'ACTIVE',
remark: null,
need_callback_url: null,
},
]);
});
it('should list express interfaces with pagination and quick where', async () => {
const { ctx, calls } = createHarness({
where: {
quickWhere: '接口',
},
whereParsed: {
dynamicConditions: [],
rawConditionsByField: {
quickWhere: {
value: '接口',
},
},
},
listRows: [
{
id: 11,
warehouse_id: 100000,
name: '顺丰接口',
express_code: 'SF',
sys_express_code: 'SF',
platform_code: 'TB',
platform_shop_id: 30001,
template_id: 88001,
brandno: 'BRAND-LIST',
providertype: 'KDNIAO',
express_platform_id: 1,
express: {
code: 'SF',
name: '顺丰',
polyapi_code: 'SF_API',
polyapi_extra_info: { channel: 'polyapi' },
pdd_code: 'SF-PDD',
},
express_platform: {
id: 1,
interface_url: 'https://api.example.com/express/sf',
platform_code: 'TB',
interface_name: '顺丰接口平台',
platid: 'plat-1',
appkey: 'app-key-1',
app_secret: 'secret-1',
token: 'token-1',
version: 'v1',
},
sender_name: '张三',
sender_tel: '13800000000',
weight: 10,
token: 'token-1',
viewer: '验视员A',
client_id: 'client-1',
is_share: 1,
share_id: 9001,
can_use_count: 3,
province_name: '广东省',
city_name: '深圳市',
district_name: '南山区',
site_code: 'SITE-01',
product_type: 'STD',
shop_name: '旗舰店',
template_url: 'https://example.com/template/list',
ext_data: {
print_mode: 'A4',
enabled: true,
},
},
],
count: 1,
});
const service = new SettingExpressInterfaceService(ctx);
const result = await service.list({
page: 2,
page_size: 1,
where: {
quickWhere: '接口',
},
});
assert.equal(calls.findAndCountAllCalls, 1);
assert.equal(calls.findAndCountAllQuery.offset, 1);
assert.equal(calls.findAndCountAllQuery.limit, 1);
assert.equal(calls.findAndCountAllQuery.where[Op.and][0].warehouse_id, 100000);
assert.equal(result.code, 200);
assert.equal(result.data.total, 1);
assert.equal(result.data.page, 2);
assert.equal(result.data.page_size, 1);
assert.equal(result.data.list[0].name, '顺丰接口');
assert.equal(result.data.list[0].platform_code, 'TB');
assert.equal(result.data.list[0].platform_shop_id, 30001);
assert.equal(result.data.list[0].template_id, 88001);
assert.equal(result.data.list[0].brandno, 'BRAND-LIST');
assert.equal(result.data.list[0].providertype, 'KDNIAO');
assert.equal(result.data.list[0].sys_express_code, 'SF');
assert.equal(result.data.list[0].template_url, 'https://example.com/template/list');
assert.equal(result.data.list[0].is_system_express_code, 1);
assert.deepEqual(result.data.list[0].ext_data, {
print_mode: 'A4',
enabled: true,
});
assert.equal(Array.isArray(calls.findAndCountAllQuery.include), true);
assert.equal(calls.findAndCountAllQuery.include.length, 1);
assert.equal(result.data.list[0].express.name, '顺丰');
assert.equal(result.data.list[0].express.polyapi_code, 'SF_API');
assert.equal(result.data.list[0].express_name, '顺丰');
assert.equal(Object.prototype.hasOwnProperty.call(result.data.list[0], 'express_platform'), false);
assert.equal(Object.prototype.hasOwnProperty.call(result.data.list[0], 'express_platform_interface_url'), false);
});
it('should mark non-system express_code as 0 in list response', async () => {
const { ctx } = createHarness({
listRows: [
{
id: 11,
warehouse_id: 100000,
sys_express_code: 'SF',
name: '拼多多顺丰接口',
express_code: 'SF-PDD',
platform_code: 'PDD',
ext_data: null,
express: {
code: 'SF',
name: '顺丰',
PDD: 'SF-PDD',
},
},
],
});
const service = new SettingExpressInterfaceService(ctx);
const result = await service.list({
page: 1,
page_size: 20,
});
assert.equal(result.code, 200);
assert.equal(result.data.list[0].express_code, 'SF-PDD');
assert.equal(result.data.list[0].sys_express_code, 'SF');
assert.equal(result.data.list[0].is_system_express_code, 0);
});
it('should load full express interface list by warehouse and platform code, sort it, and write cache on miss', async () => {
const { ctx, calls } = createHarness({
listRows: [
{
id: 20,
warehouse_id: 100000,
shop_id: 301,
template_id: 1201,
brandno: 'BRAND-YTO',
providertype: 'KDNIAO',
sys_express_code: 'SYS-YTO',
name: '圆通接口',
express_code: 'YTO',
platform_code: 'TB',
express_platform_id: 2,
weight: 20,
shop: {
id: 301,
warehouse_id: 100000,
platform_code: 'TB',
shop_name: '圆通店铺',
custom_name: '圆通店铺别名',
status: 1,
auth_expire_at: null,
auth_random_code: null,
owner_id: 'owner-301',
access_token: 'shop-token-301',
refresh_token: 'shop-refresh-301',
token_expires_at: null,
refresh_token_expires_at: null,
scope: [ 'express', 'order' ],
created_at: '2026-05-24 10:00:00',
updated_at: '2026-05-24 10:00:00',
},
express: {
code: 'YTO',
name: '圆通',
polyapi_code: 'YTO_API',
polyapi_extra_info: null,
pdd_code: 'YTO-PDD',
},
},
{
id: 11,
warehouse_id: 100000,
sys_express_code: 'SYS-SF',
template_id: 1202,
brandno: 'BRAND-SF',
providertype: 'CAINIAO',
name: '顺丰接口',
express_code: 'SF',
platform_code: 'TB',
express_platform_id: 1,
weight: 10,
express: {
code: 'SF',
name: '顺丰',
polyapi_code: 'SF_API',
polyapi_extra_info: { channel: 'polyapi' },
pdd_code: 'SF-PDD',
},
},
{
id: 15,
warehouse_id: 100000,
sys_express_code: 'SYS-ZTO',
template_id: 1203,
brandno: 'BRAND-ZTO',
providertype: 'KDNIAO',
name: '中通接口',
express_code: 'ZTO',
platform_code: 'TB',
express_platform_id: 3,
weight: 10,
express: {
code: 'ZTO',
name: '中通',
polyapi_code: 'ZTO_API',
polyapi_extra_info: null,
pdd_code: 'ZTO-PDD',
},
},
],
});
const service = new SettingExpressInterfaceService(ctx);
const result = await service.listByWarehousePlatformCode({
warehouse_id: 100000,
platform_code: 'tb',
});
assert.equal(calls.findAllCalls, 1);
assert.equal(calls.findAllQuery.where.warehouse_id, 100000);
assert.equal(calls.findAllQuery.where.platform_code, 'TB');
assert.deepEqual(calls.findAllQuery.order, [[ 'weight', 'ASC' ], [ 'id', 'ASC' ]]);
assert.equal(calls.cacheWrites.length, 1);
assert.equal(calls.cacheWrites[0].cacheKey, 'express_interface:warehouse:100000:platform:TB');
assert.equal(calls.cacheWrites[0].ttl, 600);
assert.equal(result.code, 200);
assert.equal(Array.isArray(result.data), true);
assert.equal(result.data.length, 3);
assert.equal(result.data[0].express_code, 'SF');
assert.equal(result.data[0].template_id, 1202);
assert.equal(result.data[0].brandno, 'BRAND-SF');
assert.equal(result.data[0].providertype, 'CAINIAO');
assert.equal(result.data[1].express_code, 'ZTO');
assert.equal(result.data[1].brandno, 'BRAND-ZTO');
assert.equal(result.data[2].express_code, 'YTO');
assert.equal(result.data[2].brandno, 'BRAND-YTO');
assert.equal(result.data[2].shop.id, 301);
assert.equal(result.data[2].shop.shop_name, '圆通店铺');
assert.equal(result.data[0].weight, 10);
assert.equal(result.data[1].weight, 10);
assert.equal(result.data[2].weight, 20);
assert.equal(Object.prototype.hasOwnProperty.call(result.data[0], 'express_code'), true);
assert.equal(Object.prototype.hasOwnProperty.call(result.data[0], 'express_platform'), false);
assert.equal(Array.isArray(calls.cacheWrites[0].value), true);
assert.equal(calls.cacheWrites[0].value[0].express_code, 'SF');
});
it('should return cached full express interface without querying database', async () => {
const { ctx, calls, cacheStore } = createHarness();
const service = new SettingExpressInterfaceService(ctx);
const cacheKey = 'express_interface:warehouse:100000:platform:TB';
const cachedValue = [
{
id: 12,
warehouse_id: 100000,
shop_id: 302,
template_id: 33002,
brandno: 'BRAND-CACHE-YTO',
sys_express_code: 'SYS-YTO',
name: '圆通接口',
express_code: 'YTO',
platform_code: 'TB',
express_platform_id: 2,
weight: 20,
shop: {
id: 302,
warehouse_id: 100000,
platform_code: 'TB',
shop_name: '圆通店铺缓存',
custom_name: null,
status: 1,
auth_expire_at: null,
auth_random_code: null,
owner_id: null,
access_token: 'shop-token-302',
refresh_token: null,
token_expires_at: null,
refresh_token_expires_at: null,
scope: null,
created_at: '2026-05-24 10:00:00',
updated_at: '2026-05-24 10:00:00',
},
express: {
code: 'YTO',
name: '圆通',
polyapi_code: 'YTO_API',
polyapi_extra_info: null,
pdd_code: 'YTO-PDD',
},
},
{
id: 11,
warehouse_id: 100000,
sys_express_code: 'SYS-SF',
template_id: 33001,
brandno: 'BRAND-CACHE-SF',
name: '顺丰接口',
express_code: 'SF',
platform_code: 'TB',
express_platform_id: 1,
weight: 10,
express: {
code: 'SF',
name: '顺丰',
polyapi_code: 'SF_API',
polyapi_extra_info: { channel: 'polyapi' },
pdd_code: 'SF-PDD',
},
},
];
cacheStore.set(cacheKey, cachedValue);
const result = await service.listByWarehousePlatformCode({
warehouseId: 100000,
platformCode: 'tb',
});
assert.equal(calls.findAllCalls, 0);
assert.equal(calls.cacheWrites.length, 0);
assert.equal(Array.isArray(result.data), true);
assert.equal(result.data.length, 2);
assert.equal(result.data[0].express_code, 'SF');
assert.equal(result.data[0].template_id, 33001);
assert.equal(result.data[0].brandno, 'BRAND-CACHE-SF');
assert.equal(result.data[1].express_code, 'YTO');
assert.equal(result.data[1].brandno, 'BRAND-CACHE-YTO');
assert.equal(result.data[1].shop.id, 302);
assert.equal(result.data[1].shop.shop_name, '圆通店铺缓存');
assert.equal(result.data[0].express.polyapi_code, 'SF_API');
});
it('should bypass stale cache without shop field and reload from database', async () => {
const { ctx, calls, cacheStore } = createHarness({
listRows: [
{
id: 12,
warehouse_id: 100000,
shop_id: 302,
sys_express_code: 'SYS-YTO',
name: '圆通接口',
express_code: 'YTO',
platform_code: 'TB',
express_platform_id: 2,
weight: 20,
shop: {
id: 302,
warehouse_id: 100000,
platform_code: 'TB',
shop_name: '数据库圆通店铺',
custom_name: null,
status: 1,
auth_expire_at: null,
auth_random_code: null,
owner_id: null,
access_token: 'shop-token-302',
refresh_token: null,
token_expires_at: null,
refresh_token_expires_at: null,
scope: null,
created_at: '2026-05-24 10:00:00',
updated_at: '2026-05-24 10:00:00',
},
express: {
code: 'YTO',
name: '圆通',
polyapi_code: 'YTO_API',
polyapi_extra_info: null,
pdd_code: 'YTO-PDD',
},
},
],
});
const service = new SettingExpressInterfaceService(ctx);
const cacheKey = 'express_interface:warehouse:100000:platform:TB';
cacheStore.set(cacheKey, [
{
id: 12,
warehouse_id: 100000,
shop_id: 302,
template_id: 44002,
sys_express_code: 'SYS-YTO',
name: '圆通接口',
express_code: 'YTO',
platform_code: 'TB',
express_platform_id: 2,
weight: 20,
},
]);
const result = await service.listByWarehousePlatformCode({
warehouseId: 100000,
platformCode: 'tb',
});
assert.equal(calls.findAllCalls, 1);
assert.equal(calls.cacheWrites.length, 1);
assert.equal(result.data.length, 1);
assert.equal(result.data[0].shop.id, 302);
assert.equal(result.data[0].shop.shop_name, '数据库圆通店铺');
});
it('should create a new express interface and normalize codes', async () => {
const { ctx, calls } = createHarness({
createdRowFactory: payload => ({
express: {
code: payload.sys_express_code,
name: '京东快递',
polyapi_code: 'JD_API',
polyapi_extra_info: { source: 'polyapi' },
pdd_code: 'JD-PDD',
},
express_platform: {
id: payload.express_platform_id,
interface_url: 'https://api.example.com/express/jd',
platform_code: payload.platform_code,
interface_name: '京东接口平台',
platid: 'plat-3',
appkey: 'app-key-3',
app_secret: 'secret-3',
token: 'token-3',
version: 'v3',
},
}),
});
const service = new SettingExpressInterfaceService(ctx);
const result = await service.create({
name: '京东接口',
express_code: 'jd',
platform_code: 'tb',
sender_name: '李四',
sender_tel: '13900000000',
token: ' token-2 ',
is_share: 1,
can_use_count: 5,
weight_type: 2,
providerType: 'KDNIAO',
brandNo: 'BRAND-CREATE',
address: '朝阳区望京',
town_name: '望京街道',
ispickup: 1,
shipperno: 'SNO-001',
template_url: 'https://example.com/template/create',
template_id: 889900,
extData: {
retry: 2,
channel: 'polyapi',
},
});
assert.equal(calls.createPayload.warehouse_id, 100000);
assert.equal(calls.createPayload.express_code, 'JD');
assert.equal(calls.createPayload.sys_express_code, 'JD');
assert.equal(calls.createPayload.platform_code, 'TB');
assert.equal(calls.createPayload.brandno, 'BRAND-CREATE');
assert.equal(calls.createPayload.providertype, 'KDNIAO');
assert.equal(calls.printPlatformFindOneCalls.length, 1);
assert.deepEqual(calls.printPlatformFindOneCalls[0].where, {
platform_code: 'TB',
});
assert.equal(result.data.express_code, 'JD');
assert.equal(result.data.sys_express_code, 'JD');
assert.equal(result.data.platform_code, 'TB');
assert.equal(result.data.brandno, 'BRAND-CREATE');
assert.equal(result.data.providertype, 'KDNIAO');
assert.equal(result.data.sender_name, '李四');
assert.equal(result.data.weight_type, 2);
assert.equal(result.data.address, '朝阳区望京');
assert.equal(result.data.town_name, '望京街道');
assert.equal(result.data.ispickup, 1);
assert.equal(result.data.shipperno, 'SNO-001');
assert.equal(calls.createPayload.template_url, 'https://example.com/template/create');
assert.equal(result.data.template_url, 'https://example.com/template/create');
assert.equal(calls.createPayload.template_id, 889900);
assert.equal(result.data.template_id, 889900);
assert.deepEqual(calls.createPayload.ext_data, {
retry: 2,
channel: 'polyapi',
});
assert.deepEqual(result.data.ext_data, {
retry: 2,
channel: 'polyapi',
});
assert.equal(result.data.express.name, '京东快递');
assert.equal(Object.prototype.hasOwnProperty.call(result.data, 'express_platform'), false);
assert.equal(calls.cacheDeletes.includes('express_interface:warehouse:100000:platform:TB'), true);
});
it('should allow duplicate rows with same warehouse platform and express_code', async () => {
const { ctx, calls } = createHarness();
const service = new SettingExpressInterfaceService(ctx);
const result = await service.create({
name: '重复编码接口',
express_code: 'SF',
platform_code: 'TB',
weight_type: 1,
weight: 100,
});
assert.equal(result.code, 200);
assert.equal(calls.createPayload.express_code, 'SF');
assert.equal(calls.createPayload.platform_code, 'TB');
assert.equal(calls.createPayload.template_id, null);
assert.equal(calls.createPayload.weight_type, 1);
assert.equal(calls.findOneCalls.some(item => item.where && item.where.express_code === 'SF' && item.where.warehouse_id === 100000), false);
});
it('should resolve sys_express_code by platform custom code', async () => {
const { ctx, calls } = createHarness({
expressRows: [
{ code: 'SF', name: '顺丰', PDD: 'SF-PDD', KS: 'KS-SF', DY: 'DY-SF' },
],
});
const service = new SettingExpressInterfaceService(ctx);
const result = await service.create({
name: '拼多多顺丰接口',
express_code: 'sf-pdd',
platform_code: 'PDD',
});
assert.equal(result.code, 200);
assert.equal(calls.createPayload.express_code, 'SF-PDD');
assert.equal(calls.createPayload.sys_express_code, 'SF');
assert.equal(calls.expressFindOneCalls.length >= 2, true);
});
it('should update a record with boundary checks and partial fields', async () => {
const { ctx, calls } = createHarness();
const service = new SettingExpressInterfaceService(ctx);
const result = await service.update({
id: 11,
name: '顺丰接口-更新',
express_code: 'sf-pdd',
platform_code: 'pdd',
templateId: '998877',
brandNo: 'BRAND-UPDATE',
providerType: 'CAINIAO',
weight_type: 3,
address: '上海市浦东新区',
town_name: '张江镇',
ispickup: 0,
shipperno: 'SHIPPER-NEW',
template_url: 'https://example.com/template/update',
ext_data: {
print_mode: 'thermal',
},
});
assert.equal(calls.findOneCalls.length >= 1, true);
assert.equal(calls.updatePayload.id, 11);
assert.equal(calls.updatePayload.updates.name, '顺丰接口-更新');
assert.equal(calls.updatePayload.updates.express_code, 'SF-PDD');
assert.equal(calls.updatePayload.updates.platform_code, 'PDD');
assert.equal(calls.updatePayload.updates.template_id, 998877);
assert.equal(calls.updatePayload.updates.brandno, 'BRAND-UPDATE');
assert.equal(calls.updatePayload.updates.providertype, 'CAINIAO');
assert.equal(calls.printPlatformFindOneCalls.length, 1);
assert.deepEqual(calls.printPlatformFindOneCalls[0].where, {
platform_code: 'PDD',
});
assert.equal(calls.updatePayload.updates.sys_express_code, 'SF');
assert.equal(calls.updatePayload.updates.weight_type, 3);
assert.equal(calls.updatePayload.updates.address, '上海市浦东新区');
assert.equal(calls.updatePayload.updates.town_name, '张江镇');
assert.equal(calls.updatePayload.updates.ispickup, 0);
assert.equal(calls.updatePayload.updates.shipperno, 'SHIPPER-NEW');
assert.equal(calls.updatePayload.updates.template_url, 'https://example.com/template/update');
assert.deepEqual(calls.updatePayload.updates.ext_data, {
print_mode: 'thermal',
});
assert.equal(result.data.name, '顺丰接口-更新');
assert.equal(result.data.template_id, 998877);
assert.equal(result.data.brandno, 'BRAND-UPDATE');
assert.equal(result.data.providertype, 'CAINIAO');
assert.equal(result.data.template_url, 'https://example.com/template/update');
assert.deepEqual(result.data.ext_data, {
print_mode: 'thermal',
});
assert.equal(result.data.express.name, '顺丰');
assert.equal(Object.prototype.hasOwnProperty.call(result.data, 'express_platform'), false);
assert.equal(calls.cacheDeletes.includes('express_interface:warehouse:100000:platform:TB'), true);
assert.equal(calls.cacheDeletes.includes('express_interface:warehouse:100000:platform:PDD'), true);
});
it('should sync token from shop access_token when updating shop_id', async () => {
const { ctx, calls } = createHarness();
const service = new SettingExpressInterfaceService(ctx);
const result = await service.update({
id: 11,
shop_id: '301',
});
assert.equal(calls.shopFindOneCalls.length, 1);
assert.equal(calls.shopFindOneCalls[0].where.id, 301);
assert.equal(calls.shopFindOneCalls[0].where.warehouse_id, 100000);
assert.equal(calls.updatePayload.updates.shop_id, 301);
assert.equal(calls.updatePayload.updates.token, 'shop-access-token-301');
assert.equal(result.data.shop_id, 301);
});
it('should normalize empty platform_shop_id to null when updating', async () => {
const { ctx, calls } = createHarness();
const service = new SettingExpressInterfaceService(ctx);
const result = await service.update({
id: 11,
platform_shop_id: '',
});
assert.equal(calls.updatePayload.updates.platform_shop_id, null);
assert.equal(result.data.platform_shop_id, null);
});
it('should normalize numeric platform_shop_id when updating', async () => {
const { ctx, calls } = createHarness();
const service = new SettingExpressInterfaceService(ctx);
const result = await service.update({
id: 11,
platform_shop_id: '20088',
});
assert.equal(calls.updatePayload.updates.platform_shop_id, 20088);
assert.equal(result.data.platform_shop_id, 20088);
});
it('should normalize empty template_id to null when updating', async () => {
const { ctx, calls } = createHarness();
const service = new SettingExpressInterfaceService(ctx);
const result = await service.update({
id: 11,
templateId: '',
});
assert.equal(calls.updatePayload.updates.template_id, null);
assert.equal(result.data.template_id, null);
});
it('should normalize empty brandno to null when updating', async () => {
const { ctx, calls } = createHarness();
const service = new SettingExpressInterfaceService(ctx);
const result = await service.update({
id: 11,
brandNo: '',
});
assert.equal(calls.updatePayload.updates.brandno, null);
assert.equal(result.data.brandno, null);
});
it('should clear token when shop_id is cleared', async () => {
const { ctx, calls } = createHarness();
const service = new SettingExpressInterfaceService(ctx);
const result = await service.update({
id: 11,
shopId: '',
});
assert.equal(calls.shopFindOneCalls.length, 0);
assert.equal(calls.updatePayload.updates.shop_id, null);
assert.equal(calls.updatePayload.updates.token, null);
assert.equal(result.data.shop_id, null);
});
it('should clear old and new cache keys when platform code changes on update', async () => {
const { ctx, calls } = createHarness();
const service = new SettingExpressInterfaceService(ctx);
await service.update({
id: 11,
platform_code: 'wx',
});
assert.equal(calls.cacheDeletes.includes('express_interface:warehouse:100000:platform:TB'), true);
assert.equal(calls.cacheDeletes.includes('express_interface:warehouse:100000:platform:WX'), true);
});
it('should reject create when print platform code does not exist', async () => {
const { ctx } = createHarness({
printPlatformRows: [{ platform_code: 'TB' }],
});
const service = new SettingExpressInterfaceService(ctx);
await assert.rejects(async () => {
await service.create({
name: '无效平台接口',
express_code: 'SF',
platform_code: 'PDD',
});
}, /platform_code 未匹配到打印平台编码/);
});
it('should allow updating ext_data only', async () => {
const { ctx, calls } = createHarness();
const service = new SettingExpressInterfaceService(ctx);
const result = await service.update({
id: 11,
extData: {
custom: 'value',
level: 1,
},
});
assert.equal(result.code, 200);
assert.deepEqual(calls.updatePayload.updates.ext_data, {
custom: 'value',
level: 1,
});
});
it('should reject update when existing print platform code does not exist', async () => {
const { ctx, calls } = createHarness({
rows: [
{
id: 11,
warehouse_id: 100000,
name: '顺丰接口',
express_code: 'SF',
platform_code: 'INVALID',
express_platform_id: 1,
},
],
printPlatformRows: [{ platform_code: 'TB' }, { platform_code: 'PDD' }],
});
const service = new SettingExpressInterfaceService(ctx);
await assert.rejects(async () => {
await service.update({
id: 11,
name: '顺丰接口-仅改名称',
});
}, /platform_code 未匹配到打印平台编码/);
assert.equal(calls.printPlatformFindOneCalls.length, 1);
assert.deepEqual(calls.printPlatformFindOneCalls[0].where, {
platform_code: 'INVALID',
});
assert.equal(calls.updatePayload, null);
});
it('should delete a record only inside the current warehouse', async () => {
const { ctx, calls } = createHarness();
const service = new SettingExpressInterfaceService(ctx);
const result = await service.delete({
id: 11,
});
assert.equal(calls.destroyPayload.id, 11);
assert.equal(result.code, 200);
assert.equal(result.data.deleted, true);
assert.equal(result.data.warehouse_id, 100000);
assert.equal(calls.cacheDeletes.includes('express_interface:warehouse:100000:platform:TB'), true);
});
it('should reject cross-warehouse updates and deletes', async () => {
const { ctx } = createHarness({
storeId: 100000,
rows: [
{
id: 21,
warehouse_id: 200000,
name: '其他仓库接口',
express_code: 'SF',
platform_code: 'TB',
express_platform_id: 1,
},
],
});
const service = new SettingExpressInterfaceService(ctx);
await assert.rejects(async () => {
await service.update({
id: 21,
name: '越权更新',
});
}, /快递接口不存在或无权限操作/);
await assert.rejects(async () => {
await service.delete({
id: 21,
});
}, /快递接口不存在或无权限操作/);
});
});