'use strict'; const assert = require('node:assert/strict'); const { Op } = require('sequelize'); const SettingPlatformService = require('../../../../../app/service/systems/setting/platform'); function buildRecord(row, calls) { return { ...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, createPayload: null, updatePayload: null, destroyPayload: null, platformFindOneCalls: [], destroyCalls: 0, cacheDeletes: [], }; const platformRows = options.platformRows || [ { code: 'TB', name: '淘宝平台', platform_code: 'QM_WMS' }, { code: 'JD', name: '京东平台', platform_code: 'OPEN_API' }, { code: 'PDD', name: '拼多多平台', platform_code: 'PDD_API' }, ]; const defaultRows = options.rows || [ { id: 11, code: 'TB-STORE', name: '淘宝设置', platform_code: 'TB', warehouse_id: 100000, platform: platformRows[0], }, { id: 12, code: 'JD-STORE', name: '京东设置', platform_code: 'JD', warehouse_id: 100000, platform: platformRows[1], }, { id: 21, code: 'OTHER-STORE', name: '其他仓库设置', platform_code: 'TB', warehouse_id: 200000, platform: platformRows[0], }, ]; const ctx = { user: { store_id: options.storeId || 100000, }, where: options.where || {}, whereParsed: options.whereParsed || null, app: { Sequelize: { Op, }, async delCacheAsync(key) { calls.cacheDeletes.push(key); }, async setCacheAsync(key, value, ttl) { calls.cacheDeletes.push({ key, value, ttl, }); }, }, model: { Platform: { async findOne(query) { calls.platformFindOneCalls.push(query); const code = String(query.where && query.where.code || '').trim(); const found = platformRows.find(row => String(row.code) === code); return found ? { ...found } : null; }, }, PlatformSetting: { async findAndCountAll(query) { calls.findAndCountAllCalls += 1; calls.findAndCountAllQuery = query; return { rows: options.listRows || defaultRows, count: options.count !== undefined ? options.count : (options.listRows || defaultRows).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 = defaultRows.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, 'code') && !Object.prototype.hasOwnProperty.call(where, 'platform_code')) { return sameWarehouse && Number(row.id) === Number(where.id) && !isExcluded; } if (Object.prototype.hasOwnProperty.call(where, 'code')) { if (String(row.code || '') !== String(where.code || '')) { return false; } } if (Object.prototype.hasOwnProperty.call(where, 'platform_code')) { if (String(row.platform_code || '') !== String(where.platform_code || '')) { return false; } } return sameWarehouse && !isExcluded; }); return found ? buildRecord({ ...found }, calls) : null; }, async findAll(query) { calls.findAllCalls += 1; calls.findAllQuery = query; const where = query.where || {}; const ids = where.id && where.id[Op.in] ? where.id[Op.in].map(item => Number(item)) : []; return defaultRows .filter(row => Number(row.warehouse_id) === Number(where.warehouse_id) && ids.includes(Number(row.id))) .map(row => ({ ...row, })); }, async destroy(query) { calls.destroyCalls += 1; calls.destroyQuery = query; const where = query.where || {}; const ids = where.id && where.id[Op.in] ? where.id[Op.in].map(item => Number(item)) : []; return defaultRows.filter(row => Number(row.warehouse_id) === Number(where.warehouse_id) && ids.includes(Number(row.id))).length; }, async create(payload) { calls.createPayload = { ...payload, }; const platform = platformRows.find(row => String(row.code) === String(payload.platform_code || '')) || null; return buildRecord({ id: options.createdId || 99, ...payload, platform, }, calls); }, }, }, }; return { ctx, calls, }; } describe('test/app/service/systems/setting/platform.test.js', () => { it('should list platform settings with pagination and quick where', async () => { const { ctx, calls } = createHarness({ where: { quickWhere: '设置', }, whereParsed: { dynamicConditions: [], rawConditionsByField: { quickWhere: { value: '设置', }, }, }, listRows: [ { id: 11, code: 'TB-STORE', name: '淘宝设置', platform_code: 'TB', warehouse_id: 100000, platform: { code: 'TB', name: '淘宝平台', platform_code: 'QM_WMS', }, }, ], count: 1, }); const service = new SettingPlatformService(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.deepEqual(calls.findAndCountAllQuery.include[0].attributes, [ 'code', 'name' ]); 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].code, 'TB-STORE'); assert.equal(result.data.list[0].platform_code, 'TB'); assert.equal(result.data.list[0].platform_name, '淘宝平台'); assert.equal(result.data.list[0].platform_label, 'TB-STORE (淘宝平台)'); assert.equal(result.data.list[0].platform.platform_code, 'TB'); }); it('should create a new platform setting and ignore external warehouse id', async () => { const { ctx, calls } = createHarness(); const service = new SettingPlatformService(ctx); const result = await service.create({ code: 'PDD-STORE', name: '淘宝设置', platform_code: 'PDD', warehouse_id: 999999, }); assert.equal(calls.createPayload.warehouse_id, 100000); assert.equal(calls.createPayload.code, 'PDD-STORE'); assert.equal(calls.createPayload.name, '淘宝设置'); assert.equal(calls.createPayload.platform_code, 'PDD'); assert.deepEqual(calls.platformFindOneCalls[0].attributes, [ 'code', 'name' ]); assert.equal(result.data.code, 'PDD-STORE'); assert.equal(result.data.platform_code, 'PDD'); assert.equal(result.data.platform_name, '拼多多平台'); assert.equal(result.data.platform_label, 'PDD-STORE (拼多多平台)'); }); it('should reject create when platform code is missing in the system platform table', async () => { const { ctx } = createHarness(); const service = new SettingPlatformService(ctx); await assert.rejects(async () => { await service.create({ code: 'TB-STORE', name: '淘宝设置', platform_code: 'UNKNOWN', }); }, /platform_code 不存在或不支持/); }); it('should reject create when code or platform code conflicts in the current warehouse', async () => { const { ctx } = createHarness(); const service = new SettingPlatformService(ctx); await assert.rejects(async () => { await service.create({ code: 'TB-STORE', name: '重复设置', platform_code: 'JD', }); }, /code 已被当前仓库使用/); await assert.rejects(async () => { await service.create({ code: 'NEW-CODE', name: '重复平台', platform_code: 'TB', }); }, /platform_code 已被当前仓库使用/); }); it('should update a record with boundary checks and return platform display fields', async () => { const { ctx, calls } = createHarness(); const service = new SettingPlatformService(ctx); const result = await service.update({ id: 11, code: 'TB-STORE-NEW', name: '淘宝设置-更新', platform_code: 'PDD', }); assert.equal(calls.findOneCalls.length >= 1, true); assert.equal(calls.updatePayload.id, 11); assert.equal(calls.updatePayload.updates.code, 'TB-STORE-NEW'); assert.equal(calls.updatePayload.updates.name, '淘宝设置-更新'); assert.equal(calls.updatePayload.updates.platform_code, 'PDD'); assert.equal(result.data.code, 'TB-STORE-NEW'); assert.equal(result.data.platform_code, 'PDD'); assert.equal(result.data.platform_name, '拼多多平台'); }); it('should delete a record only inside the current warehouse', async () => { const { ctx, calls } = createHarness(); const service = new SettingPlatformService(ctx); const result = await service.delete({ ids: [ 11 ], }); assert.equal(calls.findAllCalls, 1); assert.equal(calls.destroyCalls, 1); assert.deepEqual(calls.destroyQuery.where.id[Op.in], [ 11 ]); assert.equal(result.code, 200); assert.equal(result.data.deleted, true); assert.equal(result.data.warehouse_id, 100000); assert.equal(result.data.id, 11); assert.deepEqual(result.data.ids, [ 11 ]); assert.deepEqual(calls.cacheDeletes.sort(), [ 'platform:list:100000', 'platform:TB-STORE:100000', 'platform_setting_detail:TB-STORE:100000', ].sort()); }); it('should reject cross-warehouse updates and deletes', async () => { const { ctx } = createHarness({ storeId: 100000, rows: [ { id: 21, code: 'OTHER-STORE', name: '其他仓库设置', platform_code: 'TB', warehouse_id: 200000, platform: { code: 'TB', name: '淘宝平台', platform_code: 'QM_WMS', }, }, ], }); const service = new SettingPlatformService(ctx); await assert.rejects(async () => { await service.update({ id: 21, name: '越权更新', }); }, /平台设置不存在或无权限操作/); await assert.rejects(async () => { await service.delete({ ids: [ 21 ], }); }, /平台设置不存在或无权限操作/); }); it('should reject batch delete when any id is outside the current warehouse', async () => { const { ctx, calls } = createHarness(); const service = new SettingPlatformService(ctx); await assert.rejects(async () => { await service.delete({ ids: [ 11, 21 ], }); }, /平台设置不存在或无权限操作/); assert.equal(calls.destroyCalls, 0); assert.deepEqual(calls.cacheDeletes, []); }); });