@@ -237,4 +237,110 @@ describe('App', () => {
237237 app . send ( JSON . stringify ( invalidEvent ) , mocks . sftpClient , baseUploadDir )
238238 ) . rejects . toThrowErrorMatchingSnapshot ( ) ;
239239 } ) ;
240+
241+ test ( 'exist early and does not send if the manifest is already in the SFTP server' , async ( ) => {
242+ const { app, mocks } = setup ( ) ;
243+
244+ const personalisationFields = [ 'pdsField' ] ;
245+ const batchColumns = [ 'clientRef' , 'template' , ...personalisationFields ] ;
246+
247+ const event = mockEvent ( true , personalisationFields ) ;
248+
249+ const pdfContent = 'mock PDF content' ;
250+ const pdf = Readable . from ( pdfContent ) ;
251+
252+ const batchId = 'template-id-0000000000000_pdfversionid' ;
253+
254+ const batchData = [
255+ {
256+ clientRef : 'random1_random2_1744184100' ,
257+ template : templateId ,
258+ pdsField : 'pdsVal1' ,
259+ } ,
260+ {
261+ clientRef : 'random3_random4_1744184100' ,
262+ template : templateId ,
263+ pdsField : 'pdsVal2' ,
264+ } ,
265+ {
266+ clientRef : 'random5_random6_1744184100' ,
267+ template : templateId ,
268+ pdsField : 'pdsVal3' ,
269+ } ,
270+ ] ;
271+
272+ const batchCsv : string = [
273+ batchColumns . join ( ',' ) ,
274+ batchData
275+ . map ( ( x ) =>
276+ [ `"${ x . clientRef } "` , `"${ x . template } "` , `"${ x . pdsField } "` ] . join ( ',' )
277+ )
278+ . join ( '\n' ) ,
279+ ] . join ( '\n' ) ;
280+
281+ const batchHash = 'hash-of-batch-csv' ;
282+
283+ const manifestData : Manifest = {
284+ template : templateId ,
285+ batch : `${ batchId } .csv` ,
286+ records : '3' ,
287+ md5sum : batchHash ,
288+ } ;
289+
290+ mocks . batch . getId . mockReturnValueOnce ( batchId ) ;
291+
292+ mocks . userDataRepository . get . mockResolvedValueOnce ( {
293+ testData : undefined ,
294+ pdf,
295+ } ) ;
296+
297+ mocks . batch . buildBatch . mockReturnValueOnce ( batchData ) ;
298+
299+ mocks . batch . getHeader . mockReturnValueOnce ( batchColumns . join ( ',' ) ) ;
300+
301+ mocks . batch . buildManifest . mockReturnValueOnce ( manifestData ) ;
302+
303+ // manifest already exists
304+ mocks . sftpClient . exists . mockResolvedValueOnce ( '-' ) ;
305+
306+ await app . send ( JSON . stringify ( event ) , mocks . sftpClient , baseUploadDir ) ;
307+
308+ expect ( mocks . userDataRepository . get ) . toHaveBeenCalledTimes ( 1 ) ;
309+ expect ( mocks . userDataRepository . get ) . toHaveBeenCalledWith (
310+ owner ,
311+ templateId ,
312+ pdfVersion ,
313+ testDataVersion
314+ ) ;
315+
316+ expect ( mocks . batch . buildBatch ) . toHaveBeenCalledTimes ( 1 ) ;
317+ expect ( mocks . batch . buildBatch ) . toHaveBeenCalledWith (
318+ templateId ,
319+ personalisationFields ,
320+ undefined
321+ ) ;
322+
323+ expect ( mocks . batch . getHeader ) . toHaveBeenCalledTimes ( 1 ) ;
324+ expect ( mocks . batch . getHeader ) . toHaveBeenCalledWith ( personalisationFields ) ;
325+
326+ expect ( mocks . batch . buildManifest ) . toHaveBeenCalledTimes ( 1 ) ;
327+ expect ( mocks . batch . buildManifest ) . toHaveBeenCalledWith (
328+ templateId ,
329+ batchId ,
330+ batchCsv
331+ ) ;
332+
333+ expect ( mocks . sftpClient . exists ) . toHaveBeenCalledTimes ( 1 ) ;
334+ expect ( mocks . sftpClient . exists ) . toHaveBeenCalledWith (
335+ `${ baseUploadDir } /${ sftpEnvironment } /batches/${ templateId } /${ batchId } _MANIFEST.csv`
336+ ) ;
337+
338+ expect ( mocks . sftpClient . mkdir ) . not . toHaveBeenCalled ( ) ;
339+
340+ expect ( mocks . sftpClient . put ) . not . toHaveBeenCalled ( ) ;
341+
342+ expect (
343+ mocks . templateRepository . updateToNotYetSubmitted
344+ ) . not . toHaveBeenCalled ( ) ;
345+ } ) ;
240346} ) ;
0 commit comments