Compare commits
15 Commits
release/1.
...
v1.0.9-rc.
| Author | SHA1 | Date | |
|---|---|---|---|
| 3059e37a2e | |||
| 50384fa477 | |||
| ad74a635d4 | |||
| b9c15a7e80 | |||
| b6481e9134 | |||
| 9463ada410 | |||
| e9bee03f2e | |||
| bb1216becd | |||
| 4e3f087015 | |||
| d0605ab8f8 | |||
| 139386e8cd | |||
| 36f0521ba7 | |||
| 904cd6e457 | |||
| 55f5cebc86 | |||
| 8ea2ec726e |
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"moduleCode": "demo",
|
||||
"candidateVersion": "1.0.0-rc.1",
|
||||
"buildRevision": 1,
|
||||
"buildChecksum": "07cb8511791fc96f65d6353e47a1744e448158f2f766a4110d539806801c596c",
|
||||
"candidateVersion": "1.0.9-rc.9",
|
||||
"buildRevision": 9,
|
||||
"buildChecksum": "0f974905c1f29b5e4f9a001de3d704c7282cfd2a92fd456511d9b364c0277180",
|
||||
"schemaInputChecksum": "8a55cecb643f802ef0f39ade2f31fe8313ad437d72d12b64525c0b1a6892813d",
|
||||
"branchName": "release/1.0.0-rc.1"
|
||||
"branchName": "release/1.0.9-rc.9"
|
||||
}
|
||||
|
||||
5
bootstrap.js
vendored
5
bootstrap.js
vendored
@@ -11,10 +11,11 @@ function loadGeneratedSaasModule() {
|
||||
return require('./dist/saas/generated').GeneratedSaasModule;
|
||||
}
|
||||
|
||||
async function start() {
|
||||
await prepareSaasServiceHost({
|
||||
async function start(options = {}) {
|
||||
const session = await prepareSaasServiceHost({
|
||||
generated: loadGeneratedSaasModule(),
|
||||
});
|
||||
if (options.verifySession) await options.verifySession(session);
|
||||
requireExactReleaseEnvironment();
|
||||
return Bootstrap.configure({
|
||||
baseDir: path.join(__dirname, 'dist'),
|
||||
|
||||
12
package.json
12
package.json
@@ -10,17 +10,19 @@
|
||||
"build": "rimraf dist && mwtsc --cleanOutDir",
|
||||
"publish": "cool saas-model finalize",
|
||||
"start": "node bootstrap.js",
|
||||
"start:stable": "node start-stable.js",
|
||||
"test:stable": "node --test test/start-stable.test.cjs",
|
||||
"test:business": "node --test test/goods-business-input.test.cjs",
|
||||
"start:local": "cross-env NODE_ENV=local SAAS_RUNTIME_BOOTSTRAP_LOCAL_FALLBACK_ENABLED=true node bootstrap.js",
|
||||
"verify:packages": "node scripts/verify-local-packages.cjs",
|
||||
"start:frozen": "node start-frozen.js",
|
||||
"start:stable": "node start-stable.js",
|
||||
"test:stable": "node --test test/start-stable.test.cjs"
|
||||
"start:frozen": "node start-frozen.js"
|
||||
},
|
||||
"coolSaas": {
|
||||
"lifecycle": {
|
||||
"dev": "pnpm run build && pnpm run start:local",
|
||||
"test": "pnpm run typecheck",
|
||||
"build": "pnpm run build"
|
||||
"test": "pnpm run typecheck && pnpm run test:stable && pnpm run test:business",
|
||||
"build": "pnpm run build",
|
||||
"artifactDir": "dist"
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
|
||||
@@ -18,8 +18,8 @@ export class Goods extends SaasTableModel<GoodsRow> {
|
||||
static readonly definition = defineSaasTableModel({
|
||||
"kind": "table",
|
||||
"moduleCode": "demo",
|
||||
"revision": 1,
|
||||
"schemaVersion": 1,
|
||||
"revision": 9,
|
||||
"schemaVersion": 9,
|
||||
"schemaInputChecksum": "8a55cecb643f802ef0f39ade2f31fe8313ad437d72d12b64525c0b1a6892813d",
|
||||
"tableCode": "goods",
|
||||
"fields": [
|
||||
|
||||
@@ -4,8 +4,8 @@ import { defineGeneratedSaasModule } from '@cool-midway/module-runtime';
|
||||
export const GeneratedSaasModule = defineGeneratedSaasModule({
|
||||
"formatVersion": 2,
|
||||
"moduleCode": "demo",
|
||||
"revision": 1,
|
||||
"schemaVersion": 1,
|
||||
"revision": 9,
|
||||
"schemaVersion": 9,
|
||||
"schemaInputChecksum": "8a55cecb643f802ef0f39ade2f31fe8313ad437d72d12b64525c0b1a6892813d",
|
||||
"runtimeContractChecksum": "c1327810dd08dfd0cc1afeee99d7e9707fe14595272a3aa92ee13fc1d54aa0e5",
|
||||
"implementationManifestChecksum": "d18964a3d40e3c9a65d83996ca3141a56043986c395f5d4cac57c60aa72f4236"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"manifestVersion": 1,
|
||||
"moduleCode": "demo",
|
||||
"revision": 1,
|
||||
"revision": 9,
|
||||
"schemaChecksum": null,
|
||||
"schemaInputChecksum": "8a55cecb643f802ef0f39ade2f31fe8313ad437d72d12b64525c0b1a6892813d",
|
||||
"actionManifestChecksum": null,
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
{
|
||||
"formatVersion": 2,
|
||||
"moduleCode": "demo",
|
||||
"revision": 1,
|
||||
"revision": 9,
|
||||
"releaseVersion": null,
|
||||
"schemaVersion": 1,
|
||||
"schemaVersion": 9,
|
||||
"schemaInputChecksum": "8a55cecb643f802ef0f39ade2f31fe8313ad437d72d12b64525c0b1a6892813d",
|
||||
"schemaChecksum": null,
|
||||
"actionManifestChecksum": null,
|
||||
|
||||
@@ -44,6 +44,21 @@ export class GoodsService extends SaasCrudServiceBase<typeof Crud.Goods> {
|
||||
throw new SaasBusinessReject('STOCK_COMMAND_REQUIRED','现有商品库存只能通过受管库存命令变更');
|
||||
}
|
||||
}
|
||||
if (change.operation === 'add' || change.operation === 'update') {
|
||||
const input = change.input;
|
||||
if (Object.prototype.hasOwnProperty.call(input, 'title') && !input.title.trim()) {
|
||||
throw new SaasBusinessReject('GOODS_INPUT_INVALID', '商品名称不能为空');
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(input, 'price') && !/^(?:0|[1-9][0-9]*)(?:\.[0-9]+)?$/.test(input.price)) {
|
||||
throw new SaasBusinessReject('GOODS_INPUT_INVALID', '商品价格必须是非负金额');
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(input, 'stock') && (!Number.isSafeInteger(input.stock) || input.stock < 0 || input.stock > 2147483647)) {
|
||||
throw new SaasBusinessReject('GOODS_INPUT_INVALID', '商品库存必须是有效的非负整数');
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(input, 'status') && input.status !== 0 && input.status !== 1) {
|
||||
throw new SaasBusinessReject('GOODS_INPUT_INVALID', '商品状态仅允许下架或上架');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SaasAction(Actions.GoodsBatchInfo)
|
||||
|
||||
70
test/goods-business-input.test.cjs
Normal file
70
test/goods-business-input.test.cjs
Normal file
@@ -0,0 +1,70 @@
|
||||
'use strict';
|
||||
require('reflect-metadata');
|
||||
const assert = require('node:assert/strict');
|
||||
const test = require('node:test');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const ts = require('typescript');
|
||||
const { SaasBusinessReject } = require('@cool-midway/module-runtime');
|
||||
const sourceRoot = path.resolve(__dirname, '../src');
|
||||
const previous = require.extensions['.ts'];
|
||||
let GoodsService;
|
||||
try {
|
||||
require.extensions['.ts'] = (module, filename) => {
|
||||
assert.ok(filename.startsWith(sourceRoot + path.sep));
|
||||
const compiled = ts.transpileModule(fs.readFileSync(filename, 'utf8'), {
|
||||
fileName: filename,
|
||||
compilerOptions: { target: ts.ScriptTarget.ES2018, module: ts.ModuleKind.CommonJS,
|
||||
experimentalDecorators: true, emitDecoratorMetadata: true, esModuleInterop: true },
|
||||
});
|
||||
module._compile(compiled.outputText, filename);
|
||||
};
|
||||
({ GoodsService } = require('../src/service/goods.ts'));
|
||||
} finally {
|
||||
if (previous) require.extensions['.ts'] = previous;
|
||||
else delete require.extensions['.ts'];
|
||||
}
|
||||
function fixture(context = {}) {
|
||||
const service = new GoodsService();
|
||||
const calls = [];
|
||||
service.saasActionContext = context;
|
||||
service.inventory = { assertNoActiveReservations: async id => { calls.push(id); } };
|
||||
return { service, calls };
|
||||
}
|
||||
const rejects = code => error => error instanceof SaasBusinessReject && error.code === code;
|
||||
|
||||
test('new catalog records accept an explicit initial stock and nonnegative decimal price', async () => {
|
||||
const { service, calls } = fixture();
|
||||
await service.modifyBefore({ operation: 'add', input: { title: '验收商品', price: '12.50', stock: 20, status: 1 } });
|
||||
await service.modifyBefore({ operation: 'add', input: { title: '赠品', price: '0.00', stock: 0, status: 0 } });
|
||||
assert.deepEqual(calls, []);
|
||||
});
|
||||
|
||||
test('invalid catalog values are business rejections before persistence', async t => {
|
||||
const inputs = [{ title: ' ' }, { price: '-0.01' }, { stock: -1 }, { stock: 1.5 }, { stock: 2147483648 }, { status: 2 }];
|
||||
for (const input of inputs) await t.test(JSON.stringify(input), async () => {
|
||||
const { service } = fixture();
|
||||
await assert.rejects(service.modifyBefore({ operation: 'add', input: { title: '商品', price: '1.00', ...input } }), rejects('GOODS_INPUT_INVALID'));
|
||||
});
|
||||
});
|
||||
|
||||
test('catalog edits keep managed inventory protected even with valid stock values', async () => {
|
||||
const { service } = fixture();
|
||||
await service.modifyBefore({ operation: 'update', input: { id: '1', title: '更新名称', price: '0.00', status: 0 } });
|
||||
await assert.rejects(service.modifyBefore({ operation: 'update', input: { id: '1', stock: 2 } }), rejects('STOCK_COMMAND_REQUIRED'));
|
||||
await assert.rejects(service.modifyBefore({ operation: 'update', input: { id: '1', price: '-1.00' } }), rejects('GOODS_INPUT_INVALID'));
|
||||
});
|
||||
|
||||
test('managed stock updates retain the same domain validation', async () => {
|
||||
const { service } = fixture({ managedCrudCaller: { kind: 'business', actionName: 'goods.reserveStock' }, workflow: { coordinatorModule: 'order' } });
|
||||
await service.modifyBefore({ operation: 'update', input: { id: '1', stock: 0 } });
|
||||
await assert.rejects(service.modifyBefore({ operation: 'update', input: { id: '1', stock: -1 } }), rejects('GOODS_INPUT_INVALID'));
|
||||
});
|
||||
|
||||
test('deletion still requires the inventory reservation lock and rejects an active reservation', async () => {
|
||||
const { service, calls } = fixture();
|
||||
await service.modifyBefore({ operation: 'delete', input: { id: '1' } });
|
||||
assert.deepEqual(calls, ['1']);
|
||||
service.inventory.assertNoActiveReservations = async () => { throw new SaasBusinessReject('STOCK_RESERVATION_CONFLICT', '仍有库存预留'); };
|
||||
await assert.rejects(service.modifyBefore({ operation: 'delete', input: { id: '1' } }), rejects('STOCK_RESERVATION_CONFLICT'));
|
||||
});
|
||||
Reference in New Issue
Block a user