feat(主系统事件): 按定稿答复调整——每个 supplier_code 各拉一遍+游标/去重按(分公司,供应商)、首轮不传 since 并做初态全量对账、cursor 400 重置、批量重推建议≤100/批
This commit is contained in:
@@ -45,14 +45,20 @@
|
||||
测试:`server/tests/mainSystemEvents.test.ts` **16 passed**(含"未知事件早退"反向验证:去掉即变红);
|
||||
前端全量 1420 项 / 1416 passed(4 条既有红灯与本改无关)。
|
||||
|
||||
## 5. 待主系统联调确认(我方需要问清)
|
||||
## 5. 主系统定稿答复(2026-10-24)→ 我方已按答复调整
|
||||
|
||||
1. **事件接口鉴权与作用域**:是否复用既有 `client_credentials` 的 `supplier_code`?我方按"该分公司第一条生效绑定"的 supplier_code 调用;若一码多绑(A/B/C),是否需要对每个 supplier_code 各拉一遍(事件里已带 `supplier_code` 字段)。
|
||||
2. **`since` 与 `cursor` 的优先关系**:我方现在"有 cursor 就不传 since";请确认带 cursor 时对方会忽略 since。
|
||||
3. **`server_time` 时区/格式**:我方按 ISO-8601 解析(`created_at` 同)。
|
||||
4. **首次接入**:我方第一轮用 `now-2min` 作 since,收到响应后用其 `server_time` 作为后续基准;请确认这样不会漏事件(或不希望我方向前回看)。
|
||||
5. **`binding.changed` 的重投与批量**:我方已支持 `bindings:[…]` 批量与逐条独立处理;请确认全量重推是否也走同一 webhook。
|
||||
6. **联调环境**:双方都只在本地/测试环境联调(我方用 dev `tenant_*` schema + 假 fetch 已经能跑通链路),待两边测通后再约发布。
|
||||
| 答复 | 我方改动 | 验证 |
|
||||
|---|---|---|
|
||||
| ① 事件按**每个 supplier_code 各拉一遍**(一码多绑不能取第一条绑定,否则漏其他分公司事件) | `consumeMainSystemEventsForBranch()` 改为遍历该分公司**全部可出站绑定**的 supplier_code(`listSupplierCodesForBranch`);游标/去重键改为 **(branch_code, main_supplier_code)**(迁移 `20261079`) | 真库:造 2 条临时绑定 → `suppliers=3`(含真实 1 条),游标行按供应商各一条 |
|
||||
| ② 有 cursor 时忽略 since;**cursor 非法返回 400** | 客户端新增 `invalid_cursor`(HTTP 400)分支;消费者收到后**重置游标**、标 `needs_full_reconcile`、下轮从默认窗口重建(不猜、不重放历史) | 假 400 响应 → `{"kind":"invalid_cursor",...}` ✅ |
|
||||
| ③ `server_time/created_at` 统一 ISO-8601 UTC 带毫秒 `Z`;`since` 入参需带时区 | 我方 `bootstrapSince()` 用 `toISOString()`(毫秒 + Z),`since` 原样透传,不做本地时区换算 | 请求串里 `since=...T..:..:...000Z` ✅ |
|
||||
| ④ **首轮不传 since/cursor**(服务端默认最近 5 分钟并返回 `server_time`),同时做一次完整 active 全量对账做初态,之后只用 cursor | 首轮判定 `!stored.cursor && !stored.lastEventAt` ⇒ `since=null`、无 cursor;首轮成功后 `initialFullReconcile=true` 并立即跑一次 `followMainSystemPrepChanges` 做初态 | 真库:首轮请求仅 `?limit=500`(无 since/cursor),汇总 `initialFullReconcile:true` ✅ |
|
||||
| ⑤ `binding.changed` 全量重推:发送端目前逐条 POST(生产已成功 10 条);接收端支持 `bindings:[…]` 批量;问我们想要哪种与批量上限 | **我方答复:批量体优先,建议每批 ≤100 条**(我方接收端逐条独立处理、单条失败不拖累其余,且已支持批量;批量能显著减少对方请求数)。逐条推也能收 ✅ | 接收端代码:`isBindingChangedEvent` + `parseBindingChangePayload`(支持 `bindings:[…]`) |
|
||||
|
||||
## 6. 待主系统联调确认(已由定稿答复解决 ①②③④,剩余)
|
||||
|
||||
1. **联调环境**:双方都只在本地/测试环境联调(我方用 dev `tenant_*` schema + 假 fetch 已跑通链路),待两边测通后再约发布。
|
||||
2. **全量重推批量上限确认**:我方建议每批 ≤100 条;若主系统管理端触发可按此分批。
|
||||
|
||||
## 6. 未做 / 风险
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
-- 事件游标/去重改为**按 (分公司 × 主系统供应商编码)** 独立(2026-10-24 主系统定稿答复①):
|
||||
-- 「一码多绑(A/B/C 各自 supplier_code)时请按每个 supplier_code 各拉一遍事件,不要取第一条绑定,否则会漏其他分公司事件」
|
||||
-- 因此游标与去重键都必须是 (branch_code, main_supplier_code),不能只用 branch_code。
|
||||
|
||||
-- ① 游标表:加列 + 迁移主键
|
||||
ALTER TABLE public.main_system_event_cursors
|
||||
ADD COLUMN IF NOT EXISTS main_supplier_code VARCHAR(40) NOT NULL DEFAULT '';
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
pk_name text;
|
||||
BEGIN
|
||||
IF to_regclass('public.main_system_event_cursors') IS NULL THEN RETURN; END IF;
|
||||
SELECT conname INTO pk_name
|
||||
FROM pg_constraint
|
||||
WHERE conrelid = 'public.main_system_event_cursors'::regclass AND contype = 'p'
|
||||
LIMIT 1;
|
||||
IF pk_name IS NOT NULL THEN
|
||||
EXECUTE format('ALTER TABLE public.main_system_event_cursors DROP CONSTRAINT %I', pk_name);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- 回填:历史行(若有)没有 supplier_code 时,尽量用该分公司当前绑定补上;补不上的留空(消费时会自动跳过并重建)
|
||||
UPDATE public.main_system_event_cursors c
|
||||
SET main_supplier_code = COALESCE((
|
||||
SELECT b.main_supplier_code FROM public.main_supplier_bindings b
|
||||
WHERE b.branch_code = c.branch_code AND b.status = 'active'
|
||||
ORDER BY b.main_supplier_code LIMIT 1
|
||||
), '')
|
||||
WHERE c.main_supplier_code = '';
|
||||
|
||||
ALTER TABLE public.main_system_event_cursors
|
||||
ADD CONSTRAINT main_system_event_cursors_pkey PRIMARY KEY (branch_code, main_supplier_code);
|
||||
|
||||
-- ② 去重表:加列 + 迁移主键
|
||||
ALTER TABLE public.main_system_events_seen
|
||||
ADD COLUMN IF NOT EXISTS main_supplier_code VARCHAR(40) NOT NULL DEFAULT '';
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
pk_name text;
|
||||
BEGIN
|
||||
IF to_regclass('public.main_system_events_seen') IS NULL THEN RETURN; END IF;
|
||||
SELECT conname INTO pk_name
|
||||
FROM pg_constraint
|
||||
WHERE conrelid = 'public.main_system_events_seen'::regclass AND contype = 'p'
|
||||
LIMIT 1;
|
||||
IF pk_name IS NOT NULL THEN
|
||||
EXECUTE format('ALTER TABLE public.main_system_events_seen DROP CONSTRAINT %I', pk_name);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
ALTER TABLE public.main_system_events_seen
|
||||
ADD CONSTRAINT main_system_events_seen_pkey PRIMARY KEY (branch_code, main_supplier_code, event_id);
|
||||
@@ -37,6 +37,8 @@ export type SupplierEventsOutcome =
|
||||
clamped: boolean;
|
||||
}
|
||||
| { kind: 'not_configured'; reason: string }
|
||||
/** 游标非法(主系统定稿答复②:cursor 非法返回 400)⇒ 调用方应重置游标、下轮从默认窗口重建 */
|
||||
| { kind: 'invalid_cursor'; reason: string; serverTime: string | null }
|
||||
| { kind: 'rejected' | 'unavailable'; reason: string };
|
||||
|
||||
function text(value: unknown): string {
|
||||
@@ -86,6 +88,14 @@ export async function fetchSupplierEvents(input: {
|
||||
fetchImpl: input.fetchImpl,
|
||||
});
|
||||
if (result.kind === 'not_configured') return { kind: 'not_configured', reason: result.reason };
|
||||
if (result.kind !== 'ok' && Number(result.status) === 400) {
|
||||
const payload = (result.payload || {}) as Record<string, unknown>;
|
||||
return {
|
||||
kind: 'invalid_cursor',
|
||||
reason: text(payload.error_description) || text(payload.error) || 'cursor_invalid',
|
||||
serverTime: text(payload.server_time) || null,
|
||||
};
|
||||
}
|
||||
if (result.kind !== 'ok') return { kind: result.kind, reason: result.reason };
|
||||
|
||||
const payload = (result.data || {}) as Record<string, unknown>;
|
||||
|
||||
@@ -18,10 +18,14 @@ import { pool } from '../db';
|
||||
import { runWithTenantDbContext } from './tenantDbContext';
|
||||
import { getMainSystemToken } from './mainSystem/token';
|
||||
import { fetchSupplierEvents, type MainSystemEvent } from './mainSystem/api/events';
|
||||
import { listConnectableBindingsForBranch } from './branchAccessCode';
|
||||
import { listSupplierCodesForBranch } from './mainSystemBranchApi';
|
||||
|
||||
export type EventConsumeSummary = {
|
||||
branchCode: string;
|
||||
/** 本轮按几个 supplier_code 各拉了一遍(一码多绑时 >1) */
|
||||
suppliers: number;
|
||||
/** 首轮建游标后是否做了一次完整 active 全量对账(答复④) */
|
||||
initialFullReconcile?: boolean;
|
||||
pages: number;
|
||||
fetched: number;
|
||||
applied: number;
|
||||
@@ -44,10 +48,14 @@ function bootstrapSince(): string {
|
||||
return new Date(Date.now() - 2 * 60 * 1000).toISOString();
|
||||
}
|
||||
|
||||
async function loadCursor(branchCode: string): Promise<{ cursor: string | null; lastEventAt: string | null }> {
|
||||
async function loadCursor(
|
||||
branchCode: string,
|
||||
supplierCode: string
|
||||
): Promise<{ cursor: string | null; lastEventAt: string | null }> {
|
||||
const res = await pool.query<{ cursor: string | null; last_event_at: string | null }>(
|
||||
`SELECT cursor, last_event_at FROM public.main_system_event_cursors WHERE branch_code = $1 LIMIT 1`,
|
||||
[branchCode]
|
||||
`SELECT cursor, last_event_at FROM public.main_system_event_cursors
|
||||
WHERE branch_code = $1 AND main_supplier_code = $2 LIMIT 1`,
|
||||
[branchCode, supplierCode]
|
||||
);
|
||||
const row = res.rows[0];
|
||||
return {
|
||||
@@ -58,6 +66,7 @@ async function loadCursor(branchCode: string): Promise<{ cursor: string | null;
|
||||
|
||||
async function saveCursor(input: {
|
||||
branchCode: string;
|
||||
supplierCode: string;
|
||||
cursor: string | null;
|
||||
lastEventAt: string | null;
|
||||
serverTime: string | null;
|
||||
@@ -65,30 +74,40 @@ async function saveCursor(input: {
|
||||
}): Promise<void> {
|
||||
await pool.query(
|
||||
`INSERT INTO public.main_system_event_cursors
|
||||
(branch_code, cursor, last_event_at, server_time, needs_full_reconcile, updated_at)
|
||||
VALUES ($1, $2, $3, $4, COALESCE($5, FALSE), NOW())
|
||||
ON CONFLICT (branch_code) DO UPDATE SET
|
||||
(branch_code, main_supplier_code, cursor, last_event_at, server_time, needs_full_reconcile, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, COALESCE($6, FALSE), NOW())
|
||||
ON CONFLICT (branch_code, main_supplier_code) DO UPDATE SET
|
||||
cursor = EXCLUDED.cursor,
|
||||
last_event_at = COALESCE(EXCLUDED.last_event_at, public.main_system_event_cursors.last_event_at),
|
||||
server_time = EXCLUDED.server_time,
|
||||
needs_full_reconcile = COALESCE($5, FALSE) OR public.main_system_event_cursors.needs_full_reconcile,
|
||||
needs_full_reconcile = COALESCE($6, FALSE) OR public.main_system_event_cursors.needs_full_reconcile,
|
||||
updated_at = NOW()`,
|
||||
[input.branchCode, input.cursor, input.lastEventAt, input.serverTime, input.needsFullReconcile ?? null]
|
||||
[
|
||||
input.branchCode,
|
||||
input.supplierCode,
|
||||
input.cursor,
|
||||
input.lastEventAt,
|
||||
input.serverTime,
|
||||
input.needsFullReconcile ?? null,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/** 事件去重:返回 false 表示之前已处理过(游标翻页/失败重投可能重复) */
|
||||
async function markEventSeen(input: {
|
||||
branchCode: string;
|
||||
supplierCode: string;
|
||||
event: MainSystemEvent;
|
||||
action: string;
|
||||
}): Promise<boolean> {
|
||||
const res = await pool.query(
|
||||
`INSERT INTO public.main_system_events_seen (branch_code, event_id, event, order_code, created_at, action)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
ON CONFLICT (branch_code, event_id) DO NOTHING`,
|
||||
`INSERT INTO public.main_system_events_seen
|
||||
(branch_code, main_supplier_code, event_id, event, order_code, created_at, action)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
ON CONFLICT (branch_code, main_supplier_code, event_id) DO NOTHING`,
|
||||
[
|
||||
input.branchCode,
|
||||
input.supplierCode,
|
||||
input.event.id,
|
||||
input.event.event || null,
|
||||
input.event.orderCode,
|
||||
@@ -99,14 +118,9 @@ async function markEventSeen(input: {
|
||||
return (res.rowCount || 0) > 0;
|
||||
}
|
||||
|
||||
/** 该分公司可用的主系统供应商编码(取第一个;事件按 supplier_code 圈定,绑定变了会重建游标) */
|
||||
async function resolveSupplierCode(branchCode: string): Promise<string | null> {
|
||||
const bindings = await listConnectableBindingsForBranch(branchCode);
|
||||
return trim(bindings[0]?.mainSupplierCode) || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 消费某分公司的事件(翻页到 has_more=false 或达本轮上限)。
|
||||
* 消费某分公司的事件:**对每个生效绑定的 supplier_code 各拉一遍**(主系统定稿答复①:
|
||||
* 一码多绑 A/B/C 时不要取第一条绑定,否则会漏其他分公司事件)。
|
||||
*/
|
||||
export async function consumeMainSystemEventsForBranch(input: {
|
||||
branchCode: string;
|
||||
@@ -115,8 +129,9 @@ export async function consumeMainSystemEventsForBranch(input: {
|
||||
maxPages?: number;
|
||||
}): Promise<EventConsumeSummary> {
|
||||
const branchCode = trim(input.branchCode);
|
||||
const summary: EventConsumeSummary = {
|
||||
const empty = (): EventConsumeSummary => ({
|
||||
branchCode,
|
||||
suppliers: 0,
|
||||
pages: 0,
|
||||
fetched: 0,
|
||||
applied: 0,
|
||||
@@ -125,19 +140,90 @@ export async function consumeMainSystemEventsForBranch(input: {
|
||||
triggeredReconcile: 0,
|
||||
ignored: 0,
|
||||
clamped: false,
|
||||
};
|
||||
if (!branchCode) return { ...summary, reason: '缺少分公司编码' };
|
||||
});
|
||||
if (!branchCode) return { ...empty(), reason: '缺少分公司编码' };
|
||||
|
||||
const supplierCode = await resolveSupplierCode(branchCode);
|
||||
if (!supplierCode) return { ...summary, reason: '无生效绑定(不消费事件)' };
|
||||
const supplierCodes = await listSupplierCodesForBranch(branchCode);
|
||||
if (!supplierCodes.length) return { ...empty(), reason: '无生效绑定(不消费事件)' };
|
||||
|
||||
const summary = empty();
|
||||
summary.suppliers = supplierCodes.length;
|
||||
let needFullReconcile = false;
|
||||
for (const supplierCode of supplierCodes) {
|
||||
const part = await consumeForSupplier({
|
||||
branchCode,
|
||||
supplierCode,
|
||||
fetchImpl: input.fetchImpl,
|
||||
maxPages: input.maxPages,
|
||||
});
|
||||
summary.pages += part.pages;
|
||||
summary.fetched += part.fetched;
|
||||
summary.applied += part.applied;
|
||||
summary.duplicated += part.duplicated;
|
||||
summary.bindingSync += part.bindingSync;
|
||||
summary.triggeredReconcile += part.triggeredReconcile;
|
||||
summary.ignored += part.ignored;
|
||||
summary.clamped = summary.clamped || part.clamped;
|
||||
if (part.reason && !summary.reason) summary.reason = `${supplierCode}: ${part.reason}`;
|
||||
if (part.needsFullReconcile) needFullReconcile = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 主系统定稿答复④:**首轮**(没有游标)由服务端默认窗口 + `server_time` 起步,
|
||||
* 同时做一次**完整 active 全量对账**做初态。这里只在"确实首轮建了游标"时跑一次(幂等)。
|
||||
*/
|
||||
if (needFullReconcile) {
|
||||
try {
|
||||
await runWithTenantDbContext(branchCode, async () => {
|
||||
const { followMainSystemPrepChanges } = await import('./mainSystemReturnFollow');
|
||||
await followMainSystemPrepChanges(branchCode);
|
||||
});
|
||||
summary.initialFullReconcile = true;
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
`[main-system-events] ${branchCode} 首轮初态全量对账失败(下轮兜底会补):`,
|
||||
(error as Error)?.message || error
|
||||
);
|
||||
}
|
||||
}
|
||||
return summary;
|
||||
}
|
||||
|
||||
/** 单个 supplier_code 的事件消费(游标/去重都按 (分公司, 供应商) 隔离) */
|
||||
async function consumeForSupplier(input: {
|
||||
branchCode: string;
|
||||
supplierCode: string;
|
||||
fetchImpl?: typeof fetch;
|
||||
maxPages?: number;
|
||||
}): Promise<EventConsumeSummary & { needsFullReconcile: boolean }> {
|
||||
const { branchCode, supplierCode } = input;
|
||||
const summary: EventConsumeSummary & { needsFullReconcile: boolean } = {
|
||||
branchCode,
|
||||
suppliers: 1,
|
||||
pages: 0,
|
||||
fetched: 0,
|
||||
applied: 0,
|
||||
duplicated: 0,
|
||||
bindingSync: 0,
|
||||
triggeredReconcile: 0,
|
||||
ignored: 0,
|
||||
clamped: false,
|
||||
needsFullReconcile: false,
|
||||
};
|
||||
|
||||
const tokenResult = await getMainSystemToken(supplierCode);
|
||||
if (tokenResult.kind !== 'ok') return { ...summary, reason: tokenResult.reason };
|
||||
|
||||
const stored = await loadCursor(branchCode);
|
||||
const stored = await loadCursor(branchCode, supplierCode);
|
||||
const firstRun = !stored.cursor && !stored.lastEventAt;
|
||||
let cursor = stored.cursor;
|
||||
let since = stored.lastEventAt || bootstrapSince();
|
||||
/**
|
||||
* 主系统定稿答复④:首轮**不传 since/cursor**(服务端默认最近 5 分钟并给 `server_time`),
|
||||
* 之后只用 cursor 推进(答复②:带 cursor 时忽略 since)。
|
||||
*/
|
||||
let since = firstRun ? null : stored.lastEventAt || bootstrapSince();
|
||||
const maxPages = Math.min(Math.max(Number(input.maxPages) || MAX_PAGES_PER_RUN, 1), 50);
|
||||
let progressed = false;
|
||||
|
||||
for (let page = 0; page < maxPages; page += 1) {
|
||||
const res = await fetchSupplierEvents({
|
||||
@@ -148,16 +234,32 @@ export async function consumeMainSystemEventsForBranch(input: {
|
||||
limit: 500,
|
||||
fetchImpl: input.fetchImpl,
|
||||
});
|
||||
if (res.kind === 'invalid_cursor') {
|
||||
// 答复②:cursor 非法返回 400 ⇒ 丢弃游标、下一轮从默认窗口重建(不猜、不重放历史)
|
||||
console.warn(`[main-system-events] ${branchCode}/${supplierCode} 游标失效(400),已重置游标`);
|
||||
await saveCursor({
|
||||
branchCode,
|
||||
supplierCode,
|
||||
cursor: null,
|
||||
lastEventAt: res.serverTime || null,
|
||||
serverTime: res.serverTime || null,
|
||||
needsFullReconcile: true,
|
||||
});
|
||||
summary.needsFullReconcile = true;
|
||||
return { ...summary, reason: 'cursor_invalid_reset' };
|
||||
}
|
||||
if (res.kind !== 'ok') return { ...summary, reason: res.reason };
|
||||
summary.pages += 1;
|
||||
summary.clamped = summary.clamped || res.clamped;
|
||||
progressed = true;
|
||||
|
||||
const lastEventAt = res.events.length ? res.events[res.events.length - 1].createdAt : stored.lastEventAt;
|
||||
// 每页都落游标:中途失败也不会把已处理的页重放一遍
|
||||
await saveCursor({
|
||||
branchCode,
|
||||
supplierCode,
|
||||
cursor: res.nextCursor,
|
||||
lastEventAt: lastEventAt || null,
|
||||
lastEventAt: lastEventAt || res.serverTime || null,
|
||||
serverTime: res.serverTime,
|
||||
needsFullReconcile: res.clamped,
|
||||
});
|
||||
@@ -166,7 +268,7 @@ export async function consumeMainSystemEventsForBranch(input: {
|
||||
|
||||
for (const event of res.events) {
|
||||
const action = classifyEvent(event);
|
||||
const fresh = await markEventSeen({ branchCode, event, action });
|
||||
const fresh = await markEventSeen({ branchCode, supplierCode, event, action });
|
||||
if (!fresh) {
|
||||
summary.duplicated += 1;
|
||||
continue;
|
||||
@@ -179,7 +281,7 @@ export async function consumeMainSystemEventsForBranch(input: {
|
||||
if (done === 'triggered_reconcile') summary.triggeredReconcile += 1;
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
`[main-system-events] ${branchCode} 处理事件失败(${event.event}#${event.id}):`,
|
||||
`[main-system-events] ${branchCode}/${supplierCode} 处理事件失败(${event.event}#${event.id}):`,
|
||||
(error as Error)?.message || error
|
||||
);
|
||||
}
|
||||
@@ -187,6 +289,8 @@ export async function consumeMainSystemEventsForBranch(input: {
|
||||
|
||||
if (!res.hasMore || !res.nextCursor) break;
|
||||
}
|
||||
|
||||
if (firstRun && progressed) summary.needsFullReconcile = true; // 答复④:首轮做一次初态全量对账
|
||||
return summary;
|
||||
}
|
||||
|
||||
@@ -266,6 +370,7 @@ export async function consumeMainSystemEventsForAllBranches(input: {
|
||||
} catch (error) {
|
||||
out.push({
|
||||
branchCode: code,
|
||||
suppliers: 0,
|
||||
pages: 0,
|
||||
fetched: 0,
|
||||
applied: 0,
|
||||
|
||||
@@ -17,6 +17,10 @@ const migration = readFileSync(
|
||||
new URL('../../migrations/20261078_main_system_event_cursor.sql', import.meta.url),
|
||||
'utf8'
|
||||
);
|
||||
const perSupplierMigration = readFileSync(
|
||||
new URL('../../migrations/20261079_event_cursor_per_supplier.sql', import.meta.url),
|
||||
'utf8'
|
||||
);
|
||||
|
||||
describe('主系统 5 项配合:我方接收端', () => {
|
||||
it('① 事件接口:路径 + since/cursor/limit + server_time/next_cursor/has_more/clamped 解析', () => {
|
||||
@@ -59,6 +63,29 @@ describe('主系统 5 项配合:我方接收端', () => {
|
||||
expect(webhook).toContain('timingSafeEqual');
|
||||
});
|
||||
|
||||
it('一码多绑:按**每个 supplier_code 各拉一遍**(不取第一条绑定)+ 游标/去重按 (分公司,供应商)', () => {
|
||||
expect(consumer).toContain('listSupplierCodesForBranch');
|
||||
expect(consumer).toContain('for (const supplierCode of supplierCodes)');
|
||||
expect(consumer).toContain('main_supplier_code = $2');
|
||||
expect(consumer).toContain('main_supplier_code, event_id');
|
||||
expect(perSupplierMigration).toContain('PRIMARY KEY (branch_code, main_supplier_code)');
|
||||
expect(perSupplierMigration).toContain('PRIMARY KEY (branch_code, main_supplier_code, event_id)');
|
||||
});
|
||||
|
||||
it('首轮不传 since/cursor(服务端默认窗口)+ 首轮做一次初态全量对账', () => {
|
||||
expect(consumer).toContain('const firstRun = !stored.cursor && !stored.lastEventAt');
|
||||
expect(consumer).toContain('let since = firstRun ? null : stored.lastEventAt || bootstrapSince()');
|
||||
expect(consumer).toContain('summary.initialFullReconcile = true');
|
||||
expect(consumer).toContain('followMainSystemPrepChanges(branchCode)');
|
||||
});
|
||||
|
||||
it('cursor 非法(400)⇒ 重置游标并标记需全量兜底', () => {
|
||||
expect(consumer).toContain("res.kind === 'invalid_cursor'");
|
||||
expect(consumer).toContain("reason: 'cursor_invalid_reset'");
|
||||
expect(events).toContain("kind: 'invalid_cursor'");
|
||||
expect(events).toContain('Number(result.status) === 400');
|
||||
});
|
||||
|
||||
it('事件消费纪律:server_time 起点 + next_cursor 持久化 + 翻页到 has_more=false + 去重', () => {
|
||||
expect(consumer).toContain('bootstrapSince');
|
||||
expect(consumer).toContain('saveCursor');
|
||||
|
||||
Reference in New Issue
Block a user