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