-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.ts
129 lines (112 loc) · 3.41 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import fetch from 'node-fetch'
import { DCCData, SigningKeys, Valueset, Valuesets } from './types'
import { RuleSet } from './rules-runner/types'
import {
CertificateContent,
CERT_TYPE,
RecoveryGroup,
TestGroup,
VaccinationGroup,
} from './types/hcert'
import { buildValuesetsComputed } from '.'
export function mapToJSON(map) {
if (!(map instanceof Map)) return map
const out = Object.create(null)
map.forEach((value, key) => {
if (value instanceof Map) {
out[key] = mapToJSON(value)
} else {
out[key] = value
}
})
return out
}
export async function getDCCData(url: string): Promise<DCCData> {
const resp = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
})
if (resp.status === 200) {
const body = await resp.json()
return {
signingKeys: body.certs as SigningKeys,
ruleSet: body.rules as RuleSet,
valueSets: {
countryCodes: body.valueSets.countryCodes.valueSetValues,
diseaseAgentTargeted:
body.valueSets.diseaseAgentTargeted.valueSetValues,
testManf: body.valueSets.testManf.valueSetValues,
testResult: body.valueSets.testResult.valueSetValues,
testType: body.valueSets.testType.valueSetValues,
vaccineMahManf: body.valueSets.vaccineMahManf.valueSetValues,
vaccineMedicinalProduct:
body.valueSets.vaccineMedicinalProduct.valueSetValues,
vaccineProphylaxis: body.valueSets.vaccineProphylaxis.valueSetValues,
},
valuesetsComputed: buildValuesetsComputed(body.valueSets),
}
}
const data = resp.json ? await resp.json() : 'Unable to request data'
throw new Error(data)
}
function getValue(
valueset: Record<string, Valueset>,
key: string,
defaultValue: string = key
): string {
return valueset[key]?.display || defaultValue
}
const populateVaccineCert = (
cert: VaccinationGroup,
valueSets: Valuesets
): VaccinationGroup => {
return {
...cert,
tg: getValue(valueSets.diseaseAgentTargeted, cert.tg),
vp: getValue(valueSets.vaccineProphylaxis, cert.vp),
mp: getValue(valueSets.vaccineMedicinalProduct, cert.mp),
ma: getValue(valueSets.vaccineMahManf, cert.ma),
co: getValue(valueSets.countryCodes, cert.co),
}
}
const populateTestCert = (cert: TestGroup, valueSets: Valuesets): TestGroup => {
return {
...cert,
tg: getValue(valueSets.diseaseAgentTargeted, cert.tg),
tt: getValue(valueSets.testType, cert.tt),
ma: getValue(valueSets.vaccineMahManf, cert.ma),
tr: getValue(valueSets.testResult, cert.tr),
co: getValue(valueSets.countryCodes, cert.co),
}
}
const populateRecoveryCert = (
cert: RecoveryGroup,
valueSets: Valuesets
): RecoveryGroup => {
return {
...cert,
tg: getValue(valueSets.diseaseAgentTargeted, cert.tg),
co: getValue(valueSets.countryCodes, cert.co),
}
}
export function populateCertValues(
cert: CertificateContent,
type: CERT_TYPE,
valueSets: Valuesets
): CertificateContent {
const populatedCert: CertificateContent = {
nam: cert.nam,
dob: cert.dob,
ver: cert.ver,
}
if (type === CERT_TYPE.VACCINE) {
populatedCert.v = [populateVaccineCert(cert.v[0], valueSets)]
} else if (type === CERT_TYPE.TEST) {
populatedCert.t = [populateTestCert(cert.t[0], valueSets)]
} else if (type === CERT_TYPE.RECOVERY) {
populatedCert.r = [populateRecoveryCert(cert.r[0], valueSets)]
}
return populatedCert
}