开发者需求:订单列表加一个分享链接,点开是一张送货单(商品名 / 单位 / 总数量 / 单价(销售价) / 金额)。
实现:
- 新表 public.order_share_links(token / branch_code / order_code / expires_at / revoked_at / created_by)
—— 放 public 而非租户 schema:公开接口先拿 token 才知道读哪个租户,这映射天生平台级,
且不必动 tenant_business_tables() 与新租户克隆函数
- 三个受保护接口(需 order_manage 类权限):POST/GET/DELETE /orders/:orderCode/share-link
- 公开接口 GET /api/public/delivery-note/:token(免登录、限流 60/分,投影后返回)
- 前端公开页 /d/:token(在登录守卫之外,与溯源公开页 /t/:code 同款)+ 可打印(window.print + @media print)
- 订单列表行新增「分享」(生成/复用后复制链接)与「停止分享」;列表接口用一次查询附加 share_url
- 金额大写工具 server/src/utils/amountInWords.ts(仓库此前没有)
口径(开发者 2026-10-15 裁决):
- 单价取 order_items.price(销售价),**绝不**返回 unit_cost 成本价
- **不显示「收款记录」行**(库里只有结算单级收款、没有按单收款,不编数字)
- 令牌 32 位随机、30 天过期、可撤销;**独立进货单拒绝分享**(不是给客户的货)
- 分享入口只放 Web 订单管理列表
真接口验证(本地 BR002,改后;验证后已清理:分享行 0、账号分公司还原 BR001):
- POST share-link → 32 位令牌 + public_url /d/<token> + 30 天后过期;重复调用 reused=true 同令牌
- 免登录取送货单 200:客户 财富中心 / 合计 384.00 / 叁佰捌拾肆元整 / 明细含 品名·单位·数量·单价·金额;
返回体与明细均**无成本价字段**(已断言 cost 相关键为 0)
- 散客单 → 客户名「散客录单」;独立进货单 → 400 independent_inbound_not_shareable
- DELETE 停止分享 → 公开页立即 410 share_link_revoked
- 列表接口:有有效分享的行带 share_url,其余 null
- 前端 /d/:token 路由 200;前端与后端 npx tsc --noEmit 均 0 错误
- 全量测试:后端 26 文件/33 用例失败、前端 3 文件/6 用例失败 = 与基线一致,失败文件名无新增
(新增测试 orderShareLink 16/16、PublicDeliveryNote.contract 10/10 全绿)
- 设计门禁 run-all.sh --verify:通过 8 / 未通过 1(check-ai-cost 既有红)/ 未执行 1 = 基线
新增界面文字(按仓库规则列出):分享 / 停止分享 / 分享链接已复制 / 生成分享链接失败 / 已停止分享
(公开页文案为单据本身:送货单、编号、产品、单位、总数量、单价、金额、备注、合计、订货电话、订货地址、制单人、收货人(签字))。
⚠️ 生效条件:服务端需部署才在线上生效(需开发者明确部署指令);前端需重新构建(部署流程内)。
设计:docs/design/20261015-订单送货单分享链接(免登录公开页).md
131 lines
5.8 KiB
TypeScript
131 lines
5.8 KiB
TypeScript
/**
|
||
* 订单送货单**分享链接**(免登录公开页)契约:
|
||
* ① 金额大写工具(送货单「合计金额:…元整」那行)——真单元测试,不只是源码断言
|
||
* ② 公开投影**不得**带成本价(`unit_cost` / `cost_price`),只给送货单要展示的字段
|
||
* ③ 独立进货单不对外分享;令牌 32 位随机、30 天过期、可撤销(410 有明确错误码)
|
||
* ④ 三个受保护接口 + 公开接口都挂上了;列表用**一次查询**附加分享状态
|
||
*
|
||
* 设计:docs/design/20261015-订单送货单分享链接(免登录公开页).md
|
||
*/
|
||
import { readFileSync } from 'fs';
|
||
import { describe, expect, it } from 'vitest';
|
||
import { amountInWords } from '../src/utils/amountInWords';
|
||
|
||
const serviceSrc = readFileSync(
|
||
new URL('../src/services/orderShareLinkService.ts', import.meta.url),
|
||
'utf8'
|
||
);
|
||
const publicRouteSrc = readFileSync(
|
||
new URL('../src/routes/publicDeliveryNote.ts', import.meta.url),
|
||
'utf8'
|
||
);
|
||
const indexSrc = readFileSync(new URL('../src/index.ts', import.meta.url), 'utf8');
|
||
const orderRoutesSrc = readFileSync(
|
||
new URL('../src/modules/workflow/routes/orderRoutes.ts', import.meta.url),
|
||
'utf8'
|
||
);
|
||
|
||
describe('金额中文大写', () => {
|
||
it('整数金额结尾「整」,与截图同款(948 ⇒ 玖佰肆拾捌元整)', () => {
|
||
expect(amountInWords(948)).toBe('玖佰肆拾捌元整');
|
||
expect(amountInWords(0)).toBe('零元整');
|
||
expect(amountInWords(8)).toBe('捌元整');
|
||
expect(amountInWords(100)).toBe('壹佰元整');
|
||
expect(amountInWords(1000)).toBe('壹仟元整');
|
||
});
|
||
|
||
it('角分:角为 0 有分要补零;只有角不带「整」', () => {
|
||
expect(amountInWords(948.5)).toBe('玖佰肆拾捌元伍角');
|
||
expect(amountInWords(948.05)).toBe('玖佰肆拾捌元零伍分');
|
||
expect(amountInWords(12.3)).toBe('壹拾贰元叁角');
|
||
expect(amountInWords(1234567.89)).toBe('壹佰贰拾叁万肆仟伍佰陆拾柒元捌角玖分');
|
||
});
|
||
|
||
it('零元但有小数的写法完整(0.05 / 0.5)', () => {
|
||
expect(amountInWords(0.05)).toBe('零元零伍分');
|
||
expect(amountInWords(0.5)).toBe('零元伍角');
|
||
});
|
||
|
||
it('跨节跳零各补一个「零」,不重复也不漏', () => {
|
||
expect(amountInWords(1001)).toBe('壹仟零壹元整');
|
||
expect(amountInWords(10002)).toBe('壹万零贰元整');
|
||
expect(amountInWords(100000)).toBe('壹拾万元整');
|
||
expect(amountInWords(100000001)).toBe('壹亿零壹元整');
|
||
});
|
||
|
||
it('脏数据不炸:非有限数按 0,负数带「负」', () => {
|
||
expect(amountInWords('abc')).toBe('零元整');
|
||
expect(amountInWords(Number.NaN)).toBe('零元整');
|
||
expect(amountInWords(-8)).toBe('负捌元整');
|
||
});
|
||
});
|
||
|
||
describe('公开投影只给送货单该看的字段', () => {
|
||
it('明细只取 品名/单位/数量/单价/金额/备注 —— 不含成本价', () => {
|
||
expect(serviceSrc).toContain('SELECT item_name, unit, quantity, price, amount, remark');
|
||
expect(serviceSrc).not.toContain('unit_cost,');
|
||
expect(serviceSrc).not.toContain('unit_cost AS');
|
||
expect(serviceSrc).not.toContain('cost_price');
|
||
});
|
||
|
||
it('单价明确取销售价(order_items.price)', () => {
|
||
expect(serviceSrc).toContain('const price = Number(row.price) || 0;');
|
||
});
|
||
|
||
it('返回体里带上「金额大写」与合计', () => {
|
||
expect(serviceSrc).toContain('amount_in_words: amountInWords(totalAmount)');
|
||
expect(serviceSrc).toContain('total_quantity: totalQuantity');
|
||
});
|
||
|
||
it('散客单客户名显示「散客录单」(与网页端同词)', () => {
|
||
expect(serviceSrc).toContain('RETAIL_CUSTOMER_NAME');
|
||
expect(serviceSrc).toContain('isRetailCustomer(order.customer_code)');
|
||
});
|
||
});
|
||
|
||
describe('访问控制与生命周期', () => {
|
||
it('令牌是 32 位十六进制随机值(不可枚举,不用单号)', () => {
|
||
expect(serviceSrc).toContain("crypto.randomBytes(16).toString('hex')");
|
||
expect(serviceSrc).toContain('/^[a-f0-9]{32}$/');
|
||
});
|
||
|
||
it('默认 30 天有效(开发者 2026-10-15 选定),过期/撤销给 410', () => {
|
||
expect(serviceSrc).toContain('export const SHARE_LINK_TTL_DAYS = 30;');
|
||
expect(serviceSrc).toContain("'share_link_revoked'");
|
||
expect(serviceSrc).toContain("'share_link_expired'");
|
||
expect(serviceSrc).toContain("'share_link_invalid'");
|
||
});
|
||
|
||
it('独立进货单不对外分享', () => {
|
||
expect(serviceSrc).toContain('INDEPENDENT_INBOUND_CUSTOMER_CODE');
|
||
expect(serviceSrc).toContain("'independent_inbound_not_shareable'");
|
||
});
|
||
|
||
it('重复生成复用同一令牌(不刷屏建行)', () => {
|
||
expect(serviceSrc).toContain('const existing = await findActiveShareLink(branchCode, orderCode);');
|
||
expect(serviceSrc).toContain('return toView(existing, true);');
|
||
});
|
||
});
|
||
|
||
describe('接口挂载', () => {
|
||
it('公开接口挂在 /api/public 且限流', () => {
|
||
expect(indexSrc).toContain("import publicDeliveryNoteRoutes from './routes/publicDeliveryNote';");
|
||
expect(indexSrc).toContain("app.use('/api/public', publicDeliveryNoteRoutes);");
|
||
expect(publicRouteSrc).toContain("router.get('/delivery-note/:token'");
|
||
expect(publicRouteSrc).toContain('rateLimit(');
|
||
});
|
||
|
||
it('受保护接口:生成 / 状态 / 停止分享,且要求 order_manage 类权限', () => {
|
||
expect(orderRoutesSrc).toContain("router.post('/orders/:orderCode/share-link'");
|
||
expect(orderRoutesSrc).toContain("router.get('/orders/:orderCode/share-link'");
|
||
expect(orderRoutesSrc).toContain("router.delete('/orders/:orderCode/share-link'");
|
||
expect(orderRoutesSrc).toContain('requireWorkflowPermission(ctx, [');
|
||
});
|
||
|
||
it('列表用一次查询附加分享状态,失败不影响列表', () => {
|
||
expect(orderRoutesSrc).toContain('mapActiveShareLinks(');
|
||
expect(orderRoutesSrc).toContain('row.share_url = shareMap.get(');
|
||
expect(orderRoutesSrc).toContain('[order-share] 列表附加分享状态失败');
|
||
});
|
||
});
|