/
Specimen.ts
428 lines (388 loc) · 13.9 KB
/
Specimen.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* Copyright (c) 2017-2018 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { buildURL } from './ActionURL';
import { request } from './Ajax';
import {
displayAjaxErrorResponse,
getCallbackWrapper,
getOnFailure,
getOnSuccess,
isFunction,
RequestCallbackOptions,
RequestFailure,
RequestSuccess,
} from './Utils';
export interface AddSpecimensToRequestOptions extends RequestCallbackOptions {
containerPath?: string;
preferredLocation: number;
requestId: number;
specimenHashArray: any[];
}
/**
* Adds multiple vials to a request based on an array of hash codes uniquely identifying the primary specimens.
* The vials will be selected based on availability and current location. If called by a non-administrator,
* the target request must be owned by the calling user, and the request must be in an open (not yet submitted)
* state. Administrators may add vials to any request at any time.
* @param options
*/
export function addSpecimensToRequest(options: AddSpecimensToRequestOptions): XMLHttpRequest {
if (remapArguments(options, arguments)) {
options = {
success: arguments[0],
requestId: arguments[1],
specimenHashArray: arguments[2],
preferredLocation: arguments[3],
failure: arguments[4],
containerPath: arguments[5],
};
}
return request({
url: buildURL('specimen-api', 'addSpecimensToRequest.api', options.containerPath),
method: 'POST',
jsonData: {
preferredLocation: options.preferredLocation,
requestId: options.requestId,
specimenHashes: options.specimenHashArray,
},
success: getCallbackWrapper(onSpecimenSuccess(options), options.scope),
failure: getCallbackWrapper(rebindFailure(getOnFailure(options)), options.scope, true),
});
}
/**
* Don't use this... use {@link addSpecimensToRequest} instead.
* @deprecated
*/
export const addSamplesToRequest = addSpecimensToRequest;
export interface AddVialsToRequestOptions extends RequestCallbackOptions {
containerPath?: string;
idType?: string;
requestId: number;
vialIdArray: any[];
}
/**
* Adds multiple vials to a request based on an array of unique unique vial IDs. If called by a non-administrator,
* the target request must be owned by the calling user, and the request must be in an open (not yet submitted)
* state. Administrators may add vials to any request at any time.
* @param options
*/
export function addVialsToRequest(options: AddVialsToRequestOptions): XMLHttpRequest {
if (remapArguments(options, arguments)) {
options = {
success: arguments[0],
requestId: arguments[1],
vialIdArray: arguments[2],
idType: arguments[3],
failure: arguments[4],
containerPath: arguments[5],
};
}
if (!options.idType) {
options.idType = 'GlobalUniqueId';
}
return request({
url: buildURL('specimen-api', 'addVialsToRequest.api', options.containerPath),
method: 'POST',
jsonData: {
idType: options.idType,
requestId: options.requestId,
vialIds: options.vialIdArray,
},
success: getCallbackWrapper(onSpecimenSuccess(options), options.scope),
failure: getCallbackWrapper(rebindFailure(getOnFailure(options)), options.scope, true),
});
}
export interface CancelRequestOptions extends RequestCallbackOptions {
containerPath?: string;
requestId: number;
}
/**
* Completely and permanently cancels a request. THIS ACTION CANNOT BE UNDONE.
* If called by a non-administrator, the target request must be owned by the
* calling user, and the request must be in an open (not yet submitted) state. Administrators may delete
* requests at any time.
* @param options
*/
export function cancelRequest(options: CancelRequestOptions): XMLHttpRequest {
if (remapArguments(options, arguments)) {
options = {
success: arguments[0],
requestId: arguments[1],
failure: arguments[2],
containerPath: arguments[3],
};
}
return request({
url: buildURL('specimen-api', 'cancelRequest.api', options.containerPath),
method: 'POST',
jsonData: {
requestId: options.requestId,
},
success: getCallbackWrapper(onSpecimenSuccess(options), options.scope),
failure: getCallbackWrapper(rebindFailure(getOnFailure(options)), options.scope, true),
});
}
export interface GetOpenRequestsOptions extends RequestCallbackOptions {
allUsers?: boolean;
containerPath?: string;
}
/**
* Retrieves an array of open (non-final) specimen requests, including all requests that are in "shopping cart"
* status as well as those that have been submitted for processing but are not yet complete.
* @param options
*/
export function getOpenRequests(options: GetOpenRequestsOptions): XMLHttpRequest {
if (remapArguments(options, arguments)) {
options = {
success: arguments[0],
allUsers: arguments[1],
failure: arguments[2],
containerPath: arguments[3],
};
}
return request({
url: buildURL('specimen-api', 'getOpenRequests.api', options.containerPath),
method: 'POST',
jsonData: {
allUsers: options.allUsers,
},
success: getCallbackWrapper(onSpecimenSuccess(options), options.scope, false, data => data.requests),
failure: getCallbackWrapper(rebindFailure(getOnFailure(options)), options.scope, true),
});
}
export interface GetProvidingLocationsOptions extends RequestCallbackOptions {
containerPath?: string;
specimenHashArray: string[];
}
/**
* Retrieves an array of locations that could provide vials from all identified primary specimens.
* @param options
*/
export function getProvidingLocations(options: GetProvidingLocationsOptions): XMLHttpRequest {
if (remapArguments(options, arguments)) {
options = {
success: arguments[0],
specimenHashArray: arguments[1],
failure: arguments[2],
containerPath: arguments[3],
};
}
return request({
url: buildURL('specimen-api', 'getProvidingLocations.api', options.containerPath),
method: 'POST',
jsonData: {
specimenHashes: options.specimenHashArray,
},
success: getCallbackWrapper(onSpecimenSuccess(options), options.scope, false, data => data.locations),
failure: getCallbackWrapper(rebindFailure(getOnFailure(options)), options.scope, true),
});
}
export interface GetRepositoriesOptions extends RequestCallbackOptions {
containerPath?: string;
}
/**
* Retrieves an array of locations that are identified as specimen repositories.
* @param options
*/
export function getRepositories(options: GetRepositoriesOptions): XMLHttpRequest {
if (remapArguments(options, arguments)) {
options = {
success: arguments[0],
failure: arguments[1],
containerPath: arguments[2],
};
}
return request({
url: buildURL('specimen-api', 'getRepositories.api', options.containerPath),
method: 'POST',
// No jsonData, still json request
headers: {
'Content-Type': 'application/json',
},
success: getCallbackWrapper(onSpecimenSuccess(options), options.scope, false, data => data.repositories),
failure: getCallbackWrapper(rebindFailure(getOnFailure(options)), options.scope, true),
});
}
export interface GetRequestOptions extends RequestCallbackOptions {
containerPath?: string;
requestId: number;
}
/**
* Retrieves a specimen request for a given specimen request ID.
* @param options
*/
export function getRequest(options: GetRequestOptions): XMLHttpRequest {
if (remapArguments(options, arguments)) {
options = {
success: arguments[0],
requestId: arguments[1],
failure: arguments[2],
containerPath: arguments[3],
};
}
return request({
url: buildURL('specimen-api', 'getRequest.api', options.containerPath),
method: 'POST',
jsonData: {
requestId: options.requestId,
},
success: getCallbackWrapper(onSpecimenSuccess(options), options.scope, false, data => data.request),
failure: getCallbackWrapper(rebindFailure(getOnFailure(options)), options.scope, true),
});
}
export interface GetSpecimenWebPartGroupsOptions extends RequestCallbackOptions {
containerPath?: string;
}
/**
* Retrieves a specimen request for a given specimen request ID.
* @param options
*/
export function getSpecimenWebPartGroups(options: GetSpecimenWebPartGroupsOptions): XMLHttpRequest {
return request({
url: buildURL('specimen-api', 'getSpecimenWebPartGroups.api', options.containerPath),
method: 'POST',
// No jsonData, still json request
headers: {
'Content-Type': 'application/json',
},
success: getCallbackWrapper(onSpecimenSuccess(options), options.scope),
failure: getCallbackWrapper(rebindFailure(getOnFailure(options)), options.scope, true),
});
}
export interface GetVialsByRowIdOptions extends RequestCallbackOptions {
containerPath?: string;
vialRowIdArray: any[];
}
/**
* Retrieves an array of vials that correspond to an array of unique vial row ids.
* @param options
*/
export function getVialsByRowId(options: GetVialsByRowIdOptions): XMLHttpRequest {
if (remapArguments(options, arguments)) {
options = {
success: arguments[0],
vialRowIdArray: arguments[1],
failure: arguments[2],
containerPath: arguments[3],
};
}
return request({
url: buildURL('specimen-api', 'getVialsByRowId.api', options.containerPath),
method: 'POST',
jsonData: {
rowIds: options.vialRowIdArray,
},
success: getCallbackWrapper(onSpecimenSuccess(options), options.scope, false, data => data.vials),
failure: getCallbackWrapper(rebindFailure(getOnFailure(options)), options.scope, true),
});
}
export interface GetVialTypeSummaryOptions extends RequestCallbackOptions {
containerPath?: string;
}
/**
* Retrieves a specimen request for a given specimen request ID.
* @param options
*/
export function getVialTypeSummary(options: GetVialTypeSummaryOptions): XMLHttpRequest {
return request({
url: buildURL('specimen-api', 'getVialTypeSummary.api', options.containerPath),
method: 'POST',
// No jsonData, still json request
headers: {
'Content-Type': 'application/json',
},
success: getCallbackWrapper(onSpecimenSuccess(options), options.scope),
failure: getCallbackWrapper(rebindFailure(getOnFailure(options)), options.scope, true),
});
}
export interface RemoveVialsFromRequestOptions extends RequestCallbackOptions {
containerPath?: string;
idType?: string;
requestId: number;
vialIdArray: any[];
}
/**
* Removes multiple vials from a request based on an array of vial row IDs. If called by a non-administrator,
* the target request must be owned by the calling user, and the request must be in an open (not yet submitted)
* state. Administrators may remove vials from any request at any time.
* @param options
*/
export function removeVialsFromRequest(options: RemoveVialsFromRequestOptions): XMLHttpRequest {
if (remapArguments(options, arguments)) {
options = {
success: arguments[0],
requestId: arguments[1],
vialIdArray: arguments[2],
idType: arguments[3],
failure: arguments[4],
containerPath: arguments[5],
};
}
if (!options.idType) {
options.idType = 'GlobalUniqueId';
}
return request({
url: buildURL('specimen-api', 'removeVialsFromRequest', options.containerPath),
method: 'POST',
jsonData: {
idType: options.idType,
requestId: options.requestId,
vialIds: options.vialIdArray,
},
success: getCallbackWrapper(onSpecimenSuccess(options), options.scope),
failure: getCallbackWrapper(rebindFailure(getOnFailure(options)), options.scope, true),
});
}
/**
* @hidden
* @private
*/
function onSpecimenSuccess(options: RequestCallbackOptions): RequestSuccess {
const success = getOnSuccess(options);
const { scope } = options;
if (success) {
if (scope) {
// Explicit scope requested. Let getCallbackWrapper() handle scoped callback.
return success;
} else {
// backward compatibility: maintain caller's scope.
// Cannot use "return success" as this would alter scope.
return function (data, request, requestOptions): void {
success(data, request, requestOptions);
};
}
}
// success not specified
return undefined;
}
/**
* @hidden
* @private
*/
function remapArguments(options: any, args: IArguments): boolean {
return options && (isFunction(options) || args.length > 1);
}
/**
* @hidden
* @private
*/
const REBIND: RequestFailure = (err, response): any => displayAjaxErrorResponse(response, err);
/**
* @hidden
* @private
*/
function rebindFailure(failure?: RequestFailure): RequestFailure {
return failure ?? REBIND;
}