| | 1 | | using System.Text.Json; |
| | 2 | | using LGDXRobotCloud.API.Exceptions; |
| | 3 | | using LGDXRobotCloud.API.Services.Administration; |
| | 4 | | using LGDXRobotCloud.Data.Contracts; |
| | 5 | | using LGDXRobotCloud.Data.DbContexts; |
| | 6 | | using LGDXRobotCloud.Data.Entities; |
| | 7 | | using LGDXRobotCloud.Data.Models.Business.Automation; |
| | 8 | | using LGDXRobotCloud.Utilities.Enums; |
| | 9 | | using LGDXRobotCloud.Utilities.Helpers; |
| | 10 | | using MassTransit; |
| | 11 | | using Microsoft.EntityFrameworkCore; |
| | 12 | |
|
| | 13 | | namespace LGDXRobotCloud.API.Services.Automation; |
| | 14 | |
|
| | 15 | | public interface ITriggerService |
| | 16 | | { |
| | 17 | | Task<(IEnumerable<TriggerListBusinessModel>, PaginationHelper)> GetTriggersAsync(string? name, int pageNumber, int pag |
| | 18 | | Task<TriggerBusinessModel> GetTriggerAsync(int triggerId); |
| | 19 | | Task<TriggerBusinessModel> CreateTriggerAsync(TriggerCreateBusinessModel triggerCreateBusinessModel); |
| | 20 | | Task<bool> UpdateTriggerAsync(int triggerId, TriggerUpdateBusinessModel triggerUpdateBusinessModel); |
| | 21 | | Task<bool> TestDeleteTriggerAsync(int triggerId); |
| | 22 | | Task<bool> DeleteTriggerAsync(int triggerId); |
| | 23 | |
|
| | 24 | | Task<IEnumerable<TriggerSearchBusinessModel>> SearchTriggersAsync(string? name); |
| | 25 | |
|
| | 26 | | Task InitialiseTriggerAsync(AutoTask autoTask, FlowDetail flowDetail); |
| | 27 | | Task<bool> RetryTriggerAsync(AutoTask autoTask, Trigger trigger, string body); |
| | 28 | | } |
| | 29 | |
|
| 18 | 30 | | public sealed class TriggerService ( |
| 18 | 31 | | IBus bus, |
| 18 | 32 | | LgdxContext context, |
| 18 | 33 | | IApiKeyService apiKeyService |
| 18 | 34 | | ) : ITriggerService |
| | 35 | | { |
| 18 | 36 | | private readonly IBus _bus = bus ?? throw new ArgumentNullException(nameof(bus)); |
| 18 | 37 | | private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); |
| 18 | 38 | | private readonly IApiKeyService _apiKeyService = apiKeyService ?? throw new ArgumentNullException(nameof(apiKeyService |
| | 39 | |
|
| | 40 | | public async Task<(IEnumerable<TriggerListBusinessModel>, PaginationHelper)> GetTriggersAsync(string? name, int pageNu |
| 4 | 41 | | { |
| 4 | 42 | | var query = _context.Triggers as IQueryable<Trigger>; |
| 4 | 43 | | if(!string.IsNullOrWhiteSpace(name)) |
| 3 | 44 | | { |
| 3 | 45 | | name = name.Trim(); |
| 3 | 46 | | query = query.Where(t => t.Name.ToLower().Contains(name.ToLower())); |
| 3 | 47 | | } |
| 4 | 48 | | var itemCount = await query.CountAsync(); |
| 4 | 49 | | var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize); |
| 4 | 50 | | var triggers = await query.AsNoTracking() |
| 4 | 51 | | .OrderBy(t => t.Id) |
| 4 | 52 | | .Skip(pageSize * (pageNumber - 1)) |
| 4 | 53 | | .Take(pageSize) |
| 4 | 54 | | .Select(t => new TriggerListBusinessModel { |
| 4 | 55 | | Id = t.Id, |
| 4 | 56 | | Name = t.Name, |
| 4 | 57 | | Url = t.Url, |
| 4 | 58 | | HttpMethodId = t.HttpMethodId, |
| 4 | 59 | | }) |
| 4 | 60 | | .ToListAsync(); |
| 4 | 61 | | return (triggers, PaginationHelper); |
| 4 | 62 | | } |
| | 63 | |
|
| | 64 | | public async Task<TriggerBusinessModel> GetTriggerAsync(int triggerId) |
| 2 | 65 | | { |
| 2 | 66 | | return await _context.Triggers.Where(t => t.Id == triggerId) |
| 2 | 67 | | .Include(t => t.ApiKey) |
| 2 | 68 | | .Select(t => new TriggerBusinessModel { |
| 2 | 69 | | Id = t.Id, |
| 2 | 70 | | Name = t.Name, |
| 2 | 71 | | Url = t.Url, |
| 2 | 72 | | HttpMethodId = t.HttpMethodId, |
| 2 | 73 | | Body = t.Body, |
| 2 | 74 | | ApiKeyInsertLocationId = t.ApiKeyInsertLocationId, |
| 2 | 75 | | ApiKeyFieldName = t.ApiKeyFieldName, |
| 2 | 76 | | ApiKeyId = t.ApiKey!.Id, |
| 2 | 77 | | ApiKeyName = t.ApiKey!.Name, |
| 2 | 78 | | }) |
| 2 | 79 | | .FirstOrDefaultAsync() |
| 2 | 80 | | ?? throw new LgdxNotFound404Exception(); |
| 1 | 81 | | } |
| | 82 | |
|
| | 83 | | private async Task ValidateApiKey(int apiKeyId) |
| 3 | 84 | | { |
| 3 | 85 | | var apiKey = await _apiKeyService.GetApiKeyAsync(apiKeyId); |
| 3 | 86 | | if (apiKey == null) |
| 1 | 87 | | { |
| 1 | 88 | | throw new LgdxValidation400Expection(nameof(TriggerBusinessModel.Id), $"The API Key Id {apiKeyId} is invalid."); |
| | 89 | | } |
| 2 | 90 | | else if (!apiKey.IsThirdParty) |
| 1 | 91 | | { |
| 1 | 92 | | throw new LgdxValidation400Expection(nameof(TriggerBusinessModel.Id), "Only third party API key is allowed."); |
| | 93 | | } |
| 1 | 94 | | } |
| | 95 | |
|
| | 96 | | public async Task<TriggerBusinessModel> CreateTriggerAsync(TriggerCreateBusinessModel triggerCreateBusinessModel) |
| 3 | 97 | | { |
| 3 | 98 | | if (triggerCreateBusinessModel.ApiKeyId != null) |
| 3 | 99 | | { |
| 3 | 100 | | await ValidateApiKey((int)triggerCreateBusinessModel.ApiKeyId); |
| 1 | 101 | | } |
| | 102 | |
|
| 1 | 103 | | var trigger = new Trigger { |
| 1 | 104 | | Name = triggerCreateBusinessModel.Name, |
| 1 | 105 | | Url = triggerCreateBusinessModel.Url, |
| 1 | 106 | | HttpMethodId = triggerCreateBusinessModel.HttpMethodId, |
| 1 | 107 | | Body = triggerCreateBusinessModel.Body, |
| 1 | 108 | | ApiKeyInsertLocationId = triggerCreateBusinessModel.ApiKeyInsertLocationId, |
| 1 | 109 | | ApiKeyFieldName = triggerCreateBusinessModel.ApiKeyFieldName, |
| 1 | 110 | | ApiKeyId = triggerCreateBusinessModel.ApiKeyId, |
| 1 | 111 | | }; |
| 1 | 112 | | await _context.Triggers.AddAsync(trigger); |
| 1 | 113 | | await _context.SaveChangesAsync(); |
| 1 | 114 | | return new TriggerBusinessModel { |
| 1 | 115 | | Id = trigger.Id, |
| 1 | 116 | | Name = trigger.Name, |
| 1 | 117 | | Url = trigger.Url, |
| 1 | 118 | | HttpMethodId = trigger.HttpMethodId, |
| 1 | 119 | | Body = trigger.Body, |
| 1 | 120 | | ApiKeyInsertLocationId = trigger.ApiKeyInsertLocationId, |
| 1 | 121 | | ApiKeyFieldName = trigger.ApiKeyFieldName, |
| 1 | 122 | | ApiKeyId = trigger.ApiKeyId, |
| 1 | 123 | | ApiKeyName = trigger.ApiKey?.Name, |
| 1 | 124 | | }; |
| 1 | 125 | | } |
| | 126 | |
|
| | 127 | | public async Task<bool> UpdateTriggerAsync(int triggerId, TriggerUpdateBusinessModel triggerUpdateBusinessModel) |
| 0 | 128 | | { |
| 0 | 129 | | if (triggerUpdateBusinessModel.ApiKeyId != null) |
| 0 | 130 | | { |
| 0 | 131 | | await ValidateApiKey((int)triggerUpdateBusinessModel.ApiKeyId); |
| 0 | 132 | | } |
| | 133 | |
|
| 0 | 134 | | return await _context.Triggers |
| 0 | 135 | | .Where(t => t.Id == triggerId) |
| 0 | 136 | | .ExecuteUpdateAsync(setters => setters |
| 0 | 137 | | .SetProperty(t => t.Name, triggerUpdateBusinessModel.Name) |
| 0 | 138 | | .SetProperty(t => t.Url, triggerUpdateBusinessModel.Url) |
| 0 | 139 | | .SetProperty(t => t.HttpMethodId, triggerUpdateBusinessModel.HttpMethodId) |
| 0 | 140 | | .SetProperty(t => t.Body, triggerUpdateBusinessModel.Body) |
| 0 | 141 | | .SetProperty(t => t.ApiKeyInsertLocationId, triggerUpdateBusinessModel.ApiKeyInsertLocationId) |
| 0 | 142 | | .SetProperty(t => t.ApiKeyFieldName, triggerUpdateBusinessModel.ApiKeyFieldName) |
| 0 | 143 | | .SetProperty(t => t.ApiKeyId, triggerUpdateBusinessModel.ApiKeyId)) == 1; |
| 0 | 144 | | } |
| | 145 | |
|
| | 146 | | public async Task<bool> TestDeleteTriggerAsync(int triggerId) |
| 2 | 147 | | { |
| 2 | 148 | | var depeendencies = await _context.FlowDetails.Where(t => t.TriggerId == triggerId).CountAsync(); |
| 2 | 149 | | if (depeendencies > 0) |
| 1 | 150 | | { |
| 1 | 151 | | throw new LgdxValidation400Expection(nameof(triggerId), $"This trigger has been used by {depeendencies} details in |
| | 152 | | } |
| 1 | 153 | | return true; |
| 1 | 154 | | } |
| | 155 | |
|
| | 156 | | public async Task<bool> DeleteTriggerAsync(int triggerId) |
| 0 | 157 | | { |
| 0 | 158 | | return await _context.Triggers.Where(t => t.Id == triggerId) |
| 0 | 159 | | .ExecuteDeleteAsync() == 1; |
| 0 | 160 | | } |
| | 161 | |
|
| | 162 | | public async Task<IEnumerable<TriggerSearchBusinessModel>> SearchTriggersAsync(string? name) |
| 4 | 163 | | { |
| 4 | 164 | | var n = name ?? string.Empty; |
| 4 | 165 | | return await _context.Triggers.AsNoTracking() |
| 4 | 166 | | .Where(w => w.Name.ToLower().Contains(n.ToLower())) |
| 4 | 167 | | .Take(10) |
| 4 | 168 | | .Select(t => new TriggerSearchBusinessModel { |
| 4 | 169 | | Id = t.Id, |
| 4 | 170 | | Name = t.Name, |
| 4 | 171 | | }) |
| 4 | 172 | | .ToListAsync(); |
| 4 | 173 | | } |
| | 174 | |
|
| | 175 | | private string GetRobotName(Guid robotId) |
| 3 | 176 | | { |
| 3 | 177 | | return _context.Robots.AsNoTracking().Where(r => r.Id == robotId).FirstOrDefault()?.Name ?? string.Empty; |
| 3 | 178 | | } |
| | 179 | |
|
| | 180 | | private string GetRealmName(int realmId) |
| 3 | 181 | | { |
| 3 | 182 | | return _context.Realms.AsNoTracking().Where(r => r.Id == realmId).FirstOrDefault()?.Name ?? string.Empty; |
| 3 | 183 | | } |
| | 184 | |
|
| | 185 | | private string GeneratePresetValue(int i, AutoTask autoTask) |
| 8 | 186 | | { |
| 8 | 187 | | return i switch |
| 8 | 188 | | { |
| 1 | 189 | | (int)TriggerPresetValue.AutoTaskId => $"{autoTask.Id}", |
| 1 | 190 | | (int)TriggerPresetValue.AutoTaskName => $"{autoTask.Name}", |
| 1 | 191 | | (int)TriggerPresetValue.AutoTaskCurrentProgressId => $"{autoTask.CurrentProgressId}", |
| 1 | 192 | | (int)TriggerPresetValue.AutoTaskCurrentProgressName => $"{autoTask.CurrentProgress.Name!}", |
| 1 | 193 | | (int)TriggerPresetValue.RobotId => $"{autoTask.AssignedRobotId}", |
| 1 | 194 | | (int)TriggerPresetValue.RobotName => $"{GetRobotName((Guid)autoTask.AssignedRobotId!)}", |
| 1 | 195 | | (int)TriggerPresetValue.RealmId => $"{autoTask.RealmId}", |
| 1 | 196 | | (int)TriggerPresetValue.RealmName => $"{GetRealmName(autoTask.RealmId)}", |
| 0 | 197 | | _ => string.Empty, |
| 8 | 198 | | }; |
| 8 | 199 | | } |
| | 200 | |
|
| | 201 | | public async Task InitialiseTriggerAsync(AutoTask autoTask, FlowDetail flowDetail) |
| 2 | 202 | | { |
| 2 | 203 | | var trigger = await _context.Triggers.AsNoTracking() |
| 2 | 204 | | .Where(t => t.Id == flowDetail.TriggerId) |
| 2 | 205 | | .FirstOrDefaultAsync(); |
| 2 | 206 | | if (trigger == null) |
| 1 | 207 | | { |
| 1 | 208 | | return; |
| | 209 | | } |
| | 210 | |
|
| 1 | 211 | | var bodyDictionary = JsonSerializer.Deserialize<Dictionary<string, string>>(trigger.Body ?? "{}"); |
| 1 | 212 | | if (bodyDictionary != null) |
| 1 | 213 | | { |
| | 214 | | // Replace Preset Value |
| 19 | 215 | | foreach (var pair in bodyDictionary) |
| 8 | 216 | | { |
| 8 | 217 | | if (pair.Value.Length >= 5) // ((1)) has 5 characters |
| 8 | 218 | | { |
| 8 | 219 | | if (int.TryParse(pair.Value[2..^2], out int p)) |
| 8 | 220 | | { |
| 8 | 221 | | bodyDictionary[pair.Key] = GeneratePresetValue(p, autoTask); |
| 8 | 222 | | } |
| 8 | 223 | | } |
| 8 | 224 | | } |
| | 225 | | // Add Next Token |
| 1 | 226 | | if (flowDetail.AutoTaskNextControllerId != (int) AutoTaskNextController.Robot && autoTask.NextToken != null) |
| 1 | 227 | | { |
| 1 | 228 | | bodyDictionary.Add("NextToken", autoTask.NextToken); |
| 1 | 229 | | } |
| | 230 | |
|
| 1 | 231 | | await _bus.Publish(new AutoTaskTriggerContract { |
| 1 | 232 | | Trigger = trigger, |
| 1 | 233 | | Body = bodyDictionary, |
| 1 | 234 | | AutoTaskId = autoTask.Id, |
| 1 | 235 | | AutoTaskName = autoTask.Name!, |
| 1 | 236 | | RobotId = (Guid)autoTask.AssignedRobotId!, |
| 1 | 237 | | RobotName = GetRobotName((Guid)autoTask.AssignedRobotId!), |
| 1 | 238 | | RealmId = autoTask.RealmId, |
| 1 | 239 | | RealmName = GetRealmName(autoTask.RealmId), |
| 1 | 240 | | }); |
| 1 | 241 | | } |
| 2 | 242 | | } |
| | 243 | |
|
| | 244 | | public async Task<bool> RetryTriggerAsync(AutoTask autoTask, Trigger trigger, string body) |
| 1 | 245 | | { |
| 1 | 246 | | var bodyDictionary = JsonSerializer.Deserialize<Dictionary<string, string>>(body ?? "{}"); |
| 1 | 247 | | if (bodyDictionary != null) |
| 1 | 248 | | { |
| 1 | 249 | | await _bus.Publish(new AutoTaskTriggerContract { |
| 1 | 250 | | Trigger = trigger, |
| 1 | 251 | | Body = bodyDictionary, |
| 1 | 252 | | AutoTaskId = autoTask.Id, |
| 1 | 253 | | AutoTaskName = autoTask.Name!, |
| 1 | 254 | | RobotId = (Guid)autoTask.AssignedRobotId!, |
| 1 | 255 | | RobotName = GetRobotName((Guid)autoTask.AssignedRobotId!), |
| 1 | 256 | | RealmId = autoTask.RealmId, |
| 1 | 257 | | RealmName = GetRealmName(autoTask.RealmId), |
| 1 | 258 | | }); |
| 1 | 259 | | return true; |
| | 260 | | } |
| 0 | 261 | | return false; |
| 1 | 262 | | } |
| | 263 | | } |