-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathottehr-setup.ts
150 lines (141 loc) · 4.81 KB
/
ottehr-setup.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import inquirer from 'inquirer';
import fetch from 'node-fetch';
import { setupEHR } from '../packages/telemed-ehr/zambdas/scripts/setup';
import { setupIntake } from '../packages/telemed-intake/zambdas/scripts/setup';
const projectApiUrl = 'https://project-api.zapehr.com/v1';
async function getUserInput(): Promise<{ accessToken: string; projectId: string; providerEmail: string }> {
const { accessToken, projectId, providerEmail } = await inquirer.prompt([
{
message: 'Please enter your access token:',
name: 'accessToken',
type: 'input',
validate: (input: any) => !!input || 'Access token is required',
},
{
message: 'Please enter your project id:',
name: 'projectId',
type: 'input',
validate: (input: any) => !!input || 'Project id is required',
},
{
message: 'Please enter the email of your first provider:',
name: 'providerEmail',
type: 'input',
validate: (input: any) => !!input || 'Provider email is required',
},
]);
return { accessToken, projectId, providerEmail };
}
async function createM2M(accessToken: string, projectId: string): Promise<[string, string, string]> {
return new Promise((resolve, reject) => {
fetch(`${projectApiUrl}/m2m`, {
headers: {
accept: 'application/json',
authorization: `Bearer ${accessToken}`,
'content-type': 'application/json',
'x-zapehr-project-id': `${projectId}`,
},
method: 'POST',
body: JSON.stringify({
name: 'Example M2M Client',
description: 'This M2M Client is used for initial Ottehr setup.',
accessPolicy: {
rule: [
{
resource: ['FHIR:*'],
action: ['FHIR:*'],
effect: 'Allow',
},
{
resource: ['Telemed:*'],
action: ['Telemed:*'],
effect: 'Allow',
},
{
resource: ['App:User'],
action: ['App:CreateUser', 'App:GetUser', 'App:UpdateUser', 'App:ListAllUsers'],
effect: 'Allow',
},
{
resource: ['Zambda:*'],
action: ['Zambda:*'],
effect: 'Allow',
},
{
resource: ['IAM:M2MClient:*'],
action: ['IAM:ListAllM2MClients', 'IAM:GetM2MClient'],
effect: 'Allow',
},
{
resource: ['IAM:Role'],
action: ['IAM:GetRole', 'IAM:ListAllRoles'],
effect: 'Allow',
},
{
resource: [
`Z3:${projectId}-id-cards/*`,
`Z3:${projectId}-insurance-cards/*`,
`Z3:${projectId}-school-work-note-templates/*`,
],
action: ['Z3:PutObject', 'Z3:GetObject'],
effect: 'Allow',
},
{
action: ['Messaging:SendTransactionalSMS'],
effect: 'Allow',
resource: ['*'],
},
],
},
}),
})
.then((response) => {
if (response.status === 200) {
return response.json();
} else {
console.error('response', response);
throw new Error(`Failed to create M2M client. Status: ${response.status}`);
}
})
.then((data) => {
const m2mId = data.id;
const m2mClientId = data.clientId;
const m2mDeviceId = data.profile.replace('Device/', '');
fetch(`${projectApiUrl}/m2m/${m2mId}/rotate-secret`, {
headers: {
accept: 'application/json',
authorization: `Bearer ${accessToken}`,
'x-zapehr-project-id': `${projectId}`,
},
method: 'POST',
})
.then((response) => {
if (response.status === 200) {
return response.json();
} else {
throw new Error(`Failed to rotate M2M secret. Status: ${response.status}`);
}
})
.then((secretData) => {
const m2mSecret = secretData.secret;
resolve([m2mDeviceId, m2mClientId, m2mSecret]);
})
.catch((error) => reject(error));
})
.catch((error) => reject(error));
});
}
async function runCLI(): Promise<void> {
const { accessToken, projectId, providerEmail } = await getUserInput();
console.log('Starting setup...');
const [m2mDeviceId, m2mClientId, m2mSecret] = await createM2M(accessToken, projectId);
console.log('Created m2m:', m2mClientId);
try {
await setupEHR(projectApiUrl, accessToken, projectId, providerEmail, m2mDeviceId, m2mClientId, m2mSecret);
await setupIntake(projectApiUrl, accessToken, projectId, m2mDeviceId, m2mClientId, m2mSecret);
} catch (e) {
console.log(e);
throw e;
}
}
runCLI().catch(() => process.exit(1));