|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Session-less HTML for the actionable-link confirm/result pages (ADR-0043). |
| 5 | + * |
| 6 | + * Deliberately tiny and dependency-free: these pages are reached from an |
| 7 | + * email or IM message by a bearer with no session, so they must not assume |
| 8 | + * the Console bundle, auth state, or client-side i18n. Static bilingual |
| 9 | + * (EN / 中文) copy keeps them readable for the demo audience without a |
| 10 | + * locale negotiation step. |
| 11 | + * |
| 12 | + * The GET page NEVER mutates — the decision happens only on the POST form |
| 13 | + * submit (mail-gateway link prefetchers must not approve requests). |
| 14 | + */ |
| 15 | + |
| 16 | +import type { ApprovalRequestRow, ApprovalActionKind } from '@objectstack/spec/contracts'; |
| 17 | + |
| 18 | +function esc(s: unknown): string { |
| 19 | + return String(s ?? '') |
| 20 | + .replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>') |
| 21 | + .replaceAll('"', '"').replaceAll("'", '''); |
| 22 | +} |
| 23 | + |
| 24 | +function shell(title: string, body: string): string { |
| 25 | + return `<!doctype html> |
| 26 | +<html lang="en"><head><meta charset="utf-8"> |
| 27 | +<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 28 | +<meta name="robots" content="noindex"> |
| 29 | +<title>${esc(title)}</title> |
| 30 | +<style> |
| 31 | + body{font:15px/1.6 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"PingFang SC","Microsoft YaHei",sans-serif; |
| 32 | + background:#f6f7f9;color:#1a202c;margin:0;display:flex;min-height:100vh;align-items:center;justify-content:center} |
| 33 | + .card{background:#fff;border:1px solid #e2e8f0;border-radius:12px;max-width:440px;width:calc(100% - 32px); |
| 34 | + padding:28px 32px;box-shadow:0 1px 3px rgba(0,0,0,.06)} |
| 35 | + h1{font-size:18px;margin:0 0 4px} |
| 36 | + .sub{color:#64748b;font-size:13px;margin:0 0 20px} |
| 37 | + .row{display:flex;justify-content:space-between;gap:12px;padding:7px 0;border-bottom:1px solid #f1f5f9;font-size:14px} |
| 38 | + .row b{font-weight:600;text-align:right} |
| 39 | + .k{color:#64748b} |
| 40 | + .actions{margin-top:22px;display:flex;gap:10px} |
| 41 | + button{flex:1;padding:10px 16px;border-radius:8px;border:1px solid transparent;font-size:15px;font-weight:600;cursor:pointer} |
| 42 | + .approve{background:#059669;color:#fff} |
| 43 | + .reject{background:#fff;color:#dc2626;border-color:#fca5a5} |
| 44 | + .badge{display:inline-block;padding:2px 10px;border-radius:999px;font-size:12px;font-weight:600;margin-bottom:14px} |
| 45 | + .ok{background:#ecfdf5;color:#047857}.warn{background:#fffbeb;color:#b45309}.err{background:#fef2f2;color:#b91c1c} |
| 46 | + a{color:#2563eb;text-decoration:none} |
| 47 | + .foot{margin-top:18px;font-size:12px;color:#94a3b8} |
| 48 | +</style></head><body><div class="card">${body}</div></body></html>`; |
| 49 | +} |
| 50 | + |
| 51 | +function summaryRows(req: ApprovalRequestRow): string { |
| 52 | + const rows: Array<[string, string]> = [ |
| 53 | + ['Process · 流程', req.process_label || req.process_name], |
| 54 | + ['Step · 步骤', req.step_label || req.current_step || '—'], |
| 55 | + ['Record · 记录', req.record_title || req.record_id], |
| 56 | + ['Object · 对象', req.object_label || req.object_name], |
| 57 | + ['Requester · 申请人', req.submitter_name || req.submitter_id || '—'], |
| 58 | + ]; |
| 59 | + return rows.map(([k, v]) => `<div class="row"><span class="k">${esc(k)}</span><b>${esc(v)}</b></div>`).join(''); |
| 60 | +} |
| 61 | + |
| 62 | +/** GET page: summary + a POST form. Rendering only — no mutation. */ |
| 63 | +export function renderConfirmPage(input: { |
| 64 | + request: ApprovalRequestRow; |
| 65 | + action: Extract<ApprovalActionKind, 'approve' | 'reject'>; |
| 66 | + approverId: string; |
| 67 | + token: string; |
| 68 | + actPath: string; |
| 69 | +}): string { |
| 70 | + const approving = input.action === 'approve'; |
| 71 | + const verb = approving ? 'Approve · 通过' : 'Reject · 拒绝'; |
| 72 | + return shell(`${verb} — Approval`, ` |
| 73 | + <h1>${approving ? '✅ Approve this request?' : '⛔ Reject this request?'}</h1> |
| 74 | + <p class="sub">${approving ? '确认通过该审批请求?' : '确认拒绝该审批请求?'} |
| 75 | + Acting as · 操作身份:<b>${esc(input.approverId)}</b></p> |
| 76 | + ${summaryRows(input.request)} |
| 77 | + <form method="post" action="${esc(input.actPath)}" class="actions"> |
| 78 | + <input type="hidden" name="token" value="${esc(input.token)}"> |
| 79 | + <button type="submit" class="${approving ? 'approve' : 'reject'}">${verb}</button> |
| 80 | + </form> |
| 81 | + <p class="foot">This link is single-use and expires automatically. · 此链接一次有效,过期自动失效。</p>`); |
| 82 | +} |
| 83 | + |
| 84 | +const RESULT_COPY: Record<string, { cls: string; title: string; body: string }> = { |
| 85 | + approved: { cls: 'ok', title: '✅ Approved · 已通过', body: 'The decision was recorded. · 审批结果已记录。' }, |
| 86 | + rejected: { cls: 'ok', title: '⛔ Rejected · 已拒绝', body: 'The decision was recorded. · 审批结果已记录。' }, |
| 87 | + invalid: { cls: 'err', title: 'Invalid link · 链接无效', body: 'This link is not recognized. · 无法识别该链接。' }, |
| 88 | + expired: { cls: 'warn', title: 'Link expired · 链接已过期', body: 'Ask the requester to send a new reminder. · 请让申请人重新发送催办。' }, |
| 89 | + consumed: { cls: 'warn', title: 'Already used · 链接已使用', body: 'This link was already used once. · 该链接已被使用过。' }, |
| 90 | + not_pending: { cls: 'warn', title: 'Already decided · 请求已处理', body: 'This request is no longer pending. · 该请求已不在待审批状态。' }, |
| 91 | + not_approver: { cls: 'warn', title: 'No longer your approval · 已不在你名下', body: 'This approval was handed to someone else. · 该审批已转由他人处理。' }, |
| 92 | +}; |
| 93 | + |
| 94 | +/** Terminal page for every redemption outcome (and stale GETs). */ |
| 95 | +export function renderResultPage(kind: keyof typeof RESULT_COPY, request?: ApprovalRequestRow): string { |
| 96 | + const copy = RESULT_COPY[kind] ?? RESULT_COPY.invalid; |
| 97 | + return shell(copy.title, ` |
| 98 | + <span class="badge ${copy.cls}">${esc(copy.title)}</span> |
| 99 | + ${request ? summaryRows(request) : ''} |
| 100 | + <p>${esc(copy.body)}</p> |
| 101 | + <p class="foot"><a href="/system/approvals">Open the Approvals Inbox · 打开审批中心</a></p>`); |
| 102 | +} |
0 commit comments