@@ -327,6 +327,52 @@ func TestDispatcher(t *testing.T) {
327327 }
328328 assert .Equal (t , []string {"client3:c1" , "client3:c2" , "client3:c1" , "client3:c2" }, mockNotifier .calls )
329329 })
330+
331+ t .Run ("auto-inactivate recurring event when count is reached" , func (t * testing.T ) {
332+ cleanup ()
333+ mockHTTP := new (MockHTTPClient )
334+ dispatcher := NewDispatcher (eventRepo , occurrenceRepo , hmacService , logger , 3 , 5 * time .Second , nil , scheduler , mockHTTP )
335+ mockHTTP .On ("Do" , mock .AnythingOfType ("*http.Request" )).Return (& http.Response {
336+ StatusCode : 200 ,
337+ Body : ioutil .NopCloser (bytes .NewBuffer ([]byte {})),
338+ }, nil )
339+ whParams , _ := json .Marshal (models.WebhookActionParams {URL : "http://example.com/webhook" })
340+ count := 2
341+ event := & models.Event {
342+ ID : uuid .New (),
343+ Name : "Recurring Event" ,
344+ Description : "Test recurring event with count limit" ,
345+ StartTime : time .Now (),
346+ Action : & models.Action {Type : models .ActionTypeWebhook , Params : whParams },
347+ Status : models .EventStatusActive ,
348+ Metadata : []byte (`{"key": "value"}` ),
349+ Tags : pq.StringArray {"test" },
350+ CreatedAt : time .Now (),
351+ Schedule : & models.ScheduleConfig {Frequency : "daily" , Interval : 1 , Count : & count },
352+ }
353+ err := eventRepo .Create (ctx , event )
354+ require .NoError (t , err )
355+ for i := 0 ; i < 2 ; i ++ {
356+ schedule := & models.Schedule {
357+ Occurrence : models.Occurrence {
358+ OccurrenceID : uuid .New (),
359+ EventID : event .ID ,
360+ ScheduledAt : time .Now ().Add (time .Duration (i ) * time .Hour ),
361+ },
362+ Name : event .Name ,
363+ Description : event .Description ,
364+ Webhook : "http://example.com/webhook" ,
365+ Metadata : event .Metadata ,
366+ Tags : event .Tags ,
367+ }
368+ err = dispatcher .DispatchAction (ctx , schedule )
369+ assert .NoError (t , err )
370+ }
371+ // After two completions, event should be inactive
372+ updatedEvent , err := eventRepo .GetByID (ctx , event .ID )
373+ require .NoError (t , err )
374+ assert .Equal (t , models .EventStatusInactive , updatedEvent .Status )
375+ })
330376}
331377
332378func TestDispatcher_RedisOnlyDispatch (t * testing.T ) {
0 commit comments