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