39 lines
2.6 KiB
JavaScript
39 lines
2.6 KiB
JavaScript
'use strict';
|
|
const path=require('node:path');
|
|
const {escape}=require('../mail/parser.cjs');
|
|
const {formatOrderLine}=require('../../order-template-bridge.cjs');
|
|
const {fail}=require('./validation.cjs');
|
|
const lines=value=>escape(value).replace(/\r?\n/g,'<br>');
|
|
function fileLink(filePath){
|
|
if(filePath.startsWith('\\\\'))return 'file://'+filePath.slice(2).split('\\').map(encodeURIComponent).join('/');
|
|
if(/^[a-z]:[\\/]/i.test(filePath))return 'file:///'+filePath.replace(/\\/g,'/').split('/').map((p,i)=>i===0?p:encodeURIComponent(p)).join('/');
|
|
return 'file://'+filePath.split('/').map(encodeURIComponent).join('/');
|
|
}
|
|
// Content is derived from the event and its recorded archive, never current configuration.
|
|
function mailContent(orders){
|
|
const summaries=orders.flatMap(order=>{
|
|
const event=order.event,rows=event.titleOrders;
|
|
if(rows?.length&&rows.every(row=>typeof row.text==='string'&&row.text.trim()))return rows.map(row=>({orderNo:row.orderNo,brief:row.text}));
|
|
// Older event snapshots may lack per-order text. Recover only the declared
|
|
// title segments, using the same deterministic title parser as ingestion.
|
|
const parsed=require('./title.cjs').extractTitle(event.brief||'');
|
|
if(rows?.length&&parsed.orders.length>1)return rows.map(row=>{
|
|
const matches=parsed.orders.filter(observed=>observed.orderNo===row.orderNo);
|
|
if(!row.text?.trim()&&matches.length!==1)fail('已识别订单段落缺少可核对的简要说明原文。',409);
|
|
return {orderNo:row.orderNo,brief:row.text?.trim()?row.text:matches[0].text};
|
|
});
|
|
if(rows?.length>1)fail('已识别订单段落缺少可核对的简要说明原文。',409);
|
|
if(parsed.orders.length>1)return parsed.orders.map(row=>({orderNo:row.orderNo,brief:row.text}));
|
|
return [{orderNo:rows?.[0]?.orderNo||event.orderNo,brief:event.brief}];
|
|
});
|
|
const details=summaries.map(formatOrderLine).join('\n');
|
|
const seen=new Set(),directories=[];
|
|
for(const order of orders)for(const file of order.archive.files){
|
|
const windows=path.win32.isAbsolute(file.path),directory=(windows?path.win32:path.posix).dirname(file.path),key=windows?directory.toLowerCase():directory;
|
|
if(!seen.has(key)){seen.add(key);directories.push(directory);}
|
|
}
|
|
const htmlDirectoryLinks=directories.map(directory=>'<a style="color:#0000ff" href="'+escape(fileLink(directory))+'">'+escape(directory)+'</a>').join('<br>');
|
|
return {details,orderNos:summaries.map(row=>row.orderNo),directories,htmlDetails:'<div style="color:#0000ff">'+lines(details)+'</div>',htmlDirectoryLinks,htmlDirectories:'<div>存放地址:<br>'+htmlDirectoryLinks+'</div>'};
|
|
}
|
|
module.exports={mailContent,fileLink};
|