|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [ADR-0131 D6/D10, ruling C on §6 Q1] A `sys_email_template` row, turned into |
| 5 | + * the `email_template` DEFINITION it carries. |
| 6 | + * |
| 7 | + * Templates resolve from the registry (`email_template` metadata); the rows are |
| 8 | + * no longer read on the send path. A row an organization customized before |
| 9 | + * that door closed is the one copy of the customer's edit, and ruling C |
| 10 | + * promotes it to an environment template before the row is deleted. The |
| 11 | + * upgrade ceremony (`os migrate`, C7b) reads the row, calls this function and |
| 12 | + * saves the answer through the metadata door. ⛔ Never at boot. |
| 13 | + * |
| 14 | + * The function is pure: one row in, one body out — no engine, no I/O, and no |
| 15 | + * import, so the lean entry carries it as it stands. The mapping is the |
| 16 | + * inverse of the row shape the retired projector wrote, which is also the |
| 17 | + * render input the email service reads (`EmailTemplateRow`): |
| 18 | + * |
| 19 | + * - `name` → `name`; `label` → `label`, falling back to the name when the |
| 20 | + * column is empty or absent; |
| 21 | + * - `locale` → `locale`, falling back to `en-US` (the schema's default and |
| 22 | + * the bundle floor) when empty or absent; |
| 23 | + * - `subject` → `subject` and `body_html` → `bodyHtml`, each `''` when empty |
| 24 | + * or absent (both are required strings); |
| 25 | + * - `body_text` → `bodyText`, `reply_to` → `replyTo` and `description` → |
| 26 | + * `description`, each only when a non-empty string; |
| 27 | + * - `from_address` → `fromOverride.address`, with `from_name` → |
| 28 | + * `fromOverride.name` when non-empty; no `fromOverride` without an address; |
| 29 | + * - `variables_json` → `variables` when it is JSON text (or an already |
| 30 | + * parsed value) holding an array, else `[]`; |
| 31 | + * - `active` → `active`: `false` for a stored `false`, `0`, `'0'` or |
| 32 | + * `'false'`, `true` for anything else, an absent column included (the |
| 33 | + * schema's default). |
| 34 | + * |
| 35 | + * The retired keys (`category`, `is_system`) and every state or provenance |
| 36 | + * column (`managed_by`, `customized`, `organization_id`, the timestamps) are |
| 37 | + * not a definition and never travel into one. |
| 38 | + * |
| 39 | + * ⛔ It is not validation. A name outside the schema's dotted snake_case still |
| 40 | + * answers a body; the metadata door refuses it, and reporting that refusal is |
| 41 | + * the ceremony's. |
| 42 | + */ |
| 43 | + |
| 44 | +/** |
| 45 | + * One `sys_email_template` row, as the mapping reads it. Every other column a |
| 46 | + * row carries is accepted and ignored. |
| 47 | + */ |
| 48 | +export interface EmailTemplateRowForDefinition { |
| 49 | + readonly name?: unknown; |
| 50 | + readonly label?: unknown; |
| 51 | + readonly locale?: unknown; |
| 52 | + readonly description?: unknown; |
| 53 | + readonly subject?: unknown; |
| 54 | + readonly body_html?: unknown; |
| 55 | + readonly body_text?: unknown; |
| 56 | + readonly from_name?: unknown; |
| 57 | + readonly from_address?: unknown; |
| 58 | + readonly reply_to?: unknown; |
| 59 | + readonly active?: unknown; |
| 60 | + readonly variables_json?: unknown; |
| 61 | + readonly [column: string]: unknown; |
| 62 | +} |
| 63 | + |
| 64 | +const filled = (value: unknown): string | undefined => |
| 65 | + typeof value === 'string' && value !== '' ? value : undefined; |
| 66 | + |
| 67 | +const variablesOf = (value: unknown): unknown[] => { |
| 68 | + let parsed = value; |
| 69 | + if (typeof value === 'string') { |
| 70 | + try { |
| 71 | + parsed = JSON.parse(value); |
| 72 | + } catch { |
| 73 | + return []; |
| 74 | + } |
| 75 | + } |
| 76 | + return Array.isArray(parsed) ? parsed : []; |
| 77 | +}; |
| 78 | + |
| 79 | +/** |
| 80 | + * The `email_template` definition one `sys_email_template` row carries (module |
| 81 | + * doc). Deterministic: the same row answers the same body, key for key and in |
| 82 | + * the same key order. |
| 83 | + */ |
| 84 | +export function emailTemplateDefinitionFromRow(row: EmailTemplateRowForDefinition): Record<string, unknown> { |
| 85 | + const body: Record<string, unknown> = { |
| 86 | + name: row.name, |
| 87 | + label: filled(row.label) ?? row.name, |
| 88 | + locale: filled(row.locale) ?? 'en-US', |
| 89 | + subject: filled(row.subject) ?? '', |
| 90 | + bodyHtml: filled(row.body_html) ?? '', |
| 91 | + }; |
| 92 | + const bodyText = filled(row.body_text); |
| 93 | + if (bodyText !== undefined) body.bodyText = bodyText; |
| 94 | + body.variables = variablesOf(row.variables_json); |
| 95 | + const address = filled(row.from_address); |
| 96 | + if (address !== undefined) { |
| 97 | + const name = filled(row.from_name); |
| 98 | + body.fromOverride = name === undefined ? { address } : { name, address }; |
| 99 | + } |
| 100 | + const replyTo = filled(row.reply_to); |
| 101 | + if (replyTo !== undefined) body.replyTo = replyTo; |
| 102 | + body.active = !(row.active === false || row.active === 0 || row.active === '0' || row.active === 'false'); |
| 103 | + const description = filled(row.description); |
| 104 | + if (description !== undefined) body.description = description; |
| 105 | + return body; |
| 106 | +} |
0 commit comments