@@ -249,7 +249,82 @@ function extractDrugInfoFromXml(xmlObj: any, searchDrugName: string): any | null
249249 }
250250}
251251
252- async function getDrugXmlFromSplZip ( rems_spl_date ?: string ) : Promise < any > {
252+ async function getDrugXmlFromSplZip ( rems_spl_date : string ) : Promise < any > {
253+
254+ const baseFilePath = join ( process . cwd ( ) , 'rems-spl-files' ) ;
255+ const extractPath = join ( baseFilePath , 'extracted' ) ;
256+ const innerExtractPath = join ( baseFilePath , 'inner_extracted' ) ;
257+
258+ try {
259+ console . log ( `Looking for SPL file with date: ${ rems_spl_date } ` ) ;
260+
261+ // Check in the rems_document_spl_files subdirectory
262+ const spl_files_dir = join ( extractPath , 'rems_document_spl_files' ) ;
263+
264+ if ( ! fs . existsSync ( spl_files_dir ) ) {
265+ console . error ( `Directory ${ spl_files_dir } does not exist` ) ;
266+ return null ;
267+ }
268+
269+ const files = fs . readdirSync ( spl_files_dir ) ;
270+
271+ const targetZipFile = files . find ( file => file . startsWith ( rems_spl_date ) ) ;
272+
273+ if ( ! targetZipFile ) {
274+ console . error ( `No SPL file found for rems_spl_date: ${ rems_spl_date } ` ) ;
275+ return null ;
276+ }
277+
278+ const targetZipPath = join ( spl_files_dir , targetZipFile ) ;
279+
280+ // Check if this specific zip has already been extracted
281+ const targetDirName = targetZipFile . replace ( '.zip' , '' ) ;
282+ const specificInnerExtractPath = join ( innerExtractPath , targetDirName ) ;
283+
284+ // Extract the target zip file if needed
285+ console . log ( `Extracting latest inner zip file: ${ targetZipFile } ` ) ;
286+ const targetZip = new AdmZip ( targetZipPath ) ;
287+ targetZip . extractAllTo ( innerExtractPath , true ) ;
288+ console . log ( `Latest inner zip file extracted to ${ specificInnerExtractPath } ` ) ;
289+
290+
291+
292+ if ( ! fs . existsSync ( specificInnerExtractPath ) ) {
293+ console . error ( `Expected directory ${ specificInnerExtractPath } does not exist` ) ;
294+ return null ;
295+ }
296+
297+ // Find the XML file in the nested directory
298+ const innerFiles = fs . readdirSync ( specificInnerExtractPath ) ;
299+ const xmlFile = innerFiles . find ( file => file . endsWith ( '.xml' ) ) ;
300+
301+ if ( ! xmlFile ) {
302+ console . error ( 'No XML file found in the extracted SPL file' ) ;
303+ return null ;
304+ }
305+
306+ // Read and parse the XML file
307+ const xmlPath = join ( specificInnerExtractPath , xmlFile ) ;
308+ const xmlContent = fs . readFileSync ( xmlPath , 'utf-8' ) ;
309+
310+ // Parse XML content using xml2js
311+ return new Promise ( ( resolve , reject ) => {
312+ parseString ( xmlContent , ( err : any , result : any ) => {
313+ if ( err ) {
314+ console . error ( 'Error parsing XML:' , err ) ;
315+ reject ( err ) ;
316+ } else {
317+ resolve ( result ) ;
318+ }
319+ } ) ;
320+ } ) ;
321+ } catch ( error ) {
322+ console . error ( 'Error extracting and parsing SPL zip files:' , error ) ;
323+ return null ;
324+ }
325+ }
326+
327+ async function downloadSplZip ( ) : Promise < any > {
253328 // Create base directories to store and process the zip files
254329 const baseFilePath = join ( process . cwd ( ) , 'rems-spl-files' ) ;
255330 const extractPath = join ( baseFilePath , 'extracted' ) ;
@@ -289,90 +364,20 @@ async function getDrugXmlFromSplZip(rems_spl_date?: string): Promise<any> {
289364 mainZip . extractAllTo ( extractPath , true ) ;
290365 console . log ( 'latest SPL zip extracted successfully' ) ;
291366
292-
293- // If a specific rems_spl_date is provided, find and process that zip file
294- if ( rems_spl_date ) {
295- console . log ( `Looking for SPL file with date: ${ rems_spl_date } ` ) ;
296-
297- // Check in the rems_document_spl_files subdirectory
298- const spl_files_dir = join ( extractPath , 'rems_document_spl_files' ) ;
299-
300- if ( ! fs . existsSync ( spl_files_dir ) ) {
301- console . error ( `Directory ${ spl_files_dir } does not exist` ) ;
302- return null ;
303- }
304-
305- const files = fs . readdirSync ( spl_files_dir ) ;
306-
307- const targetZipFile = files . find ( file => file . startsWith ( rems_spl_date ) ) ;
308-
309- if ( ! targetZipFile ) {
310- console . error ( `No SPL file found for rems_spl_date: ${ rems_spl_date } ` ) ;
311- return null ;
312- }
313-
314- const targetZipPath = join ( spl_files_dir , targetZipFile ) ;
315-
316- // Check if this specific zip has already been extracted
317- const targetDirName = targetZipFile . replace ( '.zip' , '' ) ;
318- const specificInnerExtractPath = join ( innerExtractPath , targetDirName ) ;
319-
320- const zipAlreadyExtracted = fs . existsSync ( specificInnerExtractPath )
321-
322- // Extract the target zip file if needed
323- console . log ( `Extracting latest inner zip file: ${ targetZipFile } ` ) ;
324- const targetZip = new AdmZip ( targetZipPath ) ;
325- targetZip . extractAllTo ( innerExtractPath , true ) ;
326- console . log ( `Latest inner zip file extracted to ${ specificInnerExtractPath } ` ) ;
327-
328-
329-
330- if ( ! fs . existsSync ( specificInnerExtractPath ) ) {
331- console . error ( `Expected directory ${ specificInnerExtractPath } does not exist` ) ;
332- return null ;
333- }
334-
335- // Find the XML file in the nested directory
336- const innerFiles = fs . readdirSync ( specificInnerExtractPath ) ;
337- const xmlFile = innerFiles . find ( file => file . endsWith ( '.xml' ) ) ;
338-
339- if ( ! xmlFile ) {
340- console . error ( 'No XML file found in the extracted SPL file' ) ;
341- return null ;
342- }
343-
344- // Read and parse the XML file
345- const xmlPath = join ( specificInnerExtractPath , xmlFile ) ;
346- const xmlContent = fs . readFileSync ( xmlPath , 'utf-8' ) ;
347-
348- // Parse XML content using xml2js
349- return new Promise ( ( resolve , reject ) => {
350- parseString ( xmlContent , ( err : any , result : any ) => {
351- if ( err ) {
352- console . error ( 'Error parsing XML:' , err ) ;
353- reject ( err ) ;
354- } else {
355- resolve ( result ) ;
356- }
357- } ) ;
358- } ) ;
359- } else {
360- // If no specific date is provided, return a list of available dates
361- const spl_files_dir = join ( extractPath , 'rems_document_spl_files' ) ;
362- if ( ! fs . existsSync ( spl_files_dir ) ) {
363- console . error ( `Directory ${ spl_files_dir } does not exist` ) ;
364- return [ ] ;
365- }
366-
367- const files = fs . readdirSync ( spl_files_dir ) ;
368- return files . map ( file => {
369- // Extract date information from filenames
370- const datePart = file . split ( '_' ) [ 0 ] ; // Assuming format is DATE_otherinfo.zip
371- return { date : datePart , filename : file } ;
372- } ) ;
367+ const spl_files_dir = join ( extractPath , 'rems_document_spl_files' ) ;
368+ if ( ! fs . existsSync ( spl_files_dir ) ) {
369+ console . error ( `Directory ${ spl_files_dir } does not exist` ) ;
370+ return [ ] ;
373371 }
372+
373+ const files = fs . readdirSync ( spl_files_dir ) ;
374+ return files . map ( file => {
375+ // Extract date information from filenames
376+ const datePart = file . split ( '_' ) [ 0 ] ; // Assuming format is DATE_otherinfo.zip
377+ return { date : datePart , filename : file } ;
378+ } ) ;
374379 } catch ( error ) {
375- console . error ( 'Error processing SPL zip files:' , error ) ;
380+ console . error ( 'Error downloading SPL zip files:' , error ) ;
376381 return null ;
377382 }
378383}
@@ -414,6 +419,8 @@ async function getRemsFromDirectoryApi(ndc_code: string): Promise<MedicationApiR
414419
415420export async function loadPhonebook ( ) {
416421 const model = Connection ;
422+
423+ let downloadedSplZip = false ;
417424
418425 for ( const entry of phonebook ) {
419426 try {
@@ -451,6 +458,11 @@ export async function loadPhonebook() {
451458 // Case 2: SPL lookup
452459 else if ( entry . directoryLookupType === 'spl' && entry . rems_spl_date ) {
453460 console . log ( `Using SPL lookup for ${ entry . brand_name } (${ entry . code } )` ) ;
461+
462+ if ( ! downloadedSplZip ) {
463+ await downloadSplZip ( ) ;
464+ downloadedSplZip = true ;
465+ }
454466
455467 // Get the XML data from the SPL zip
456468 const xmlData = await getDrugXmlFromSplZip ( entry . rems_spl_date ) ;
0 commit comments