204 lines
6.1 KiB
JavaScript
204 lines
6.1 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const StoreWarehouseService = require('../../../../app/service/systems/store_warehouse');
|
|
|
|
describe('test/app/service/systems/store_warehouse.adjustPrintQuantity.test.js', () => {
|
|
/**
|
|
* 构造固定主体的测试上下文。
|
|
* @return {{capture: object, transaction: object, ctx: object}} 测试上下文与埋点数据。
|
|
*/
|
|
const buildContext = () => {
|
|
const capture = {
|
|
relationPatch: null,
|
|
changeLogPayload: null,
|
|
noticePayload: null,
|
|
};
|
|
const transaction = {
|
|
committed: false,
|
|
rolledBack: false,
|
|
async commit() {
|
|
this.committed = true;
|
|
},
|
|
async rollback() {
|
|
this.rolledBack = true;
|
|
},
|
|
};
|
|
const relation = {
|
|
id: 88,
|
|
store_id: 1000000,
|
|
warehouse_id: 100000,
|
|
print_quantity: 10,
|
|
async update(patch) {
|
|
capture.relationPatch = patch;
|
|
this.print_quantity = patch.print_quantity;
|
|
},
|
|
};
|
|
|
|
const ctx = {
|
|
app: {
|
|
config: {},
|
|
},
|
|
user: {
|
|
id: 10001,
|
|
store_id: 100000,
|
|
employees_name: '仓管A',
|
|
store_name: '测试仓',
|
|
},
|
|
model: {
|
|
transaction: async () => transaction,
|
|
Store: {
|
|
async findOne({ where }) {
|
|
if (Number(where.id) === 100000) {
|
|
return {
|
|
id: 100000,
|
|
company_name: '测试仓',
|
|
project: 'warehouse',
|
|
status: 1,
|
|
};
|
|
}
|
|
return {
|
|
id: 1000000,
|
|
company_name: '测试货主',
|
|
project: 'store',
|
|
status: 1,
|
|
};
|
|
},
|
|
},
|
|
StoreWarehouse: {
|
|
async findOne() {
|
|
return relation;
|
|
},
|
|
async findByPk(id) {
|
|
return {
|
|
get() {
|
|
return {
|
|
id,
|
|
store_id: 1000000,
|
|
warehouse_id: 100000,
|
|
auth_status: 2,
|
|
apply_reason: '',
|
|
review_comment: '',
|
|
apply_time: new Date('2026-04-07T08:00:00.000Z'),
|
|
review_time: new Date('2026-04-07T08:00:00.000Z'),
|
|
auth_start: new Date('2026-04-07T08:00:00.000Z'),
|
|
auth_end: new Date('2026-12-31T23:59:59.000Z'),
|
|
actual_end: null,
|
|
print_quantity: relation.print_quantity,
|
|
applied_by: 10001,
|
|
reviewed_by: 10001,
|
|
ended_by: null,
|
|
store_remask: '测试货主别名',
|
|
warehouse_remask: '测试仓别名',
|
|
use_status: 1,
|
|
policy_config: null,
|
|
store: {
|
|
id: 1000000,
|
|
company_name: '测试货主',
|
|
},
|
|
warehouse: {
|
|
id: 100000,
|
|
company_name: '测试仓',
|
|
},
|
|
};
|
|
},
|
|
};
|
|
},
|
|
},
|
|
StoreWarehousePrintQuantityLog: {
|
|
async create(payload) {
|
|
capture.changeLogPayload = payload;
|
|
return {
|
|
id: 556,
|
|
get() {
|
|
return {
|
|
...payload,
|
|
id: 556,
|
|
created_at: new Date('2026-04-07T09:00:00.000Z'),
|
|
};
|
|
},
|
|
};
|
|
},
|
|
},
|
|
},
|
|
service: {
|
|
systems: {
|
|
notice: {
|
|
async send(options) {
|
|
capture.noticePayload = options;
|
|
return {
|
|
success: true,
|
|
data: {
|
|
sentCount: 0,
|
|
},
|
|
};
|
|
},
|
|
},
|
|
},
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
};
|
|
|
|
ctx.app.model = ctx.model;
|
|
|
|
return {
|
|
capture,
|
|
transaction,
|
|
ctx,
|
|
};
|
|
};
|
|
|
|
it('should require reason when manually adjusting print quantity', async () => {
|
|
const { ctx } = buildContext();
|
|
const service = new StoreWarehouseService(ctx);
|
|
service.getSingleQimenConfig = async () => null;
|
|
|
|
await assert.rejects(async () => {
|
|
await service.adjustPrintQuantity({
|
|
id: 88,
|
|
change_quantity: 5,
|
|
});
|
|
}, /reason 参数不能为空/);
|
|
});
|
|
|
|
it('should overwrite target balance instead of incrementing it when manually adjusting print quantity', async () => {
|
|
const { ctx, capture, transaction } = buildContext();
|
|
const service = new StoreWarehouseService(ctx);
|
|
service.getSingleQimenConfig = async () => null;
|
|
|
|
const result = await service.adjustPrintQuantity({
|
|
id: 88,
|
|
change_quantity: 7,
|
|
reason: '人工覆盖余额测试',
|
|
business_type: 'order_express',
|
|
business_id: 321,
|
|
});
|
|
|
|
assert.equal(result.code, 200);
|
|
assert.equal(Number(result.data.contract.id), 88);
|
|
assert.equal(Number(result.data.contract.print_quantity), 7);
|
|
assert.equal(Number(result.data.change_log.change_quantity), -3);
|
|
assert.equal(Number(result.data.change_log.balance_before), 10);
|
|
assert.equal(Number(result.data.change_log.balance_after), 7);
|
|
assert.equal(String(result.data.change_log.reason || ''), '人工覆盖余额测试');
|
|
|
|
assert.deepEqual(capture.relationPatch, {
|
|
print_quantity: 7,
|
|
});
|
|
assert.equal(String(capture.changeLogPayload.change_type || ''), 'manual_adjust');
|
|
assert.equal(Number(capture.changeLogPayload.change_quantity), -3);
|
|
assert.equal(Number(capture.changeLogPayload.balance_before), 10);
|
|
assert.equal(Number(capture.changeLogPayload.balance_after), 7);
|
|
assert.equal(String(capture.changeLogPayload.reason || ''), '人工覆盖余额测试');
|
|
assert.equal(String(capture.noticePayload.event || ''), 'store_warehouse_print_quantity_adjusted');
|
|
assert.equal(String(capture.noticePayload.businessName || ''), 'store_warehouse');
|
|
assert.equal(Number(capture.noticePayload.metadata.changeQuantity), -3);
|
|
assert.equal(transaction.committed, true);
|
|
assert.equal(transaction.rolledBack, false);
|
|
});
|
|
});
|