@mindline/sync 1.0.30 → 1.0.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/hybridspa.ts CHANGED
@@ -1,533 +1,536 @@
1
1
  //hybridspa.ts - calls to Mindline Config API
2
2
  import {
3
- User,
4
- Tenant,
5
- Config,
6
- APIResult
3
+ User,
4
+ Tenant,
5
+ Config,
6
+ APIResult
7
7
  } from "./index";
8
8
  import {
9
- IPublicClientApplication,
10
- AuthenticationResult,
9
+ IPublicClientApplication,
10
+ AuthenticationResult,
11
11
  } from "@azure/msal-browser";
12
12
  import { deserializeArray } from "class-transformer";
13
13
 
14
14
  // add here endpoints for API services you would like to use.
15
15
  export const graphConfig = {
16
- adminEndpoint:
17
- "https://dev-configurationapi-westus.azurewebsites.net/api/v1/admin",
18
- adminIncompleteEndpoint:
19
- "https://dev-configurationapi-westus.azurewebsites.net/api/v1/incomplete-admin",
20
- adminsEndpoint:
21
- "https://dev-configurationapi-westus.azurewebsites.net/api/v1/admins",
22
- configEndpoint:
23
- "https://dev-configurationapi-westus.azurewebsites.net/api/v1/configuration",
24
- configsEndpoint:
25
- "https://dev-configurationapi-westus.azurewebsites.net/api/v1/configurations",
26
- initEndpoint:
27
- "https://dev-configurationapi-westus.azurewebsites.net/api/v1/configuration/init",
28
- tenantEndpoint:
29
- "https://dev-configurationapi-westus.azurewebsites.net/api/v1/tenant",
30
- tenantsEndpoint:
31
- "https://dev-configurationapi-westus.azurewebsites.net/api/v1/tenants",
32
- workspaceEndpoint:
33
- "https://dev-configurationapi-westus.azurewebsites.net/api/v1/workspaces",
34
-
35
- graphGroupsEndpoint: "https://graph.microsoft.com/v1.0/groups",
36
- graphMailEndpoint: "https://graph.microsoft.com/v1.0/me/messages",
37
- graphMeEndpoint: "https://graph.microsoft.com/v1.0/me",
38
- graphTenantByDomainEndpoint:
39
- "https://graph.microsoft.com/beta/tenantRelationships/findTenantInformationByDomainName",
40
- graphTenantByIdEndpoint:
41
- "https://graph.microsoft.com/beta/tenantRelationships/findTenantInformationByTenantId",
42
- graphUsersEndpoint: "https://graph.microsoft.com/v1.0/users",
16
+ // config API endpoints
17
+ adminEndpoint:
18
+ "https://dev-configurationapi-westus.azurewebsites.net/api/v1/admin",
19
+ adminIncompleteEndpoint:
20
+ "https://dev-configurationapi-westus.azurewebsites.net/api/v1/incomplete-admin",
21
+ adminsEndpoint:
22
+ "https://dev-configurationapi-westus.azurewebsites.net/api/v1/admins",
23
+ configEndpoint:
24
+ "https://dev-configurationapi-westus.azurewebsites.net/api/v1/configuration",
25
+ configsEndpoint:
26
+ "https://dev-configurationapi-westus.azurewebsites.net/api/v1/configurations",
27
+ initEndpoint:
28
+ "https://dev-configurationapi-westus.azurewebsites.net/api/v1/configuration/init",
29
+ tenantEndpoint:
30
+ "https://dev-configurationapi-westus.azurewebsites.net/api/v1/tenant",
31
+ tenantsEndpoint:
32
+ "https://dev-configurationapi-westus.azurewebsites.net/api/v1/tenants",
33
+ workspaceEndpoint:
34
+ "https://dev-configurationapi-westus.azurewebsites.net/api/v1/workspaces",
35
+ // graph API endpoints
36
+ graphGroupsEndpoint: "https://graph.microsoft.com/v1.0/groups",
37
+ graphMailEndpoint: "https://graph.microsoft.com/v1.0/me/messages",
38
+ graphMeEndpoint: "https://graph.microsoft.com/v1.0/me",
39
+ graphTenantByDomainEndpoint:
40
+ "https://graph.microsoft.com/beta/tenantRelationships/findTenantInformationByDomainName",
41
+ graphTenantByIdEndpoint:
42
+ "https://graph.microsoft.com/beta/tenantRelationships/findTenantInformationByTenantId",
43
+ graphUsersEndpoint: "https://graph.microsoft.com/v1.0/users",
44
+ // reader endpoint to trigger sync start
45
+ readerEndpoint: "https://dev-fn-reader-westus.azurewebsites.net/api/startSync/"
43
46
  };
44
47
  // helper functions
