This commit is contained in:
2026-06-19 04:53:54 -04:00
parent 3640bd1e01
commit 2b7b080138

View File

@@ -7,9 +7,9 @@
* See VS-RENDERER-SPEC.md — Spool POST Handler section.
*/
// ----------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// POST HANDLER
// ----------------------------------------------------------------------------
// ---------------------------------------------------------------------------
function vs01_handle_post($association_slug, $vs_code, $access) {
$schemas = vs01_load_schemas();
@@ -62,12 +62,11 @@ function vs01_handle_post($association_slug, $vs_code, $access) {
$out .= '<li>' . vs01_h($e) . '</li>';
}
$out .= '</ul></div>';
// Re-render form with submitted values and errors
$out .= vs01_render_vs_form_with_values($association_slug, $vs_code, $access, $fields);
return $out;
}
// Build spool envelope
// Build and POST spool envelope
$config = vs01_load_config();
$assoc = $config['associations'][$association_slug] ?? [];
$envelope = vs01_build_spool_envelope(
@@ -75,17 +74,17 @@ function vs01_handle_post($association_slug, $vs_code, $access) {
$perspective_key,
$fields,
$association_slug,
$assoc['channel_id'] ?? ''
$assoc['channel_id'] ?? '',
$access,
$config['node_token'] ?? ''
);
// POST to orchestrator
$result = vs01_post_to_orchestrator($envelope);
$result = vs01_post_to_orchestrator($envelope, $config);
if ($result === true) {
// THROWAWAY — replace with TMP review redirect or confirmation page
return '<div class="vs01-content">
<div class="alert alert-success">
Your record for ' . vs01_h($vs_code) . ' has been submitted.
Your record for ' . vs01_h($vs_code) . ' has been saved.
<a href="' . z_root() . '/vs01/' . vs01_h($association_slug) . '">
Return to ' . vs01_h($assoc['name'] ?? $association_slug) . '
</a>
@@ -99,9 +98,9 @@ function vs01_handle_post($association_slug, $vs_code, $access) {
</div>';
}
// ----------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// VALIDATION
// ----------------------------------------------------------------------------
// ---------------------------------------------------------------------------
function vs01_validate_required($perspective, $fields) {
$errors = [];
@@ -116,30 +115,63 @@ function vs01_validate_required($perspective, $fields) {
return $errors;
}
// ----------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// SPOOL ENVELOPE
// ----------------------------------------------------------------------------
// Matches contracts/vs01/record-v1.json — _header / _payload shape.
// ---------------------------------------------------------------------------
function vs01_build_spool_envelope($vs_code, $perspective, $fields, $association_slug, $channel_id) {
function vs01_build_spool_envelope($vs_code, $perspective, $fields, $association_slug, $channel_id, $standing, $node_token) {
return [
'_header' => [
'addon' => 'vs01',
'contract_version' => '1.0',
'vs_code' => $vs_code,
'perspective' => $perspective,
'association_slug' => $association_slug,
'association_channel_id' => (string) $channel_id,
'participant_id' => vs01_get_participant_id($association_slug),
'submitted_at' => date('c'),
'standing' => vs01_access_state($association_slug),
'standing' => $standing,
'node_token_hash' => hash('sha256', $node_token),
],
'_payload' => [
'vs_code' => $vs_code,
'perspective' => $perspective,
'fields' => $fields,
],
];
}
// ----------------------------------------------------------------------------
// ORCHESTRATOR POST
// ----------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// PARTICIPANT ID — Placekey derived from SASE channel address
// ---------------------------------------------------------------------------
function vs01_post_to_orchestrator($envelope) {
function vs01_get_participant_id($association_slug) {
// The participant_id is the Placekey — the SASE-verified channel address
// for this participant, which encodes their postal address.
// It is stored in the channel's xchan_addr field.
// Format: SASE1-{encoded-address}
$observer = get_observer_hash();
if (!$observer) return '';
$r = q("SELECT xchan_addr FROM xchan WHERE xchan_hash = '%s' LIMIT 1",
dbesc($observer)
);
if (!$r) return '';
// xchan_addr is in the form "channelname@hostname"
// The channel name IS the Placekey for SASE participants.
$addr = $r[0]['xchan_addr'] ?? '';
$parts = explode('@', $addr);
return $parts[0] ?? '';
}
// ---------------------------------------------------------------------------
// ORCHESTRATOR POST
// ---------------------------------------------------------------------------
function vs01_post_to_orchestrator($envelope, $config = null) {
if ($config === null) {
$config = vs01_load_config();
}
$receiver_url = $config['receiver_url'] ?? '';
$node_token = $config['node_token'] ?? '';
@@ -184,9 +216,9 @@ function vs01_post_to_orchestrator($envelope) {
return 'Orchestrator error (HTTP ' . $http_code . ').';
}
// ----------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// RE-RENDER FORM WITH SUBMITTED VALUES
// ----------------------------------------------------------------------------
// ---------------------------------------------------------------------------
function vs01_render_vs_form_with_values($association_slug, $vs_code, $access, $submitted_values) {
$schemas = vs01_load_schemas();