// @requirements REQ-EU-ORDER-CONFIRMATION REQ-PRODUCT-ORDER-CONFIRMATION REQ-PRODUCT-CF-ADMIN REQ-PRODUCT-A11Y REQ-PRODUCT-MOBILE import {escapeHtml as e,readableTime} from './render.js'; import {readConfirmationState} from './order-confirmation-state.js'; import type {OrderConfirmationArchive} from './order-confirmation-state.js'; import type {EvidenceRecord} from './types.js'; export const confirmationStatus=(value:unknown):string=>({pending:'Waiting for dispatch',leased:'Dispatch in progress',uncertain:'Acceptance uncertain',accepted:'Provider accepted',cancelled:'Dispatch cancelled'}[String(value)]??'Not prepared'); const url=(base:string,route:string,id:string,file?:string)=>base+'/'+route+'?'+new URLSearchParams({id,...(file?{file}:{})}); export async function validateConfirmationArchive(input:unknown,record:EvidenceRecord):Promise{ if(!input||typeof input!=='object'||Array.isArray(input))throw Error('Invalid confirmation.');const a=input as OrderConfirmationArchive,d=record.data as Record; if(d?.kind!=='paid-order-admission')throw Error('Invalid admission.'); const archive=await readConfirmationState({get:async(key:string)=>(key==='manifest'?a.manifest:a.delivery) as T},'',{siteId:String(d.siteId),scopeId:String(d.scopeId),contextRef:String(d.streamRef),admissionId:String(d.id),reviewId:String(d.reviewId)}); if(!archive)throw Error('Invalid confirmation.');return archive; } export function confirmationBody(a:OrderConfirmationArchive,record:EvidenceRecord,base:string,primaryText:string):string{ const m=a.manifest,j=a.delivery,rows=[['Contract reference',m.contract.reference],['Addressee',m.addressee.name],['Contract concluded',m.contract.concludedAt],['Document issued',m.issuedAt],['Files recorded',j.createdAt]],preview=primaryText.slice(0,20000); return `
← Back to order request

Contract confirmation

${e(m.contract.reference)}

Prepared for ${e(m.addressee.name)}

${e(confirmationStatus(j.status))}

${j.status==='accepted'?'The configured provider accepted this delivery job. Recipient delivery has not been established by this ledger.':j.status==='uncertain'?'Reconcile this job with your provider using its stable delivery ID before retrying.':j.status==='cancelled'?'The host reported external delivery work stopped and cancelled further attempts.':j.status==='leased'?'A host runner holds a bounded delivery lease. A timeout does not prove that the provider rejected the message.':'The files are retained. A host runner must deliver them through the configured channel.'}

${j.attempts} attempt${j.attempts===1?'':'s'} · Updated ${readableTime(j.updatedAt)}

Document context

${rows.map(([k,v])=>`
${e(k!)}
${k==='Contract concluded'||k==='Document issued'||k==='Files recorded'?readableTime(v):e(v!)}
`).join('')}

Customer copy

The retained plain-text document. Downloads below contain the exact original files.

${e(preview)}
${preview.lengthPreview shortened after 20,000 characters. Download the complete file below.

':''}

Confirmation files

${m.files.map(f=>`
${e(f.filename)} ↓${f.bytes.toLocaleString('en')} bytes · ${e(f.mediaType)}${f.sha256}
`).join('')}
Delivery and evidence references
${[['Confirmation ID',m.confirmationId],['Delivery ID',j.deliveryId],['Provider reference',j.providerRef??'Not recorded'],['Recipient reference',m.addressee.recipientRef],['Destination reference',m.addressee.destinationRef],['Destination version',m.addressee.destinationVersion],['Review ID',m.reviewId]].map(([k,v])=>`
${e(k!)}
${e(v!)}
`).join('')}

Changing files or a destination requires a separate corrective-confirmation workflow. This archive cannot be overwritten.

`; }