@@ -199,70 +199,26 @@ export async function switchPublicity(projectId: string): Promise<any> {
199199 }
200200}
201201
202- export const extractAndReplaceKeys = ( pattern : string , replacement : string , dictionary : Record < string , any > ) => {
203- return Object . keys ( dictionary )
204- . filter ( ( key ) => key . startsWith ( pattern ) )
205- . reduce < Record < string , number > > ( ( acc , key ) => {
206- acc [ key . replace ( pattern , replacement ) ] = dictionary [ key ] ;
207- return acc ;
208- } , { } ) ;
209- } ;
210-
211- const processData = ( response : any ) : ExperimentResult [ ] => {
212- const experimentResults : ExperimentResult [ ] = response . flatMap ( ( item : any ) => {
213- const experimentResult = item . data . labData . concentrations . entries . map ( ( entry : any ) => {
214- const species = entry . species ;
215-
216- const inputConcentrations = extractAndReplaceKeys ( "In_" , "" , species ) ;
217- const inputConcentrationsCapitalized = Object . fromEntries (
218- Object . entries ( inputConcentrations ) . map ( ( [ key , value ] ) => [ key . toUpperCase ( ) , value ] )
219- ) ;
220- const outputConcentrations = extractAndReplaceKeys ( "Out_" , "" , species ) ;
221- const outputConcentrationsCapitalized = Object . fromEntries (
222- Object . entries ( outputConcentrations ) . map ( ( [ key , value ] ) => [ key . toUpperCase ( ) , value ] )
223- ) ;
224- const experimentResult : ExperimentResult = {
225- name : item . data . general . name + "-" + entry . step ,
226- initialConcentrations : inputConcentrationsCapitalized ,
227- finalConcentrations : outputConcentrationsCapitalized ,
228- pressure : entry . pressure ?? null ,
229- temperature : entry . temperature ?? null ,
230- time : entry . time ?? null ,
231- } ;
232- return experimentResult ;
233- } ) ;
234-
235- return experimentResult ;
236- } ) ;
237-
238- return experimentResults ;
239- } ;
240- export async function getLabResults ( ) : Promise < ExperimentResult [ ] > {
241- const token = await getUserToken ( config . OASIS_SCOPE ) ;
242- const response = await apiRequest (
243- "GET" ,
244- "/oasis/CO2LabResults" ,
245- {
246- headers : {
247- Authorization : `Bearer ${ token } ` ,
248- } ,
249- } ,
250- true
202+ export const getKeyValuesFromPrefix = ( pattern : string , dict : Record < string , any > ) =>
203+ Object . fromEntries (
204+ Object . keys ( dict )
205+ . filter ( ( key ) => key . startsWith ( pattern ) )
206+ . map ( ( key ) => [ key . slice ( pattern . length ) . toUpperCase ( ) , dict [ key ] ] )
251207 ) ;
252208
253- if ( ! response . ok ) {
254- if ( response . status === 401 ) {
255- throw new Error ( "Unauthorized: Apply for access to CO2 lab results in AccessIT" ) ;
256- } else if ( response . status === 403 ) {
257- throw new Error (
258- "You do not have permission to access this resource. Apply for access to CO2 lab results in AccessIT"
259- ) ;
260- } else {
261- throw new Error ( "Network response was not ok" ) ;
262- }
263- }
264- const data = await response . json ( ) ;
209+ const formatLabData = ( response : any ) : ExperimentResult [ ] =>
210+ response . flatMap ( ( item : any ) =>
211+ item . data . labData . concentrations . entries . map ( ( entry : any ) => ( {
212+ name : `${ item . data . general . name } -${ entry . step } ` ,
213+ initialConcentrations : getKeyValuesFromPrefix ( "In_" , entry . species ) ,
214+ finalConcentrations : getKeyValuesFromPrefix ( "Out_" , entry . species ) ,
215+ pressure : entry . pressure ?? null ,
216+ temperature : entry . temperature ?? null ,
217+ time : entry . time ?? null ,
218+ } ) )
219+ ) ;
265220
266- const transformedData = processData ( data ) ;
267- return transformedData ;
221+ export async function getLabResults ( ) : Promise < ExperimentResult [ ] > {
222+ const data = await apiRequest < any [ ] > ( "GET" , "/oasis" ) ;
223+ return formatLabData ( data ) ;
268224}
0 commit comments