diff --git a/Harpoon.Registrations.EFStorage/WebHookReplayService.cs b/Harpoon.Registrations.EFStorage/WebHookReplayService.cs index 87667ba..1d36e16 100644 --- a/Harpoon.Registrations.EFStorage/WebHookReplayService.cs +++ b/Harpoon.Registrations.EFStorage/WebHookReplayService.cs @@ -14,12 +14,14 @@ public class WebHookReplayService { private readonly TContext _context; private readonly IWebHookSender _sender; + private readonly ISecretProtector _secretProtector; /// Initializes a new instance of the class. - public WebHookReplayService(TContext context, IWebHookSender sender) + public WebHookReplayService(TContext context, IWebHookSender sender, ISecretProtector secretProtector) { _context = context ?? throw new ArgumentNullException(nameof(context)); _sender = sender ?? throw new ArgumentNullException(nameof(sender)); + _secretProtector = secretProtector ?? throw new ArgumentNullException(nameof(secretProtector)); } /// @@ -33,11 +35,22 @@ public async Task ReplayFailedNotification(DateTime start) .Where(l => l.Error != null && l.CreatedAt >= start) .Include(e => e.WebHookNotification) .Include(e => e.WebHook) + .AsNoTracking() .ToListAsync(); foreach (var fail in failedNotifications) { - await _sender.SendAsync(new WebHookWorkItem(fail.WebHookNotificationId, fail.WebHookNotification, fail.WebHook), default); + var hasSuccesfulLogs = await _context.WebHookLogs + .Where(l => l.WebHookNotificationId == fail.WebHookNotificationId + && l.WebHookId == fail.WebHookId + && l.CreatedAt > fail.CreatedAt + && l.Error == null).AnyAsync(); + + if (!hasSuccesfulLogs) + { + fail.WebHook.Secret = _secretProtector.Unprotect(fail.WebHook.ProtectedSecret); + await _sender.SendAsync(new WebHookWorkItem(fail.WebHookNotificationId, fail.WebHookNotification, fail.WebHook), default); + } } } }