|
| 1 | +import type { SftpClient } from '../infra/sftp-client'; |
| 2 | +import type { UserDataRepository } from '../infra/user-data-repository'; |
| 3 | +import type { Logger } from 'nhs-notify-web-template-management-utils'; |
| 4 | +import type { Batch } from '../domain/batch'; |
| 5 | +import type { TemplateRepository } from '../infra/template-repository'; |
1 | 6 | import { parseTestPersonalisation } from '../domain/test-data'; |
2 | 7 | import { serialise } from '../infra/serialise-csv'; |
3 | | -import { SftpClient } from '../infra/sftp-client'; |
4 | | -import type { UserDataRepository } from '../infra/user-data-repository'; |
5 | 8 | import { z } from 'zod'; |
6 | 9 | import path from 'node:path'; |
7 | 10 | import { Readable } from 'node:stream'; |
8 | | -import { Logger } from 'nhs-notify-web-template-management-utils'; |
9 | | -import { Batch } from '../domain/batch'; |
10 | | -import { TemplateRepository } from '../infra/template-repository'; |
11 | 11 |
|
12 | 12 | export function parseProofingRequest(event: string) { |
13 | 13 | return z |
@@ -66,43 +66,46 @@ export class App { |
66 | 66 | 'template,batch,records,md5' |
67 | 67 | ); |
68 | 68 |
|
| 69 | + const sftpEnvDir = path.join(baseUploadDir, this.sftpEnvironment); |
| 70 | + |
| 71 | + const templateDir = path.join(sftpEnvDir, 'templates', templateId); |
| 72 | + const batchDir = path.join(sftpEnvDir, 'batches', templateId); |
| 73 | + |
| 74 | + const pdfName = `${templateId}.pdf`; |
| 75 | + const csvName = `${batch.id}.csv`; |
| 76 | + const manifestName = `${batch.id}_MANIFEST.csv`; |
| 77 | + |
| 78 | + const pdfDestination = path.join(templateDir, pdfName); |
| 79 | + const batchDestination = path.join(batchDir, csvName); |
| 80 | + const manifestDestination = path.join(batchDir, manifestName); |
| 81 | + |
| 82 | + const batchStream = Readable.from(batchCsv); |
| 83 | + const manifestStream = Readable.from(manifestCsv); |
| 84 | + |
| 85 | + batchLogger.info('Updating template to SENDING_PROOF'); |
| 86 | + |
| 87 | + await this.templateRepository.updateToSendingProof(owner, templateId); |
| 88 | + |
69 | 89 | batchLogger.info('Sending PDF'); |
70 | 90 |
|
71 | | - await sftpClient.put( |
72 | | - userData.pdf, |
73 | | - path.join( |
74 | | - baseUploadDir, |
75 | | - this.sftpEnvironment, |
76 | | - 'templates', |
77 | | - `${templateId}.pdf` |
78 | | - ) |
79 | | - ); |
| 91 | + await Promise.all([ |
| 92 | + sftpClient.mkdir(templateDir, true), |
| 93 | + sftpClient.mkdir(batchDir, true), |
| 94 | + ]); |
| 95 | + |
| 96 | + await sftpClient.put(userData.pdf, pdfDestination); |
80 | 97 |
|
81 | 98 | batchLogger.info('Sending batch'); |
82 | 99 |
|
83 | | - await sftpClient.put( |
84 | | - Readable.from(batchCsv), |
85 | | - path.join( |
86 | | - baseUploadDir, |
87 | | - this.sftpEnvironment, |
88 | | - 'batches', |
89 | | - templateId, |
90 | | - `${batch.id}.csv` |
91 | | - ) |
92 | | - ); |
| 100 | + await sftpClient.put(batchStream, batchDestination); |
93 | 101 |
|
94 | 102 | batchLogger.info('Sending manifest'); |
95 | 103 |
|
96 | | - await sftpClient.put( |
97 | | - Readable.from(manifestCsv), |
98 | | - path.join( |
99 | | - baseUploadDir, |
100 | | - this.sftpEnvironment, |
101 | | - 'batches', |
102 | | - templateId, |
103 | | - `${batch.id}_MANIFEST.csv` |
104 | | - ) |
105 | | - ); |
| 104 | + await sftpClient.put(manifestStream, manifestDestination); |
| 105 | + |
| 106 | + batchLogger.info('Updating template to awaiting proof'); |
| 107 | + |
| 108 | + await this.templateRepository.updateToAwaitingProof(owner, templateId); |
106 | 109 |
|
107 | 110 | batchLogger.info('Sent proofing request'); |
108 | 111 | } |
|
0 commit comments