-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
95 lines (70 loc) · 2.67 KB
/
test.js
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
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone')
const dayjs = require('dayjs')
dayjs.extend(utc)
dayjs.extend(timezone)
const RAPID_TEST_START_HOUR = "rapid_test_start_hours";
const RAPID_TEST_END_HOUR = "rapid_test_end_hours";
const MOLECULAR_TEST_START_HOUR = "molecular_test_start_hours";
const MOLECULAR_TEST_END_HOUR = "molecular_test_end_hours";
const DETECTED = "260373001";
const NOT_DETECTED = "260415000";
// https://ec.europa.eu/health/sites/default/files/ehealth/docs/digital-green-certificates_dt-specifications_en.pdf
const TYPE_RAPID = "LP217198-3"; // RAT, Rapid Antigen Test
const TYPE_MOLECULAR = "LP6464-4"; // NAAT, Nucleic Acid Amplification Test
const validateTest = function(settings, dcc) {
var obj = dcc.payload.t[dcc.payload.t.length -1];
var testType = obj.tt;
var testResult = obj.tr;
var dateTimeOfSampleCollection = obj.sc;
var now = dayjs();
// if result is Detected, green pass is not valid
if(testResult == DETECTED) return {result: false, message: "Test result is Detected" };
try {
var odtDateTimeOfCollection = dayjs.tz(dateTimeOfSampleCollection, "UTC");
var ldtDateTimeOfCollection = odtDateTimeOfCollection.tz(dayjs.tz.guess());
var startDate;
var endDate;
// Rapid Test
if(testType == TYPE_RAPID) {
startDate = ldtDateTimeOfCollection.add(parseInt(getRapidTestStartHour(settings).value), 'hours')
endDate = ldtDateTimeOfCollection.add(parseInt(getRapidTestEndHour(settings).value), 'hours')
}
// Molecular Test
else if(testType == TYPE_MOLECULAR) {
startDate = ldtDateTimeOfCollection.add(parseInt(getMolecularTestStartHour(settings).value), 'hours')
endDate = ldtDateTimeOfCollection.add(parseInt(getMolecularTestEndHour(settings).value), 'hours')
}
// Unknown Test
else return {result: false, message: "Unknown test type"};
if(startDate.isAfter(now)) return {result: false, message: "Non valido"};
if(now.isAfter(endDate)) return {result: false, message: "Scaduto"};
return {result: true, message: "Valido"};
// Exception
} catch (e) {
return {result: false, message: e.message};
}
}
function getRapidTestStartHour(settings) {
return settings.find(it => {
return it.name == RAPID_TEST_START_HOUR
})
}
function getRapidTestEndHour(settings) {
return settings.find(it => {
return it.name == RAPID_TEST_END_HOUR
})
}
function getMolecularTestStartHour(settings) {
return settings.find(it => {
return it.name == MOLECULAR_TEST_START_HOUR
})
}
function getMolecularTestEndHour(settings) {
return settings.find(it => {
return it.name == MOLECULAR_TEST_END_HOUR
})
}
module.exports = {
validateTest
}