@@ -9,7 +9,12 @@ import {
99 $TemplateDeletedEventV1 ,
1010 $TemplateDraftedEventV1 ,
1111} from '@nhsdigital/nhs-notify-event-schemas-template-management' ;
12- import { differenceInSeconds , addHours } from 'date-fns' ;
12+ import {
13+ differenceInSeconds ,
14+ addHours ,
15+ startOfHour ,
16+ endOfHour ,
17+ } from 'date-fns' ;
1318import { S3Helper } from '../s3-helper' ;
1419
1520const $NHSNotifyTemplateEvent = z . discriminatedUnion ( 'type' , [
@@ -95,16 +100,16 @@ export class EventCacheHelper {
95100 JSON . parse ( line )
96101 ) ;
97102
98- if ( ! success ) {
99- throw new Error (
100- `Unrecognized event schema detected in S3 file: ${ fileName } ` ,
101- {
102- cause : { error } ,
103- }
104- ) ;
103+ if ( success ) {
104+ return data ;
105105 }
106106
107- return data ;
107+ throw new Error (
108+ `Unrecognized event schema detected in S3 file: ${ fileName } ` ,
109+ {
110+ cause : { error } ,
111+ }
112+ ) ;
108113 } ) ;
109114
110115 events . push ( ...parsedEvents ) ;
@@ -123,11 +128,13 @@ export class EventCacheHelper {
123128 private buildFilePaths ( start : Date , toleranceInSeconds = 30 ) : string [ ] {
124129 const paths = [ this . buildPathPrefix ( start ) ] ;
125130
126- const end = addHours ( start , 1 ) ;
131+ const end = addHours ( startOfHour ( start ) , 1 ) ;
127132
128- const difference = differenceInSeconds ( end , start ) ;
133+ const difference = differenceInSeconds ( endOfHour ( start ) , start , {
134+ roundingMethod : 'ceil' ,
135+ } ) ;
129136
130- if ( difference >= toleranceInSeconds ) {
137+ if ( toleranceInSeconds >= difference ) {
131138 paths . push ( this . buildPathPrefix ( end ) ) ;
132139 }
133140
0 commit comments