Skip to main content
Version: v0.0.3

Function: buildAck()

buildAck(inbound, options): Hl7Message

Build a spec-clean ACK (MSH + MSA [+ ERR…]) responding to inbound.

Behavior:

  • MSH: sender/receiver are swapped (inbound MSH-5/6 → ACK MSH-3/4; inbound MSH-3/4 → ACK MSH-5/6); MSH-7 is the current UTC time; MSH-9 is ACK (with the inbound trigger event echoed as ACK^<trigger>^ACK when present); MSH-10 is a freshly generated control id; MSH-11 (processing id) and MSH-12 (version) echo the inbound values.
  • MSA: MSA-1 = code; MSA-2 echoes the full inbound MSH-10 field (the raw field structure is carried over whole: a vendor-quirk id like ID^X is never truncated to its first component). The echo carries the inbound field's escape-fidelity overlay, so an id bearing a hex escape (ID\X41\Q) or a preserved escape (\H\) echoes byte-verbatim, not canonicalized: exactly the bytes the sender put on the wire, which is what MSA-2 correlation compares. The only structural transform is trailing-empty canonicalization (D-02). (A sender that used custom encoding characters is re-delimited spec-cleanly: the overlay carries the sender's raw bytes in the sender's alphabet, so echoing them verbatim under the ACK's default alphabet would corrupt the field's structure and break correlation: the overlay is therefore bypassed and the decoded id is re-escaped under default, which re-parses back to the same control id. Default-delimiter senders, the norm, keep the byte-exact overlay echo.)
  • ERR: one segment per supplied AckErrorDetail: ERR-2 location (when given), ERR-3 the Table 0357 condition code as a CWE (code^text^HL70357), ERR-4 the Table 0516 severity.

Fail-safe. If the inbound message has no MSH-10, the ACK cannot be correlated. buildAck then leaves MSA-2 empty and, if a positive accept (AA/CA) was requested, downgrades it to the matching error code (AE/CE): it never fabricates an unverifiable positive ACK. The returned message carries an ACK_NO_CORRELATION_ID warning. (This is the inbound-side complement to @cosyte/mllp's "no commit ⇒ never AA".)

Pure aside from the generated control id + timestamp; never throws except on a programming error (inbound not an Hl7Message, or an unknown code).

Parameters

inbound

Hl7Message

options

BuildAckOptions

Returns

Hl7Message

Example

import { buildAck, parseHL7 } from "@cosyte/hl7";
const inbound = parseHL7(raw);
const ack = buildAck(inbound, { code: "AA" });
console.log(ack.toString()); // MSH|...\rMSA|AA|<inbound MSH-10>
parseHL7(ack.toString()).meta.type; // "ACK" (round-trips clean)