Skip to main content
Version: v0.0.3

Function: buildCcda()

buildCcda(init, options?): CcdaDocument

Build a spec-clean C-CDA R2.1 document from structured input and return the parsed CcdaDocument. Emits a CCD by default, or a Referral Note when documentType: "referralNote", each with its own US Realm Header specialization (document templateId + LOINC code) and SHALL section set. The emitted document round-trips through parseCcda by construction (see the module doc); a clean build carries zero warnings.

Parameters

init

BuildCcdaInit

The document content; see BuildCcdaInit. patient is required.

options?

BuildCcdaOptions = {}

Optional BuildCcdaOptions; pass a bring-your-own terminology adapter to have the returned document flag adapter-rejected codes (SEMANTIC_CODE_INVALID) and to emit <translation> alternate codings from its optional translate. The builder never coerces a code to satisfy the adapter, every primary value is emitted verbatim and a <translation> is only ever an additional alternate coding.

Returns

CcdaDocument

The parsed document, the parse of the spec-clean XML just emitted.

Throws

When documentType is anything other than "ccd" or "referralNote" (the only two types this builder supports), when an allergy is neither an allergen nor noKnownAllergy, when a result does not carry exactly one value form (quantity / codedValue / stringValue), when a "observation"-variant procedure omits its SHALL value, when a family-history entry carries an empty observations list, or when a problem or allergy supplies a resolution date without status: "resolved".

Example

import { buildCcda, serializeCcda } from "@cosyte/ccda";
const doc = buildCcda({
patient: { mrn: "MRN001", given: ["Jane"], family: "Doe", gender: "F" },
problems: [{ problem: { code: "59621000", displayName: "Essential hypertension" } }],
allergies: [{ allergen: { code: "7980", displayName: "Penicillin G" } }],
});
console.log(doc.getMrn()); // "MRN001"
console.log(doc.getProblems().length); // 1
const xml = serializeCcda(doc); // spec-clean C-CDA R2.1