@teamscala/service-boot 0.23.414 → 0.23.416

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,201 @@
1
+ /**
2
+ * @system service-boot
3
+ * @status handwritten
4
+ * @edit edit directly
5
+ *
6
+ * New-flow signing-shell injection (no pre-placed gate required): agreement
7
+ * meta stashing, body-content discovery, the SignIn gate block builder, and
8
+ * the DB-driven signing shell (config/agreement-signing-shell) with its
9
+ * hardcoded fallback.
10
+ *
11
+ * Split out of agreement-gate.ts (909 lines) — byte-for-byte move; import
12
+ * paths adjusted only.
13
+ */
14
+
15
+ import { loadRegistryConfig } from "@teamscala/db/registry/load-config";
16
+ import * as v from "valibot";
17
+ import { findAgreementGate } from "./signing-session.ts";
18
+ import type { ContentBlock } from "./signing-session.ts";
19
+ import type { AgreementGateSettings } from "./settings.ts";
20
+
21
+ /** Agreement metadata stashed on resolved during getResourceId. */
22
+ export const AGREEMENT_META_KEY = "_agreementMeta";
23
+
24
+ export function stashAgreementMeta(
25
+ resolved: Record<string, unknown>,
26
+ id: string,
27
+ name: string,
28
+ ): void {
29
+ resolved[AGREEMENT_META_KEY] = { id, name };
30
+ }
31
+
32
+ /**
33
+ * The body content array for the agreement. The public shell composes the body
34
+ * region FROM resolved.page (register-registry-page-route.ts Branch 2:
35
+ * `bodyPuck = extractPuckData(resolved.page)`, returned un-cloned), so this IS
36
+ * the live array whose mutation reaches the SSR render. Header/footer are
37
+ * separate region sources (resolved.header / resolved.footer) — never the body.
38
+ *
39
+ * A prior version scored every region by top-level non-shell block count and
40
+ * picked the highest. That misroutes when the body wraps its clauses inside a
41
+ * Container (body's top-level count is ~2 while its nested clause count is ~40):
42
+ * a /header region with more top-level blocks out-scores the body, so
43
+ * onUnauthorized cleared the HEADER and injected the gate there, leaving the
44
+ * entire agreement body visible past the gate. The body is the resource page —
45
+ * identified, not heuristically guessed.
46
+ */
47
+ export function getBodyContentArray(
48
+ resolved: Record<string, unknown>,
49
+ ): ContentBlock[] | null {
50
+ const pd = (
51
+ resolved.page as { puck_data?: { content?: unknown[] } } | undefined
52
+ )?.puck_data;
53
+ if (!pd || !Array.isArray(pd.content)) return null;
54
+ return pd.content as ContentBlock[];
55
+ }
56
+
57
+ /** Construct the SignIn gate block for injection. */
58
+ export function buildGateBlock(
59
+ agreementUuid: string,
60
+ title: string,
61
+ settings: AgreementGateSettings,
62
+ ): ContentBlock {
63
+ return {
64
+ type: "SignIn",
65
+ props: {
66
+ id: `agreement-gate-${agreementUuid}`,
67
+ mode: "gate",
68
+ title: title || settings.title,
69
+ subtitle: settings.subtitle,
70
+ gateAction: "/api/agreement-access",
71
+ showBackToHome: true,
72
+ gateContextField: "agreementId",
73
+ gateContextValue: agreementUuid,
74
+ gateSubmitLabel: settings.submitLabel,
75
+ // Require OTP is DB-driven (config/content-gate-agreement.requireOtp).
76
+ // String "true"/"false" (not boolean) to match the select-field enum the
77
+ // render validates against — a boolean is stripped before reaching the
78
+ // component (the same coercion that bit the pre-placed gate block).
79
+ gateRequireOtp: settings.requireOtp ? "true" : "false",
80
+ },
81
+ };
82
+ }
83
+
84
+ /**
85
+ * Construct the signing shell blocks that wrap the body content when authed:
86
+ * [0] title ({{agreement.name}}), [1] execution clause, [2] signature panel
87
+ * (grid Container holding a per-party SignatureField template), [3] print
88
+ * button, [4] audit footer. The title is unshift'd before body; the rest are
89
+ * pushed after. expandRosterTemplates expands the SignatureField per party;
90
+ * seedSigningBlocks seeds each cell with the verified session.
91
+ */
92
+ const AGREEMENT_SIGNING_SHELL_DEFAULT: ContentBlock[] = [
93
+ {
94
+ type: "Text",
95
+ props: {
96
+ id: "agreement-title",
97
+ text: "{{agreement.name}}",
98
+ size: "xl",
99
+ weight: "bold",
100
+ align: "left",
101
+ element: "h1",
102
+ },
103
+ },
104
+ {
105
+ type: "Text",
106
+ props: {
107
+ id: "agreement-execution",
108
+ text: "IN WITNESS WHEREOF, the parties have executed this Agreement as of the date(s) of signature below.",
109
+ size: "sm",
110
+ weight: "normal",
111
+ align: "left",
112
+ element: "p",
113
+ },
114
+ },
115
+ {
116
+ type: "Container",
117
+ props: {
118
+ id: "agreement-signature-panel",
119
+ layout: "grid",
120
+ columns: 2,
121
+ direction: "column",
122
+ gap: "md",
123
+ padding: "md",
124
+ mobileBehavior: "stack",
125
+ content: [
126
+ {
127
+ type: "SignatureField",
128
+ props: {
129
+ id: "agreement-signature-field",
130
+ label: "",
131
+ },
132
+ },
133
+ ],
134
+ },
135
+ },
136
+ // Print button — preset:"action-primary" is the single styling knob: it
137
+ // resolves (button-presets config) to variant:primary → brand-coloured
138
+ // background. NO explicit `variant` prop: button-styles.ts does
139
+ // `variant ?? preset.variant`, so any explicit variant overrides the preset
140
+ // (a stray variant:"secondary" — not a real variant — fell back to a
141
+ // transparent ghost; that was the bare-text bug). icon:"lucide:printer"
142
+ // renders the printer glyph (baseStyle provides the icon↔label gap).
143
+ {
144
+ type: "Button",
145
+ props: {
146
+ id: "agreement-print",
147
+ label: "Print",
148
+ icon: "lucide:printer",
149
+ actionType: "print",
150
+ preset: "action-primary",
151
+ size: "default",
152
+ borderColor: "var(--border-color-default)",
153
+ borderWidth: "var(--border-width-default)",
154
+ borderRadius: "var(--radius-md)",
155
+ fullWidth: false,
156
+ pushRight: false,
157
+ href: "",
158
+ target: "_self",
159
+ },
160
+ },
161
+ {
162
+ type: "Text",
163
+ props: {
164
+ id: "agreement-audit",
165
+ text: "Secured electronic signature · {{agreement.org}}",
166
+ size: "xs",
167
+ align: "center",
168
+ element: "p",
169
+ },
170
+ },
171
+ ];
172
+
173
+ const SigningShellSchema = v.object({ blocks: v.array(v.unknown()) });
174
+
175
+ /**
176
+ * The signing-shell block tree, DB-driven (config/agreement-signing-shell) so the
177
+ * clauses, labels, and structure are editable without a deploy. Deep-cloned per
178
+ * call because injection (expandRosterTemplates/seedSigningBlocks) mutates the
179
+ * tree. Falls back to AGREEMENT_SIGNING_SHELL_DEFAULT so signing ALWAYS renders.
180
+ */
181
+ export async function buildSigningShellBlocks(): Promise<ContentBlock[]> {
182
+ try {
183
+ const cfg = await loadRegistryConfig(
184
+ "config",
185
+ "agreement-signing-shell",
186
+ SigningShellSchema,
187
+ );
188
+ const blocks = cfg?.blocks as ContentBlock[] | undefined;
189
+ if (Array.isArray(blocks) && blocks.length > 0) {
190
+ return structuredClone(blocks);
191
+ }
192
+ } catch {
193
+ // fall through to the default so the signing shell ALWAYS renders
194
+ }
195
+ return structuredClone(AGREEMENT_SIGNING_SHELL_DEFAULT);
196
+ }
197
+
198
+ /** True if the page has a pre-placed SignIn gate block (old-flow page). */
199
+ export function hasPrePlacedGate(resolved: Record<string, unknown>): boolean {
200
+ return findAgreementGate(resolved).agreementUuid !== "";
201
+ }