来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
215 lines
7.9 KiB
TypeScript
215 lines
7.9 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import type { TenantContext } from '../src/services/tenantScope';
|
|
|
|
const { query } = vi.hoisted(() => ({
|
|
query: vi.fn(async () => ({ rows: [] })),
|
|
}));
|
|
|
|
vi.mock('../src/db', () => ({
|
|
pool: { query, connect: vi.fn() },
|
|
getRawPoolQuery: () => vi.fn(),
|
|
}));
|
|
|
|
vi.mock('../src/services/selfPurchaseStockInboundService', () => ({
|
|
healOrphanIndependentInboundsAfterPass: vi.fn(async () => undefined),
|
|
}));
|
|
|
|
vi.mock('../src/services/inspectionPhotoService', () => ({
|
|
attachCategoryPhotoInspectionRequirements: vi.fn(async (rows: unknown[]) => rows),
|
|
}));
|
|
|
|
vi.mock('../src/services/skuDirectDelivery', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('../src/services/skuDirectDelivery')>();
|
|
return {
|
|
...actual,
|
|
listSkuDirectDeliveryCustomers: vi.fn(async () => ({})),
|
|
};
|
|
});
|
|
|
|
import { listPurchaseOrdersForManagement } from '../src/services/purchaseOrderQueryService';
|
|
|
|
const purchaser: TenantContext = {
|
|
userId: '11111111-1111-1111-1111-111111111111',
|
|
roles: ['purchaser'],
|
|
permissions: ['purchase_manage'],
|
|
branchCode: 'BR001',
|
|
supplierCode: null,
|
|
purchaserCode: 'P001',
|
|
isSuperAdmin: false,
|
|
isBranchAdmin: false,
|
|
isInternalStaff: false,
|
|
isSupplierAccount: false,
|
|
};
|
|
|
|
const admin: TenantContext = {
|
|
...purchaser,
|
|
userId: '22222222-2222-2222-2222-222222222222',
|
|
roles: ['admin'],
|
|
purchaserCode: null,
|
|
isInternalStaff: true,
|
|
isBranchAdmin: true,
|
|
};
|
|
|
|
describe('listPurchaseOrdersForManagement owner scope', () => {
|
|
beforeEach(() => {
|
|
query.mockClear();
|
|
query.mockImplementation(async () => ({ rows: [] }));
|
|
});
|
|
|
|
it('purchaser SQL includes own + free-pool and SPI operator filter', async () => {
|
|
await listPurchaseOrdersForManagement(purchaser, 'BR001', {
|
|
page: 1,
|
|
pageSize: 20,
|
|
statusGroup: 'all',
|
|
dateRangePreset: 'custom',
|
|
dateFrom: '2026-09-09',
|
|
dateTo: '2026-09-09',
|
|
dateField: 'created_at',
|
|
});
|
|
const sqls = query.mock.calls.map((call) => String(call[0] || ''));
|
|
const poSql = sqls.find((sql) => sql.includes('FROM purchase_orders po'));
|
|
expect(poSql).toContain('po.purchase_status IN (10, 32)');
|
|
expect(poSql).toContain('po.purchaser_code = $');
|
|
const spiSql = sqls.find((sql) => sql.includes('FROM self_purchase_stock_inbounds spi'));
|
|
expect(spiSql).toContain('spi.operator_id::text = $');
|
|
const poParams = query.mock.calls.find((call) => String(call[0] || '').includes('FROM purchase_orders po'))?.[1] as unknown[];
|
|
expect(poParams).toContain('P001');
|
|
});
|
|
|
|
it('admin SQL does not bind purchaser_code', async () => {
|
|
await listPurchaseOrdersForManagement(admin, 'BR001', {
|
|
page: 1,
|
|
pageSize: 20,
|
|
statusGroup: 'all',
|
|
dateRangePreset: 'custom',
|
|
dateFrom: '2026-09-09',
|
|
dateTo: '2026-09-09',
|
|
dateField: 'created_at',
|
|
});
|
|
const sqls = query.mock.calls.map((call) => String(call[0] || ''));
|
|
const poSql = sqls.find((sql) => sql.includes('FROM purchase_orders po')) || '';
|
|
expect(poSql).not.toContain('po.purchaser_code = $');
|
|
const poParams = query.mock.calls.find((call) => String(call[0] || '').includes('FROM purchase_orders po'))?.[1] as unknown[];
|
|
expect(poParams || []).not.toContain('P001');
|
|
});
|
|
|
|
it('paginates after UNION ALL; statsOnly does not query list UNION', async () => {
|
|
await listPurchaseOrdersForManagement(admin, 'BR001', {
|
|
page: 1,
|
|
pageSize: 20,
|
|
statusGroup: 'all',
|
|
dateRangePreset: 'custom',
|
|
dateFrom: '2026-09-09',
|
|
dateTo: '2026-09-09',
|
|
dateField: 'created_at',
|
|
statsOnly: true,
|
|
});
|
|
const statsSqls = query.mock.calls.map((call) => String(call[0] || ''));
|
|
expect(statsSqls.some((sql) => sql.includes('UNION ALL'))).toBe(false);
|
|
expect(statsSqls.some((sql) => sql.includes('purchase_code = ANY'))).toBe(false);
|
|
|
|
query.mockClear();
|
|
await listPurchaseOrdersForManagement(admin, 'BR001', {
|
|
page: 2,
|
|
pageSize: 20,
|
|
statusGroup: 'all',
|
|
dateRangePreset: 'custom',
|
|
dateFrom: '2026-09-09',
|
|
dateTo: '2026-09-09',
|
|
dateField: 'created_at',
|
|
});
|
|
const listSqls = query.mock.calls.map((call) => String(call[0] || ''));
|
|
const unionSql = listSqls.find((sql) => sql.includes('UNION ALL')) || '';
|
|
expect(unionSql).toMatch(/LIMIT \$\d+ OFFSET \$\d+/);
|
|
expect(unionSql).toContain('NULL::uuid AS spi_id');
|
|
expect(unionSql).not.toContain('bigint');
|
|
expect(unionSql).toContain("AT TIME ZONE 'Asia/Shanghai'");
|
|
expect(unionSql).toContain('l1_sort');
|
|
expect(unionSql).toContain('DISTINCT ON (purchase_code)');
|
|
expect(unionSql).toMatch(/ORDER BY created_day ASC[\s\S]*purchase_code ASC/);
|
|
expect(unionSql).toContain('LIMIT 1');
|
|
const unionParams = query.mock.calls.find((call) => String(call[0] || '').includes('UNION ALL'))?.[1] as unknown[];
|
|
expect(unionParams?.slice(-2)).toEqual([20, 20]);
|
|
});
|
|
|
|
it('category_stats GROUP BY window without list LIMIT and ignores selected category', async () => {
|
|
await listPurchaseOrdersForManagement(admin, 'BR001', {
|
|
page: 1,
|
|
pageSize: 20,
|
|
statusGroup: 'pending',
|
|
dateRangePreset: 'custom',
|
|
dateFrom: '2026-09-19',
|
|
dateTo: '2026-09-24',
|
|
dateField: 'delivery_date',
|
|
sortDay: 'delivery_date',
|
|
category: '豆制品',
|
|
includeCategoryStats: true,
|
|
});
|
|
const sqls = query.mock.calls.map((call) => String(call[0] || ''));
|
|
const chipSql = sqls.find((sql) => sql.includes('COUNT(DISTINCT group_key)')) || '';
|
|
expect(chipSql).toContain('GROUP BY 1');
|
|
expect(chipSql).toContain('UNION ALL');
|
|
expect(chipSql).not.toMatch(/LIMIT \$\d+ OFFSET/);
|
|
expect(chipSql).not.toContain('oi.category_name');
|
|
const listUnion = sqls.find((sql) => sql.includes('DISTINCT ON (purchase_code)')) || '';
|
|
expect(listUnion).toContain("|| ' / ' ||");
|
|
const chipParams = query.mock.calls.find((call) =>
|
|
String(call[0] || '').includes('COUNT(DISTINCT group_key)')
|
|
)?.[1] as unknown[];
|
|
expect(chipParams || []).not.toContain('豆制品');
|
|
});
|
|
|
|
it('date_range=all skips date BETWEEN; empty custom still today', async () => {
|
|
await listPurchaseOrdersForManagement(admin, 'BR001', {
|
|
page: 1,
|
|
pageSize: 20,
|
|
statusGroup: 'all',
|
|
dateRangePreset: 'all',
|
|
dateField: 'delivery_date',
|
|
});
|
|
const allSqls = query.mock.calls.map((call) => String(call[0] || ''));
|
|
expect(allSqls.some((sql) => /BETWEEN/i.test(sql))).toBe(false);
|
|
|
|
query.mockClear();
|
|
await listPurchaseOrdersForManagement(admin, 'BR001', {
|
|
page: 1,
|
|
pageSize: 20,
|
|
statusGroup: 'all',
|
|
dateRangePreset: 'custom',
|
|
dateField: 'created_at',
|
|
});
|
|
const todaySqls = query.mock.calls.map((call) => String(call[0] || ''));
|
|
expect(todaySqls.some((sql) => /BETWEEN/i.test(sql))).toBe(true);
|
|
});
|
|
|
|
it('sort_day=delivery_date uses order delivery day; default stays po.created_at', async () => {
|
|
await listPurchaseOrdersForManagement(admin, 'BR001', {
|
|
page: 1,
|
|
pageSize: 20,
|
|
statusGroup: 'all',
|
|
dateRangePreset: 'custom',
|
|
dateFrom: '2026-09-21',
|
|
dateTo: '2026-09-21',
|
|
dateField: 'created_at',
|
|
});
|
|
const defaultUnion =
|
|
query.mock.calls.map((call) => String(call[0] || '')).find((sql) => sql.includes('UNION ALL')) || '';
|
|
expect(defaultUnion).toContain("(po.created_at AT TIME ZONE 'Asia/Shanghai')::date AS created_day");
|
|
expect(defaultUnion).not.toContain('o.delivery_date::date');
|
|
|
|
query.mockClear();
|
|
await listPurchaseOrdersForManagement(admin, 'BR001', {
|
|
page: 1,
|
|
pageSize: 20,
|
|
statusGroup: 'all',
|
|
dateRangePreset: 'all',
|
|
dateField: 'created_at',
|
|
sortDay: 'delivery_date',
|
|
});
|
|
const deliveryUnion =
|
|
query.mock.calls.map((call) => String(call[0] || '')).find((sql) => sql.includes('UNION ALL')) || '';
|
|
expect(deliveryUnion).toContain('o.delivery_date::date');
|
|
expect(deliveryUnion).toContain('AS created_day');
|
|
});
|
|
});
|