diff --git a/healthcare/fhir/importFhirResources.js b/healthcare/fhir/importFhirResources.js index 4fae19bf55..758d7b2856 100644 --- a/healthcare/fhir/importFhirResources.js +++ b/healthcare/fhir/importFhirResources.js @@ -28,6 +28,7 @@ const main = ( auth: new google.auth.GoogleAuth({ scopes: ['https://www.googleapis.com/auth/cloud-platform'], }), + responseType: 'json', }); const sleep = ms => { return new Promise(resolve => setTimeout(resolve, ms)); @@ -50,33 +51,42 @@ const main = ( }, }, }; + try { + const operation = + await healthcare.projects.locations.datasets.fhirStores.import(request); + const operationName = operation.data.name; - const operation = - await healthcare.projects.locations.datasets.fhirStores.import(request); - const operationName = operation.data.name; + console.log(`Import operation started: ${operationName}`); - const operationRequest = {name: operationName}; + // --- POLLING STRATEGY --- + let done = false; + let operationStatus; - // Wait twenty seconds for the LRO to finish. - await sleep(20000); + while (!done) { + console.log('Waiting for import operation to complete...'); + await sleep(5000); // Wait 5 seconds between polls - // Check the LRO's status - const operationStatus = - await healthcare.projects.locations.datasets.operations.get( - operationRequest - ); + operationStatus = + await healthcare.projects.locations.datasets.operations.get({ + name: operationName, + }); - const success = operationStatus.data.metadata.counter.success; + done = operationStatus.data.done; + } - if (typeof success !== 'undefined') { - console.log( - `Import FHIR resources succeeded. ${success} resources imported.` - ); - } else { - console.log( - 'Imported FHIR resources failed. Details available in Cloud Logging at the following URL:\n', - operationStatus.data.metadata.logsUrl - ); + if (operationStatus.data.error) { + console.error( + 'Import FHIR resources failed:', + operationStatus.data.error + ); + } else { + const successCount = operationStatus.data.metadata.counter.success || 0; + console.log( + `Import FHIR resources succeeded. ${successCount} resources imported.` + ); + } + } catch (error) { + console.error('Error initiating import:', error.message || error); } };