45
48
  async function defineHeaders(
46
- instance: IPublicClientApplication,
47
- user: User
49
+ instance: IPublicClientApplication,
50
+ user: User
48
51
  ): Promise<Headers> {
49
- const headers = new Headers();
50
- headers.append("Content-Type", "application/json");
51
- headers.append("accept", "*/*");
52
- // authorization header - if needed, retrieve and cache access token
53
- if (user.accessToken == null || user.accessToken === "") {
54
- try {
55
- let response: AuthenticationResult = await instance.acquireTokenByCode({
56
- code: user.spacode,
57
- });
58
- user.accessToken = response.accessToken; // cache access token
59
- console.log("Front end token acquired: " + user.accessToken.slice(0,20));
60
- }
61
- catch (error: any) {
62
- console.log("Front end token failure: " + error);
52
+ const headers = new Headers();
53
+ headers.append("Content-Type", "application/json");
54
+ headers.append("accept", "*/*");
55
+ // authorization header - if needed, retrieve and cache access token
56
+ if (user.accessToken == null || user.accessToken === "") {
57
+ try {
58
+ let response: AuthenticationResult = await instance.acquireTokenByCode({
59
+ code: user.spacode,
60
+ });
61
+ user.accessToken = response.accessToken; // cache access token
62
+ console.log("Front end token acquired: " + user.accessToken.slice(0, 20));
63
+ }
64
+ catch (error: any) {
65
+ console.log("Front end token failure: " + error);
66
+ }
63
67
  }
64
- }
65
- headers.append("Authorization", `Bearer ${user.accessToken}`);
66
- return headers;
68
+ headers.append("Authorization", `Bearer ${user.accessToken}`);
69
+ return headers;
67
70
  }
68
71
  async function processErrors(response: Response): Promise<string> {
69
- let errorString: string = "";
70
- if(response.status === 401) {
71
- errorString = response.statusText;
72
- }
73
- else{
74
- let data = await response.json();
75
- if (data.error !== undefined) {
76
- errorString = `Error: ${data.error} Message: ${data.message}`;
77
- } else if (data.errors !== undefined) {
78
- let errorArray = Object.keys(data.errors);
79
- let errorlist: string = "";
80
- errorString = errorArray.reduce(
81
- (acc, curr) => acc + curr + ": " + data.errors[curr] + " ",
82
- errorlist
83
- );
84
- } else if (data.title !== undefined) {
85
- errorString = data.title;
86
- } else {
87
- debugger;
88
- }
89
- }
90
- return errorString;
72
+ let errorString: string = "";
73
+ if (response.status === 401) {
74
+ errorString = response.statusText;
75
+ }
76
+ else {
77
+ let data = await response.json();
78
+ if (data.error !== undefined) {
79
+ errorString = `Error: ${data.error} Message: ${data.message}`;
80
+ } else if (data.errors !== undefined) {
81
+ let errorArray = Object.keys(data.errors);
82
+ let errorlist: string = "";
83
+ errorString = errorArray.reduce(
84
+ (acc, curr) => acc + curr + ": " + data.errors[curr] + " ",
85
+ errorlist
86
+ );
87
+ } else if (data.title !== undefined) {
88
+ errorString = data.title;
89
+ } else {
90
+ debugger;
91
+ }
92
+ }
93
+ return errorString;
91
94
  }
92
95
  //adminDelete
93
96
  export async function adminDelete(
94
- instance: IPublicClientApplication,
95
- authorizedUser: User,
96
- user: User,
97
- workspaceId: string
97
+ instance: IPublicClientApplication,
98
+ authorizedUser: User,
99
+ user: User,
100
+ workspaceId: string
98
101
  ): Promise<APIResult> {
99
- let result: APIResult = new APIResult();
100
- // we need either oid or mail for the user and valid workspace id
101
- if ((user.oid == "" && user.mail == "") || workspaceId == "") {
102
- result.result = false;
103
- result.error = "configPost: invalid config ID";
104
- result.status = 500;
102
+ let result: APIResult = new APIResult();
103
+ // we need either oid or mail for the user and valid workspace id
104
+ if ((user.oid == "" && user.mail == "") || workspaceId == "") {
105
+ result.result = false;
106
+ result.error = "adminDelete: invalid parameters";
107
+ result.status = 500;
108
+ return result;
109
+ }
110
+ // are we performing deletion of a full admin?
111
+ let url: URL | null = null;
112
+ if (user.oid !== user.mail) {
113
+ url = new URL(graphConfig.adminEndpoint);
114
+ url.searchParams.append("userId", user.oid);
115
+ url.searchParams.append("workspaceId", workspaceId);
116
+ }
117
+ // or of an incomplete admin?
118
+ else if (user.mail !== "") {
119
+ url = new URL(graphConfig.adminIncompleteEndpoint);
120
+ url.searchParams.append("email", user.mail);
121
+ url.searchParams.append("workspaceId", workspaceId);
122
+ }
123
+ // create headers
124
+ const headers = await defineHeaders(instance, authorizedUser);
125
+ // make endpoint call
126
+ let options = { method: "DELETE", headers: headers };
127
+ try {
128
+ console.log("Attempting DELETE from /admin: " + url!.href);
129
+ let response = await fetch(url!.href, options);
130
+ if (response.status === 200 && response.statusText === "OK") {
131
+ console.log(`Successful DELETE from //admin: ${url!.href}`);
132
+ return result;
133
+ } else {
134
+ result.error = await processErrors(response);
135
+ console.log(`Failed DELETE from /admin: ${url.href}`);
136
+ console.log(result.error);
137
+ result.status = 500;
138
+ result.result = false;
139
+ return result;
140
+ }
141
+ } catch (error: any) {
142
+ result.error = error.message;
143
+ result.status = 500;
144
+ result.result = false;
145
+ console.log(error.message);
146
+ }
105
147
  return result;
106
- }
107
- // are we performing deletion of a full admin?
108
- let url: URL | null = null;
109
- if (user.oid !== user.mail) {
110
- url = new URL(graphConfig.adminEndpoint);
111
- url.searchParams.append("userId", user.oid);
112
- url.searchParams.append("workspaceId", workspaceId);
113
- }
114
- // or of an incomplete admin?
115
- else if (user.mail !== "") {
116
- url = new URL(graphConfig.adminIncompleteEndpoint);
117
- url.searchParams.append("email", user.mail);
118
- url.searchParams.append("workspaceId", workspaceId);
119
- }
120
- // create headers
121
- const headers = await defineHeaders(instance, authorizedUser);
122
- // make endpoint call
123
- let options = { method: "DELETE", headers: headers };
124
- try {
125
- console.log("Attempting DELETE from /admin: " + url!.href);
126
- let response = await fetch(url!.href, options);
127
- if (response.status === 200 && response.statusText === "OK") {
128
- console.log(`Successful DELETE from //admin: ${url!.href}`);
129
- return result;
130
- } else {
131
- result.error = await processErrors(response);
132
- console.log(`Failed DELETE from /admin: ${url.href}`);
133
- console.log(result.error);
134
- result.status = 500;
135
- result.result = false;
136
- return result;
137
- }
138
- } catch (error: any) {
139
- result.error = error.message;
140
- result.status = 500;
141
- result.result = false;
142
- console.log(error.message);
143
- }
144
- return result;
145
148
  }
146
149
  //adminsGet
147
150
  export async function adminsGet(
148
- instance: IPublicClientApplication,
149
- authorizedUser: User,
150
- workspaceID: string,
151
- debug: boolean
151
+ instance: IPublicClientApplication,
152
+ authorizedUser: User,
153
+ workspaceID: string,
154
+ debug: boolean
152
155
  ): Promise<APIResult> {
153
- let result: APIResult = new APIResult();
154
- // we need a workspace id
155
- if (workspaceID === "") {
156
- result.result = false;
157
- result.status = 500;
158
- result.error = "adminsGet: no workspace provided";
159
- return result;
160
- }
161
- // create endpoint
162
- let adminsEndpoint: string = graphConfig.adminsEndpoint;
163
- // add parameter to endpoint
164
- let url: URL = new URL(adminsEndpoint);
165
- url.searchParams.append("workspaceId", workspaceID);
166
- // create headers
167
- const headers = await defineHeaders(instance, authorizedUser);
168
- // make endpoint call
169
- let options = { method: "GET", headers: headers };
170
- try {
171
- if (debug) debugger;
172
- console.log("Attempting GET from /admins: " + url.href);
173
- let response = await fetch(url.href, options);
174
- if (response.status === 200 && response.statusText === "OK") {
175
- let returnedArray: Array<Object> = await response.json();
176
- if(returnedArray != null) {
177
- result.array = returnedArray;
178
- let initialValue: string = "";
179
- console.log(`Successful GET from /admins: ${result.array.reduce((acc, curr) => acc + curr.email + " ", initialValue)}`);
156
+ let result: APIResult = new APIResult();
157
+ // we need a workspace id
158
+ if (workspaceID === "") {
159
+ result.result = false;
160
+ result.status = 500;
161
+ result.error = "adminsGet: no workspace provided";
180
162
  return result;
181
- }
182
- else {
183
- result.error = `Failed GET from /admins: failed to JSON-parse response`;
184
- console.log(result.error);
163
+ }
164
+ // create endpoint
165
+ let adminsEndpoint: string = graphConfig.adminsEndpoint;
166
+ // add parameter to endpoint
167
+ let url: URL = new URL(adminsEndpoint);
168
+ url.searchParams.append("workspaceId", workspaceID);
169
+ // create headers
170
+ const headers = await defineHeaders(instance, authorizedUser);
171
+ // make endpoint call
172
+ let options = { method: "GET", headers: headers };
173
+ try {
174
+ if (debug) debugger;
175
+ console.log("Attempting GET from /admins: " + url.href);
176
+ let response = await fetch(url.href, options);
177
+ if (response.status === 200 && response.statusText === "OK") {
178
+ let returnedArray: Array<Object> = await response.json();
179
+ if (returnedArray != null) {
180
+ result.array = returnedArray;
181
+ let initialValue: string = "";
182
+ console.log(`Successful GET from /admins: ${result.array.reduce((acc, curr) => acc + curr.email + " ", initialValue)}`);
183
+ return result;
184
+ }
185
+ else {
186
+ result.error = `Failed GET from /admins: failed to JSON-parse response`;
187
+ console.log(result.error);
188
+ result.status = 500;
189
+ result.result = false;
190
+ return result;
191
+ }
192
+ }
193
+ else {
194
+ console.log(`Failed GET from /admins: ${url.href}`);
195
+ result.error = await processErrors(response);
196
+ result.status = 500;
197
+ result.result = false;
198
+ console.log(result.error);
199
+ return result;
200
+ }
201
+ }
202
+ catch (error: any) {
203
+ result.error = error.message;
185
204
  result.status = 500;
186
205
  result.result = false;
187
- return result;
188
- }
206
+ console.log(error.message);
189
207
  }
190
- else {
191
- console.log(`Failed GET from /admins: ${url.href}`);
192
- result.error = await processErrors(response);
193
- result.status = 500;
194
- result.result = false;
195
- console.log(result.error);
196
- return result;
197
- }
198
- }
199
- catch (error: any) {
200
- result.error = error.message;
201
- result.status = 500;
202
- result.result = false;
203
- console.log(error.message);
204
- }
205
- return result;
208
+ return result;
206
209
  }
207
210
  //adminPost: write validated admin to back end
208
211
  export async function adminPost(
209
- instance: IPublicClientApplication,
210
- authorizedUser: User,
211
- user: User,
212
- workspaceId: string
212
+ instance: IPublicClientApplication,
213
+ authorizedUser: User,
214
+ user: User,
215
+ workspaceId: string
213
216
  ): Promise<APIResult> {
214
- let result: APIResult = new APIResult();
215
- if (
216
- user.mail == null ||
217
- user.mail === "" ||
218
- user.authority == null ||
219
- user.authority === "" ||
220
- user.tid == null ||
221
- user.tid === "" ||
222
- user.companyName == null ||
223
- user.companyName === "" ||
224
- user.companyDomain == null ||
225
- user.companyDomain === ""
226
- ) {
227
- result.result = false;
228
- result.error = "configPost: invalid config ID";
229
- result.status = 500;
230
- return result;
231
- }
232
- // create admin endpoint
233
- let adminEndpoint: string = graphConfig.adminEndpoint;
234
- // create headers
235
- const headers = await defineHeaders(instance, authorizedUser);
236
- // create admin body
237
- let adminBody: string = `
217
+ let result: APIResult = new APIResult();
218
+ if (
219
+ user.mail == null ||
220
+ user.mail === "" ||
221
+ user.authority == null ||
222
+ user.authority === "" ||
223
+ user.tid == null ||
224
+ user.tid === "" ||
225
+ user.companyName == null ||
226
+ user.companyName === "" ||
227
+ user.companyDomain == null ||
228
+ user.companyDomain === ""
229
+ ) {
230
+ result.result = false;
231
+ result.error = "configPost: invalid config ID";
232
+ result.status = 500;
233
+ return result;
234
+ }
235
+ // create admin endpoint
236
+ let adminEndpoint: string = graphConfig.adminEndpoint;
237
+ // create headers
238
+ const headers = await defineHeaders(instance, authorizedUser);
239
+ // create admin body
240
+ let adminBody: string = `
238
241
  {"email": "${user.mail}",
239
242
  "tenantId": "${user.tid}",
240
243
  "workspaceId": "${workspaceId}"
241
244
  }`;
242
- let options = { method: "POST", headers: headers, body: adminBody };
243
- // make admin endpoint call
244
- try {
245
- console.log("Attempting POST to /admin: " + adminEndpoint);
246
- let response = await fetch(adminEndpoint, options);
247
- if (response.status === 200 && response.statusText === "OK") {
248
- console.log(`Successful POST to /admin: ${adminBody}`);
249
- return result;
250
- } else {
251
- result.error = await processErrors(response);
252
- console.log(`Failed POST to /admin: ${adminBody}`);
253
- console.log(result.error);
254
- result.status = 500;
255
- result.result = false;
256
- return result;
257
- }
258
- } catch (error: any) {
259
- result.error = error.message;
260
- result.status = 500;
261
- result.result = false;
262
- console.log(error.message);
263
- }
264
- return result;
245
+ let options = { method: "POST", headers: headers, body: adminBody };
246
+ // make admin endpoint call
247
+ try {
248
+ console.log("Attempting POST to /admin: " + adminEndpoint);
249
+ let response = await fetch(adminEndpoint, options);
250
+ if (response.status === 200 && response.statusText === "OK") {
251
+ console.log(`Successful POST to /admin: ${adminBody}`);
252
+ return result;
253
+ } else {
254
+ result.error = await processErrors(response);
255
+ console.log(`Failed POST to /admin: ${adminBody}`);
256
+ console.log(result.error);
257
+ result.status = 500;
258
+ result.result = false;
259
+ return result;
260
+ }
261
+ } catch (error: any) {
262
+ result.error = error.message;
263
+ result.status = 500;
264
+ result.result = false;
265
+ console.log(error.message);
266
+ }
267
+ return result;
265
268
  }
266
269
  //configDelete
267
270
  export async function configDelete(
268
- instance: IPublicClientApplication,
269
- authorizedUser: User,
270
- config: Config,
271
- workspaceId: string,
272
- debug: boolean
271
+ instance: IPublicClientApplication,
272
+ authorizedUser: User,
273
+ config: Config,
274
+ workspaceId: string,
275
+ debug: boolean
273
276
  ): Promise<APIResult> {
274
- let result: APIResult = new APIResult();
275
- if (config.id === "" || workspaceId == "") {
276
- result.result = false;
277
- result.error = "configPost: invalid config ID";
278
- result.status = 500;
277
+ let result: APIResult = new APIResult();
278
+ if (config.id === "" || workspaceId == "") {
279
+ result.result = false;
280
+ result.error = "configPost: invalid config ID";
281
+ result.status = 500;
282
+ return result;
283
+ }
284
+ let url: URL | null = null;
285
+ url = new URL(graphConfig.configEndpoint);
286
+ url.searchParams.append("configurationId", config.id);
287
+ // create headers
288
+ const headers = await defineHeaders(instance, authorizedUser);
289
+ // make endpoint call
290
+ let options = { method: "DELETE", headers: headers };
291
+ try {
292
+ console.log("Attempting DELETE from /config: " + url.href);
293
+ let response = await fetch(url.href, options);
294
+ if (response.status === 200 && response.statusText === "OK") {
295
+ console.log(`Successful DELETE from /config`);
296
+ return result;
297
+ }
298
+ else {
299
+ result.error = await processErrors(response);
300
+ console.log(`Failed DELETE from ${url.href}`);
301
+ console.log(result.error);
302
+ result.status = 500;
303
+ result.result = false;
304
+ return result;
305
+ }
306
+ }
307
+ catch (error: any) {
308
+ result.error = error.message;
309
+ result.status = 500;
310
+ result.result = false;
311
+ console.log(error.message);
312
+ }
279
313
  return result;
280
- }
281
- let url: URL | null = null;
282
- url = new URL(graphConfig.configEndpoint);
283
- url.searchParams.append("configurationId", config.id);
284
- // create headers
285
- const headers = await defineHeaders(instance, authorizedUser);
286
- // make endpoint call
287
- let options = { method: "DELETE", headers: headers };
288
- try {
289
- console.log("Attempting DELETE from /config: " + url.href);
290
- let response = await fetch(url.href, options);
291
- if (response.status === 200 && response.statusText === "OK") {
292
- console.log(`Successful DELETE from /config`);
293
- return result;
294
- }
295
- else {
296
- result.error = await processErrors(response);
297
- console.log(`Failed DELETE from ${url.href}`);
298
- console.log(result.error);
299
- result.status = 500;
300
- result.result = false;
301
- return result;
302
- }
303
- }
304
- catch (error: any) {
305
- result.error = error.message;
306
- result.status = 500;
307
- result.result = false;
308
- console.log(error.message);
309
- }
310
- return result;
311
314
  }
312
315
  //configPost
313
316
  export async function configPost(
314
- instance: IPublicClientApplication,
315
- authorizedUser: User,
316
- config: Config,
317
- workspaceId: string,
318
- debug: boolean
317
+ instance: IPublicClientApplication,
318
+ authorizedUser: User,
319
+ config: Config,
320
+ workspaceId: string,
321
+ debug: boolean
319
322
  ): Promise<APIResult> {
320
- let result: APIResult = new APIResult();
321
- if (config.id === ""){
322
- result.result = false;
323
- result.error = "configPost: invalid config ID";
324
- result.status = 500;
325
- return result;
326
- }
327
- // create no parameter config endpoint
328
- let configEndpoint: string = graphConfig.configEndpoint;
329
- // create config headers
330
- const headers = await defineHeaders(instance, authorizedUser);
331
- // create config body
332
- let configBody: string = `
323
+ let result: APIResult = new APIResult();
324
+ if (config.id === "") {
325
+ result.result = false;
326
+ result.error = "configPost: invalid config ID";
327
+ result.status = 500;
328
+ return result;
329
+ }
330
+ // create no parameter config endpoint
331
+ let configEndpoint: string = graphConfig.configEndpoint;
332
+ // create config headers
333
+ const headers = await defineHeaders(instance, authorizedUser);
334
+ // create config body
335
+ let configBody: string = `
333
336
  {
334
337
  "workspaceId": "${workspaceId}",
335
338
  "name": "${config.name}",
336
339
  "description": "${config.description}",
337
340
  "isEnabled": ${config.isEnabled},
338
341
  "tenants": [`;
339
- config.tenants.map((tci) => {
340
- // if last character is } we need a comma first
341
- let needComma: boolean = configBody.slice(-1) === "}";
342
- if (needComma) configBody += ",";
343
- configBody += `{
342
+ config.tenants.map((tci) => {
343
+ // if last character is } we need a comma first
344
+ let needComma: boolean = configBody.slice(-1) === "}";
345
+ if (needComma) configBody += ",";
346
+ configBody += `{
344
347
  "tenantId": "${tci.tid}",
345
348
  "sourceGroupId": "${tci.sourceGroupId}",
346
349
  "sourceGroupName": "${tci.sourceGroupName}",
347
350
  "configurationTenantType": "${tci.configurationTenantType}"
348
351
  }`;
349
- });
350
- configBody += `]}`;
351
- let options = { method: "POST", headers: headers, body: configBody };
352
- // make config endpoint call
353
- try {
354
- if (debug) debugger;
355
- console.log("Attempting POST to /config: " + configEndpoint);
356
- let response = await fetch(configEndpoint, options);
357
- if (response.status === 200 && response.statusText === "OK") {
358
- let data = await response.json();
359
- config.id = data;
360
- console.log(
361
- `Successful ConfigID: ${data} from POST to /config: ${configBody}`
362
- );
363
- return result;
364
- }
365
- else {
366
- result.error = await processErrors(response);
367
- console.log(`Failed PUT to /config: ${configBody}`);
368
- console.log(result.error);
369
- result.status = 500;
370
- result.result = false;
371
- return result;
372
- }
373
- }
374
- catch (error: any) {
375
- result.status = 500;
376
- result.result = false;
377
- result.error = error.message;
378
- console.log(result.error);
379
- return result;
380
- }
352
+ });
353
+ configBody += `]}`;
354
+ let options = { method: "POST", headers: headers, body: configBody };
355
+ // make config endpoint call
356
+ try {
357
+ if (debug) debugger;
358
+ console.log("Attempting POST to /config: " + configEndpoint);
359
+ let response = await fetch(configEndpoint, options);
360
+ if (response.status === 200 && response.statusText === "OK") {
361
+ let data = await response.json();
362
+ config.id = data;
363
+ console.log(
364
+ `Successful ConfigID: ${data} from POST to /config: ${configBody}`
365
+ );
366
+ return result;
367
+ }
368
+ else {
369
+ result.error = await processErrors(response);
370
+ console.log(`Failed PUT to /config: ${configBody}`);
371
+ console.log(result.error);
372
+ result.status = 500;
373
+ result.result = false;
374
+ return result;
375
+ }
376
+ }
377
+ catch (error: any) {
378
+ result.status = 500;
379
+ result.result = false;
380
+ result.error = error.message;
381
+ console.log(result.error);
382
+ return result;
383
+ }
381
384
  }
382
385
  //configPut
383
386
  export async function configPut(
384
- instance: IPublicClientApplication,
385
- authorizedUser: User,
386
- config: Config,
387
- debug: boolean
387
+ instance: IPublicClientApplication,
388
+ authorizedUser: User,
389
+ config: Config,
390
+ debug: boolean
388
391
  ): Promise<APIResult> {
389
- let result: APIResult = new APIResult();
390
- if (config.id === "") {
391
- result.result = false;
392
- result.error = "configPost: invalid config ID";
393
- result.status = 500;
394
- return result;
395
- }
396
- // create parametrized config endpoint
397
- let configEndpoint: string = graphConfig.configEndpoint;
398
- let url: URL = new URL(configEndpoint);
399
- url.searchParams.append("configurationId", config.id);
400
- // create config headers
401
- const headers = await defineHeaders(instance, authorizedUser);
402
- // create config body
403
- let configBody: string = `
392
+ let result: APIResult = new APIResult();
393
+ if (config.id === "") {
394
+ result.result = false;
395
+ result.error = "configPost: invalid config ID";
396
+ result.status = 500;
397
+ return result;
398
+ }
399
+ // create parametrized config endpoint
400
+ let configEndpoint: string = graphConfig.configEndpoint;
401
+ let url: URL = new URL(configEndpoint);
402
+ url.searchParams.append("configurationId", config.id);
403
+ // create config headers
404
+ const headers = await defineHeaders(instance, authorizedUser);
405
+ // create config body
406
+ let configBody: string = `
404
407
  {
405
408
  "name": "${config.name}",
406
409
  "description": "${config.description}",
407
410
  "isEnabled": ${config.isEnabled},
408
411
  "tenants": [`;
409
- config.tenants.map((tci) => {
410
- // if last character is } we need a comma first
411
- let needComma: boolean = configBody.slice(-1) === "}";
412
- if (needComma) configBody += ",";
413
- // decide whethere to send source group values
414
- configBody += `{
412
+ config.tenants.map((tci) => {
413
+ // if last character is } we need a comma first
414
+ let needComma: boolean = configBody.slice(-1) === "}";
415
+ if (needComma) configBody += ",";
416
+ // decide whethere to send source group values
417
+ configBody += `{
415
418
  "tenantId": "${tci.tid}",
416
419
  "sourceGroupId": "${tci.sourceGroupId}",
417
420
  "sourceGroupName": "${tci.sourceGroupName}",
418
421
  "configurationTenantType": "${tci.configurationTenantType}"
419
422
  }`;
420
- });
421
- configBody += `]}`;
422
- let options = { method: "PUT", headers: headers, body: configBody };
423
- // make config endpoint call
424
- try {
425
- if (debug) debugger;
426
- console.log("Attempting PUT to /config: " + url.href);
427
- let response = await fetch(url.href, options);
428
- if (response.status === 200 && response.statusText === "OK") {
429
- console.log(`Successful PUT to ${url.href}: ${configBody}`);
430
- return result;
423
+ });
424
+ configBody += `]}`;
425
+ let options = { method: "PUT", headers: headers, body: configBody };
426
+ // make config endpoint call
427
+ try {
428
+ if (debug) debugger;
429
+ console.log("Attempting PUT to /config: " + url.href);
430
+ let response = await fetch(url.href, options);
431
+ if (response.status === 200 && response.statusText === "OK") {
432
+ console.log(`Successful PUT to ${url.href}: ${configBody}`);
433
+ return result;
434
+ }
435
+ else {
436
+ result.error = await processErrors(response);
437
+ console.log(`Failed PUT to ${url.href}: ${configBody}`);
438
+ console.log(result.error);
439
+ result.status = 500;
440
+ result.result = false;
441
+ return result;
442
+ }
431
443
  }
432
- else {
433
- result.error = await processErrors(response);
434
- console.log(`Failed PUT to ${url.href}: ${configBody}`);
435
- console.log(result.error);
436
- result.status = 500;
437
- result.result = false;
438
- return result;
439
- }
440
- }
441
- catch (error: any) {
442
- result.error = error.message;
443
- result.status = 500;
444
- result.result = false;
445
- console.log(error.message);
446
- }
447
- return result;
444
+ catch (error: any) {
445
+ result.error = error.message;
446
+ result.status = 500;
447
+ result.result = false;
448
+ console.log(error.message);
449
+ }
450
+ return result;
448
451
  }
449
452
  //configsGet
450
453
  export async function configsGet(
451
- instance: IPublicClientApplication,
452
- authorizedUser: User,
453
- workspaceID: string,
454
- debug: boolean
454
+ instance: IPublicClientApplication,
455
+ authorizedUser: User,
456
+ workspaceID: string,
457
+ debug: boolean
455
458
  ): Promise<APIResult> {
456
- let result: APIResult = new APIResult();
457
- // we need a workspace id
458
- if (workspaceID === "") {
459
- result.result = false;
460
- result.status = 500;
461
- result.error = "configsGet: no workspace provided";
462
- return result;
463
- }
464
- // create endpoint
465
- let configsEndpoint: string = graphConfig.configsEndpoint;
466
- // add parameter to endpoint
467
- let url: URL = new URL(configsEndpoint);
468
- url.searchParams.append("workspaceId", workspaceID);
469
- // create headers
470
- const headers = await defineHeaders(instance, authorizedUser);
471
- // make endpoint call
472
- let options = { method: "GET", headers: headers };
473
- try {
474
- if (debug) debugger;
475
- console.log("Attempting GET from /configurations: " + url.href);
476
- let response = await fetch(url.href, options);
477
- if (response.status === 200 && response.statusText === "OK") {
478
- let returnedArray: Array<Object> = await response.json();
479
- if(returnedArray != null){
480
- result.array = returnedArray;
481
- let initialValue: string = "";
482
- console.log(`Successful GET from /configurations: ${result.array.reduce((acc, curr) => acc + curr.name + " ", initialValue)}`);
459
+ let result: APIResult = new APIResult();
460
+ // we need a workspace id
461
+ if (workspaceID === "") {
462
+ result.result = false;
463
+ result.status = 500;
464
+ result.error = "configsGet: no workspace provided";
483
465
  return result;
484
- }
485
- else {
486
- result.error = `Failed GET from /configurations: failed to JSON-parse response`;
487
- console.log(result.error);
466
+ }
467
+ // create endpoint
468
+ let configsEndpoint: string = graphConfig.configsEndpoint;
469
+ // add parameter to endpoint
470
+ let url: URL = new URL(configsEndpoint);
471
+ url.searchParams.append("workspaceId", workspaceID);
472
+ // create headers
473
+ const headers = await defineHeaders(instance, authorizedUser);
474
+ // make endpoint call
475
+ let options = { method: "GET", headers: headers };
476
+ try {
477
+ if (debug) debugger;
478
+ console.log("Attempting GET from /configurations: " + url.href);
479
+ let response = await fetch(url.href, options);
480
+ if (response.status === 200 && response.statusText === "OK") {
481
+ let returnedArray: Array<Object> = await response.json();
482
+ if (returnedArray != null) {
483
+ result.array = returnedArray;
484
+ let initialValue: string = "";
485
+ console.log(`Successful GET from /configurations: ${result.array.reduce((acc, curr) => acc + curr.name + " ", initialValue)}`);
486
+ return result;
487
+ }
488
+ else {
489
+ result.error = `Failed GET from /configurations: failed to JSON-parse response`;
490
+ console.log(result.error);
491
+ result.status = 500;
492
+ result.result = false;
493
+ return result;
494
+ }
495
+ }
496
+ else {
497
+ console.log(`Failed GET from /configurations: ${url.href}`);
498
+ result.error = await processErrors(response);
499
+ result.status = 500;
500
+ result.result = false;
501
+ console.log(result.error);
502
+ return result;
503
+ }
504
+ }
505
+ catch (error: any) {
506
+ result.error = error.message;
488
507
  result.status = 500;
489
508
  result.result = false;
490
- return result;
491
- }
509
+ console.log(error.message);
492
510
  }
493
- else {
494
- console.log(`Failed GET from /configurations: ${url.href}`);
495
- result.error = await processErrors(response);
496
- result.status = 500;
497
- result.result = false;
498
- console.log(result.error);
499
- return result;
500
- }
501
- }
502
- catch (error: any) {
503
- result.error = error.message;
504
- result.status = 500;
505
- result.result = false;
506
- console.log(error.message);
507
- }
508
- return result;
511
+ return result;
509
512
  }
510
513
  //initPost
511
514
  export async function initPost(
512
- instance: IPublicClientApplication,
513
- authorizedUser: User,
514
- user: User,
515
- debug: boolean
515
+ instance: IPublicClientApplication,
516
+ authorizedUser: User,
517
+ user: User,
518
+ debug: boolean
516
519
  ): Promise<APIResult> {
517
- let result: APIResult = new APIResult();
518
- // we expect valid company name and domain by this point
519
- if (user.companyName === "" || user.companyDomain === "") {
520
- result.result = false;
521
- result.error = "initPost: invalid company name or domain"
522
- result.status = 500;
523
- return result;
524
- }
525
- // create init endpoint
526
- let initEndpoint: string = graphConfig.initEndpoint;
527
- // create init headers
528
- const headers = await defineHeaders(instance, authorizedUser);
529
- // create init body
530
- let initBody: string = `
520
+ let result: APIResult = new APIResult();
521
+ // we expect valid company name and domain by this point
522
+ if (user.companyName === "" || user.companyDomain === "") {
523
+ result.result = false;
524
+ result.error = "initPost: invalid company name or domain"
525
+ result.status = 500;
526
+ return result;
527
+ }
528
+ // create init endpoint
529
+ let initEndpoint: string = graphConfig.initEndpoint;
530
+ // create init headers
531
+ const headers = await defineHeaders(instance, authorizedUser);
532
+ // create init body
533
+ let initBody: string = `
531
534
  {
532
535
  "tenantCreateModel": {
533
536
  "tenantId": "${user.tid}",
@@ -537,311 +540,354 @@ export async function initPost(
537
540
  "authority": "${user.authority}"
538
541
  }
539
542
  }`;
