Feat openemr #7952 newpatient common twig, Fixes #7960, #7953, #7956 (#7969)
[openemr.git] / ccdaservice / utils / demographics / populate-demographics.js
bloba6968f0b0b35330becbd2974a4a6d94cf1e3c4cd
1 'use strict';
3 const { fetchPreviousAddresses } = require('./previous-addresses');
4 const { fDate } = require('../date/date');
5 const {
6 NOT_INFORMED,
7 NULL_FLAVOR,
8 DECLINED_TO_SPECIFY,
9 } = require('../constants');
11 function getGuardianNames(display_name) {
12 const parts = display_name.split(' ');
13 return parts.length === 3
14 ? [{ first: parts[0], last: parts[2] }]
15 : parts.length === 2
16 ? [{ first: parts[0], last: parts[1] }]
17 : [{ first: NOT_INFORMED, last: NOT_INFORMED }];
20 function getGuardianInfo(guardian) {
21 return [
23 relation: guardian.relation,
24 addresses: [
26 street_lines: [guardian.address],
27 city: guardian.city,
28 state: guardian.state,
29 zip: guardian.postalCode,
30 country: guardian.country || 'US',
31 use: 'primary home',
34 names: getGuardianNames(guardian.display_name),
35 phone: [
37 number: guardian.telecom,
38 type: 'primary home',
45 function setNullFlavorIfUnspecifiedOrEmpty(patient, property) {
46 if (patient[property] === DECLINED_TO_SPECIFY || patient[property] === '') {
47 patient[property] = NULL_FLAVOR;
51 function getLanguageCode(patient) {
52 return patient.language === 'English'
53 ? 'en-US'
54 : patient.language === 'Spanish'
55 ? 'sp-US'
56 : 'en-US';
59 function getNpiFacility(documentData, useFallback) {
60 return useFallback
61 ? documentData.encounter_provider.facility_npi || NOT_INFORMED
62 : documentData.encounter_provider.facility_npi;
65 function populateDemographics(documentData, npiFacility) {
66 const patient = documentData.patient;
67 const guardian = documentData.guardian;
68 const oidFacility =
69 documentData.encounter_provider.facility_oid ||
70 '2.16.840.1.113883.19.5.99999.1';
72 setNullFlavorIfUnspecifiedOrEmpty(patient, 'race');
73 setNullFlavorIfUnspecifiedOrEmpty(patient, 'race_group');
74 setNullFlavorIfUnspecifiedOrEmpty(patient, 'ethnicity');
76 return {
77 name: {
78 prefix: patient.prefix,
79 suffix: patient.suffix,
80 middle: [patient.mname],
81 last: patient.lname,
82 first: patient.fname,
84 birth_name: {
85 middle: patient.birth_mname || '',
86 last: patient.birth_lname || '',
87 first: patient.birth_fname || '',
89 dob: {
90 point: {
91 date: fDate(patient.dob),
92 precision: 'day',
95 gender: patient.gender.toUpperCase() || NULL_FLAVOR,
96 identifiers: [
98 identifier: oidFacility || npiFacility,
99 extension: patient.uuid,
102 marital_status: patient.status.toUpperCase(),
103 addresses: fetchPreviousAddresses(patient),
104 phone: [
106 number: patient.phone_home,
107 type: 'primary home',
110 number: patient.phone_mobile,
111 type: 'primary mobile',
114 number: patient.phone_work,
115 type: 'work place',
118 number: patient.phone_emergency,
119 type: 'emergency contact',
122 email: patient.email,
123 type: 'contact_email',
126 ethnicity: patient.ethnicity || '',
127 race: patient.race || NULL_FLAVOR,
128 race_additional: patient.race_group || NULL_FLAVOR,
129 languages: [
131 language: getLanguageCode(patient),
132 preferred: true,
133 mode: 'Expressed spoken',
134 proficiency: 'Good',
137 attributed_provider: {
138 identity: [
140 root: '2.16.840.1.113883.4.6',
141 extension: npiFacility || '',
144 phone: [
146 number:
147 documentData.encounter_provider.facility_phone || '',
150 name: [
152 full: documentData.encounter_provider.facility_name || '',
155 address: [
157 street_lines: [
158 documentData.encounter_provider.facility_street,
160 city: documentData.encounter_provider.facility_city,
161 state: documentData.encounter_provider.facility_state,
162 zip: documentData.encounter_provider.facility_postal_code,
163 country:
164 documentData.encounter_provider.facility_country_code ||
165 'US',
166 use: 'work place',
170 // not required
171 guardians: guardian.display_name ? getGuardianInfo(guardian) : '',
175 exports.populateDemographics = populateDemographics;
176 exports.getNpiFacility = getNpiFacility;