feat: collect editable scheduled batches and show order details in modals

This commit is contained in:
abaishu0730 committed 2026-10-26 09:53:45 +08:00
1 parent 8d010a05cd
commit c19be938fc
20 files changed
+212 -34

No files matched your search

@@ -0,0 +1,14 @@
'use strict';
const test=require('node:test'),assert=require('node:assert/strict'),crypto=require('node:crypto');
const {orderDrivenUI}=require('../fixtures/order-driven-ui.cjs');
async function setup(t){const f=await orderDrivenUI(t,'#notification-batches'),s=f.runtime.orderRules;s.clock=()=>new Date('2026-10-26T01:00:00Z');s.capabilities={liveTodo:true,pdfExport:true,automaticArchive:true};const {activateReferenceMapping}=require('../fixtures/activate-field-mapping.cjs'),sample=activateReferenceMapping(s).sample,old=s.get('rules','bs-change').draft,rule=s.saveRule({...old,archiveDirectory:'\\\\fixture\\orders',to:['2639266652@qq.com'],cc:[],filenameDate:'receivedAt',bodyTemplateId:''},'admin');s.put('rules',{...s.get('rules',rule.id),published:{...rule,status:'enabled'}});s.saveSettings({...s.settings(),channelId:'fixture-channel',enabled:true,processingEnabled:true},'admin');const order=s.ingest({...sample,connectionId:f.sample.connectionId,taskId:'batch-ui-order',instanceId:'batch-ui-instance',receivedAt:'2026-10-26T00:30:00Z'},'fixture');assert.equal(order.status,'identified',order.error);const bytes=Buffer.from('%PDF-1.7 fixture');s.recordArchive(order.id,{archivedAt:s.now(),files:[{name:'order.pdf',path:'C:\\fixture\\order.pdf',size:bytes.length,sha256:crypto.createHash('sha256').update(bytes).digest('hex'),kind:'pdf'}]},'fixture');await f.page.getByRole('button',{name:'刷新数据',exact:true}).click();return {...f,order};}
test('collecting batch saves HTML, opens linked orders in a modal and returns without navigation',{timeout:90000},async t=>{
const {page,runtime,order}=await setup(t);await page.getByRole('button',{name:'查看详情',exact:true}).click();const dialog=page.getByRole('dialog',{name:'业务详情',exact:true});await dialog.getByRole('heading',{name:'编辑本批次邮件',exact:true}).waitFor();
await dialog.getByLabel('本批次邮件标题',{exact:true}).fill('当前批次 {{订单号}}');await dialog.getByLabel('批次正文格式',{exact:true}).selectOption('html');await dialog.getByLabel('本批次邮件正文',{exact:true}).fill('<p>批次专用说明</p><div>{{订单明细}}</div><p>{{存放地址}}</p>');await dialog.getByRole('button',{name:'保存批次内容',exact:true}).click();await dialog.getByText('批次内容已保存,按计划自动发送。',{exact:true}).waitFor();assert.equal(runtime.orderRules.all('batch-content-drafts').length,1);assert.equal(runtime.orderRules.all('batches').length,0);
await dialog.getByRole('button',{name:order.event.orderNo,exact:true}).click();await dialog.getByRole('heading',{name:'处理进度',exact:true}).waitFor();assert.match(page.url(),/#notification-batches/);assert.equal(await dialog.getByRole('button',{name:'返回批次',exact:true}).count(),1);await dialog.getByRole('button',{name:'返回批次',exact:true}).click();assert.match(await dialog.getByLabel('本批次邮件正文',{exact:true}).inputValue(),/批次专用说明/);
await dialog.getByRole('button',{name:'关闭详情',exact:true}).click();await page.reload();await page.getByRole('button',{name:'查看详情',exact:true}).click();await dialog.getByLabel('本批次邮件正文',{exact:true}).waitFor();assert.match(await dialog.getByLabel('本批次邮件正文',{exact:true}).inputValue(),/批次专用说明/);await page.setViewportSize({width:390,height:844});assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true);await page.goto(new URL('/#notification-orders',page.url()).href);await page.getByRole('button',{name:'查看收集中批次',exact:true}).waitFor();await page.waitForFunction(()=>!document.querySelector('.modal-backdrop')&&document.body.style.overflow==='');assert.equal(await page.evaluate(()=>document.body.style.overflow),'');
});
test('order center opens its collecting batch and failed saves keep inputs and dirty-close protection',{timeout:90000},async t=>{
const {page,runtime}=await setup(t);await page.goto(new URL('/#notification-orders',page.url()).href);await page.getByRole('button',{name:'查看收集中批次',exact:true}).click();const dialog=page.getByRole('dialog',{name:'业务详情',exact:true});await dialog.getByLabel('本批次邮件标题',{exact:true}).fill('未保存修改');
await page.route('**/api/order-rules/batches/*/content',r=>r.fulfill({status:503,json:{error:'隔离批次保存失败'}}));await dialog.getByRole('button',{name:'保存批次内容',exact:true}).click();await dialog.getByText('隔离批次保存失败',{exact:true}).waitFor();await dialog.getByRole('button',{name:'关闭详情',exact:true}).click();await page.getByRole('button',{name:'留在此页',exact:true}).click();assert.equal(await dialog.getByLabel('本批次邮件标题',{exact:true}).inputValue(),'未保存修改');assert.equal(runtime.orderRules.all('batch-content-drafts').length,0);
});
@@ -0,0 +1,40 @@
'use strict';
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path'),crypto=require('node:crypto');
const {RuleStore}=require('../../src/modules/order-rules/store.cjs'),{activateReferenceMapping}=require('../fixtures/activate-field-mapping.cjs');
const actor={username:'admin',role:'admin'};
function fixture(t){const dir=fs.mkdtempSync(path.join(os.tmpdir(),'collecting-batches-'));let now=new Date('2026-10-26T01:00:00Z');const s=new RuleStore(dir,{clock:()=>now,capabilities:{liveTodo:true,pdfExport:true,automaticArchive:true}});t.after(()=>{s.close();assert.equal(path.dirname(fs.realpathSync(dir)).toLowerCase(),fs.realpathSync(os.tmpdir()).toLowerCase());fs.rmSync(dir,{recursive:true,force:true});});const e=activateReferenceMapping(s).sample,old=s.get('rules','bs-change').draft,rule=s.saveRule({...old,archiveDirectory:'\\\\fixture\\orders',to:['2639266652@qq.com'],cc:[],filenameDate:'receivedAt',bodyTemplateId:''},'admin');s.put('rules',{...s.get('rules',rule.id),published:{...rule,status:'enabled'}});s.saveSettings({...s.settings(),channelId:'fixture-channel',processingEnabled:true,enabled:true},'admin');
const add=(id,time='2026-10-26T00:30:00Z')=>{const o=s.ingest({...e,taskId:id,instanceId:'instance-'+id,receivedAt:time,title:'订单号:'+id+'机芯号:CORE',fields:{...e.fields,[require('../../src/modules/order-rules/catalog.cjs').adapters.find(a=>a.id==='bs-change').fields.orderNo]:id}},'fixture');assert.equal(o.status,'identified',o.error);const bytes=Buffer.from('%PDF-1.7 fixture');s.recordArchive(o.id,{archivedAt:now.toISOString(),files:[{name:id+'.pdf',path:'C:\\fixture\\'+id+'.pdf',size:bytes.length,sha256:crypto.createHash('sha256').update(bytes).digest('hex'),kind:'pdf'}]},'fixture');return s.get('orders',o.id);};return {s,dir,rule,add,setNow:value=>now=new Date(value)};}
test('matched current-window orders collect by category, stay dynamic and freeze edited HTML once',t=>{
const f=fixture(t),c=require('../../src/modules/order-rules/collecting-batches.cjs'),a=f.add('ONE'),initial=c.list(f.s);assert.equal(initial.length,1);assert.equal(f.s.all('batches').length,0);assert.equal(initial[0].status,'collecting');assert.equal(initial[0].orderIds[0],a.id);
const draft=c.save(f.s,initial[0].id,{version:0,context:initial[0].context,subject:'本批 {{订单号}}',format:'html',body:'<p>自定义说明</p><div>{{订单明细}}</div><p>{{存放地址}}</p>'},actor);assert.equal(draft.version,1);
const b=f.add('TWO'),next=c.get(f.s,initial[0].id);assert.equal(next.orderIds.length,2);assert.match(next.text,/自定义说明/);assert.match(next.text,/ONE/);assert.match(next.text,/TWO/);assert.equal(next.orderIds[1],b.id);assert.equal(f.s.get('rules','bs-change').draft.intro,f.rule.intro);
assert.throws(()=>c.save(f.s,next.id,{version:0,context:next.context,subject:'冲突',format:'text',body:'{{订单明细}}\n{{存放地址}}'},actor),e=>e.status===409);
f.setNow('2026-10-26T03:00:00Z');assert.throws(()=>c.save(f.s,next.id,{version:1,context:next.context,subject:'太晚',format:'text',body:'{{订单明细}}\n{{存放地址}}'},actor),e=>e.status===409);
const [frozen]=f.s.prepare('2026-10-26T03:00:00Z','fixture');assert(frozen);assert.match(frozen.text,/自定义说明/);assert.equal(frozen.contentEdit.version,1);assert.deepEqual(frozen.orderIds,[a.id,b.id]);assert.deepEqual(frozen.attachments,[]);assert.equal(c.list(f.s).length,0);
});
test('unknown candidates and historical windows do not collect; editing validates variables and operator scope',t=>{
const f=fixture(t),c=require('../../src/modules/order-rules/collecting-batches.cjs');f.add('CURRENT');const group=c.list(f.s)[0];f.s.put('orders',{id:'unmatched',status:'blocked',event:{receivedAt:'2026-10-26T00:00:00Z'}});assert.equal(c.list(f.s).length,1);
for(const body of ['没有订单区','{{订单明细}}','{{不存在}}\n{{订单明细}}\n{{存放地址}}'])assert.throws(()=>c.save(f.s,group.id,{version:0,context:group.context,subject:'标题',format:'text',body},actor));assert.equal(f.s.all('batch-content-drafts').length,0);
assert.throws(()=>c.save(f.s,group.id,{version:0,context:group.context,subject:'标题',format:'text',body:'{{订单明细}}\n{{存放地址}}'},{username:'viewer',role:'viewer'}),e=>e.status===403);
f.setNow('2026-10-26T04:00:00Z');assert.equal(c.list(f.s).length,0);assert.equal(f.s.all('batches').length,0);
});
test('query links both directions, preserves edits on disk and rechecks template context before freeze',t=>{
const f=fixture(t),c=require('../../src/modules/order-rules/collecting-batches.cjs'),q=require('../../src/modules/order-rules/query.cjs'),a=f.add('DISK');let template={id:'sample-template',version:1,subject:'通知',text:'{{订单明细}}\n{{存放地址}}',html:'',attachments:[]};f.s.templateResolver=()=>template;
const context={orderRules:f.s},group=c.list(f.s)[0];assert.equal(q.queryBatches(context,{},actor).items[0].id,group.id);assert.equal(q.queryOrders(context,{},actor).items[0].collectingBatches[0].id,group.id);assert.equal(q.orderDetail(context,a.id,actor).batches[0].id,group.id);assert.equal(q.batchDetail(context,group.id,actor).orders[0].id,a.id);
c.save(f.s,group.id,{version:0,context:group.context,subject:'持久编辑',format:'text',body:'持久说明\n{{订单明细}}\n{{存放地址}}'},actor);const db=new RuleStore(f.dir,{clock:()=>new Date('2026-10-26T01:00:00Z')});try{assert.equal(db.all('batch-content-drafts')[0].subject,'持久编辑');}finally{db.close();}
template={...template,version:2};assert.match(c.get(f.s,group.id).error,/已变化/);f.setNow('2026-10-26T03:00:00Z');assert.equal(f.s.prepare('2026-10-26T03:00:00Z','fixture').length,0);assert.match(f.s.all('window-results')[0].error,/已变化/);
});
test('different categories stay separate and missing templates do not hide the entire batch page',t=>{
const f=fixture(t),c=require('../../src/modules/order-rules/collecting-batches.cjs'),a=f.add('CATEGORY-A'),other=structuredClone(a);other.id='second-category-order';other.event.processName='第二流程';other.ruleSnapshot={...other.ruleSnapshot,id:'second-category',categoryId:'second-category',name:'另一分类',scopes:[{processName:'第二流程',nodeName:other.event.nodeName}]};other.categoryId='second-category';f.s.put('orders',other);
assert.equal(c.list(f.s).length,2);f.s.templateResolver=(category)=>{if(category==='second-category')throw Object.assign(Error('隔离模板已停用'),{status:409});return null;};const groups=c.list(f.s);assert.equal(groups.length,2);assert.match(groups.find(b=>b.categoryId==='second-category').error,/模板已停用/);assert(groups.find(b=>b.categoryId==='bs-change').text);
});
test('content can be saved while a member is still preparing its archive without bypassing send prerequisites',t=>{
const f=fixture(t),c=require('../../src/modules/order-rules/collecting-batches.cjs'),a=f.add('ARCHIVING');a.status='preparing';a.archive=null;f.s.put('orders',a);const group=c.list(f.s)[0];assert(group.error);
const saved=c.save(f.s,group.id,{version:0,context:group.context,subject:'提前编辑',format:'text',body:'提前说明\n{{订单明细}}\n{{存放地址}}'},actor);assert.equal(saved.version,1);assert(saved.error);assert.equal(f.s.all('batch-content-drafts').length,1);
f.setNow('2026-10-26T03:00:00Z');assert.equal(f.s.prepare('2026-10-26T03:00:00Z','fixture').length,0);assert.equal(f.s.all('batches').length,0);
});
test('manually admitted backlog uses the same current-window eligibility as the freeze operation',t=>{
const f=fixture(t),c=require('../../src/modules/order-rules/collecting-batches.cjs'),a=f.add('ADMITTED'),row=a.sample,keys=['source','portalSource','taskId','instanceId','revision','processName','nodeName','processVersion','receivedAt'],snapshot=Object.fromEntries(keys.filter(k=>Object.hasOwn(row,k)).map(k=>[k,row[k]])),snapshotHash=crypto.createHash('sha256').update(JSON.stringify(Object.fromEntries(Object.keys(snapshot).sort().map(k=>[k,snapshot[k]])))).digest('hex');
f.s.put('restricted-test',{id:'main',value:{id:'admitted-run',enabled:true,rules:[],currentBacklog:[{snapshot,snapshotHash,testRunId:'admitted-run',admittedAt:'2026-10-26T01:00:00Z'}]}});f.setNow('2026-10-26T08:00:00Z');
const window=require('../../src/modules/order-rules/window.cjs').upcomingWindow(f.s.clock(),f.s.schedule(),f.s.calendar());assert.equal(require('../../src/modules/order-rules/restricted-test.cjs').orderInWindow(f.s,a,window),true);assert.equal(c.list(f.s)[0]?.orderIds[0],a.id);
});
+2 -2
View File
@@ -85,10 +85,10 @@ test('order list retains execution failure and conflict causes beside independen
const f=await fixture(t),{runtime,page}=f;
const cases=[{id:'execution-failed',status:'failed',error:'归档读取校验失败,通知尚待处理'},{id:'execution-conflict',status:'blocked',error:'新来源修订与已归档版本冲突,请核对',conflict:true}];
for(const item of cases){
runtime.orderRules.put('orders',{id:item.id,status:'sent',error:'普通路已有结果说明',event:{connectionId:'c',orderNo:item.id,receivedAt:'2026-10-21T01:00:00Z'},notifications:{huizhou:{status:'pending',batchId:null,error:'惠州路独立等待原因',orderNos:['H-200'],issues:[]}}});
runtime.orderRules.put('orders',{id:item.id,categoryId:'bs-change',status:'sent',error:'普通路已有结果说明',event:{connectionId:'c',orderNo:item.id,receivedAt:'2026-10-21T01:00:00Z'},notifications:{huizhou:{status:'pending',batchId:null,error:'惠州路独立等待原因',orderNos:['H-200'],issues:[]}}});
runtime.orderRules.put('execution-tasks',{id:'task-'+item.id,orderId:item.id,connectionId:'c',version:1,status:item.status,error:item.error,conflict:item.conflict||false,row:{orderNo:item.id}});
}
runtime.orderRules.put('execution-tasks',{id:'bare-execution',connectionId:'c',version:1,status:'failed',error:'尚未生成订单的采集失败',row:{orderNo:'bare-execution'}});
runtime.orderRules.put('execution-tasks',{id:'bare-execution',categoryIds:['bs-change'],connectionId:'c',version:1,status:'failed',error:'尚未生成订单的采集失败',row:{orderNo:'bare-execution'}});
await f.open('#notification-orders');
for(const item of cases){const row=page.locator('tr').filter({has:page.getByText(item.id,{exact:true})});await row.waitFor();const text=await row.innerText();assert.ok(text.includes(item.error),text);assert.match(text,/普通通知.*已提交邮件服务器/s);assert.match(text,/惠州通知.*等待汇总/s);assert.match(text,/普通路已有结果说明/);assert.match(text,/惠州路独立等待原因/);}
const bare=page.locator('tr').filter({has:page.getByText('bare-execution',{exact:true})});assert.match(await bare.innerText(),/尚未生成订单的采集失败/);assert.doesNotMatch(await bare.innerText(),/普通通知|惠州通知/);