fix: enforce notification reservations and evidence based retry

This commit is contained in:
abaishu0730 committed 2026-10-19 08:52:14 +08:00
1 parent 0951dd451d
commit acba28c879
10 files changed
+205 -83

No files matched your search

@@ -0,0 +1,38 @@
# Task 3: Notification transaction boundary and manual recovery
## Changes
- Split combined implementation into `store.cjs` (SQLite, encrypted records and transactions), `service.cjs` (schedule/SMTP orchestration), and `mapping.cjs` (source paths and event validation). Removed combined internal module; API and web public URLs remain unchanged.
- Construction no longer recovers active work. Explicit `NotificationStore.recover()` must run after application ownership acquisition. Recovery cancels prepared batches, returns eligible reservations to queue, exhausts reservations already at the cap, and preserves sending results as unknown for human resolution.
- `beginSending(batchId)` uses BEGIN IMMEDIATE and validates prepared batch state plus the complete reserved item identity set and ownership before changing batch/items to sending. SMTP sendMail happens only after this succeeds. Terminal/cancelled batch state is not overwritten by failure cleanup.
- Final verification failure marks items exhausted with accurate queue and batch errors. Lowering maxAttempts normalizes eligible queue items; increasing it never revives exhausted items automatically. Unknown results retain separate manual resolution semantics.
- Admin POST `/api/notifications/items/:id/retry` accepts `{batchId,note}` (at least five evidence characters). Transactional status + batch identity CAS resets exactly one exhausted item, encrypts durable retry evidence, and rejects duplicate/stale submissions. Successful API requests also record workspace audit with the evidence. BatchId may be null for pre-existing capped rows without a batch.
- UI shows exhausted explicitly and presents evidence/action only to admins. Actual browser regression submits the form and confirms queued state and persisted evidence.
## Red evidence
`NODE_PATH` used original checkout node_modules and temporary review deps only.
1. Core notification suite before implementation: 6 pass / 4 fail. Second store changed reserved -> pending; cancelled batch reached fake SMTP (1 rather than 0 sends); last verification failure stayed pending rather than exhausted; recover method absent.
2. API/UI regressions before implementation: retry returned 404 instead of permission response; real Chrome timed out waiting for exhausted state/action.
3. Final review regression: exhausted batch still said it would retry next slot. Failure observed, then changed batch error to direct users to queue retry status.
## Green verification
Command: `node --test tests/notifications/*.test.cjs`
Environment: `NODE_PATH=Z:\Order Notification Automation\node_modules;C:\Users\ABS\AppData\Local\Temp\order-review-deps-20261019\node_modules`.
Final result: 13 tests passed, 0 failed, 0 skipped (including real local Chrome HTTP/UI).
Coverage includes safe second store construction; cancellation and stolen reservation CAS; cap exhaustion/no subsequent automatic send; actual successful fake SMTP delivery after manual repair; admin/operator/viewer permissions; evidence validation and persisted audit; one-item reset; immediate and later-cycle stale submission rejection; settings cap changes; prepared/sending recovery; and unknown-result non-retry behavior. Existing deduplication, original body, source mapping, slot timing and IMAP exclusivity remain green.
No provider, production state or process access occurred. Tests use temporary SQLite databases and fake SMTP only.
## Integration and exports
- `require('./modules/notifications/store.cjs').NotificationStore`
- `require('./modules/notifications/service.cjs').NotificationService`
- mapping exports DEFAULT_MAP, validateMap, hash, mapEvent.
- New store methods: recover(), beginSending(batchId), retryItem(id,input,actor); new service method retryItem(id,input,actor).
- `src/server.cjs` imports were updated in shared working tree, but deliberately left for Task 5's lifecycle commit; Task 5 must call recover after obtaining the instance lock.
- Added encrypted retry_audit table; existing state paths, vault AAD, secrets and item/batch payload formats remain readable. Batch snapshots add itemIds to enforce the reservation boundary.
+1
View File
@@ -7,6 +7,7 @@ async function notificationApi({notifications,workspace,user,route,method,body,r
if(user.role==='viewer')fail('只读成员不能提交通知数据或修改配置。',403);
if(route==='/api/notifications/settings'){if(user.role!=='admin')fail('仅管理员可以修改业务通知配置。',403);const saved=notifications.saveSettings(body);workspace?.record(user,'保存业务通知配置',saved.enabled?'已启用':'已暂停');reply(200,saved);return true;}
if(route==='/api/notifications/preview'||route==='/api/notifications/events'){if(!body||Object.keys(body).some(k=>k!=='data'))fail('请提交来源数据对象。');const preview=route.endsWith('/preview');const result=preview?notifications.preview(body.data):notifications.ingest(body.data);if(!preview)workspace?.record(user,'通知数据入队',result.sourceId);reply(preview?200:201,result);return true;}
const retry=/^\/api\/notifications\/items\/([\w-]+)\/retry$/.exec(route);if(retry){if(user.role!=='admin')fail('仅管理员可以重新入队次数耗尽通知。',403);const result=notifications.retryItem(retry[1],body,user.username);workspace?.record(user,'重新入队次数耗尽通知',retry[1]+':'+body.note.trim());reply(200,result);return true;}
const match=/^\/api\/notifications\/batches\/([\w-]+)\/resolve$/.exec(route);if(match){if(user.role!=='admin')fail('仅管理员可以核验发送结果。',403);const result=notifications.resolve(match[1],body,user.username);workspace?.record(user,'核验通知发送结果',match[1]);reply(200,result);return true;}
fail('没有找到此通知操作。',404);
}
+10
View File
@@ -0,0 +1,10 @@
'use strict';
const fail=(message,status=400)=>{throw Object.assign(new Error(message),{status});};
const crypto=require('node:crypto');
const hash=value=>crypto.createHash('sha256').update(value).digest('hex');
const DEFAULT_MAP={id:'id',version:'version',industry:'industry',brief:'brief',subject:'subject',occurredAt:'occurredAt'};
function validateMap(map){if(!map||typeof map!=='object'||Array.isArray(map)||Object.keys(map).some(k=>!Object.hasOwn(DEFAULT_MAP,k)))fail('通知字段映射无效。');for(const key of Object.keys(DEFAULT_MAP)){const p=map[key];if(typeof p!=='string'||p.length>200||(p&&!/^[\p{L}\p{N}_-]+(?:\.[\p{L}\p{N}_-]+)*$/u.test(p))||p.split('.').some(s=>['__proto__','constructor','prototype'].includes(s)))fail('字段路径只支持普通对象的点分隔路径。');if(['id','industry','brief'].includes(key)&&!p)fail('请配置通知 ID、产业与简要说明字段。');}return {...map};}
function valueAt(data,p){if(!p)return undefined;for(const part of p.split('.')){if(!data||typeof data!=='object'||!Object.hasOwn(data,part))return undefined;data=data[part];}return data;}
function mapEvent(data,config,clock=()=>new Date()){if(!data||typeof data!=='object'||Array.isArray(data))fail('通知来源必须是 JSON 对象。');const raw=Object.fromEntries(Object.entries(config.fieldMap).map(([key,p])=>[key,valueAt(data,p)]));if(!['string','number'].includes(typeof raw.id)||!String(raw.id).trim()||String(raw.id).length>200||/[\x00-\x1f]/.test(String(raw.id))||typeof raw.industry!=='string'||!raw.industry.trim()||raw.industry.length>100||/[\x00-\x1f]/.test(raw.industry)||typeof raw.brief!=='string'||!raw.brief.trim()||raw.brief.length>20000)fail('通知 ID、产业和简要说明不能为空,正文最多 20000 字。');if(raw.subject!==undefined&&(typeof raw.subject!=='string'||raw.subject.length>300||/[\r\n\x00]/.test(raw.subject)))fail('通知标题最多 300 字,不能包含换行。');if(raw.version!==undefined&&(!['string','number'].includes(typeof raw.version)||String(raw.version).length>200||/[\x00-\x1f]/.test(String(raw.version))))fail('来源版本无效。');if(raw.occurredAt!==undefined&&(typeof raw.occurredAt!=='string'||!Number.isFinite(Date.parse(raw.occurredAt))))fail('业务时间无效。');return {source:config.sourceName,sourceKey:config.sourceKey,sourceId:String(raw.id).trim(),sourceVersion:raw.version===undefined||raw.version===''?hash(raw.brief):String(raw.version),industry:raw.industry.trim(),brief:raw.brief,subject:raw.subject||raw.industry.trim()+'业务通知',occurredAt:raw.occurredAt||clock().toISOString()};}
module.exports={DEFAULT_MAP,validateMap,hash,mapEvent};
@@ -1,76 +0,0 @@
'use strict';
const fs=require('node:fs');const path=require('node:path');const crypto=require('node:crypto');const {DatabaseSync}=require('node:sqlite');const {BpmVault}=require('../../platform/security/vault.cjs');const {nextSlots}=require('../../shared/schedule.cjs');
const fail=(message,status=400)=>{throw Object.assign(new Error(message),{status});};
const hash=value=>crypto.createHash('sha256').update(value).digest('hex');
const DEFAULT_MAP={id:'id',version:'version',industry:'industry',brief:'brief',subject:'subject',occurredAt:'occurredAt'};
function validateMap(map){if(!map||typeof map!=='object'||Array.isArray(map)||Object.keys(map).some(k=>!Object.hasOwn(DEFAULT_MAP,k)))fail('通知字段映射无效。');for(const key of Object.keys(DEFAULT_MAP)){const p=map[key];if(typeof p!=='string'||p.length>200||(p&&!/^[\p{L}\p{N}_-]+(?:\.[\p{L}\p{N}_-]+)*$/u.test(p))||p.split('.').some(s=>['__proto__','constructor','prototype'].includes(s)))fail('字段路径只支持普通对象的点分隔路径。');if(['id','industry','brief'].includes(key)&&!p)fail('请配置通知 ID、产业与简要说明字段。');}return {...map};}
function valueAt(data,p){if(!p)return undefined;for(const part of p.split('.')){if(!data||typeof data!=='object'||!Object.hasOwn(data,part))return undefined;data=data[part];}return data;}
class NotificationStore{
constructor(dir){fs.mkdirSync(dir,{recursive:true});this.vault=new BpmVault({stateDir:path.join(dir,'secrets')});this.db=new DatabaseSync(path.join(dir,'notifications.sqlite'));this.db.exec(`PRAGMA journal_mode=WAL; PRAGMA busy_timeout=5000; PRAGMA secure_delete=ON;
CREATE TABLE IF NOT EXISTS settings(id INTEGER PRIMARY KEY CHECK(id=1),value TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS items(id TEXT PRIMARY KEY,payload BLOB NOT NULL,status TEXT NOT NULL,attempts INTEGER NOT NULL DEFAULT 0,error TEXT,batch_id TEXT,created_at TEXT NOT NULL,industry TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS batches(id TEXT PRIMARY KEY,status TEXT NOT NULL,payload BLOB NOT NULL,error TEXT,created_at TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS metadata(key TEXT PRIMARY KEY,value TEXT NOT NULL);
UPDATE items SET status='pending',batch_id=NULL WHERE batch_id IN(SELECT id FROM batches WHERE status='prepared');
UPDATE items SET status='unknown',error='服务重启,发送结果需要核实。' WHERE batch_id IN(SELECT id FROM batches WHERE status='sending');
UPDATE batches SET status='cancelled' WHERE status='prepared';
UPDATE batches SET status='unknown',error='服务重启,发送结果需要核实。' WHERE status='sending';`);
this.db.prepare('INSERT OR IGNORE INTO settings VALUES(1,?)').run(JSON.stringify({version:1,enabled:false,sourceKey:crypto.randomUUID(),sourceName:'业务来源',channelId:'',fieldMap:DEFAULT_MAP,maxAttempts:3}));
if(!this.db.prepare('PRAGMA table_info(items)').all().some(c=>c.name==='industry')){this.db.exec("ALTER TABLE items ADD COLUMN industry TEXT NOT NULL DEFAULT ''");for(const row of this.db.prepare('SELECT id,payload FROM items').all())this.db.prepare('UPDATE items SET industry=? WHERE id=?').run(this.decode(row.id,row.payload).industry,row.id);}
this.db.exec('CREATE INDEX IF NOT EXISTS notification_pending ON items(status,industry,attempts)');
}
transaction(work){this.db.exec('BEGIN IMMEDIATE');try{const result=work();this.db.exec('COMMIT');return result;}catch(e){this.db.exec('ROLLBACK');throw e;}}
encode(id,data){try{return this.vault.encode(id,data);}catch{fail('通知内容加密不可用,请联系管理员。',503);}}
decode(id,data){try{return this.vault.decode(id,data);}catch{fail('通知内容无法解密,请联系管理员。',503);}}
settings(){return JSON.parse(this.db.prepare('SELECT value FROM settings WHERE id=1').get().value);}
saveSettings(input){const old=this.settings();if(!input||Object.keys(input).some(k=>!['version','enabled','sourceKey','sourceName','channelId','fieldMap','maxAttempts'].includes(k)))fail('通知配置无效。');if(input.version!==old.version)fail('通知配置已被修改,请刷新。',409);if(typeof input.enabled!=='boolean'||typeof input.sourceName!=='string'||!input.sourceName.trim()||input.sourceName.length>80||typeof input.channelId!=='string'||input.channelId.length>100||!Number.isInteger(input.maxAttempts)||input.maxAttempts<1||input.maxAttempts>10)fail('请检查来源、邮箱和尝试次数(1–10)。');if(input.sourceKey&&input.sourceKey!==old.sourceKey)fail('来源标识不能更改。');const saved={...input,sourceKey:old.sourceKey,sourceName:input.sourceName.trim(),fieldMap:validateMap(input.fieldMap),version:old.version+1};this.db.prepare('UPDATE settings SET value=? WHERE id=1').run(JSON.stringify(saved));return saved;}
scheduleState(){const row=this.db.prepare("SELECT value FROM metadata WHERE key='schedule'").get();return row?JSON.parse(row.value):null;}
setSchedule(value){this.db.prepare("INSERT INTO metadata VALUES('schedule',?) ON CONFLICT(key) DO UPDATE SET value=excluded.value").run(JSON.stringify(value));}
ingest(event){const id=hash([event.sourceKey,event.sourceId,event.sourceVersion,event.industry].join('\0')),old=this.db.prepare('SELECT * FROM items WHERE id=?').get(id);if(old){const value=this.decode(id,old.payload);if(value.brief!==event.brief||value.subject!==event.subject)fail('同一通知版本的内容已变化,请核实来源版本。',409);return this.item(old);}this.db.prepare('INSERT INTO items(id,payload,status,created_at,industry) VALUES(?,?,?,?,?)').run(id,this.encode(id,event),'pending',new Date().toISOString(),event.industry);return this.item(this.db.prepare('SELECT * FROM items WHERE id=?').get(id));}
item({payload,...row}){return {...this.decode(row.id,payload),...row};}
items(){return this.db.prepare('SELECT * FROM items ORDER BY rowid DESC LIMIT 100').all().map(r=>this.item(r));}
pending(attempts,industries){if(!industries.length)return [];return this.db.prepare("SELECT * FROM items WHERE status IN('pending','blocked') AND attempts<? AND industry IN("+industries.map(()=>'?').join(',')+") ORDER BY rowid LIMIT 200").all(attempts,...industries).map(r=>this.item(r));}
blockUnmapped(industries){this.db.prepare("UPDATE items SET status='blocked',error='此产业尚未配置接口人收件组。' WHERE status IN('pending','blocked')"+(industries.length?' AND industry NOT IN('+industries.map(()=>'?').join(',')+')':'')).run(...industries);}
block(id,message){this.db.prepare("UPDATE items SET status='blocked',error=? WHERE id=? AND status IN('pending','blocked')").run(message,id);}
prepare(items,snapshot){return this.transaction(()=>{const id=crypto.randomUUID();this.db.prepare('INSERT INTO batches VALUES(?,?,?,?,?)').run(id,'prepared',this.encode(id,snapshot),null,new Date().toISOString());for(const item of items){const result=this.db.prepare("UPDATE items SET status='reserved',attempts=attempts+1,error=NULL,batch_id=? WHERE id=? AND status IN('pending','blocked')").run(id,item.id);if(!result.changes)fail('通知队列已变化,请重试。',409);}return id;});}
mark(id,status,message=null){this.transaction(()=>{this.db.prepare('UPDATE batches SET status=?,error=? WHERE id=?').run(status,message,id);if(status!=='sending')this.db.prepare('UPDATE items SET status=?,error=? WHERE batch_id=?').run(status==='failed'?'pending':status,message,id);});}
batches(){return this.db.prepare("SELECT * FROM batches ORDER BY CASE status WHEN 'unknown' THEN 0 ELSE 1 END,rowid DESC LIMIT 100").all().map(({payload,...r})=>({...this.decode(r.id,payload),...r}));}
resolve(id,input,actor){return this.transaction(()=>{const row=this.db.prepare('SELECT * FROM batches WHERE id=?').get(id);if(!row||row.status!=='unknown')fail('只能核验结果待核实的批次。',409);if(!input||!['sent','retry'].includes(input.resolution)||typeof input.note!=='string'||input.note.trim().length<5||input.note.length>2000)fail('请选择核验结果并填写至少 5 字的核验依据。');const data={...this.decode(id,row.payload),resolution:input.resolution,note:input.note.trim(),resolvedBy:actor,resolvedAt:new Date().toISOString()};this.db.prepare('UPDATE batches SET status=?,payload=? WHERE id=?').run('resolved_'+input.resolution,this.encode(id,data),id);this.db.prepare('UPDATE items SET status=?,error=NULL,attempts=CASE WHEN ? THEN 0 ELSE attempts END WHERE batch_id=?').run(input.resolution==='sent'?'sent':'pending',input.resolution==='retry'?1:0,id);return {ok:true};});}
close(){if(this.db){this.db.close();this.db=null;}this.vault.destroy();}
}
class NotificationService{
constructor({store,mail,settings,calendar=()=>[],clock=()=>new Date()}){this.store=store;this.mail=mail;this.settings=settings;this.calendar=calendar;this.clock=clock;this.running=null;this.stopped=false;}
map(data){if(!data||typeof data!=='object'||Array.isArray(data))fail('通知来源必须是 JSON 对象。');const config=this.store.settings(),raw=Object.fromEntries(Object.entries(config.fieldMap).map(([key,p])=>[key,valueAt(data,p)]));if(!['string','number'].includes(typeof raw.id)||!String(raw.id).trim()||String(raw.id).length>200||/[\x00-\x1f]/.test(String(raw.id))||typeof raw.industry!=='string'||!raw.industry.trim()||raw.industry.length>100||/[\x00-\x1f]/.test(raw.industry)||typeof raw.brief!=='string'||!raw.brief.trim()||raw.brief.length>20000)fail('通知 ID、产业和简要说明不能为空,正文最多 20000 字。');if(raw.subject!==undefined&&(typeof raw.subject!=='string'||raw.subject.length>300||/[\r\n\x00]/.test(raw.subject)))fail('通知标题最多 300 字,不能包含换行。');if(raw.version!==undefined&&(!['string','number'].includes(typeof raw.version)||String(raw.version).length>200||/[\x00-\x1f]/.test(String(raw.version))))fail('来源版本无效。');if(raw.occurredAt!==undefined&&(typeof raw.occurredAt!=='string'||!Number.isFinite(Date.parse(raw.occurredAt))))fail('业务时间无效。');return {source:config.sourceName,sourceKey:config.sourceKey,sourceId:String(raw.id).trim(),sourceVersion:raw.version===undefined||raw.version===''?hash(raw.brief):String(raw.version),industry:raw.industry.trim(),brief:raw.brief,subject:raw.subject||raw.industry.trim()+'业务通知',occurredAt:raw.occurredAt||this.clock().toISOString()};}
preview(data){try{const event=this.map(data),group=this.settings().groups.find(g=>g.productLine===event.industry);return {valid:!!group,event,to:group?.to||[],cc:group?.cc||[],issues:group?[]:['此产业尚未配置接口人收件组。']};}catch(e){if(!e.status)throw e;return {valid:false,issues:[e.message]};}}
ingest(data){return this.store.ingest(this.map(data));}
saveSettings(input){if(this.running)fail('通知正在发送,请稍后修改配置。',409);if(input?.enabled){const c=this.mail.store.publicChannel(input.channelId);if(!c.state.smtp)fail('请先验证发件邮箱的 SMTP 连接。',409);if(!this.settings().groups.length)fail('请先配置产业接口人收件组。',409);}const old=this.store.settings(),saved=this.store.saveSettings(input);if(saved.enabled&&!old.enabled)this.resetSchedule(this.clock());return saved;}
resetSchedule(now){const schedule=this.settings().schedule,calendar=this.calendar();const state={signature:JSON.stringify([schedule,calendar]),next:nextSlots(schedule,now,1,calendar)[0]};this.store.setSchedule(state);return state;}
tick(now=this.clock()){
if(this.stopped||this.running)return this.running||Promise.resolve();
const task=this.perform(now);this.running=task;task.finally(()=>{if(this.running===task)this.running=null;}).catch(()=>{});return task;
}
async perform(now){
const config=this.store.settings();if(!config.enabled)return;
const settings=this.settings(),calendar=this.calendar(),signature=JSON.stringify([settings.schedule,calendar]);let schedule=this.store.scheduleState();
if(!schedule||schedule.signature!==signature){this.resetSchedule(now);return;}if(!schedule.next||Date.parse(schedule.next)>now.getTime())return;
const due=Date.parse(schedule.next);if(now.getTime()-due>60000){this.resetSchedule(now);return;}
const industries=settings.groups.filter(g=>g.to?.length).map(g=>g.productLine);this.store.blockUnmapped(industries);const grouped=new Map();for(const item of this.store.pending(config.maxAttempts,industries)){const group=settings.groups.find(g=>g.productLine===item.industry);if(!group||!group.to?.length){this.store.block(item.id,'此产业尚未配置接口人收件组。');continue;}const key=settings.schedule.mode==='order'?item.id:item.industry;if(!grouped.has(key))grouped.set(key,{items:[],group});if(grouped.get(key).items.length<50)grouped.get(key).items.push(item);}
if(!grouped.size){this.resetSchedule(now);return;}
await this.mail.exclusive(config.channelId,async()=>{
this.resetSchedule(now);
for(const {items,group}of grouped.values()){
if(this.stopped)break;
const channel=this.mail.store.channelSecret(config.channelId),snapshot={channelId:channel.id,industry:items[0].industry,sourceIds:items.map(i=>i.sourceId),subject:items.length===1?items[0].subject:items[0].industry+'业务通知',text:items.map(i=>i.brief).join('\n\n'),to:[...group.to],cc:[...group.cc],slot:new Date(due).toISOString()};
const batch=this.store.prepare(items,snapshot);let transport,sending=false;
try{transport=this.mail.smtpFactory(channel.smtp);await transport.verify();this.mail.store.guard(channel.id,channel.version);this.store.mark(batch,'sending');sending=true;
const result=await transport.sendMail({from:{address:channel.email,name:channel.name},to:snapshot.to,cc:snapshot.cc,subject:snapshot.subject,text:snapshot.text,attachments:[],messageId:`<${batch}@${channel.email.split('@')[1]}>`,disableFileAccess:true,disableUrlAccess:true});
const accepted=new Set((result.accepted||[]).map(a=>String(a.address||a).toLowerCase()));if(result.rejected?.length||[...snapshot.to,...snapshot.cc].some(a=>!accepted.has(a.toLowerCase()))){this.store.mark(batch,'unknown','部分收件人未被邮件服务器接受,请核实后处理。');}else this.store.mark(batch,'sent');
}catch{this.store.mark(batch,sending?'unknown':'failed',sending?'发信结果待核实,请查询邮件服务器记录。':'SMTP 检查失败,等待下个时段按次数上限重试。');}finally{transport?.close?.();}
}
}).catch(e=>{if(e.status!==409)throw e;});
}
resolve(id,input,actor){if(this.running)fail('通知正在发送,请稍后核验。',409);return this.store.resolve(id,input,actor);}
start(){this.timer=setInterval(()=>this.tick().catch(()=>{}),30000);this.timer.unref?.();}
async stop(){this.stopped=true;clearInterval(this.timer);if(this.running)await this.running.catch(()=>{});}
}
module.exports={NotificationStore,NotificationService};
+41
View File
@@ -0,0 +1,41 @@
'use strict';
const fail=(message,status=400)=>{throw Object.assign(new Error(message),{status});};
const {nextSlots}=require('../../shared/schedule.cjs');const {mapEvent}=require('./mapping.cjs');
class NotificationService{
constructor({store,mail,settings,calendar=()=>[],clock=()=>new Date()}){this.store=store;this.mail=mail;this.settings=settings;this.calendar=calendar;this.clock=clock;this.running=null;this.stopped=false;}
map(data){return mapEvent(data,this.store.settings(),this.clock);}
preview(data){try{const event=this.map(data),group=this.settings().groups.find(g=>g.productLine===event.industry);return {valid:!!group,event,to:group?.to||[],cc:group?.cc||[],issues:group?[]:['此产业尚未配置接口人收件组。']};}catch(e){if(!e.status)throw e;return {valid:false,issues:[e.message]};}}
ingest(data){return this.store.ingest(this.map(data));}
saveSettings(input){if(this.running)fail('通知正在发送,请稍后修改配置。',409);if(input?.enabled){const c=this.mail.store.publicChannel(input.channelId);if(!c.state.smtp)fail('请先验证发件邮箱的 SMTP 连接。',409);if(!this.settings().groups.length)fail('请先配置产业接口人收件组。',409);}const old=this.store.settings(),saved=this.store.saveSettings(input);if(saved.enabled&&!old.enabled)this.resetSchedule(this.clock());return saved;}
resetSchedule(now){const schedule=this.settings().schedule,calendar=this.calendar();const state={signature:JSON.stringify([schedule,calendar]),next:nextSlots(schedule,now,1,calendar)[0]};this.store.setSchedule(state);return state;}
tick(now=this.clock()){
if(this.stopped||this.running)return this.running||Promise.resolve();
const task=this.perform(now);this.running=task;task.finally(()=>{if(this.running===task)this.running=null;}).catch(()=>{});return task;
}
async perform(now){
const config=this.store.settings();if(!config.enabled)return;
const settings=this.settings(),calendar=this.calendar(),signature=JSON.stringify([settings.schedule,calendar]);let schedule=this.store.scheduleState();
if(!schedule||schedule.signature!==signature){this.resetSchedule(now);return;}if(!schedule.next||Date.parse(schedule.next)>now.getTime())return;
const due=Date.parse(schedule.next);if(now.getTime()-due>60000){this.resetSchedule(now);return;}
const industries=settings.groups.filter(g=>g.to?.length).map(g=>g.productLine);this.store.blockUnmapped(industries);const grouped=new Map();for(const item of this.store.pending(config.maxAttempts,industries)){const group=settings.groups.find(g=>g.productLine===item.industry);if(!group||!group.to?.length){this.store.block(item.id,'此产业尚未配置接口人收件组。');continue;}const key=settings.schedule.mode==='order'?item.id:item.industry;if(!grouped.has(key))grouped.set(key,{items:[],group});if(grouped.get(key).items.length<50)grouped.get(key).items.push(item);}
if(!grouped.size){this.resetSchedule(now);return;}
await this.mail.exclusive(config.channelId,async()=>{
this.resetSchedule(now);
for(const {items,group}of grouped.values()){
if(this.stopped)break;
const channel=this.mail.store.channelSecret(config.channelId),snapshot={channelId:channel.id,industry:items[0].industry,sourceIds:items.map(i=>i.sourceId),subject:items.length===1?items[0].subject:items[0].industry+'业务通知',text:items.map(i=>i.brief).join('\n\n'),to:[...group.to],cc:[...group.cc],slot:new Date(due).toISOString()};
const batch=this.store.prepare(items,snapshot);let transport,sending=false;
try{transport=this.mail.smtpFactory(channel.smtp);await transport.verify();this.mail.store.guard(channel.id,channel.version);this.store.beginSending(batch);sending=true;
const result=await transport.sendMail({from:{address:channel.email,name:channel.name},to:snapshot.to,cc:snapshot.cc,subject:snapshot.subject,text:snapshot.text,attachments:[],messageId:`<${batch}@${channel.email.split('@')[1]}>`,disableFileAccess:true,disableUrlAccess:true});
const accepted=new Set((result.accepted||[]).map(a=>String(a.address||a).toLowerCase()));if(result.rejected?.length||[...snapshot.to,...snapshot.cc].some(a=>!accepted.has(a.toLowerCase()))){this.store.mark(batch,'unknown','部分收件人未被邮件服务器接受,请核实后处理。');}else this.store.mark(batch,'sent');
}catch{this.store.mark(batch,sending?'unknown':'failed',sending?'发信结果待核实,请查询邮件服务器记录。':'SMTP 连接检查失败,请查看通知队列的重试状态。');}finally{transport?.close?.();}
}
}).catch(e=>{if(e.status!==409)throw e;});
}
retryItem(id,input,actor){if(this.running)fail('通知正在发送,请稍后重新入队。',409);return this.store.retryItem(id,input,actor);}
resolve(id,input,actor){if(this.running)fail('通知正在发送,请稍后核验。',409);return this.store.resolve(id,input,actor);}
start(){this.timer=setInterval(()=>this.tick().catch(()=>{}),30000);this.timer.unref?.();}
async stop(){this.stopped=true;clearInterval(this.timer);if(this.running)await this.running.catch(()=>{});}
}
module.exports={NotificationService};
+58
View File
@@ -0,0 +1,58 @@
'use strict';
const fail=(message,status=400)=>{throw Object.assign(new Error(message),{status});};
const fs=require('node:fs');const path=require('node:path');const crypto=require('node:crypto');const {DatabaseSync}=require('node:sqlite');const {BpmVault}=require('../../platform/security/vault.cjs');const {DEFAULT_MAP,validateMap,hash}=require('./mapping.cjs');
class NotificationStore{
constructor(dir){fs.mkdirSync(dir,{recursive:true});this.vault=new BpmVault({stateDir:path.join(dir,'secrets')});this.db=new DatabaseSync(path.join(dir,'notifications.sqlite'));this.db.exec(`PRAGMA journal_mode=WAL; PRAGMA busy_timeout=5000; PRAGMA secure_delete=ON;
CREATE TABLE IF NOT EXISTS settings(id INTEGER PRIMARY KEY CHECK(id=1),value TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS items(id TEXT PRIMARY KEY,payload BLOB NOT NULL,status TEXT NOT NULL,attempts INTEGER NOT NULL DEFAULT 0,error TEXT,batch_id TEXT,created_at TEXT NOT NULL,industry TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS batches(id TEXT PRIMARY KEY,status TEXT NOT NULL,payload BLOB NOT NULL,error TEXT,created_at TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS metadata(key TEXT PRIMARY KEY,value TEXT NOT NULL);
`);
this.db.exec('CREATE TABLE IF NOT EXISTS retry_audit(id TEXT PRIMARY KEY,item_id TEXT NOT NULL,payload BLOB NOT NULL)');
this.db.prepare('INSERT OR IGNORE INTO settings VALUES(1,?)').run(JSON.stringify({version:1,enabled:false,sourceKey:crypto.randomUUID(),sourceName:'业务来源',channelId:'',fieldMap:DEFAULT_MAP,maxAttempts:3}));
if(!this.db.prepare('PRAGMA table_info(items)').all().some(c=>c.name==='industry')){this.db.exec("ALTER TABLE items ADD COLUMN industry TEXT NOT NULL DEFAULT ''");for(const row of this.db.prepare('SELECT id,payload FROM items').all())this.db.prepare('UPDATE items SET industry=? WHERE id=?').run(this.decode(row.id,row.payload).industry,row.id);}
this.db.exec('CREATE INDEX IF NOT EXISTS notification_pending ON items(status,industry,attempts)');
}
recover(){return this.transaction(()=>{
this.db.exec("UPDATE items SET status='pending' WHERE status='reserved' AND batch_id IN(SELECT id FROM batches WHERE status='prepared'); UPDATE items SET status='unknown',error='服务重启,发送结果需要核实。' WHERE batch_id IN(SELECT id FROM batches WHERE status='sending') AND status IN('reserved','sending'); UPDATE batches SET status='cancelled' WHERE status='prepared'; UPDATE batches SET status='unknown',error='服务重启,发送结果需要核实。' WHERE status='sending';");this.exhaustEligible();
});}
exhaustEligible(){this.db.prepare("UPDATE items SET status='exhausted',error='SMTP 检查尝试次数已耗尽,请管理员核实修复后单项重新入队。' WHERE status IN('pending','blocked') AND attempts>=?").run(this.settings().maxAttempts);}
transaction(work){this.db.exec('BEGIN IMMEDIATE');try{const result=work();this.db.exec('COMMIT');return result;}catch(e){this.db.exec('ROLLBACK');throw e;}}
encode(id,data){try{return this.vault.encode(id,data);}catch{fail('通知内容加密不可用,请联系管理员。',503);}}
decode(id,data){try{return this.vault.decode(id,data);}catch{fail('通知内容无法解密,请联系管理员。',503);}}
settings(){return JSON.parse(this.db.prepare('SELECT value FROM settings WHERE id=1').get().value);}
saveSettings(input){const old=this.settings();if(!input||Object.keys(input).some(k=>!['version','enabled','sourceKey','sourceName','channelId','fieldMap','maxAttempts'].includes(k)))fail('通知配置无效。');if(input.version!==old.version)fail('通知配置已被修改,请刷新。',409);if(typeof input.enabled!=='boolean'||typeof input.sourceName!=='string'||!input.sourceName.trim()||input.sourceName.length>80||typeof input.channelId!=='string'||input.channelId.length>100||!Number.isInteger(input.maxAttempts)||input.maxAttempts<1||input.maxAttempts>10)fail('请检查来源、邮箱和尝试次数(1–10)。');if(input.sourceKey&&input.sourceKey!==old.sourceKey)fail('来源标识不能更改。');const saved={...input,sourceKey:old.sourceKey,sourceName:input.sourceName.trim(),fieldMap:validateMap(input.fieldMap),version:old.version+1};return this.transaction(()=>{this.db.prepare('UPDATE settings SET value=? WHERE id=1').run(JSON.stringify(saved));this.exhaustEligible();return saved;});}
scheduleState(){const row=this.db.prepare("SELECT value FROM metadata WHERE key='schedule'").get();return row?JSON.parse(row.value):null;}
setSchedule(value){this.db.prepare("INSERT INTO metadata VALUES('schedule',?) ON CONFLICT(key) DO UPDATE SET value=excluded.value").run(JSON.stringify(value));}
ingest(event){const id=hash([event.sourceKey,event.sourceId,event.sourceVersion,event.industry].join('\0')),old=this.db.prepare('SELECT * FROM items WHERE id=?').get(id);if(old){const value=this.decode(id,old.payload);if(value.brief!==event.brief||value.subject!==event.subject)fail('同一通知版本的内容已变化,请核实来源版本。',409);return this.item(old);}this.db.prepare('INSERT INTO items(id,payload,status,created_at,industry) VALUES(?,?,?,?,?)').run(id,this.encode(id,event),'pending',new Date().toISOString(),event.industry);return this.item(this.db.prepare('SELECT * FROM items WHERE id=?').get(id));}
item({payload,...row}){return {...this.decode(row.id,payload),...row};}
items(){return this.db.prepare('SELECT * FROM items ORDER BY rowid DESC LIMIT 100').all().map(r=>this.item(r));}
pending(attempts,industries){if(!industries.length)return [];return this.db.prepare("SELECT * FROM items WHERE status IN('pending','blocked') AND attempts<? AND industry IN("+industries.map(()=>'?').join(',')+") ORDER BY rowid LIMIT 200").all(attempts,...industries).map(r=>this.item(r));}
blockUnmapped(industries){this.db.prepare("UPDATE items SET status='blocked',error='此产业尚未配置接口人收件组。' WHERE status IN('pending','blocked')"+(industries.length?' AND industry NOT IN('+industries.map(()=>'?').join(',')+')':'')).run(...industries);}
block(id,message){this.db.prepare("UPDATE items SET status='blocked',error=? WHERE id=? AND status IN('pending','blocked')").run(message,id);}
prepare(items,snapshot){return this.transaction(()=>{const id=crypto.randomUUID();this.db.prepare('INSERT INTO batches VALUES(?,?,?,?,?)').run(id,'prepared',this.encode(id,{...snapshot,itemIds:items.map(item=>item.id)}),null,new Date().toISOString());for(const item of items){const result=this.db.prepare("UPDATE items SET status='reserved',attempts=attempts+1,error=NULL,batch_id=? WHERE id=? AND status IN('pending','blocked') AND attempts<?").run(id,item.id,this.settings().maxAttempts);if(!result.changes)fail('通知队列已变化,请重试。',409);}return id;});}
beginSending(id){return this.transaction(()=>{
const batch=this.db.prepare('SELECT * FROM batches WHERE id=?').get(id);if(!batch||batch.status!=='prepared')fail('通知批次预留已失效。',409);
const expected=this.decode(id,batch.payload).itemIds,rows=this.db.prepare('SELECT id,status FROM items WHERE batch_id=?').all(id);
if(!expected?.length||rows.length!==expected.length||rows.some(row=>row.status!=='reserved'||!expected.includes(row.id)))fail('通知队列预留归属已变化。',409);
const changed=this.db.prepare("UPDATE batches SET status='sending' WHERE id=? AND status='prepared'").run(id);if(!changed.changes)fail('通知批次已变化。',409);
this.db.prepare("UPDATE items SET status='sending' WHERE batch_id=? AND status='reserved'").run(id);return true;
});}
mark(id,status,message=null){if(status==='sending')return this.beginSending(id);return this.transaction(()=>{
const row=this.db.prepare('SELECT status FROM batches WHERE id=?').get(id);if(!row||!['prepared','sending'].includes(row.status))return false;
if(status==='failed'&&row.status!=='prepared')return false;
this.db.prepare('UPDATE batches SET status=?,error=? WHERE id=?').run(status,message,id);
this.db.prepare("UPDATE items SET status=?,error=? WHERE batch_id=? AND status IN('reserved','sending')").run(status==='failed'?'pending':status,message,id);if(status==='failed')this.exhaustEligible();return true;
});}
retryItem(id,input,actor){return this.transaction(()=>{
if(!input||typeof input.note!=='string'||input.note.trim().length<5||input.note.length>2000||!(input.batchId===null||typeof input.batchId==='string')||!actor)fail('请填写至少 5 字的修复核验依据及当前批次。');
const row=this.db.prepare('SELECT * FROM items WHERE id=?').get(id);if(!row||row.status!=='exhausted'||row.batch_id!==input.batchId)fail('只能重新入队当前次数耗尽的通知,请刷新。',409);
const changed=this.db.prepare("UPDATE items SET status='pending',attempts=0,error=NULL,batch_id=NULL WHERE id=? AND status='exhausted' AND batch_id IS ?").run(id,input.batchId);if(!changed.changes)fail('通知已被修改,请刷新。',409);
const auditId=crypto.randomUUID();this.db.prepare('INSERT INTO retry_audit VALUES(?,?,?)').run(auditId,id,this.encode(auditId,{itemId:id,batchId:input.batchId,note:input.note.trim(),actor,at:new Date().toISOString()}));return {ok:true};
});}
batches(){return this.db.prepare("SELECT * FROM batches ORDER BY CASE status WHEN 'unknown' THEN 0 ELSE 1 END,rowid DESC LIMIT 100").all().map(({payload,...r})=>({...this.decode(r.id,payload),...r}));}
resolve(id,input,actor){return this.transaction(()=>{const row=this.db.prepare('SELECT * FROM batches WHERE id=?').get(id);if(!row||row.status!=='unknown')fail('只能核验结果待核实的批次。',409);if(!input||!['sent','retry'].includes(input.resolution)||typeof input.note!=='string'||input.note.trim().length<5||input.note.length>2000)fail('请选择核验结果并填写至少 5 字的核验依据。');const data={...this.decode(id,row.payload),resolution:input.resolution,note:input.note.trim(),resolvedBy:actor,resolvedAt:new Date().toISOString()};this.db.prepare('UPDATE batches SET status=?,payload=? WHERE id=?').run('resolved_'+input.resolution,this.encode(id,data),id);this.db.prepare('UPDATE items SET status=?,error=NULL,attempts=CASE WHEN ? THEN 0 ELSE attempts END WHERE batch_id=?').run(input.resolution==='sent'?'sent':'pending',input.resolution==='retry'?1:0,id);return {ok:true};});}
close(){if(this.db){this.db.close();this.db=null;}this.vault.destroy();}
}
module.exports={NotificationStore};
@@ -1,9 +1,16 @@
'use strict';
const {test}=require('node:test');const assert=require('node:assert/strict');const fs=require('node:fs/promises');const os=require('node:os');const path=require('node:path');const {Store}=require('../../src/modules/dashboard/store.cjs');const {NotificationStore,NotificationService}=require('../../src/modules/notifications/notifications.cjs');const {createDashboard}=require('../../src/app/dashboard.cjs');
const {test}=require('node:test');const assert=require('node:assert/strict');const fs=require('node:fs/promises');const os=require('node:os');const path=require('node:path');const {Store}=require('../../src/modules/dashboard/store.cjs');const {NotificationStore}=require('../../src/modules/notifications/store.cjs');const {NotificationService}=require('../../src/modules/notifications/service.cjs');const {WorkspaceStore}=require('../../src/modules/workspace/store.cjs');const {createDashboard}=require('../../src/app/dashboard.cjs');
test('notification HTTP exposes configurable mapping and preview without sending, with role and origin checks',async t=>{
const dir=await fs.mkdtemp(path.join(os.tmpdir(),'notice-http-')),store=new Store(path.join(dir,'users'));store.bootstrap('admin','Test-password-123');for(const role of ['operator','viewer'])store.addUser({username:role,password:'Test-password-123',role},'admin');const notificationStore=new NotificationStore(path.join(dir,'notifications')),notifications=new NotificationService({store:notificationStore,mail:{},settings:()=>store.settings()}),server=createDashboard({store,notifications,outputDir:dir});await new Promise(r=>server.listen(0,'127.0.0.1',r));t.after(async()=>{await notifications.stop();server.closeAllConnections();await new Promise(r=>server.close(r));notificationStore.close();await fs.rm(dir,{recursive:true,force:true});});const base='http://127.0.0.1:'+server.address().port,call=(route,body,cookie,origin=base)=>fetch(base+route,{method:body===undefined?'GET':'POST',headers:{...(body===undefined?{}:{'Content-Type':'application/json',Origin:origin}),...(cookie?{Cookie:cookie}:{})},body:body===undefined?undefined:JSON.stringify(body)}),login=async username=>(await call('/api/login',{username,password:'Test-password-123'})).headers.get('set-cookie').split(';')[0];
const dir=await fs.mkdtemp(path.join(os.tmpdir(),'notice-http-')),store=new Store(path.join(dir,'users'));store.bootstrap('admin','Test-password-123');for(const role of ['operator','viewer'])store.addUser({username:role,password:'Test-password-123',role},'admin');const notificationStore=new NotificationStore(path.join(dir,'notifications')),notifications=new NotificationService({store:notificationStore,mail:{},settings:()=>store.settings()}),workspace=new WorkspaceStore(path.join(dir,'workspace')),server=createDashboard({store,notifications,workspace,outputDir:dir});await new Promise(r=>server.listen(0,'127.0.0.1',r));t.after(async()=>{await notifications.stop();server.closeAllConnections();await new Promise(r=>server.close(r));notificationStore.close();workspace.close();await fs.rm(dir,{recursive:true,force:true});});const base='http://127.0.0.1:'+server.address().port,call=(route,body,cookie,origin=base)=>fetch(base+route,{method:body===undefined?'GET':'POST',headers:{...(body===undefined?{}:{'Content-Type':'application/json',Origin:origin}),...(cookie?{Cookie:cookie}:{})},body:body===undefined?undefined:JSON.stringify(body)}),login=async username=>(await call('/api/login',{username,password:'Test-password-123'})).headers.get('set-cookie').split(';')[0];
assert.equal((await call('/api/notifications')).status,401);const admin=await login('admin'),operator=await login('operator'),viewer=await login('viewer');const config=notificationStore.settings();assert.equal((await call('/api/notifications/settings',config,operator)).status,403);assert.equal((await call('/api/notifications/settings',config,admin,'https://evil.test')).status,403);assert.equal((await call('/api/notifications/settings',config,admin)).status,200);
const data={id:'external-1',industry:'待配置产业',brief:'简要说明原文\n保持换行'};const preview=await(await call('/api/notifications/preview',{data},operator)).json();assert.equal(preview.event.brief,data.brief);assert.equal(notificationStore.items().length,0);
assert.equal((await call('/api/notifications/events',{data},viewer)).status,403);assert.equal((await call('/api/notifications/events',{data},operator)).status,201);assert.equal(notificationStore.items().length,1);
assert.equal((await call('/api/notifications',undefined,viewer)).status,200);
notificationStore.saveSettings({...notificationStore.settings(),maxAttempts:1});const item=notificationStore.items()[0],batchId=notificationStore.prepare([item],{to:[]});notificationStore.mark(batchId,'failed','fixture SMTP failure');
const retry='/api/notifications/items/'+item.id+'/retry',evidence={batchId,note:'已修复 SMTP 并核实未发送'};
assert.equal((await call(retry,evidence,viewer)).status,403);assert.equal((await call(retry,evidence,operator)).status,403);
assert.equal((await call(retry,{batchId,note:''},admin)).status,400);assert.equal((await call(retry,evidence,admin)).status,200);
assert.equal(notificationStore.items()[0].attempts,0);assert.equal((await call(retry,evidence,admin)).status,409);
const audit=workspace.db.prepare('SELECT value FROM audit').all().map(row=>JSON.parse(row.value));assert.equal(audit.filter(row=>row.action==='重新入队次数耗尽通知').length,1);assert.match(audit.find(row=>row.action==='重新入队次数耗尽通知').detail,/SMTP/);
});
@@ -1,5 +1,6 @@
'use strict';
const {test}=require('node:test');const assert=require('node:assert/strict');const fs=require('node:fs/promises');const os=require('node:os');const path=require('node:path');const {Store}=require('../../src/modules/dashboard/store.cjs');const {NotificationStore,NotificationService}=require('../../src/modules/notifications/notifications.cjs');const {createDashboard}=require('../../src/app/dashboard.cjs');const {loadPlaywright,browserOptions}=require('../../src/platform/browser.cjs');
const retryHistory=(store,id)=>store.db.prepare('SELECT id,payload FROM retry_audit WHERE item_id=? ORDER BY rowid').all(id).map(row=>store.decode(row.id,row.payload));
const {test}=require('node:test');const assert=require('node:assert/strict');const fs=require('node:fs/promises');const os=require('node:os');const path=require('node:path');const {Store}=require('../../src/modules/dashboard/store.cjs');const {NotificationStore}=require('../../src/modules/notifications/store.cjs');const {NotificationService}=require('../../src/modules/notifications/service.cjs');const {createDashboard}=require('../../src/app/dashboard.cjs');const {loadPlaywright,browserOptions}=require('../../src/platform/browser.cjs');
test('notification page saves field maps and previews original text without queuing or sending',{timeout:90000},async t=>{
const dir=await fs.mkdtemp(path.join(os.tmpdir(),'notice-ui-')),store=new Store(path.join(dir,'users'));store.bootstrap('admin','Test-password-123');const notificationStore=new NotificationStore(path.join(dir,'notifications')),notifications=new NotificationService({store:notificationStore,mail:{},settings:()=>store.settings()}),server=createDashboard({store,notifications,outputDir:dir});await new Promise(r=>server.listen(0,'127.0.0.1',r));let browser;t.after(async()=>{await browser?.close();await notifications.stop();server.closeAllConnections();await new Promise(r=>server.close(r));notificationStore.close();await fs.rm(dir,{recursive:true,force:true});});browser=await loadPlaywright().chromium.launch(browserOptions());const page=await browser.newPage({viewport:{width:1440,height:1000}});page.setDefaultTimeout(6000);const errors=[];page.on('pageerror',e=>errors.push(e.message));await page.goto('http://127.0.0.1:'+server.address().port+'/#notifications');await page.getByLabel('账号',{exact:true}).fill('admin');await page.getByLabel('密码',{exact:true}).fill('Test-password-123');await page.getByRole('button',{name:'登录',exact:true}).click();await page.getByLabel('来源名称',{exact:true}).fill('团队业务');await page.getByRole('button',{name:'保存通知配置',exact:true}).click();await page.getByText('业务通知配置已保存。',{exact:true}).waitFor();assert.equal(notificationStore.settings().sourceName,'团队业务');await page.getByLabel('来源 JSON 样本',{exact:true}).fill(JSON.stringify({id:'SAMPLE-001',industry:'同事后续配置',brief:'原始简要说明\n第二行'}));await page.getByRole('button',{name:'预览字段映射',exact:true}).click();await page.locator('#notification-preview pre').getByText('原始简要说明\n第二行',{exact:true}).waitFor();assert.equal(notificationStore.items().length,0);assert.equal(await page.locator('#notification-preview pre').evaluate(el=>{const node=el.firstChild,index=node.textContent.indexOf('\n'),range=document.createRange();range.setStart(node,0);range.setEnd(node,1);const first=range.getBoundingClientRect().top;range.setStart(node,index+1);range.setEnd(node,index+2);return range.getBoundingClientRect().top>first;}),true);await fs.mkdir(path.resolve('output/verification'),{recursive:true});await page.evaluate(()=>window.scrollTo(0,0));await page.screenshot({path:path.resolve('output/verification/notifications-desktop.png'),fullPage:true});await page.setViewportSize({width:390,height:844});assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true);assert.deepEqual(errors,[]);
const dir=await fs.mkdtemp(path.join(os.tmpdir(),'notice-ui-')),store=new Store(path.join(dir,'users'));store.bootstrap('admin','Test-password-123');const notificationStore=new NotificationStore(path.join(dir,'notifications')),notifications=new NotificationService({store:notificationStore,mail:{},settings:()=>store.settings()}),server=createDashboard({store,notifications,outputDir:dir});await new Promise(r=>server.listen(0,'127.0.0.1',r));let browser;t.after(async()=>{await browser?.close();await notifications.stop();server.closeAllConnections();await new Promise(r=>server.close(r));notificationStore.close();await fs.rm(dir,{recursive:true,force:true});});browser=await loadPlaywright().chromium.launch(browserOptions());const page=await browser.newPage({viewport:{width:1440,height:1000}});page.setDefaultTimeout(6000);const errors=[];page.on('pageerror',e=>errors.push(e.message));await page.goto('http://127.0.0.1:'+server.address().port+'/#notifications');await page.getByLabel('账号',{exact:true}).fill('admin');await page.getByLabel('密码',{exact:true}).fill('Test-password-123');await page.getByRole('button',{name:'登录',exact:true}).click();await page.getByLabel('来源名称',{exact:true}).fill('团队业务');await page.getByRole('button',{name:'保存通知配置',exact:true}).click();await page.getByText('业务通知配置已保存。',{exact:true}).waitFor();assert.equal(notificationStore.settings().sourceName,'团队业务');await page.getByLabel('来源 JSON 样本',{exact:true}).fill(JSON.stringify({id:'SAMPLE-001',industry:'同事后续配置',brief:'原始简要说明\n第二行'}));await page.getByRole('button',{name:'预览字段映射',exact:true}).click();await page.locator('#notification-preview pre').getByText('原始简要说明\n第二行',{exact:true}).waitFor();assert.equal(notificationStore.items().length,0);assert.equal(await page.locator('#notification-preview pre').evaluate(el=>{const node=el.firstChild,index=node.textContent.indexOf('\n'),range=document.createRange();range.setStart(node,0);range.setEnd(node,1);const first=range.getBoundingClientRect().top;range.setStart(node,index+1);range.setEnd(node,index+2);return range.getBoundingClientRect().top>first;}),true);const retryItem=notifications.ingest({id:'RETRY-UI',industry:'宽带',brief:'仅重新入队此项'});notificationStore.saveSettings({...notificationStore.settings(),maxAttempts:1});const retryBatch=notificationStore.prepare([retryItem],{to:[]});notificationStore.mark(retryBatch,'failed','fixture SMTP failure');await page.reload();await page.getByText('次数已耗尽',{exact:true}).waitFor();await page.getByLabel('修复核验依据',{exact:true}).fill('已修复 SMTP 并核实未发送');await page.getByRole('button',{name:'重新入队此通知',exact:true}).click();await page.getByText('通知已重新入队。',{exact:true}).waitFor();assert.equal(notificationStore.items()[0].status,'pending');assert.equal(retryHistory(notificationStore,retryItem.id).length,1);await fs.mkdir(path.resolve('output/verification'),{recursive:true});await page.evaluate(()=>window.scrollTo(0,0));await page.screenshot({path:path.resolve('output/verification/notifications-desktop.png'),fullPage:true});await page.setViewportSize({width:390,height:844});assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true);assert.deepEqual(errors,[]);
});
+42 -1
View File
@@ -1,6 +1,7 @@
'use strict';
const retryHistory=(store,id)=>store.db.prepare('SELECT id,payload FROM retry_audit WHERE item_id=? ORDER BY rowid').all(id).map(row=>store.decode(row.id,row.payload));
const {test}=require('node:test');const assert=require('node:assert/strict');const fs=require('node:fs/promises');const os=require('node:os');const path=require('node:path');
const {NotificationStore,NotificationService}=require('../../src/modules/notifications/notifications.cjs');const {MailStore}=require('../../src/modules/mail/store.cjs');const {MailService}=require('../../src/modules/mail/service.cjs');
const {NotificationStore}=require('../../src/modules/notifications/store.cjs');const {NotificationService}=require('../../src/modules/notifications/service.cjs');const {MailStore}=require('../../src/modules/mail/store.cjs');const {MailService}=require('../../src/modules/mail/service.cjs');
const fields={id:'flow.id',version:'flow.version',industry:'flow.industry',brief:'flow.brief',subject:'flow.subject',occurredAt:'flow.at'};
const event={flow:{id:'FLOW-001',version:'v1',industry:'宽带',brief:'第一行原文\n第二行 <原文> & 保留',subject:'业务提醒',at:'2026-10-17T08:00:00+08:00'}};
async function fixture(t,{uncertain=false}={}){const dir=await fs.mkdtemp(path.join(os.tmpdir(),'order-notice-'));const store=new NotificationStore(path.join(dir,'notifications'));const mailStore=new MailStore(path.join(dir,'mail'));const channel=mailStore.saveChannel({name:'工作邮箱',email:'notice@company.test',smtp:{host:'smtp.company.test',port:465,security:'tls',user:'notice',password:'Fixture-only-password'},imap:{host:'imap.company.test',port:993,security:'tls',user:'notice',password:'Fixture-only-password'},senders:['author@company.test'],enabled:false});mailStore.setState(channel.id,1,{smtp:true});const sent=[];const mail=new MailService({store:mailStore,smtpFactory:()=>({verify:async()=>{},sendMail:async payload=>{sent.push(payload);if(uncertain)throw new Error('socket disconnected');return {accepted:['interface@company.test'],rejected:[]};},close(){}})});const service=new NotificationService({store,mail,settings:()=>({schedule:{times:['09:00','17:00'],timezone:'Asia/Shanghai',weekends:true,mode:'product_line',missed:'next_window'},groups:[{productLine:'宽带',to:['interface@company.test'],cc:[]}]}),calendar:()=>[],clock:()=>new Date('2026-10-17T08:59:00+08:00')});service.saveSettings({version:1,enabled:false,sourceName:'业务流程',channelId:channel.id,fieldMap:fields,maxAttempts:3});t.after(async()=>{await service.stop();await mail.stop();store.close();mailStore.close();await fs.rm(dir,{recursive:true,force:true});});return {dir,store,service,sent,channel};}
@@ -50,3 +51,43 @@ test('field mapping supports Chinese object keys and rejects control characters
const sample={来源:{编号:'FLOW-CN',产业:'宽带',简要说明:'中文字段原文'}};assert.equal(service.preview(sample).event.brief,'中文字段原文');
assert.throws(()=>service.ingest({来源:{...sample.来源,编号:'bad\u0000identity'}}),e=>e.status===400);
});
test('opening another store never recovers the active owner reservation',async t=>{
const {dir,store,service}=await fixture(t);const item=service.ingest(event),batch=store.prepare([item],{to:[],sourceIds:[item.sourceId]});
const other=new NotificationStore(path.join(dir,'notifications'));try{
assert.equal(other.items()[0].status,'reserved');assert.equal(other.batches()[0].status,'prepared');assert.equal(store.items()[0].batch_id,batch);
}finally{other.close();}
});
test('cancelled or stolen reservations cannot cross the SMTP send boundary',async t=>{
const {store,service,sent}=await fixture(t);service.ingest(event);service.saveSettings({...store.settings(),enabled:true});
service.mail.smtpFactory=()=>({verify:async()=>{store.db.prepare("UPDATE batches SET status='cancelled' WHERE status='prepared'").run();},sendMail:async payload=>{sent.push(payload);return {accepted:['interface@company.test']};},close(){}});
await service.tick(new Date('2026-10-17T09:00:00+08:00'));assert.equal(sent.length,0);assert.equal(store.batches()[0].status,'cancelled');
const item=service.ingest({flow:{...event.flow,id:'stolen'}}),batch=store.prepare([item],{to:[]});store.db.prepare("UPDATE items SET batch_id='another-owner' WHERE id=?").run(item.id);
assert.throws(()=>store.beginSending(batch),e=>e.status===409);assert.equal(store.batches().find(b=>b.id===batch).status,'prepared');
});
test('last SMTP verification failure exhausts one item until evidence-based CAS manual retry',async t=>{
const {store,service,sent}=await fixture(t);const item=service.ingest(event);service.saveSettings({...store.settings(),enabled:true,maxAttempts:1});
service.mail.smtpFactory=()=>({verify:async()=>{throw new Error('fixture verification failure');},close(){}});
await service.tick(new Date('2026-10-17T09:00:00+08:00'));assert.equal(store.items()[0].status,'exhausted');assert.doesNotMatch(store.items()[0].error,/等待下个时段/);assert.doesNotMatch(store.batches()[0].error,/等待下个时段/);
await service.tick(new Date('2026-10-17T17:00:00+08:00'));assert.equal(store.items()[0].attempts,1);assert.equal(sent.length,0);
const batchId=store.items()[0].batch_id;assert.throws(()=>service.retryItem(item.id,{batchId,note:''},'admin'),e=>e.status===400);
service.retryItem(item.id,{batchId,note:'已修复 SMTP 并完成连接检查'},'admin');assert.equal(store.items()[0].status,'pending');assert.equal(store.items()[0].attempts,0);
assert.throws(()=>service.retryItem(item.id,{batchId,note:'重复提交同一核验依据'},'admin'),e=>e.status===409);
const audit=retryHistory(store,item.id);assert.equal(audit.length,1);assert.equal(audit[0].actor,'admin');assert.match(audit[0].note,/SMTP/);
service.mail.smtpFactory=()=>({verify:async()=>{},sendMail:async payload=>{sent.push(payload);return {accepted:['interface@company.test'],rejected:[]};},close(){}});await service.tick(new Date('2026-10-18T09:00:00+08:00'));assert.equal(sent.length,1);assert.equal(store.items()[0].status,'sent');
});
test('explicit recovery and lowering attempts normalize eligible work but never revive unknown or exhausted',async t=>{
const {store,service}=await fixture(t);const item=service.ingest(event),batch=store.prepare([item],{to:[]});
store.saveSettings({...store.settings(),maxAttempts:1});store.recover();assert.equal(store.items()[0].status,'exhausted');
store.saveSettings({...store.settings(),maxAttempts:3});assert.equal(store.items()[0].status,'exhausted');
service.retryItem(item.id,{batchId:batch,note:'已核实未发送且完成连接修复'},'admin');const second=store.prepare(store.items(),{to:[]});store.beginSending(second);store.recover();assert.equal(store.items()[0].status,'unknown');
assert.throws(()=>service.retryItem(item.id,{batchId:second,note:'不能绕过未知发送结果核验'},'admin'),e=>e.status===409);
store.recover();assert.equal(store.items()[0].status,'unknown');
const next=service.ingest({flow:{...event.flow,id:'lower-limit'}});store.db.prepare('UPDATE items SET attempts=2 WHERE id=?').run(next.id);store.saveSettings({...store.settings(),maxAttempts:2});assert.equal(store.items().find(i=>i.id===next.id).status,'exhausted');
});
test('manual retry resets only one exhausted item and stale evidence cannot reset its next failure',async t=>{
const {store,service}=await fixture(t);const first=service.ingest(event),second=service.ingest({flow:{...event.flow,id:'SECOND'}});store.saveSettings({...store.settings(),maxAttempts:1});const batchId=store.prepare([first,second],{to:[]});store.mark(batchId,'failed','fixture verify failure');
const input={batchId,note:'已修复 SMTP 并核实第一项未发送'};service.retryItem(first.id,input,'admin');assert.equal(store.items().find(i=>i.id===first.id).attempts,0);assert.equal(store.items().find(i=>i.id===second.id).status,'exhausted');assert.equal(store.items().find(i=>i.id===second.id).attempts,1);
const nextBatch=store.prepare([store.items().find(i=>i.id===first.id)],{to:[]});store.mark(nextBatch,'failed','fixture verify failure again');assert.throws(()=>service.retryItem(first.id,input,'admin'),e=>e.status===409);assert.equal(store.items().find(i=>i.id===first.id).attempts,1);assert.equal(retryHistory(store,first.id).length,1);
});
+3 -2
View File
@@ -2,14 +2,15 @@
'use strict';
let api,esc,formatDate,notify,user;
const names={id:'通知 ID 字段',version:'版本字段',industry:'产业字段',brief:'简要说明字段',subject:'标题字段',occurredAt:'业务时间字段'};
const labels={pending:'待发送',blocked:'配置待补全',reserved:'处理中',sent:'已提交邮件服务器',unknown:'结果待核实',prepared:'已准备',sending:'发送中',failed:'检查失败',cancelled:'已取消',resolved_sent:'已核实发送',resolved_retry:'已核实重新入队'};
const labels={exhausted:'次数已耗尽',pending:'待发送',blocked:'配置待补全',reserved:'处理中',sent:'已提交邮件服务器',unknown:'结果待核实',prepared:'已准备',sending:'发送中',failed:'检查失败',cancelled:'已取消',resolved_sent:'已核实发送',resolved_retry:'已核实重新入队'};
async function render(container,isActive=()=>true){
const [data,mail,business]=await Promise.all([api('/notifications'),api('/mail').catch(()=>({channels:[]})),api('/settings')]);if(!isActive())return;
const config=data.settings,admin=user.role==='admin',operator=user.role!=='viewer',disabled=admin?'':'disabled';
container.innerHTML=`<div class="alert alert-${config.enabled?'success':'info'}">业务通知${config.enabled?'已启用,将按发送计划处理已入队数据':'已暂停,可先配置并预览字段映射'}</div><form class="card mb-3" id="notification-settings"><div class="card-header"><h2 class="card-title">业务通知配置</h2><a class="btn btn-sm ms-auto" href="#schedule">发送计划</a></div><div class="card-body"><div class="row g-3"><div class="col-md-6"><label class="form-label" for="notification-source">来源名称</label><input class="form-control" id="notification-source" value="${esc(config.sourceName)}" maxlength="80" required ${disabled}></div><div class="col-md-6"><label class="form-label" for="notification-channel">发件邮箱</label><select class="form-select" id="notification-channel" ${disabled}><option value="">请选择托管邮箱</option>${mail.channels.map(c=>`<option value="${c.id}" ${config.channelId===c.id?'selected':''}>${esc(c.name)} · ${esc(c.email)}</option>`).join('')}</select><div class="form-hint"><a href="#mail">管理邮箱及检查 SMTP</a></div></div><div class="col-md-6"><label class="form-label" for="notification-attempts">连接检查失败尝试上限</label><input class="form-control" id="notification-attempts" type="number" min="1" max="10" value="${config.maxAttempts}" required ${disabled}><div class="form-hint">发送结果不明的批次需要人工核验。</div></div><div class="col-md-6"><div class="form-label">产业接口人</div><p class="mb-1">已配置 ${business.groups.length} 个收件组</p><a href="#templates">维护产业 / 产品线收件群组</a></div></div><hr><h3 class="card-title">来源字段映射</h3><div class="text-secondary mb-3">填写来源对象中的字段路径,例如 <code>flow.brief</code>。通知 ID、产业和简要说明为必填;正文直接使用简要说明原文。</div><div class="row g-3">${Object.entries(names).map(([key,label])=>`<div class="col-md-6"><label class="form-label" for="notification-field-${key}">${label}</label><input class="form-control" id="notification-field-${key}" value="${esc(config.fieldMap[key])}" maxlength="200" ${['id','industry','brief'].includes(key)?'required':''} ${disabled}></div>`).join('')}</div><label class="form-check form-switch mt-4"><input class="form-check-input" type="checkbox" id="notification-enabled" ${config.enabled?'checked':''} ${disabled}><span class="form-check-label">启用自动业务通知</span></label></div>${admin?'<div class="card-footer text-end"><button class="btn btn-primary" type="submit">保存通知配置</button></div>':''}</form>${operator?'<form class="card mb-3" id="notification-preview-form"><div class="card-header"><h3 class="card-title">来源样本预览</h3></div><div class="card-body"><label class="form-label" for="notification-sample">来源 JSON 样本</label><textarea class="form-control font-monospace" id="notification-sample" rows="6" required maxlength="80000"></textarea><div class="form-hint">按已保存的字段映射预览正文和收件人。预览不会创建通知或发送邮件。</div><div id="notification-preview" class="mt-3"></div></div><div class="card-footer text-end"><button class="btn" type="submit">预览字段映射</button></div></form>':''}<section class="card mb-3"><div class="card-header"><h3 class="card-title">通知队列</h3></div><div class="table-responsive"><table class="table card-table"><thead><tr><th>来源 / 产业</th><th>简要说明</th><th>状态</th></tr></thead><tbody>${data.items.map(i=>`<tr><td class="text-break">${esc(i.sourceId)}<div class="small text-secondary">${esc(i.industry)}</div></td><td class="text-break"><details><summary>${esc(i.subject)}</summary><pre class="notification-brief mt-2">${esc(i.brief)}</pre></details></td><td>${esc(labels[i.status]||i.status)}${i.error?`<div class="small text-danger">${esc(i.error)}</div>`:''}</td></tr>`).join('')||'<tr><td colspan="3" class="text-secondary">暂无通知。待来源字段与产业收件组确定后接入业务数据。</td></tr>'}</tbody></table></div></section><section class="card"><div class="card-header"><h3 class="card-title">发送批次与核验</h3></div><div class="list-group list-group-flush">${data.batches.map(b=>`<div class="list-group-item"><div class="d-flex flex-wrap gap-2"><strong>${esc(b.industry)} · ${esc(b.subject)}</strong><span class="ms-auto">${esc(labels[b.status]||b.status)}</span></div><div class="small text-secondary mt-2">${formatDate(b.created_at)} · ${esc(b.to.join('、'))}</div>${b.error?`<div class="text-danger mt-2">${esc(b.error)}</div>`:''}${b.status==='unknown'&&admin?`<form class="mt-3" data-notification-resolve="${b.id}"><label class="form-label" for="notice-resolution-${b.id}">核验结果</label><select class="form-select mb-2" id="notice-resolution-${b.id}" name="resolution"><option value="sent">已核实发送成功</option><option value="retry">已核实需要重发,重新入队</opti Line truncated
container.innerHTML=`<div class="alert alert-${config.enabled?'success':'info'}">业务通知${config.enabled?'已启用,将按发送计划处理已入队数据':'已暂停,可先配置并预览字段映射'}</div><form class="card mb-3" id="notification-settings"><div class="card-header"><h2 class="card-title">业务通知配置</h2><a class="btn btn-sm ms-auto" href="#schedule">发送计划</a></div><div class="card-body"><div class="row g-3"><div class="col-md-6"><label class="form-label" for="notification-source">来源名称</label><input class="form-control" id="notification-source" value="${esc(config.sourceName)}" maxlength="80" required ${disabled}></div><div class="col-md-6"><label class="form-label" for="notification-channel">发件邮箱</label><select class="form-select" id="notification-channel" ${disabled}><option value="">请选择托管邮箱</option>${mail.channels.map(c=>`<option value="${c.id}" ${config.channelId===c.id?'selected':''}>${esc(c.name)} · ${esc(c.email)}</option>`).join('')}</select><div class="form-hint"><a href="#mail">管理邮箱及检查 SMTP</a></div></div><div class="col-md-6"><label class="form-label" for="notification-attempts">连接检查失败尝试上限</label><input class="form-control" id="notification-attempts" type="number" min="1" max="10" value="${config.maxAttempts}" required ${disabled}><div class="form-hint">次数耗尽后仅管理员核实修复并单项重新入队;发送结果不明的批次需要人工核验。</div></div><div class="col-md-6"><div class="form-label">产业接口人</div><p class="mb-1">已配置 ${business.groups.length} 个收件组</p><a href="#templates">维护产业 / 产品线收件群组</a></div></div><hr><h3 class="card-title">来源字段映射</h3><div class="text-secondary mb-3">填写来源对象中的字段路径,例如 <code>flow.brief</code>。通知 ID、产业和简要说明为必填;正文直接使用简要说明原文。</div><div class="row g-3">${Object.entries(names).map(([key,label])=>`<div class="col-md-6"><label class="form-label" for="notification-field-${key}">${label}</label><input class="form-control" id="notification-field-${key}" value="${esc(config.fieldMap[key])}" maxlength="200" ${['id','industry','brief'].includes(key)?'required':''} ${disabled}></div>`).join('')}</div><label class="form-check form-switch mt-4"><input class="form-check-input" type="checkbox" id="notification-enabled" ${config.enabled?'checked':''} ${disabled}><span class="form-check-label">启用自动业务通知</span></label></div>${admin?'<div class="card-footer text-end"><button class="btn btn-primary" type="submit">保存通知配置</button></div>':''}</form>${operator?'<form class="card mb-3" id="notification-preview-form"><div class="card-header"><h3 class="card-title">来源样本预览</h3></div><div class="card-body"><label class="form-label" for="notification-sample">来源 JSON 样本</label><textarea class="form-control font-monospace" id="notification-sample" rows="6" required maxlength="80000"></textarea><div class="form-hint">按已保存的字段映射预览正文和收件人。预览不会创建通知或发送邮件。</div><div id="notification-preview" class="mt-3"></div></div><div class="card-footer text-end"><button class="btn" type="submit">预览字段映射</button></div></form>':''}<section class="card mb-3"><div class="card-header"><h3 class="card-title">通知队列</h3></div><div class="table-responsive"><table class="table card-table"><thead><tr><th>来源 / 产业</th><th>简要说明</th><th>状态</th></tr></thead><tbody>${data.items.map(i=>`<tr><td class="text-break">${esc(i.sourceId)}<div class="small text-secondary">${esc(i.industry)}</div></td><td class="text-break"><details><summary>${esc(i.subject)}</summary><pre class="notification-brief mt-2">${esc(i.brief)}</pre></details></td><td><span>${esc(labels[i.status]||i.status)}</span>${i.error?`<div class="small text-danger">${esc(i.error)}</div>`:''}${i.status==='exhausted'&&admin?`<form class="mt-2" data-notification-retry="${i.id}" data-batch-id="${esc(i.batch_id||'')}"><label class="form-label" for="retry-note-${i.id}">修复核验依据</label><textarea class="form-control mb-2" id="retry-note-${i.id}" name="note" required minlength="5" maxlength="2000"></textarea><button class="btn btn-sm" type="submit">重新入队此通知</button></form>`:''}</td></tr>`).join('')||'<tr><td colspan="3" class="text-secondary">暂无通知。待来源字段与产业收件组确定后接入业务数据。</td></tr>'}</tbody></table></div></section><section class="card"><div class="card-header"><h3 class="card-title">发送批次与核验</h3></div><div class="list-group list-group-flush">${data.batches.map(b=>`<div class="list-group-item"><div class="d-flex flex-wrap gap-2"><strong>${esc(b.industry)} · ${esc(b.subject)}</strong><span class="ms-auto">${esc(labels[b.status]||b.status)}</span></div><div class="small text-secondary mt-2">${forLine truncated
async function action(button,work){button.disabled=true;try{await work();}catch(e){if(isActive())notify(e.message,'danger');}finally{if(button.isConnected)button.disabled=false;}}
if(admin)container.querySelector('#notification-settings').onsubmit=e=>{e.preventDefault();return action(e.submitter,async()=>{const fieldMap=Object.fromEntries(Object.keys(names).map(k=>[k,container.querySelector('#notification-field-'+k).value.trim()]));await api('/notifications/settings',{...config,sourceName:container.querySelector('#notification-source').value,channelId:container.querySelector('#notification-channel').value,fieldMap,maxAttempts:Number(container.querySelector('#notification-attempts').value),enabled:container.querySelector('#notification-enabled').checked});if(isActive()){notify('业务通知配置已保存。');await render(container,isActive);}});};
if(operator)container.querySelector('#notification-preview-form').onsubmit=e=>{e.preventDefault();return action(e.submitter,async()=>{let sample;try{sample=JSON.parse(container.querySelector('#notification-sample').value);}catch{throw new Error('样本不是有效的 JSON 对象。');}const result=await api('/notifications/preview',{data:sample});if(!isActive())return;container.querySelector('#notification-preview').innerHTML=`${result.issues?.length?`<div class="alert alert-warning">${result.issues.map(esc).join('<br>')}</div>`:''}${result.event?`<div class="card card-body"><div class="fw-medium">${esc(result.event.industry)} · ${esc(result.event.subject)}</div><div class="small text-secondary mt-2">收件人:${esc(result.to.join('、')||'待配置')}</div><pre class="notification-brief mt-3 mb-0">${esc(result.event.brief)}</pre></div>`:''}`;});};
container.querySelectorAll('[data-notification-retry]').forEach(form=>form.onsubmit=e=>{e.preventDefault();return action(e.submitter,async()=>{await api('/notifications/items/'+form.dataset.notificationRetry+'/retry',{batchId:form.dataset.batchId||null,note:form.elements.note.value});if(isActive()){notify('通知已重新入队。');await render(container,isActive);}});});
container.querySelectorAll('[data-notification-resolve]').forEach(form=>form.onsubmit=e=>{e.preventDefault();return action(e.submitter,async()=>{await api('/notifications/batches/'+form.dataset.notificationResolve+'/resolve',{resolution:form.elements.resolution.value,note:form.elements.note.value});if(isActive()){notify('核验结果已保存。');await render(container,isActive);}});});
}
window.NotificationViews={configure:helpers=>({api,esc,formatDate,notify,user}=helpers),render};