540
- let options = { method: "POST", headers: headers, body: initBody };
541
- // make init endpoint call
542
- try {
543
- if (debug) debugger;
544
- console.log("Attempting POST to /configuration/init: " + initEndpoint);
545
- let response = await fetch(initEndpoint, options);
546
- if (response.status === 200 && response.statusText === "OK") {
547
- console.log(`Successful POST to /configuration/init: ${initBody}`);
548
- return result;
549
- }
550
- else {
551
- result.error = await processErrors(response);
552
- result.status = 500;
553
- result.result = false;
554
- console.log(`Failed POST to /configuration/init: ${initBody}`);
555
- console.log(result.error);
556
- return result;
557
- }
558
- }
559
- catch (error: any) {
560
- result.error = error.message;
561
- console.log(result.error);
562
- }
563
- result.status = 500;
564
- result.result = false;
565
- return result;
543
+ let options = { method: "POST", headers: headers, body: initBody };
544
+ // make init endpoint call
545
+ try {
546
+ if (debug) debugger;
547
+ console.log("Attempting POST to /configuration/init: " + initEndpoint);
548
+ let response = await fetch(initEndpoint, options);
549
+ if (response.status === 200 && response.statusText === "OK") {
550
+ console.log(`Successful POST to /configuration/init: ${initBody}`);
551
+ return result;
552
+ }
553
+ else {
554
+ result.error = await processErrors(response);
555
+ result.status = 500;
556
+ result.result = false;
557
+ console.log(`Failed POST to /configuration/init: ${initBody}`);
558
+ console.log(result.error);
559
+ return result;
560
+ }
561
+ }
562
+ catch (error: any) {
563
+ result.error = error.message;
564
+ console.log(result.error);
565
+ }
566
+ result.status = 500;
567
+ result.result = false;
568
+ return result;
566
569
  }
