|  |  | 1 |  | using LGDXRobotCloud.API.Exceptions; | 
|  |  | 2 |  | using LGDXRobotCloud.API.Services.Administration; | 
|  |  | 3 |  | using LGDXRobotCloud.Data.DbContexts; | 
|  |  | 4 |  | using LGDXRobotCloud.Data.Entities; | 
|  |  | 5 |  | using LGDXRobotCloud.Data.Models.Business.Administration; | 
|  |  | 6 |  | using LGDXRobotCloud.Data.Models.Business.Automation; | 
|  |  | 7 |  | using LGDXRobotCloud.Utilities.Enums; | 
|  |  | 8 |  | using LGDXRobotCloud.Utilities.Helpers; | 
|  |  | 9 |  | using Microsoft.EntityFrameworkCore; | 
|  |  | 10 |  |  | 
|  |  | 11 |  | namespace LGDXRobotCloud.API.Services.Automation; | 
|  |  | 12 |  |  | 
|  |  | 13 |  | public interface ITriggerRetryService | 
|  |  | 14 |  | { | 
|  |  | 15 |  |   Task<(IEnumerable<TriggerRetryListBusinessModel>, PaginationHelper)> GetTriggerRetriesAsync(int pageNumber, int pageSi | 
|  |  | 16 |  |   Task<TriggerRetryBusinessModel> GetTriggerRetryAsync(int triggerRetryId); | 
|  |  | 17 |  |   Task<bool> DeleteTriggerRetryAsync(int triggerRetryId); | 
|  |  | 18 |  |   Task RetryTriggerRetryAsync(int triggerRetryId); | 
|  |  | 19 |  |   Task RetryAllFailedTriggerAsync(int triggerId); | 
|  |  | 20 |  | } | 
|  |  | 21 |  |  | 
|  | 5 | 22 |  | public class TriggerRetryService ( | 
|  | 5 | 23 |  |   IActivityLogService activityService, | 
|  | 5 | 24 |  |   ITriggerService triggerService, | 
|  | 5 | 25 |  |   LgdxContext context | 
|  | 5 | 26 |  | ) : ITriggerRetryService | 
|  |  | 27 |  | { | 
|  | 5 | 28 |  |   private readonly IActivityLogService _activityService = activityService ?? throw new ArgumentNullException(nameof(acti | 
|  | 5 | 29 |  |   private readonly ITriggerService _triggerService = triggerService ?? throw new ArgumentNullException(nameof(triggerSer | 
|  | 5 | 30 |  |   private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); | 
|  |  | 31 |  |  | 
|  |  | 32 |  |   public async Task<(IEnumerable<TriggerRetryListBusinessModel>, PaginationHelper)> GetTriggerRetriesAsync(int pageNumbe | 
|  | 1 | 33 |  |   { | 
|  | 1 | 34 |  |     var triggerRetries = await _context.TriggerRetries.AsNoTracking() | 
|  | 1 | 35 |  |       .Include(tr => tr.Trigger) | 
|  | 1 | 36 |  |       .Include(tr => tr.AutoTask) | 
|  | 1 | 37 |  |       .AsSplitQuery() | 
|  | 1 | 38 |  |       .Select(tr => new TriggerRetryListBusinessModel { | 
|  | 1 | 39 |  |         Id = tr.Id, | 
|  | 1 | 40 |  |         TriggerId = tr.TriggerId, | 
|  | 1 | 41 |  |         TriggerName = tr.Trigger.Name, | 
|  | 1 | 42 |  |         AutoTaskId = tr.AutoTaskId, | 
|  | 1 | 43 |  |         AutoTaskName = tr.AutoTask.Name, | 
|  | 1 | 44 |  |         CreatedAt = tr.CreatedAt | 
|  | 1 | 45 |  |       }) | 
|  | 1 | 46 |  |       .OrderBy(t => t.Id) | 
|  | 1 | 47 |  |       .Skip(pageSize * (pageNumber - 1)) | 
|  | 1 | 48 |  |       .Take(pageSize) | 
|  | 1 | 49 |  |       .ToListAsync(); | 
|  | 1 | 50 |  |       var itemCount = triggerRetries.Count; | 
|  | 1 | 51 |  |       var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize); | 
|  | 1 | 52 |  |       return (triggerRetries, PaginationHelper); | 
|  | 1 | 53 |  |   } | 
|  |  | 54 |  |  | 
|  |  | 55 |  |   public async Task<TriggerRetryBusinessModel> GetTriggerRetryAsync(int triggerRetryId) | 
|  | 2 | 56 |  |   { | 
|  | 2 | 57 |  |     TriggerRetry triggerRetry = await _context.TriggerRetries.AsNoTracking() | 
|  | 2 | 58 |  |       .Where(tr => tr.Id == triggerRetryId) | 
|  | 2 | 59 |  |       .Include(tr => tr.Trigger) | 
|  | 2 | 60 |  |       .Include(tr => tr.AutoTask) | 
|  | 2 | 61 |  |       .FirstOrDefaultAsync() ?? throw new LgdxNotFound404Exception(); | 
|  |  | 62 |  |  | 
|  | 1 | 63 |  |     int SameTriggerFailed = await _context.TriggerRetries.AsNoTracking() | 
|  | 1 | 64 |  |       .Where(tr => tr.TriggerId == triggerRetry.TriggerId) | 
|  | 1 | 65 |  |       .CountAsync(); | 
|  |  | 66 |  |  | 
|  | 1 | 67 |  |     return new TriggerRetryBusinessModel { | 
|  | 1 | 68 |  |       Id = triggerRetry.Id, | 
|  | 1 | 69 |  |       TriggerId = triggerRetry.TriggerId, | 
|  | 1 | 70 |  |       TriggerName = triggerRetry.Trigger.Name, | 
|  | 1 | 71 |  |       TriggerUrl = triggerRetry.Trigger.Url, | 
|  | 1 | 72 |  |       TriggerHttpMethodId = triggerRetry.Trigger.HttpMethodId, | 
|  | 1 | 73 |  |       AutoTaskId = triggerRetry.AutoTaskId, | 
|  | 1 | 74 |  |       AutoTaskName = triggerRetry.AutoTask.Name, | 
|  | 1 | 75 |  |       Body = triggerRetry.Body, | 
|  | 1 | 76 |  |       SameTriggerFailed = SameTriggerFailed, | 
|  | 1 | 77 |  |       CreatedAt = triggerRetry.CreatedAt | 
|  | 1 | 78 |  |     }; | 
|  | 1 | 79 |  |   } | 
|  |  | 80 |  |  | 
|  |  | 81 |  |   public async Task<bool> DeleteTriggerRetryAsync(int triggerRetryId) | 
|  | 0 | 82 |  |   { | 
|  | 0 | 83 |  |     return await _context.TriggerRetries.Where(tr => tr.Id == triggerRetryId) | 
|  | 0 | 84 |  |       .ExecuteDeleteAsync() >= 1; | 
|  | 0 | 85 |  |   } | 
|  |  | 86 |  |  | 
|  |  | 87 |  |   public async Task RetryTriggerRetryAsync(int triggerRetryId) | 
|  | 2 | 88 |  |   { | 
|  | 2 | 89 |  |     var triggerRetry = await _context.TriggerRetries.AsNoTracking() | 
|  | 2 | 90 |  |       .Where(tr => tr.Id == triggerRetryId) | 
|  | 2 | 91 |  |       .FirstOrDefaultAsync() ?? throw new LgdxNotFound404Exception(); | 
|  |  | 92 |  |  | 
|  | 1 | 93 |  |     var autoTask = await _context.AutoTasks.AsNoTracking() | 
|  | 1 | 94 |  |       .Where(at => at.Id == triggerRetry.AutoTaskId) | 
|  | 1 | 95 |  |       .FirstOrDefaultAsync() ?? throw new LgdxValidation400Expection(nameof(triggerRetry.AutoTaskId), "AutoTask ID is in | 
|  |  | 96 |  |  | 
|  | 1 | 97 |  |     var trigger = await _context.Triggers.AsNoTracking() | 
|  | 1 | 98 |  |       .Where(t => t.Id == triggerRetry.TriggerId) | 
|  | 1 | 99 |  |       .FirstOrDefaultAsync() ?? throw new LgdxValidation400Expection(nameof(triggerRetry.TriggerId), "Trigger ID is inva | 
|  |  | 100 |  |  | 
|  | 1 | 101 |  |     if (await _triggerService.RetryTriggerAsync(autoTask, trigger, triggerRetry.Body)) | 
|  | 0 | 102 |  |     { | 
|  | 0 | 103 |  |       await DeleteTriggerRetryAsync(triggerRetryId); | 
|  | 0 | 104 |  |     } | 
|  |  | 105 |  |  | 
|  | 1 | 106 |  |     await _activityService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel | 
|  | 1 | 107 |  |     { | 
|  | 1 | 108 |  |       EntityName = nameof(Trigger), | 
|  | 1 | 109 |  |       EntityId = trigger.Id.ToString(), | 
|  | 1 | 110 |  |       Action = ActivityAction.TriggerRetry, | 
|  | 1 | 111 |  |     }); | 
|  | 1 | 112 |  |   } | 
|  |  | 113 |  |  | 
|  |  | 114 |  |   public async Task RetryAllFailedTriggerAsync(int triggerId) | 
|  | 0 | 115 |  |   { | 
|  | 0 | 116 |  |     var triggerRetries = await _context.TriggerRetries | 
|  | 0 | 117 |  |       .Where(tr => tr.TriggerId == triggerId) | 
|  | 0 | 118 |  |       .Include(tr => tr.Trigger) | 
|  | 0 | 119 |  |       .Include(tr => tr.AutoTask) | 
|  | 0 | 120 |  |       .AsSplitQuery() | 
|  | 0 | 121 |  |       .ToListAsync(); | 
|  |  | 122 |  |  | 
|  | 0 | 123 |  |     foreach (var tr in triggerRetries) | 
|  | 0 | 124 |  |     { | 
|  | 0 | 125 |  |       if (await _triggerService.RetryTriggerAsync(tr.AutoTask, tr.Trigger, tr.Body)) | 
|  | 0 | 126 |  |       { | 
|  | 0 | 127 |  |         _context.TriggerRetries.Remove(tr); | 
|  | 0 | 128 |  |       } | 
|  | 0 | 129 |  |     } | 
|  | 0 | 130 |  |     _context.SaveChanges(); | 
|  |  | 131 |  |  | 
|  | 0 | 132 |  |     await _activityService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel | 
|  | 0 | 133 |  |     { | 
|  | 0 | 134 |  |       EntityName = nameof(Trigger), | 
|  | 0 | 135 |  |       EntityId = triggerId.ToString(), | 
|  | 0 | 136 |  |       Action = ActivityAction.TriggerRetry, | 
|  | 0 | 137 |  |       Note = $"Batch retry for {triggerRetries.Count} requests." | 
|  | 0 | 138 |  |     }); | 
|  | 0 | 139 |  |   } | 
|  |  | 140 |  | } |