feat(records): add source history and Tabler record ledgers

This commit is contained in:
abaishu0730 committed 2026-10-20 17:34:17 +08:00
1 parent 1421edd6d4
commit 20bb68615f
27 files changed
+291 -91

No files matched your search

+8 -8
View File
@@ -1,6 +1,6 @@
const fs=require('node:fs');const path=require('node:path');
const {nextSlots}=require('../../shared/schedule.cjs');
const publicJob=({path:ignored,...job})=>({...job,downloadable:job.status==='success'});
const {queryJobs,publicJobs,jobFile}=require('./job-records.cjs');
async function dashboardApi({store,outputDir,workspace,mail,scanner,storage,orderRules,user,route,request,response,body,reply,fail}){
const handled=(...args)=>{reply(...args);return true;};
if(route==='/api/overview' && request.method==='GET') {
@@ -11,13 +11,17 @@ const handled=(...args)=>{reply(...args);return true;};
const rules=orderRules?.snapshot(),published=rules?.rules?.filter(r=>r.status==='enabled')||[];
const capabilities=rules?.capabilities||{},ready=['liveTodo','pdfExport','automaticArchive'].every(key=>capabilities[key]===true);
const automation={enabled:rules?.settings?.enabled===true,ready,reason:rules?.settings?.enabled!==true?'分类自动发信已暂停;请先完成类别字段、目录、邮箱与预览核验。':ready?'分类执行链路已完成实际接入检查。':'分类执行链路仍有未通过的实际检查,请查看订单执行中的阻塞原因。'};
return handled(200,{stats:{total:jobs.length,today:todayJobs.length,completed:jobs.filter(job=>job.status==='success').length,failed:jobs.filter(job=>['error','interrupted'].includes(job.status)).length,running:jobs.filter(job=>job.status==='running').length},scan,automation,connections:[{name:'PDF 导出',ready:true,detail:'浏览器扩展导出与任务记录已接入'},{name:'Excel 分类规则',ready:published.length>0,detail:`15类规则,已发布 ${published.length} 类;实际字段按流程版本核验`},{name:'BPM 待办执行',ready:capabilities.liveTodo===true,detail:capabilities.liveTodo===true?'真实待办接口检查通过;各类别仍需字段核验':'请在订单通知管理的订单执行中检查实时待办,字段映射在业务设置维护'},{name:'到达历史扫描',ready:!!scan?.runs.some(r=>r.status==='success'&&(r.kind||'inform')==='inform'),detail:scan?`${scan.config.enabled?'已开启':'已暂停'},每 ${scan.config.intervalMinutes} 分钟扫描 · 已收录 ${scan.total} 条流程`:'历史扫描服务未启动'},{name:'存储服务器',ready:!!storage?.store.servers().some(s=>s.enabled&&s.state.readable&&s.state.writable),detail:storage?`已配置 ${storage.store.servers().length} 个 SMB 存储服务器`:'存储服务未启动'},{name:'邮件渠道',ready:mailChannels.some(c=>!c.readOnly&&c.state.smtp),detail:`已配置 ${mailChannels.length} 个邮箱`},{name:'邮件模板',ready:mailTemplates.some(t=>t.status==='active'),detail:`${mailTemplates.filter(t=>t.status==='active').length} 个已生效,${mailTemplates.filter(t=>t.status==='pending').length} 个待确认;按类别显式关联`}],nextSlots:nextSlots(settings.schedule,new Date(),2,workspace?workspace.snapshot(store.users()).calendar.overrides:[]),schedule:settings.schedule,jobs:jobs.slice(0,6).map(publicJob),audit:store.audit().slice(0,5)});
return handled(200,{stats:{total:jobs.length,today:todayJobs.length,completed:jobs.filter(job=>job.status==='success').length,failed:jobs.filter(job=>['error','interrupted'].includes(job.status)).length,running:jobs.filter(job=>job.status==='running').length},scan,automation,connections:[{name:'PDF 导出',ready:true,detail:'浏览器扩展导出与任务记录已接入'},{name:'Excel 分类规则',ready:published.length>0,detail:`15类规则,已发布 ${published.length} 类;实际字段按流程版本核验`},{name:'BPM 待办执行',ready:capabilities.liveTodo===true,detail:capabilities.liveTodo===true?'真实待办接口检查通过;各类别仍需字段核验':'请在订单通知管理的订单执行中检查实时待办,字段映射在业务设置维护'},{name:'到达历史扫描',ready:!!scan?.runs.some(r=>r.status==='success'&&(r.kind||'inform')==='inform'),detail:scan?`${scan.config.enabled?'已开启':'已暂停'},每 ${scan.config.intervalMinutes} 分钟扫描 · 已收录 ${scan.total} 条流程`:'历史扫描服务未启动'},{name:'存储服务器',ready:!!storage?.store.servers().some(s=>s.enabled&&s.state.readable&&s.state.writable),detail:storage?`已配置 ${storage.store.servers().length} 个 SMB 存储服务器`:'存储服务未启动'},{name:'邮件渠道',ready:mailChannels.some(c=>!c.readOnly&&c.state.smtp),detail:`已配置 ${mailChannels.length} 个邮箱`},{name:'邮件模板',ready:mailTemplates.some(t=>t.status==='active'),detail:`${mailTemplates.filter(t=>t.status==='active').length} 个已生效,${mailTemplates.filter(t=>t.status==='pending').length} 个待确认;按类别显式关联`}],nextSlots:nextSlots(settings.schedule,new Date(),2,workspace?workspace.snapshot(store.users()).calendar.overrides:[]),schedule:settings.schedule,jobs:await publicJobs(jobs.slice(0,6),outputDir,storage?.store),audit:store.audit().slice(0,5)});
}
if(route==='/api/settings') {
if(request.method==='GET')return handled(200,store.settings());
if(request.method==='POST'){if(user.role!=='admin')fail(403,'仅管理员可以修改配置。');if(Object.hasOwn(body,'groups')||Object.hasOwn(body,'templates'))fail(410,'旧群组与通用模板写入已停用,请在分类规则维护收件人,在邮件模板维护类别关联。');return handled(200,store.saveSettings({...body,schedule:{...store.settings().schedule,...body.schedule}},body.version,user.username));}
}
if(route==='/api/jobs' && request.method==='GET')return handled(200,{jobs:store.jobs().map(publicJob)});
if(route==='/api/jobs' && request.method==='GET'){
const params=new URL(request.url,'http://localhost').searchParams;
if(params.get('view')==='ledger'){const filters={};for(const key of ['q','status','page','pageSize'])if(params.has(key))filters[key]=['page','pageSize'].includes(key)?Number(params.get(key)):params.get(key);return handled(200,await queryJobs({jobs:store.jobs(),outputDir,archiveStore:storage?.store},filters));}
return handled(200,{jobs:await publicJobs(store.jobs(),outputDir,storage?.store)});
}
if(route==='/api/audit' && request.method==='GET')return handled(200,{audit:store.audit()});
if(route==='/api/users') {
if(user.role!=='admin')fail(403,'仅管理员可以管理账号。');
@@ -27,11 +31,7 @@ const handled=(...args)=>{reply(...args);return true;};
const download=/^\/api\/jobs\/([\w-]+)\/file$/.exec(route);
if(download && request.method==='GET') {
const job=store.jobs().find(job=>job.jobId===download[1]);
if(!job || job.status!=='success' || !job.path)fail(404,'文件不可用。');
let realRoot,realFile;
try{[realRoot,realFile]=await Promise.all([fs.promises.realpath(outputDir),fs.promises.realpath(job.path)]);}catch{fail(404,'文件不存在。');}
const relative=path.relative(realRoot,realFile);
if(!relative || relative.startsWith('..'+path.sep) || relative==='..' || path.isAbsolute(relative) || path.extname(realFile).toLowerCase()!=='.pdf')fail(404,'文件不可用。');
const {file:realFile}=await jobFile(job,outputDir);
const bytes=await fs.promises.readFile(realFile);
response.writeHead(200,{'Content-Type':'application/pdf','Content-Disposition':`attachment; filename*=UTF-8''${encodeURIComponent(path.basename(realFile))}`});response.end(bytes);return true;
}