567
570
  //tenantDelete
568
571
  export async function tenantDelete(
569
- instance: IPublicClientApplication,
570
- authorizedUser: User,
571
- tenant: Tenant,
572
- workspaceId: string,
573
- debug: boolean
572
+ instance: IPublicClientApplication,
573
+ authorizedUser: User,
574
+ tenant: Tenant,
575
+ workspaceId: string,
576
+ debug: boolean
574
577
  ): Promise<APIResult> {
575
- let result: APIResult = new APIResult();
576
- // we expect valid tid amd workspaceId
577
- if (tenant.tid === "" || workspaceId === "") {
578
- result.result = false;
579
- result.error = "tenantDelete: invalid tid, workspaceId";
580
- result.status = 500;
581
- return result;
582
- }
583
- // create parametrized tenant endpoint
584
- let url: URL = new URL(graphConfig.tenantEndpoint);
585
- url.searchParams.append("tenantId", tenant.tid);
586
- url.searchParams.append("workspaceId", workspaceId);
587
- // create headers
588
- const headers = await defineHeaders(instance, authorizedUser);
589
- // make tenant endpoint call
590
- let options = { method: "DELETE", headers: headers };
591
- try {
592
- console.log("Attempting DELETE from /tenant: " + url.href);
593
- let response = await fetch(url.href, options);
594
- if (response.status === 200 && response.statusText === "OK") {
595
- console.log(`Successful DELETE from /tenant: ${url.href}`);
578
+ let result: APIResult = new APIResult();
579
+ // we expect valid tid amd workspaceId
580
+ if (tenant.tid === "" || workspaceId === "") {
581
+ result.result = false;
582
+ result.error = "tenantDelete: invalid tid, workspaceId";
583
+ result.status = 500;
596
584
  return result;
597
585
  }
598
- else {
599
- console.log(`Failed DELETE from /tenant: ${url.href}`);
600
- result.error = await processErrors(response);
601
- console.log(result.error);
602
- result.status = 500;
603
- result.result = false;
604
- return result;
605
- }
606
- }
607
- catch (error: any) {
608
- result.error = error.message;
609
- result.status = 500;
610
- result.result = false;
611
- console.log(result.error);
612
- }
613
- return result;
586
+ // create parametrized tenant endpoint
587
+ let url: URL = new URL(graphConfig.tenantEndpoint);
588
+ url.searchParams.append("tenantId", tenant.tid);
589
+ url.searchParams.append("workspaceId", workspaceId);
590
+ // create headers
591
+ const headers = await defineHeaders(instance, authorizedUser);
592
+ // make tenant endpoint call
593
+ let options = { method: "DELETE", headers: headers };
594
+ try {
595
+ console.log("Attempting DELETE from /tenant: " + url.href);
596
+ let response = await fetch(url.href, options);
597
+ if (response.status === 200 && response.statusText === "OK") {
598
+ console.log(`Successful DELETE from /tenant: ${url.href}`);
599
+ return result;
600
+ }
601
+ else {
602
+ console.log(`Failed DELETE from /tenant: ${url.href}`);
603
+ result.error = await processErrors(response);
604
+ console.log(result.error);
605
+ result.status = 500;
606
+ result.result = false;
607
+ return result;
608
+ }
609
+ }
610
+ catch (error: any) {
611
+ result.error = error.message;
612
+ result.status = 500;
613
+ result.result = false;
614
+ console.log(result.error);
615
+ }
616
+ return result;
614
617
  }
615
618
  //tenantsGet
616
619
  export async function tenantsGet(
617
- instance: IPublicClientApplication,
618
- authorizedUser: User,
619
- workspaceID: string,
620
- debug: boolean
620
+ instance: IPublicClientApplication,
621
+ authorizedUser: User,
622
+ workspaceID: string,
623
+ debug: boolean
621
624
  ): Promise<APIResult> {
622
- let result: APIResult = new APIResult();
623
- // we need a workspace id
624
- if (workspaceID === "") {
625
- result.result = false;
626
- result.status = 500;
627
- result.error = "tenantsGet: no workspace provided";
628
- return result;
629
- }
630
- // create endpoint
631
- let tenantsEndpoint: string = graphConfig.tenantsEndpoint;
632
- // add parameter to endpoint
633
- let url: URL = new URL(tenantsEndpoint);
634
- url.searchParams.append("workspaceId", workspaceID);
635
- // create headers
636
- const headers = await defineHeaders(instance, authorizedUser);
637
- // make endpoint call
638
- let options = { method: "GET", headers: headers };
639
- try {
640
- if (debug) debugger;
641
- console.log(`Attempting GET from /tenants: ${url.href}`);
642
- let response = await fetch(url.href, options);
643
- if (response.status === 200 && response.statusText === "OK") {
644
- let returnedArray: Array<Object> = await response.json();
645
- if(returnedArray != null) {
646
- result.array = returnedArray;
647
- let initialValue: string = "";
648
- console.log(`Successful GET from /tenants: ${result.array.reduce((acc, curr) => acc + curr.domain + " ", initialValue)}`);
625
+ let result: APIResult = new APIResult();
626
+ // we need a workspace id
627
+ if (workspaceID === "") {
628
+ result.result = false;
629
+ result.status = 500;
630
+ result.error = "tenantsGet: no workspace provided";
649
631
  return result;
650
- }
651
- else {
652
- result.error = `Failed GET from /tenants: failed to JSON-parse response`;
653
- console.log(result.error);
632
+ }
633
+ // create endpoint
634
+ let tenantsEndpoint: string = graphConfig.tenantsEndpoint;
635
+ // add parameter to endpoint
636
+ let url: URL = new URL(tenantsEndpoint);
637
+ url.searchParams.append("workspaceId", workspaceID);
638
+ // create headers
639
+ const headers = await defineHeaders(instance, authorizedUser);
640
+ // make endpoint call
641
+ let options = { method: "GET", headers: headers };
642
+ try {
643
+ if (debug) debugger;
644
+ console.log(`Attempting GET from /tenants: ${url.href}`);
645
+ let response = await fetch(url.href, options);
646
+ if (response.status === 200 && response.statusText === "OK") {
647
+ let returnedArray: Array<Object> = await response.json();
648
+ if (returnedArray != null) {
649
+ result.array = returnedArray;
650
+ let initialValue: string = "";
651
+ console.log(`Successful GET from /tenants: ${result.array.reduce((acc, curr) => acc + curr.domain + " ", initialValue)}`);
652
+ return result;
653
+ }
654
+ else {
655
+ result.error = `Failed GET from /tenants: failed to JSON-parse response`;
656
+ console.log(result.error);
657
+ result.status = 500;
658
+ result.result = false;
659
+ return result;
660
+ }
661
+ }
662
+ else {
663
+ console.log(`Failed GET from /tenants: ${url.href}`);
664
+ result.error = await processErrors(response);
665
+ result.status = 500;
666
+ result.result = false;
667
+ console.log(result.error);
668
+ return result;
669
+ }
670
+ }
671
+ catch (error: any) {
672
+ result.error = error.message;
654
673
  result.status = 500;
655
674
  result.result = false;
656
- return result;
657
- }
675
+ console.log(error.message);
658
676
  }
659
- else {
660
- console.log(`Failed GET from /tenants: ${url.href}`);
661
- result.error = await processErrors(response);
662
- result.status = 500;
663
- result.result = false;
664
- console.log(result.error);
665
- return result;
666
- }
667
- }
668
- catch (error: any) {
669
- result.error = error.message;
670
- result.status = 500;
671
- result.result = false;
672
- console.log(error.message);
673
- }
674
- return result;
677
+ return result;
675
678
  }
676
679
  //tenantPost: write validated tenant to back end
677
680
  export async function tenantPost(
678
- instance: IPublicClientApplication,
679
- addingUser: User,
680
- tenant: Tenant,
681
- workspaceId: string
681
+ instance: IPublicClientApplication,
682
+ addingUser: User,
683
+ tenant: Tenant,
684
+ workspaceId: string
682
685
  ): Promise<APIResult> {
683
- let result: APIResult = new APIResult();
684
- // we expect valid tid, name, validated domain
685
- if (tenant.tid === "" || tenant.name === "" || tenant.domain === "") {
686
- result.result = false;
687
- result.error = "tenantPost: invalid tid, name, domain";
688
- result.status = 500;
689
- return result;
690
- }
691
- // create parametrized tenant endpoint
692
- let tenantEndpoint: string = graphConfig.tenantEndpoint;
693
- let url: URL = new URL(tenantEndpoint);
694
- url.searchParams.append("workspaceId", workspaceId);
695
- // create tenant headers
696
- const headers = await defineHeaders(instance, addingUser);
697
- // create tenant body
698
- let tenantBody: string = `
686
+ let result: APIResult = new APIResult();
687
+ // we expect valid tid, name, validated domain
688
+ if (tenant.tid === "" || tenant.name === "" || tenant.domain === "") {
689
+ result.result = false;
690
+ result.error = "tenantPost: invalid tid, name, domain";
691
+ result.status = 500;
692
+ return result;
693
+ }
694
+ // create parametrized tenant endpoint
695
+ let tenantEndpoint: string = graphConfig.tenantEndpoint;
696
+ let url: URL = new URL(tenantEndpoint);
697
+ url.searchParams.append("workspaceId", workspaceId);
698
+ // create tenant headers
699
+ const headers = await defineHeaders(instance, addingUser);
700
+ // create tenant body
701
+ let tenantBody: string = `
699
702
  {"tenantId": "${tenant.tid}",
700
703
  "name": "${tenant.name}",
701
704
  "domain": "${tenant.domain}",
702
705
  "type": 1,
703
706
  "authority": "${tenant.authority}"
704
707
  }`;
705
- let options = { method: "POST", headers: headers, body: tenantBody };
706
- // make tenant endpoint call
707
- try {
708
- console.log(`Attempting POST to ${url.href}: ${tenantBody}`);
709
- let response = await fetch(url.href, options);
710
- if (response.status === 200 && response.statusText === "OK") {
711
- console.log(`Successful POST to ${url.href}: ${tenantBody}`);
712
- return result;
708
+ let options = { method: "POST", headers: headers, body: tenantBody };
709
+ // make tenant endpoint call
710
+ try {
711
+ console.log(`Attempting POST to ${url.href}: ${tenantBody}`);
712
+ let response = await fetch(url.href, options);
713
+ if (response.status === 200 && response.statusText === "OK") {
714
+ console.log(`Successful POST to ${url.href}: ${tenantBody}`);
715
+ return result;
716
+ }
717
+ else {
718
+ console.log(`Failed POST to ${url.href}: ${tenantBody}`);
719
+ result.error = await processErrors(response);
720
+ console.log(result.error);
721
+ result.status = 500;
722
+ result.result = false;
723
+ return result;
724
+ }
713
725
  }
714
- else {
715
- console.log(`Failed POST to ${url.href}: ${tenantBody}`);
716
- result.error = await processErrors(response);
717
- console.log(result.error);
718
- result.status = 500;
719
- result.result = false;
720
- return result;
721
- }
722
- }
723
- catch (error: any) {
724
- result.error = error.message;
725
- result.status = 500;
726
- result.result = false;
727
- console.log(result.error);
728
- }
729
- return result;
726
+ catch (error: any) {
727
+ result.error = error.message;
728
+ result.status = 500;
729
+ result.result = false;
730
+ console.log(result.error);
731
+ }
732
+ return result;
730
733
  }
731
734
  //tenantPut: write access token for onboarded tenant
732
735
  export async function tenantPut(
733
- instance: IPublicClientApplication,
734
- authorizedUser: User,
735
- tenant: Tenant,
736
- debug: boolean
736
+ instance: IPublicClientApplication,
737
+ authorizedUser: User,
738
+ tenant: Tenant,
739
+ debug: boolean
737
740
  ): Promise<APIResult> {
738
- let result: APIResult = new APIResult();
739
- // we expect valid tid
740
- if (tenant.tid === "") {
741
- result.result = false;
742
- result.error = "tenantPut: invalid tid";
743
- result.status = 500;
744
- return result;
745
- }
746
- // create tenant endpoint
747
- let tenantEndpoint: string = graphConfig.tenantEndpoint;
748
- // create tenant headers
749
- const headers = await defineHeaders(instance, authorizedUser);
750
- // be sure we send null and not "null" in body
751
- let readAccessToken: string = tenant.readServicePrincipal
752
- ? `"${tenant.readServicePrincipal}"`
753
- : "null";
754
- let writeAccessToken: string = tenant.writeServicePrincipal
755
- ? `"${tenant.writeServicePrincipal}"`
756
- : "null";
757
- // create tenant body
758
- let tenantBody: string = `
741
+ let result: APIResult = new APIResult();
742
+ // we expect valid tid
743
+ if (tenant.tid === "") {
744
+ result.result = false;
745
+ result.error = "tenantPut: invalid tid";
746
+ result.status = 500;
747
+ return result;
748
+ }
749
+ // create tenant endpoint
750
+ let tenantEndpoint: string = graphConfig.tenantEndpoint;
751
+ // create tenant headers
752
+ const headers = await defineHeaders(instance, authorizedUser);
753
+ // be sure we send null and not "null" in body
754
+ let readAccessToken: string = tenant.readServicePrincipal
755
+ ? `"${tenant.readServicePrincipal}"`
756
+ : "null";
757
+ let writeAccessToken: string = tenant.writeServicePrincipal
758
+ ? `"${tenant.writeServicePrincipal}"`
759
+ : "null";
760
+ // create tenant body
761
+ let tenantBody: string = `
759
762
  {"tenantId": "${tenant.tid}", "readServicePrincipal": ${readAccessToken}, "writeServicePrincipal": ${writeAccessToken}}`;
760
- let options = { method: "PUT", headers: headers, body: tenantBody };
761
- // make tenant endpoint call
762
- try {
763
- if (debug) debugger;
764
- console.log(`Attempting PUT to ${tenantEndpoint}`);
765
- let response = await fetch(tenantEndpoint, options);
766
- if (response.status === 200 && response.statusText === "OK") {
767
- console.log(`Successful PUT to ${tenantEndpoint}`);
768
- return result;
769
- }
770
- else {
771
- console.log(`Failed PUT to ${tenantEndpoint}: ${tenantBody}`);
772
- result.error = await processErrors(response);
773
- console.log(result.error);
774
- result.status = 500;
775
- result.result = false;
776
- return result;
777
- }
778
- }
779
- catch (error: any) {
780
- result.error = error.message;
781
- result.status = 500;
782
- result.result = false;
783
- console.log(result.error);
784
- }
785
- return result;
763
+ let options = { method: "PUT", headers: headers, body: tenantBody };
764
+ // make tenant endpoint call
765
+ try {
766
+ if (debug) debugger;
767
+ console.log(`Attempting PUT to ${tenantEndpoint}`);
768
+ let response = await fetch(tenantEndpoint, options);
769
+ if (response.status === 200 && response.statusText === "OK") {
770
+ console.log(`Successful PUT to ${tenantEndpoint}`);
771
+ return result;
772
+ }
773
+ else {
774
+ console.log(`Failed PUT to ${tenantEndpoint}: ${tenantBody}`);
775
+ result.error = await processErrors(response);
776
+ console.log(result.error);
777
+ result.status = 500;
778
+ result.result = false;
779
+ return result;
780
+ }
781
+ }
782
+ catch (error: any) {
783
+ result.error = error.message;
784
+ result.status = 500;
785
+ result.result = false;
786
+ console.log(result.error);
787
+ }
788
+ return result;
786
789
  }
787
790
  //workspacesGet
788
791
  export async function workspacesGet(
789
- instance: IPublicClientApplication,
790
- authorizedUser: User,
791
- user: User,
792
- debug: boolean
792
+ instance: IPublicClientApplication,
793
+ authorizedUser: User,
794
+ user: User,
795
+ debug: boolean
793
796
  ): Promise<APIResult> {
794
- let result: APIResult = new APIResult();
795
- // we need a valid email address
796
- if (user.mail == undefined || user.mail == "") {
797
- result.result = false;
798
- result.status = 500;
799
- result.error = "adminsGet: no workspace provided";
800
- return result;
801
- }
802
- // create workspaces endpoint
803
- let workspaceEndpoint: string = graphConfig.workspaceEndpoint;
804
- // add email parameter to endpoint
805
- let url: URL = new URL(workspaceEndpoint);
806
- url.searchParams.append("userEmail", user.mail);
807
- // create workspace headers
808
- const headers = await defineHeaders(instance, authorizedUser);
809
- // make workspace endpoint call
810
- let options = { method: "GET", headers: headers };
811
- try {
812
- if (debug) debugger;
813
- console.log("Attempting GET from /workspaces endpoint: " + url.href);
814
- let response = await fetch(url.href, options);
815
- if (response.status === 200 && response.statusText === "OK") {
816
- let returnedArray: Array<Object> = await response.json();
817
- if(returnedArray != null) {
818
- result.array = returnedArray;
819
- let initialValue: string = "";
820
- console.log(`Successful GET from /workspaces: ${result.array.reduce((acc, curr) => acc + curr.name + " ", initialValue)}`);
797
+ let result: APIResult = new APIResult();
798
+ // we need a valid email address
799
+ if (user.mail == undefined || user.mail == "") {
800
+ result.result = false;
801
+ result.status = 500;
802
+ result.error = "adminsGet: no workspace provided";
821
803
  return result;
822
- }
823
- else {
824
- result.error = `Failed GET from /workspaces: failed to JSON-parse response`;
825
- console.log(result.error);
804
+ }
805
+ // create workspaces endpoint
806
+ let workspaceEndpoint: string = graphConfig.workspaceEndpoint;
807
+ // add email parameter to endpoint
808
+ let url: URL = new URL(workspaceEndpoint);
809
+ url.searchParams.append("userEmail", user.mail);
810
+ // create workspace headers
811
+ const headers = await defineHeaders(instance, authorizedUser);
812
+ // make workspace endpoint call
813
+ let options = { method: "GET", headers: headers };
814
+ try {
815
+ if (debug) debugger;
816
+ console.log("Attempting GET from /workspaces endpoint: " + url.href);
817
+ let response = await fetch(url.href, options);
818
+ if (response.status === 200 && response.statusText === "OK") {
819
+ let returnedArray: Array<Object> = await response.json();
820
+ if (returnedArray != null) {
821
+ result.array = returnedArray;
822
+ let initialValue: string = "";
823
+ console.log(`Successful GET from /workspaces: ${result.array.reduce((acc, curr) => acc + curr.name + " ", initialValue)}`);
824
+ return result;
825
+ }
826
+ else {
827
+ result.error = `Failed GET from /workspaces: failed to JSON-parse response`;
828
+ console.log(result.error);
829
+ result.status = 500;
830
+ result.result = false;
831
+ return result;
832
+ }
833
+ }
834
+ else {
835
+ console.log(`Failed GET from /workspaces: ${url.href}`);
836
+ result.error = await processErrors(response);
837
+ result.status = 500;
838
+ result.result = false;
839
+ console.log(result.error);
840
+ return result;
841
+ }
842
+ }
843
+ catch (error: any) {
844
+ result.error = error.message;
826
845
  result.status = 500;
827
846
  result.result = false;
847
+ console.log(error.message);
848
+ }
849
+ return result;
850
+ }
851
+ //readerPost
852
+ export async function readerPost(
853
+ instance: IPublicClientApplication,
854
+ authorizedUser: User,
855
+ config: Config
856
+ ): Promise<APIResult> {
857
+ let result: APIResult = new APIResult();
858
+ if (instance == null || authorizedUser == null)
859
+ {
860
+ result.result = false;
861
+ result.error = "readerPost: invalid parameters";
862
+ result.status = 500;
828
863
  return result;
829
- }
830
864
  }
831
- else {
832
- console.log(`Failed GET from /workspaces: ${url.href}`);
833
- result.error = await processErrors(response);
834
- result.status = 500;
835
- result.result = false;
836
- console.log(result.error);
837
- return result;
838
- }
839
- }
840
- catch (error: any) {
841
- result.error = error.message;
842
- result.status = 500;
843
- result.result = false;
844
- console.log(error.message);
845
- }
846
- return result;
847
- }
865
+ // create reader endpoint with config ID
866
+ let readerEndpoint: string = graphConfig.readerEndpoint + config.id;
867
+ // create headers
868
+ const headers = await defineHeaders(instance, authorizedUser);
869
+ // make reader endpoint call
870
+ let options = { method: "POST", headers: headers };
871
+ try {
872
+ console.log("Attempting POST to /startSync: " + readerEndpoint);
873
+ debugger;
874
+ let response = await fetch(readerEndpoint, options);
875
+ if (response.status === 200 && response.statusText === "OK") {
876
+ console.log(`Successful POST to /startSync: ${readerEndpoint}`);
877
+ return result;
878
+ } else {
879
+ result.error = await processErrors(response);
880
+ console.log(`Failed POST to /startSync: ${readerEndpoint}`);
881
+ console.log(result.error);
882
+ result.status = 500;
883
+ result.result = false;
884
+ return result;
885
+ }
886
+ } catch (error: any) {
887
+ result.error = error.message;
888
+ result.status = 500;
889
+ result.result = false;
890
+ console.log(error.message);
891
+ }
892
+ return result;
893
+ }