· 5 years ago · Apr 29, 2020, 08:12 PM
1const https = require("https")
2const CryptoJS = require("crypto-js")
3
4const isProd = false
5
6const USERNAME = isProd ? "PROD_USERNAME" : "TEST_USERNAME"
7const PASSWORD = isProd ? "PROD_PASSWORD" : "TEST_PASSWORD"
8
9function getHash() {
10 const uri = isProd
11 ? "https://authservice.priaid.ch/login"
12 : "https://sandbox-authservice.priaid.ch/login"
13 const secret_key = PASSWORD
14 const computedHash = CryptoJS.HmacMD5(uri, secret_key)
15 const computedHashString = computedHash.toString(CryptoJS.enc.Base64)
16 return computedHashString
17}
18
19function auth(hash) {
20 return new Promise((resolve, reject) => {
21 const options = {
22 hostname: isProd ? "authservice.priaid.ch" : "sandbox-authservice.priaid.ch",
23 port: 443,
24 path: "/login",
25 method: "POST",
26 headers: {
27 "Authorization": "Bearer " + USERNAME + ":" + hash
28 }
29 }
30
31 const req = https.request(options, res => {
32 if (res.statusCode === 200) {
33 res.on("data", d => {
34 resolve(d)
35 })
36 } else {
37 reject("statuscode=" + res.statusCode)
38 }
39 })
40
41 req.on("error", error => {
42 reject(error)
43 })
44
45 req.end()
46 })
47}
48
49function getSymptoms(token) {
50 return new Promise((resolve, reject) => {
51 const options = {
52 hostname: isProd ? "healthservice.priaid.ch" : "sandbox-healthservice.priaid.ch",
53 port: 443,
54 path: "/symptoms?token=" + token + "&language=en-gb",
55 method: "GET"
56 }
57
58 const req = https.request(options, res => {
59 if (res.statusCode === 200) {
60 res.on("data", d => {
61 resolve(d)
62 })
63 } else {
64 reject("statuscode=" + res.statusCode)
65 }
66 })
67
68 req.on("error", error => {
69 reject(error)
70 })
71
72 req.end()
73 })
74}
75
76function diag(token, symptoms, gender, year_of_birth) {
77 return new Promise((resolve, reject) => {
78 const options = {
79 hostname: isProd ? "healthservice.priaid.ch" : "sandbox-healthservice.priaid.ch",
80 port: 443,
81 path: "/diagnosis?token=" + token + "&symptoms=[" + symptoms + "]&gender=" + gender + "&year_of_birth=" + year_of_birth + "&language=en-gb",
82 method: "GET"
83 }
84
85 const req = https.request(options, res => {
86 if (res.statusCode === 200) {
87 res.on("data", d => {
88 resolve(d)
89 })
90 } else {
91 reject("statuscode=" + res.statusCode)
92 }
93 })
94
95 req.on("error", error => {
96 reject(error)
97 })
98
99 req.end()
100 })
101}
102
103function getSymptomIDs(allSymptoms, symptoms) {
104 const ids = []
105 symptoms.forEach(s => {
106 for (let el of allSymptoms) {
107 if (el.Name === s) {
108 ids.push(el.ID)
109 break
110 }
111 }
112 })
113 return ids
114}
115
116const hash = getHash()
117
118let authToken
119
120Promise.resolve()
121 .then(() => auth(hash))
122 .then(res => {
123 return JSON.parse(res.toString())
124 })
125 .then(authObj => {
126 authToken = authObj.Token
127 })
128 .then(() => {
129 return getSymptoms(authToken)
130 })
131 .then(res => {
132 return JSON.parse(res.toString())
133 })
134 .then(allSymptoms => {
135 const symptoms = isProd
136 ? ["Abdominal pain", "Cough", "Diarrhea", "Difficult defecation", "Nausea", "Vomiting", "Fever", "Reduced appetite"]
137 //? ["Abdominal pain", "Diarrhea", "Nausea", "Vomiting", "Fever"]
138 : ["Abdominal pain"]
139 const symptomIDs = getSymptomIDs(allSymptoms, symptoms)
140 if (symptoms.length !== symptomIDs.length) {
141 throw new Error("mismatch length")
142 }
143 return diag(authToken, symptomIDs, "male", 1993)
144 })
145 .then(res => {
146 return JSON.parse(res.toString())
147 })
148 .then(res => {
149 console.log("success", res)
150 console.log(JSON.stringify(res, null, 4))
151 })
152 .then(() => {
153 console.log("done")
154 })
155 .catch(err => {
156 console.log("===error===")
157 console.log(err)
158 })
159
160console.log("stared")