@@ -4,11 +4,14 @@ import {
4
4
type ScoreOutputSchemaType ,
5
5
type ScoreType ,
6
6
type CalculateFn ,
7
+ type CategoryType ,
8
+ type TerminologyType ,
7
9
} from '../types'
8
10
import {
9
11
parseReadmeToHtml ,
10
12
getUnionType ,
11
13
createZodObjectFromSchema ,
14
+ parseToAwellApiSchema ,
12
15
} from '../lib'
13
16
import {
14
17
tryCastToBoolean ,
@@ -19,6 +22,7 @@ import {
19
22
} from '../lib/castFunctions'
20
23
import _ from 'lodash'
21
24
import { simulateDateInput , simulateStringInput } from '../lib/simulation'
25
+ import { parseToApiResultFormat } from '../lib/parseToApiResultFormat'
22
26
23
27
/**
24
28
* Class representing a Score, which calculates results based on input and output schemas.
@@ -30,6 +34,11 @@ export class Score<
30
34
OutputSchema extends ScoreOutputSchemaType ,
31
35
> implements Omit < ScoreType < InputSchema , OutputSchema > , 'calculate' >
32
36
{
37
+ /**
38
+ * The ID of the score.
39
+ */
40
+ id : string
41
+
33
42
/**
34
43
* The name of the score.
35
44
*/
@@ -55,6 +64,11 @@ export class Score<
55
64
*/
56
65
outputSchema : OutputSchema
57
66
67
+ /**
68
+ * The FHIR terminology of the score.
69
+ */
70
+ terminology : TerminologyType | undefined
71
+
58
72
/**
59
73
* The function to calculate the score.
60
74
*/
@@ -76,15 +90,18 @@ export class Score<
76
90
*/
77
91
public constructor (
78
92
score : ScoreType < InputSchema , OutputSchema > & {
93
+ id ?: string
79
94
inputSchema : InputSchema
80
95
outputSchema : OutputSchema
81
96
} ,
82
97
) {
98
+ this . id = score . id ?? score . name
83
99
this . name = score . name
84
100
this . readme_location = score . readme_location
85
101
this . description = parseReadmeToHtml ( this . readme_location )
86
102
this . inputSchema = score . inputSchema
87
103
this . outputSchema = score . outputSchema
104
+ this . terminology = score . terminology
88
105
this . _calculate = score . calculate
89
106
this . inputSchemaAsObject = createZodObjectFromSchema ( this . inputSchema )
90
107
}
@@ -103,35 +120,28 @@ export class Score<
103
120
* @default false
104
121
*/
105
122
strictMode ?: boolean
106
- /**
107
- * Specifies the format of the output:
108
- * - `simple`: Key-value where the key is the result ID and the value is the result.
109
- * - `awell`: Enriched array format.
110
- * @default 'simple'
111
- */
112
- outputFormat ?: 'simple' | 'awell'
113
123
}
114
124
} ) : Record <
115
125
keyof OutputSchema ,
116
126
z . infer < OutputSchema [ keyof OutputSchema ] [ 'type' ] > | null
117
127
> {
118
- const parsedData = this . inputSchemaAsObject . parse (
128
+ const d = this . inputSchemaAsObject . parse (
119
129
params ?. opts ?. strictMode === true
120
130
? params . payload
121
- : this . castInputsToExactTypes ( params . payload ) ,
131
+ : this . tryCastInputsToExactTypes ( params . payload ) ,
122
132
)
123
133
124
134
return this . _calculate ( {
125
- data : parsedData ,
135
+ data : d ,
126
136
} )
127
137
}
128
138
129
139
/**
130
- * Casts input values to their exact types based on the input schema.
140
+ * Tries to cast input values to their exact types based on the input schema.
131
141
* @param input - The input data to cast.
132
142
* @returns The cast input data.
133
143
*/
134
- castInputsToExactTypes (
144
+ tryCastInputsToExactTypes (
135
145
data : Record < string , unknown > ,
136
146
) : Record < string , unknown > {
137
147
/**
@@ -204,7 +214,10 @@ export class Score<
204
214
*/
205
215
simulate ( ) : {
206
216
simulatedInput : Record < string , unknown >
207
- simulatedOutput : Record < string , unknown >
217
+ results : Record <
218
+ keyof OutputSchema ,
219
+ z . infer < OutputSchema [ keyof OutputSchema ] [ 'type' ] > | null
220
+ >
208
221
} {
209
222
const simulatedInput = _ . mapValues (
210
223
this . inputSchemaAsObject . shape ,
@@ -266,7 +279,52 @@ export class Score<
266
279
267
280
return {
268
281
simulatedInput : _ . pickBy ( simulatedInput , value => value !== undefined ) ,
269
- simulatedOutput : this . calculate ( { payload : simulatedInput } ) ,
282
+ results : this . calculate ( { payload : simulatedInput } ) ,
270
283
}
271
284
}
285
+
286
+ /**
287
+ * Formats the results of the score calculation.
288
+ * @param results - The results to format.
289
+ * @param opts - The options for formatting the results.
290
+ * @returns The formatted results.
291
+ */
292
+ formatResults (
293
+ results : Record <
294
+ keyof OutputSchema ,
295
+ z . infer < OutputSchema [ keyof OutputSchema ] [ 'type' ] > | null
296
+ > ,
297
+ {
298
+ opts,
299
+ } : {
300
+ /**
301
+ * Specifies the format of the output:
302
+ * - `simple`: Key-value where the key is the result ID and the value is the result.
303
+ * - `awell`: Enriched array format.
304
+ * @default 'simple'
305
+ */
306
+ opts ?: { format : 'simple' | 'awell' }
307
+ } ,
308
+ ) {
309
+ if ( opts ?. format === 'simple' ) {
310
+ return results
311
+ }
312
+
313
+ return parseToApiResultFormat < OutputSchema > ( results , this . outputSchema )
314
+ }
315
+
316
+ /**
317
+ * Returns the API schema for the score.
318
+ * @returns The API schema for the score.
319
+ */
320
+ get apiSchema ( ) {
321
+ return parseToAwellApiSchema ( {
322
+ scoreId : this . id ,
323
+ scoreName : this . name ,
324
+ scoreDescription : this . description ,
325
+ inputSchema : this . inputSchema ,
326
+ outputSchema : this . outputSchema ,
327
+ terminology : this . terminology ,
328
+ } )
329
+ }
272
330
}
0 commit comments