feat: persist captured order execution and controlled batch resend
This commit is contained in:
1 parent
b8cb1e11c6
commit
20589dd950
12 files changed
+419
-42
No files matched your search
@@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
const crypto=require('node:crypto');
|
||||
const {adapters}=require('./catalog.cjs');
|
||||
const {fail}=require('./validation.cjs');
|
||||
const hash=value=>crypto.createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
||||
const keys=new Set(['orderNo','brief','appDate','customer','productLine','quantity','changeReason','changeContent','totalAmount']);
|
||||
function registry(store){return [...structuredClone(adapters),...store.all('field-adapters')];}
|
||||
function fields(store){return {samples:store.all('capture-samples').map(({snapshot,...sample})=>({...sample,fields:snapshot.fields})),adapters:registry(store)};}
|
||||
function recordCapture(store,{sample,snapshot,taskId}){
|
||||
if(sample?.demo||!snapshot||!Array.isArray(snapshot.fields)||snapshot.processVersion!==sample.processVersion)fail('捕获样本缺少一致的流程版本或实际字段证据。',409);
|
||||
const paths=new Set();for(const f of snapshot.fields){if(!f||typeof f.path!=='string'||!f.path||paths.has(f.path)||!Object.hasOwn(sample.fields||{},f.path)||JSON.stringify(sample.fields[f.path])!==JSON.stringify(f.value))fail('捕获字段冲突或与样本不一致。',409);paths.add(f.path);}
|
||||
const id=hash([taskId,sample,snapshot]);const captured={id,taskId,processName:sample.processName,processVersion:sample.processVersion,sample:structuredClone(sample),snapshot:structuredClone(snapshot),capturedAt:store.now(),provenance:'server-provider',evidenceHash:hash({sample,snapshot})};store.put('capture-samples',captured);return captured;
|
||||
}
|
||||
function saveAdapter(store,input,actor){return store.transaction(()=>{
|
||||
if(actor?.role!=='admin')fail('仅管理员可以配置字段适配。',403);
|
||||
if(!input||Object.keys(input).some(k=>!['sampleId','processName','processVersion','fields','version'].includes(k)))fail('字段适配配置无效。');
|
||||
const captured=store.get('capture-samples',input.sampleId);
|
||||
if(!captured||captured.provenance!=='server-provider'||captured.processName!==input.processName||captured.processVersion!==input.processVersion)fail('必须选择服务端已采集且流程版本一致的真实样本。',409);
|
||||
const paths=new Set(captured.snapshot.fields.map(f=>f.path));
|
||||
if(!input.fields||Array.isArray(input.fields)||!input.fields.orderNo||!input.fields.brief||Object.entries(input.fields).some(([key,value])=>!keys.has(key)||typeof value!=='string'||!paths.has(value)))fail('字段绑定必须来自已捕获样本的实际字段目录,包含订单号与摘要。');
|
||||
const id='captured-'+hash([input.processName,input.processVersion]).slice(0,24),old=store.get('field-adapters',id);
|
||||
if((old&&input.version!==old.version)||(!old&&input.version!==undefined))fail('字段适配版本已变化,请刷新。',409);
|
||||
const result={id,version:(old?.version||0)+1,label:input.processName+' '+input.processVersion+'(真实采集)',processName:input.processName,processVersion:input.processVersion,fields:structuredClone(input.fields),verified:true,sampleId:captured.id,evidenceHash:captured.evidenceHash,verifiedAt:store.now(),verifiedBy:actor.username};
|
||||
store.put('field-adapters',result);for(const record of store.all('rules'))if(record.draft.adapterIds.includes(id)||record.published?.adapterIds.includes(id)){record.trialVersion=null;record.trialSampleId=null;if(record.published?.adapterIds.includes(id))record.published.status='paused';store.put('rules',record);}store.audit(actor.username,'绑定真实流程字段',id);return result;
|
||||
});}
|
||||
module.exports={registry,fields,recordCapture,saveAdapter};
|
||||
Reference in new issue
Block a user