Skip to content

Commit c098659

Browse files
angelcaamaliennae
andauthored
fix(healthcare): set responseType to JSON instead of Buffer in importFhirResources.js (#4252)
* fix(healthcare): set responseType to JSON instead of Buffer * fix(healthcare): use optional chaining for safe metadata access in FHIR import --------- Co-authored-by: Jennifer Davis <sigje@google.com>
1 parent 5987eab commit c098659

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

healthcare/fhir/importFhirResources.js

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const main = (
2828
auth: new google.auth.GoogleAuth({
2929
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
3030
}),
31+
responseType: 'json',
3132
});
3233
const sleep = ms => {
3334
return new Promise(resolve => setTimeout(resolve, ms));
@@ -50,32 +51,48 @@ const main = (
5051
},
5152
},
5253
};
54+
try {
55+
const operation =
56+
await healthcare.projects.locations.datasets.fhirStores.import(request);
57+
const operationName = operation.data.name;
5358

54-
const operation =
55-
await healthcare.projects.locations.datasets.fhirStores.import(request);
56-
const operationName = operation.data.name;
59+
console.log(`Import operation started: ${operationName}`);
5760

58-
const operationRequest = {name: operationName};
61+
let done = false;
62+
let operationStatus;
63+
let attempts = 0;
5964

60-
// Wait twenty seconds for the LRO to finish.
61-
await sleep(20000);
65+
while (!done && attempts < 100) {
66+
console.log('Waiting for import operation to complete...');
67+
attempts++;
68+
await sleep(5000); // Wait 5 seconds between polls
6269

63-
// Check the LRO's status
64-
const operationStatus =
65-
await healthcare.projects.locations.datasets.operations.get(
66-
operationRequest
67-
);
70+
operationStatus =
71+
await healthcare.projects.locations.datasets.operations.get({
72+
name: operationName,
73+
});
6874

69-
const success = operationStatus.data.metadata.counter.success;
75+
done = operationStatus.data.done;
76+
}
7077

71-
if (typeof success !== 'undefined') {
72-
console.log(
73-
`Import FHIR resources succeeded. ${success} resources imported.`
74-
);
75-
} else {
76-
console.log(
77-
'Imported FHIR resources failed. Details available in Cloud Logging at the following URL:\n',
78-
operationStatus.data.metadata.logsUrl
78+
if (operationStatus.data.error) {
79+
console.error(
80+
'Import FHIR resources failed:',
81+
operationStatus.data.error
82+
);
83+
} else if (done) {
84+
const successCount =
85+
operationStatus.data.metadata?.counter?.success || 0;
86+
console.log(
87+
`Import FHIR resources succeeded. ${successCount} resources imported.`
88+
);
89+
} else {
90+
console.error('Import operation timed out in the sample.');
91+
}
92+
} catch (error) {
93+
console.error(
94+
'An error occurred during the import process:',
95+
error.message || error
7996
);
8097
}
8198
};

0 commit comments

Comments
 (0)