@@ -17,58 +17,60 @@ export class App {
1717 const { sftpClient, baseDownloadDir } =
1818 await this . sftpSupplierClientRepository . getClient ( supplier ) ;
1919
20- this . logger . info ( 'Polling SFTP' ) ;
20+ const baseSftpPath = `${ baseDownloadDir } /${ this . sftpEnvironment } /proofs` ;
21+ const baseS3Path = `proofs/${ supplier } ` ;
22+
23+ const basePathLogger = this . logger . child ( { baseSftpPath, baseS3Path } ) ;
24+
25+ basePathLogger . info ( 'Polling SFTP' ) ;
2126 await sftpClient . connect ( ) ;
2227
23- this . logger . info (
28+ basePathLogger . info (
2429 `Copying files from folder ${ baseDownloadDir } /${ this . sftpEnvironment } /proofs to proofs`
2530 ) ;
2631
2732 try {
2833 await this . pollBaseFolder (
2934 sftpClient ,
30- `${ baseDownloadDir } /${ this . sftpEnvironment } /proofs` ,
31- `proofs/${ supplier } `
35+ baseSftpPath ,
36+ baseS3Path ,
37+ basePathLogger
3238 ) ;
3339 } finally {
3440 await sftpClient . end ( ) ;
3541 }
3642
37- this . logger . info ( 'Finished polling SFTP' ) ;
43+ basePathLogger . info ( 'Finished polling SFTP' ) ;
3844 }
3945
4046 private async pollBaseFolder (
4147 sftpClient : SftpClient ,
4248 baseSftpPath : string ,
43- baseS3Path : string
49+ baseS3Path : string ,
50+ logger : Logger
4451 ) {
45- this . logger . info ( {
46- description : 'Copying folder' ,
47- baseSftpPath,
48- baseS3Path,
49- } ) ;
52+ logger . info ( 'Copying folder' ) ;
5053 const isDir = await this . isDirectory ( sftpClient , baseSftpPath ) ;
5154
5255 if ( ! isDir ) {
53- this . logger . info ( `Path ' ${ baseSftpPath } ' does not exist` ) ;
56+ logger . info ( `Base SFTP path does not exist` ) ;
5457 return ;
5558 }
5659
57- const templateIdFolders = await sftpClient . list ( baseSftpPath ) ;
60+ const idFolders = await sftpClient . list ( baseSftpPath ) ;
5861
59- for ( const templateIdFolder of templateIdFolders ) {
60- if ( templateIdFolder . type === 'd' ) {
61- await this . pollTemplateIdFolder (
62+ for ( const idFolder of idFolders ) {
63+ if ( idFolder . type === 'd' ) {
64+ await this . pollIdFolder (
6265 sftpClient ,
6366 baseSftpPath ,
6467 baseS3Path ,
65- templateIdFolder . name
68+ idFolder . name ,
69+ logger
6670 ) ;
6771 } else {
68- this . logger . info ( {
69- description : 'Unexpected non-directory item found' ,
70- baseSftpPath,
71- templateIdFolder,
72+ logger . info ( 'Unexpected non-directory item found' , {
73+ expandedTemplateId : idFolder ,
7274 } ) ;
7375 }
7476 }
@@ -78,38 +80,44 @@ export class App {
7880 return ( await sftpClient . exists ( path ) ) === 'd' ;
7981 }
8082
81- private async pollTemplateIdFolder (
83+ private getInternalTemplateId ( expandedTemplateId : string ) {
84+ return expandedTemplateId . split ( '_' ) [ 2 ] ;
85+ }
86+
87+ private async pollIdFolder (
8288 sftpClient : SftpClient ,
8389 baseSftpPath : string ,
8490 baseS3Path : string ,
85- templateId : string
91+ expandedTemplateId : string ,
92+ logger : Logger
8693 ) {
87- const proofFiles = await sftpClient . list ( `${ baseSftpPath } /${ templateId } ` ) ;
94+ const proofFiles = await sftpClient . list (
95+ `${ baseSftpPath } /${ expandedTemplateId } `
96+ ) ;
97+ const templateId = this . getInternalTemplateId ( expandedTemplateId ) ;
98+ const idLogger = logger . child ( { expandedTemplateId, templateId } ) ;
8899
89100 for ( const proofFile of proofFiles ) {
90101 if ( proofFile . type === '-' ) {
91102 await this . copyFileToS3 (
92103 sftpClient ,
93- `${ baseSftpPath } /${ templateId } /${ proofFile . name } ` ,
94- `${ baseS3Path } /${ templateId } /${ proofFile . name } `
104+ `${ baseSftpPath } /${ expandedTemplateId } /${ proofFile . name } ` ,
105+ `${ baseS3Path } /${ templateId } /${ proofFile . name } ` ,
106+ logger
95107 ) ;
96108 } else {
97- this . logger . info ( {
98- description : 'Unexpected non-file item found' ,
99- baseSftpPath,
100- templateId,
101- proofFile,
102- } ) ;
109+ idLogger . info ( 'Unexpected non-file item found' ) ;
103110 }
104111 }
105112 }
106113
107114 private async copyFileToS3 (
108115 sftpClient : SftpClient ,
109116 sftpPath : string ,
110- s3Path : string
117+ s3Path : string ,
118+ logger : Logger
111119 ) {
112- this . logger . info ( { description : 'Copying file' , sftpPath , s3Path } ) ;
120+ logger . info ( 'Copying file' ) ;
113121 try {
114122 const data = ( await sftpClient . get ( sftpPath ) ) as Buffer ;
115123
@@ -118,18 +126,12 @@ export class App {
118126 if ( fileValidation ) {
119127 await this . s3Repository . putRawData ( data , s3Path ) ;
120128 } else {
121- this . logger . error ( {
122- description : 'PDF file failed validation' ,
123- sftpPath,
124- s3Path,
125- } ) ;
129+ logger . error ( 'PDF file failed validation' ) ;
126130 }
127131
128132 await sftpClient . delete ( sftpPath ) ;
129133 } catch ( error ) {
130- this . logger
131- . child ( { description : 'Failed to process file' , sftpPath, s3Path } )
132- . error ( error ) ;
134+ logger . error ( 'Failed to process file' , error ) ;
133135 }
134136 }
135137
0 commit comments