using System.Security.Cryptography; using Akka.Actor; using Newtonsoft.Json; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using ScadaLink.Commons.Entities.ExternalSystems; using ScadaLink.Commons.Entities.InboundApi; using ScadaLink.Commons.Entities.Instances; using ScadaLink.Commons.Entities.Scripts; using ScadaLink.Commons.Entities.Templates; using ScadaLink.Commons.Entities.Notifications; using ScadaLink.Commons.Entities.Security; using ScadaLink.Commons.Entities.Sites; using ScadaLink.Commons.Interfaces.Repositories; using ScadaLink.Commons.Messages.DebugView; using ScadaLink.Commons.Messages.Management; using ScadaLink.Commons.Messages.RemoteQuery; using ScadaLink.DeploymentManager; using ScadaLink.HealthMonitoring; using ScadaLink.Communication; using ScadaLink.TemplateEngine; using ScadaLink.TemplateEngine.Services; namespace ScadaLink.ManagementService; /// /// Central actor that handles all management commands from the CLI (via ClusterClient). /// Receives ManagementEnvelope messages, authorizes based on roles, then delegates to /// the appropriate service or repository using scoped DI. /// public class ManagementActor : ReceiveActor { private readonly IServiceProvider _serviceProvider; private readonly ILogger _logger; public ManagementActor(IServiceProvider serviceProvider, ILogger logger) { _serviceProvider = serviceProvider; _logger = logger; Receive(HandleEnvelope); } private void HandleEnvelope(ManagementEnvelope envelope) { var sender = Sender; var correlationId = envelope.CorrelationId; var user = envelope.User; // Check authorization var requiredRole = GetRequiredRole(envelope.Command); if (requiredRole != null && !user.Roles.Contains(requiredRole, StringComparer.OrdinalIgnoreCase)) { sender.Tell(new ManagementUnauthorized(correlationId, $"Role '{requiredRole}' required for {envelope.Command.GetType().Name}")); return; } // Process command asynchronously with scoped DI Task.Run(async () => { using var scope = _serviceProvider.CreateScope(); try { var result = await DispatchCommand(scope.ServiceProvider, envelope.Command, user.Username); var json = JsonConvert.SerializeObject(result, Formatting.None); sender.Tell(new ManagementSuccess(correlationId, json)); } catch (Exception ex) { _logger.LogError(ex, "Management command {Command} failed (CorrelationId={CorrelationId})", envelope.Command.GetType().Name, correlationId); sender.Tell(new ManagementError(correlationId, ex.Message, "COMMAND_FAILED")); } }); } private static string? GetRequiredRole(object command) => command switch { // Admin operations CreateSiteCommand or UpdateSiteCommand or DeleteSiteCommand or ListRoleMappingsCommand or CreateRoleMappingCommand or UpdateRoleMappingCommand or DeleteRoleMappingCommand or ListApiKeysCommand or CreateApiKeyCommand or DeleteApiKeyCommand or UpdateApiKeyCommand or ListScopeRulesCommand or AddScopeRuleCommand or DeleteScopeRuleCommand => "Admin", // Design operations CreateAreaCommand or DeleteAreaCommand or CreateTemplateCommand or UpdateTemplateCommand or DeleteTemplateCommand or ValidateTemplateCommand or CreateExternalSystemCommand or UpdateExternalSystemCommand or DeleteExternalSystemCommand or CreateExternalSystemMethodCommand or UpdateExternalSystemMethodCommand or DeleteExternalSystemMethodCommand or CreateNotificationListCommand or UpdateNotificationListCommand or DeleteNotificationListCommand or UpdateSmtpConfigCommand or CreateDataConnectionCommand or UpdateDataConnectionCommand or DeleteDataConnectionCommand or AssignDataConnectionToSiteCommand or UnassignDataConnectionFromSiteCommand or AddTemplateAttributeCommand or UpdateTemplateAttributeCommand or DeleteTemplateAttributeCommand or AddTemplateAlarmCommand or UpdateTemplateAlarmCommand or DeleteTemplateAlarmCommand or AddTemplateScriptCommand or UpdateTemplateScriptCommand or DeleteTemplateScriptCommand or AddTemplateCompositionCommand or DeleteTemplateCompositionCommand or CreateSharedScriptCommand or UpdateSharedScriptCommand or DeleteSharedScriptCommand or CreateDatabaseConnectionDefCommand or UpdateDatabaseConnectionDefCommand or DeleteDatabaseConnectionDefCommand or CreateApiMethodCommand or UpdateApiMethodCommand or DeleteApiMethodCommand or UpdateAreaCommand => "Design", // Deployment operations CreateInstanceCommand or MgmtDeployInstanceCommand or MgmtEnableInstanceCommand or MgmtDisableInstanceCommand or MgmtDeleteInstanceCommand or SetConnectionBindingsCommand or MgmtDeployArtifactsCommand or DebugSnapshotCommand => "Deployment", // Read-only queries -- any authenticated user _ => null }; private async Task DispatchCommand(IServiceProvider sp, object command, string user) { return command switch { // Templates ListTemplatesCommand => await HandleListTemplates(sp), GetTemplateCommand cmd => await HandleGetTemplate(sp, cmd), CreateTemplateCommand cmd => await HandleCreateTemplate(sp, cmd, user), UpdateTemplateCommand cmd => await HandleUpdateTemplate(sp, cmd, user), DeleteTemplateCommand cmd => await HandleDeleteTemplate(sp, cmd, user), ValidateTemplateCommand cmd => await HandleValidateTemplate(sp, cmd), // Template members AddTemplateAttributeCommand cmd => await HandleAddAttribute(sp, cmd, user), UpdateTemplateAttributeCommand cmd => await HandleUpdateAttribute(sp, cmd, user), DeleteTemplateAttributeCommand cmd => await HandleDeleteAttribute(sp, cmd, user), AddTemplateAlarmCommand cmd => await HandleAddAlarm(sp, cmd, user), UpdateTemplateAlarmCommand cmd => await HandleUpdateAlarm(sp, cmd, user), DeleteTemplateAlarmCommand cmd => await HandleDeleteAlarm(sp, cmd, user), AddTemplateScriptCommand cmd => await HandleAddScript(sp, cmd, user), UpdateTemplateScriptCommand cmd => await HandleUpdateScript(sp, cmd, user), DeleteTemplateScriptCommand cmd => await HandleDeleteScript(sp, cmd, user), AddTemplateCompositionCommand cmd => await HandleAddComposition(sp, cmd, user), DeleteTemplateCompositionCommand cmd => await HandleDeleteComposition(sp, cmd, user), // Instances ListInstancesCommand cmd => await HandleListInstances(sp, cmd), GetInstanceCommand cmd => await HandleGetInstance(sp, cmd), CreateInstanceCommand cmd => await HandleCreateInstance(sp, cmd, user), MgmtDeployInstanceCommand cmd => await HandleDeployInstance(sp, cmd, user), MgmtEnableInstanceCommand cmd => await HandleEnableInstance(sp, cmd, user), MgmtDisableInstanceCommand cmd => await HandleDisableInstance(sp, cmd, user), MgmtDeleteInstanceCommand cmd => await HandleDeleteInstance(sp, cmd, user), SetConnectionBindingsCommand cmd => await HandleSetConnectionBindings(sp, cmd, user), // Sites ListSitesCommand => await HandleListSites(sp), GetSiteCommand cmd => await HandleGetSite(sp, cmd), CreateSiteCommand cmd => await HandleCreateSite(sp, cmd), UpdateSiteCommand cmd => await HandleUpdateSite(sp, cmd), DeleteSiteCommand cmd => await HandleDeleteSite(sp, cmd), ListAreasCommand cmd => await HandleListAreas(sp, cmd), CreateAreaCommand cmd => await HandleCreateArea(sp, cmd), DeleteAreaCommand cmd => await HandleDeleteArea(sp, cmd), UpdateAreaCommand cmd => await HandleUpdateArea(sp, cmd), // Data Connections ListDataConnectionsCommand => await HandleListDataConnections(sp), GetDataConnectionCommand cmd => await HandleGetDataConnection(sp, cmd), CreateDataConnectionCommand cmd => await HandleCreateDataConnection(sp, cmd), UpdateDataConnectionCommand cmd => await HandleUpdateDataConnection(sp, cmd), DeleteDataConnectionCommand cmd => await HandleDeleteDataConnection(sp, cmd), AssignDataConnectionToSiteCommand cmd => await HandleAssignDataConnectionToSite(sp, cmd), UnassignDataConnectionFromSiteCommand cmd => await HandleUnassignDataConnectionFromSite(sp, cmd), // External Systems ListExternalSystemsCommand => await HandleListExternalSystems(sp), GetExternalSystemCommand cmd => await HandleGetExternalSystem(sp, cmd), CreateExternalSystemCommand cmd => await HandleCreateExternalSystem(sp, cmd), UpdateExternalSystemCommand cmd => await HandleUpdateExternalSystem(sp, cmd), DeleteExternalSystemCommand cmd => await HandleDeleteExternalSystem(sp, cmd), ListExternalSystemMethodsCommand cmd => await HandleListExternalSystemMethods(sp, cmd), GetExternalSystemMethodCommand cmd => await HandleGetExternalSystemMethod(sp, cmd), CreateExternalSystemMethodCommand cmd => await HandleCreateExternalSystemMethod(sp, cmd), UpdateExternalSystemMethodCommand cmd => await HandleUpdateExternalSystemMethod(sp, cmd), DeleteExternalSystemMethodCommand cmd => await HandleDeleteExternalSystemMethod(sp, cmd), // Notification Lists ListNotificationListsCommand => await HandleListNotificationLists(sp), GetNotificationListCommand cmd => await HandleGetNotificationList(sp, cmd), CreateNotificationListCommand cmd => await HandleCreateNotificationList(sp, cmd), UpdateNotificationListCommand cmd => await HandleUpdateNotificationList(sp, cmd), DeleteNotificationListCommand cmd => await HandleDeleteNotificationList(sp, cmd), ListSmtpConfigsCommand => await HandleListSmtpConfigs(sp), UpdateSmtpConfigCommand cmd => await HandleUpdateSmtpConfig(sp, cmd), // Shared Scripts ListSharedScriptsCommand => await HandleListSharedScripts(sp), GetSharedScriptCommand cmd => await HandleGetSharedScript(sp, cmd), CreateSharedScriptCommand cmd => await HandleCreateSharedScript(sp, cmd, user), UpdateSharedScriptCommand cmd => await HandleUpdateSharedScript(sp, cmd, user), DeleteSharedScriptCommand cmd => await HandleDeleteSharedScript(sp, cmd, user), // Database Connections (External System) ListDatabaseConnectionsCommand => await HandleListDatabaseConnections(sp), GetDatabaseConnectionCommand cmd => await HandleGetDatabaseConnection(sp, cmd), CreateDatabaseConnectionDefCommand cmd => await HandleCreateDatabaseConnection(sp, cmd), UpdateDatabaseConnectionDefCommand cmd => await HandleUpdateDatabaseConnection(sp, cmd), DeleteDatabaseConnectionDefCommand cmd => await HandleDeleteDatabaseConnection(sp, cmd), // Inbound API Methods ListApiMethodsCommand => await HandleListApiMethods(sp), GetApiMethodCommand cmd => await HandleGetApiMethod(sp, cmd), CreateApiMethodCommand cmd => await HandleCreateApiMethod(sp, cmd), UpdateApiMethodCommand cmd => await HandleUpdateApiMethod(sp, cmd), DeleteApiMethodCommand cmd => await HandleDeleteApiMethod(sp, cmd), // Security ListRoleMappingsCommand => await HandleListRoleMappings(sp), CreateRoleMappingCommand cmd => await HandleCreateRoleMapping(sp, cmd), UpdateRoleMappingCommand cmd => await HandleUpdateRoleMapping(sp, cmd), DeleteRoleMappingCommand cmd => await HandleDeleteRoleMapping(sp, cmd), ListApiKeysCommand => await HandleListApiKeys(sp), CreateApiKeyCommand cmd => await HandleCreateApiKey(sp, cmd), DeleteApiKeyCommand cmd => await HandleDeleteApiKey(sp, cmd), UpdateApiKeyCommand cmd => await HandleUpdateApiKey(sp, cmd), ListScopeRulesCommand cmd => await HandleListScopeRules(sp, cmd), AddScopeRuleCommand cmd => await HandleAddScopeRule(sp, cmd), DeleteScopeRuleCommand cmd => await HandleDeleteScopeRule(sp, cmd), // Deployments MgmtDeployArtifactsCommand cmd => await HandleDeployArtifacts(sp, cmd, user), QueryDeploymentsCommand cmd => await HandleQueryDeployments(sp, cmd), // Audit Log QueryAuditLogCommand cmd => await HandleQueryAuditLog(sp, cmd), // Health GetHealthSummaryCommand => HandleGetHealthSummary(sp), GetSiteHealthCommand cmd => HandleGetSiteHealth(sp, cmd), // Remote Queries QueryEventLogsCommand cmd => await HandleQueryEventLogs(sp, cmd), QueryParkedMessagesCommand cmd => await HandleQueryParkedMessages(sp, cmd), DebugSnapshotCommand cmd => await HandleDebugSnapshot(sp, cmd), _ => throw new NotSupportedException($"Unknown command type: {command.GetType().Name}") }; } // ======================================================================== // Template handlers // ======================================================================== private static async Task HandleListTemplates(IServiceProvider sp) { var repo = sp.GetRequiredService(); return await repo.GetAllTemplatesAsync(); } private static async Task HandleGetTemplate(IServiceProvider sp, GetTemplateCommand cmd) { var repo = sp.GetRequiredService(); return await repo.GetTemplateWithChildrenAsync(cmd.TemplateId); } private static async Task HandleCreateTemplate(IServiceProvider sp, CreateTemplateCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.CreateTemplateAsync(cmd.Name, cmd.Description, cmd.ParentTemplateId, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleUpdateTemplate(IServiceProvider sp, UpdateTemplateCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.UpdateTemplateAsync(cmd.TemplateId, cmd.Name, cmd.Description, cmd.ParentTemplateId, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleDeleteTemplate(IServiceProvider sp, DeleteTemplateCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.DeleteTemplateAsync(cmd.TemplateId, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleValidateTemplate(IServiceProvider sp, ValidateTemplateCommand cmd) { var svc = sp.GetRequiredService(); return await svc.DetectCollisionsAsync(cmd.TemplateId); } // ======================================================================== // Instance handlers // ======================================================================== private static async Task HandleListInstances(IServiceProvider sp, ListInstancesCommand cmd) { var repo = sp.GetRequiredService(); return await repo.GetInstancesFilteredAsync(cmd.SiteId, cmd.TemplateId, cmd.SearchTerm); } private static async Task HandleGetInstance(IServiceProvider sp, GetInstanceCommand cmd) { var repo = sp.GetRequiredService(); return await repo.GetInstanceByIdAsync(cmd.InstanceId); } private static async Task HandleCreateInstance(IServiceProvider sp, CreateInstanceCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.CreateInstanceAsync(cmd.UniqueName, cmd.TemplateId, cmd.SiteId, cmd.AreaId, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleDeployInstance(IServiceProvider sp, MgmtDeployInstanceCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.DeployInstanceAsync(cmd.InstanceId, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleEnableInstance(IServiceProvider sp, MgmtEnableInstanceCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.EnableInstanceAsync(cmd.InstanceId, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleDisableInstance(IServiceProvider sp, MgmtDisableInstanceCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.DisableInstanceAsync(cmd.InstanceId, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleDeleteInstance(IServiceProvider sp, MgmtDeleteInstanceCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.DeleteInstanceAsync(cmd.InstanceId, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleSetConnectionBindings(IServiceProvider sp, SetConnectionBindingsCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.SetConnectionBindingsAsync(cmd.InstanceId, cmd.Bindings, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } // ======================================================================== // Site handlers // ======================================================================== private static async Task HandleListSites(IServiceProvider sp) { var repo = sp.GetRequiredService(); return await repo.GetAllSitesAsync(); } private static async Task HandleGetSite(IServiceProvider sp, GetSiteCommand cmd) { var repo = sp.GetRequiredService(); return await repo.GetSiteByIdAsync(cmd.SiteId); } private static async Task HandleCreateSite(IServiceProvider sp, CreateSiteCommand cmd) { var repo = sp.GetRequiredService(); var site = new Site(cmd.Name, cmd.SiteIdentifier) { Description = cmd.Description, NodeAAddress = cmd.NodeAAddress, NodeBAddress = cmd.NodeBAddress }; await repo.AddSiteAsync(site); await repo.SaveChangesAsync(); var commService = sp.GetService(); commService?.RefreshSiteAddresses(); return site; } private static async Task HandleUpdateSite(IServiceProvider sp, UpdateSiteCommand cmd) { var repo = sp.GetRequiredService(); var site = await repo.GetSiteByIdAsync(cmd.SiteId) ?? throw new InvalidOperationException($"Site with ID {cmd.SiteId} not found."); site.Name = cmd.Name; site.Description = cmd.Description; site.NodeAAddress = cmd.NodeAAddress; site.NodeBAddress = cmd.NodeBAddress; await repo.UpdateSiteAsync(site); await repo.SaveChangesAsync(); var commService = sp.GetService(); commService?.RefreshSiteAddresses(); return site; } private static async Task HandleDeleteSite(IServiceProvider sp, DeleteSiteCommand cmd) { var repo = sp.GetRequiredService(); // Check for instances referencing this site var instances = await repo.GetInstancesBySiteIdAsync(cmd.SiteId); if (instances.Count > 0) throw new InvalidOperationException( $"Cannot delete site {cmd.SiteId}: it has {instances.Count} instance(s)."); await repo.DeleteSiteAsync(cmd.SiteId); await repo.SaveChangesAsync(); var commService = sp.GetService(); commService?.RefreshSiteAddresses(); return true; } private static async Task HandleListAreas(IServiceProvider sp, ListAreasCommand cmd) { var repo = sp.GetRequiredService(); return await repo.GetAreaTreeBySiteIdAsync(cmd.SiteId); } private static async Task HandleCreateArea(IServiceProvider sp, CreateAreaCommand cmd) { var repo = sp.GetRequiredService(); var area = new Area(cmd.Name) { SiteId = cmd.SiteId, ParentAreaId = cmd.ParentAreaId }; await repo.AddAreaAsync(area); await repo.SaveChangesAsync(); return area; } private static async Task HandleDeleteArea(IServiceProvider sp, DeleteAreaCommand cmd) { var repo = sp.GetRequiredService(); await repo.DeleteAreaAsync(cmd.AreaId); await repo.SaveChangesAsync(); return true; } // ======================================================================== // Data Connection handlers // ======================================================================== private static async Task HandleListDataConnections(IServiceProvider sp) { var repo = sp.GetRequiredService(); return await repo.GetAllDataConnectionsAsync(); } private static async Task HandleGetDataConnection(IServiceProvider sp, GetDataConnectionCommand cmd) { var repo = sp.GetRequiredService(); return await repo.GetDataConnectionByIdAsync(cmd.DataConnectionId); } private static async Task HandleCreateDataConnection(IServiceProvider sp, CreateDataConnectionCommand cmd) { var repo = sp.GetRequiredService(); var conn = new DataConnection(cmd.Name, cmd.Protocol) { Configuration = cmd.Configuration }; await repo.AddDataConnectionAsync(conn); await repo.SaveChangesAsync(); return conn; } private static async Task HandleUpdateDataConnection(IServiceProvider sp, UpdateDataConnectionCommand cmd) { var repo = sp.GetRequiredService(); var conn = await repo.GetDataConnectionByIdAsync(cmd.DataConnectionId) ?? throw new InvalidOperationException($"DataConnection with ID {cmd.DataConnectionId} not found."); conn.Name = cmd.Name; conn.Protocol = cmd.Protocol; conn.Configuration = cmd.Configuration; await repo.UpdateDataConnectionAsync(conn); await repo.SaveChangesAsync(); return conn; } private static async Task HandleDeleteDataConnection(IServiceProvider sp, DeleteDataConnectionCommand cmd) { var repo = sp.GetRequiredService(); await repo.DeleteDataConnectionAsync(cmd.DataConnectionId); await repo.SaveChangesAsync(); return true; } private static async Task HandleAssignDataConnectionToSite(IServiceProvider sp, AssignDataConnectionToSiteCommand cmd) { var repo = sp.GetRequiredService(); var assignment = new SiteDataConnectionAssignment { SiteId = cmd.SiteId, DataConnectionId = cmd.DataConnectionId }; await repo.AddSiteDataConnectionAssignmentAsync(assignment); await repo.SaveChangesAsync(); return assignment; } private static async Task HandleUnassignDataConnectionFromSite(IServiceProvider sp, UnassignDataConnectionFromSiteCommand cmd) { var repo = sp.GetRequiredService(); await repo.DeleteSiteDataConnectionAssignmentAsync(cmd.AssignmentId); await repo.SaveChangesAsync(); return true; } // ======================================================================== // External System handlers // ======================================================================== private static async Task HandleListExternalSystems(IServiceProvider sp) { var repo = sp.GetRequiredService(); return await repo.GetAllExternalSystemsAsync(); } private static async Task HandleGetExternalSystem(IServiceProvider sp, GetExternalSystemCommand cmd) { var repo = sp.GetRequiredService(); return await repo.GetExternalSystemByIdAsync(cmd.ExternalSystemId); } private static async Task HandleCreateExternalSystem(IServiceProvider sp, CreateExternalSystemCommand cmd) { var repo = sp.GetRequiredService(); var def = new ExternalSystemDefinition(cmd.Name, cmd.EndpointUrl, cmd.AuthType) { AuthConfiguration = cmd.AuthConfiguration }; await repo.AddExternalSystemAsync(def); await repo.SaveChangesAsync(); return def; } private static async Task HandleUpdateExternalSystem(IServiceProvider sp, UpdateExternalSystemCommand cmd) { var repo = sp.GetRequiredService(); var def = await repo.GetExternalSystemByIdAsync(cmd.ExternalSystemId) ?? throw new InvalidOperationException($"ExternalSystem with ID {cmd.ExternalSystemId} not found."); def.Name = cmd.Name; def.EndpointUrl = cmd.EndpointUrl; def.AuthType = cmd.AuthType; def.AuthConfiguration = cmd.AuthConfiguration; await repo.UpdateExternalSystemAsync(def); await repo.SaveChangesAsync(); return def; } private static async Task HandleDeleteExternalSystem(IServiceProvider sp, DeleteExternalSystemCommand cmd) { var repo = sp.GetRequiredService(); await repo.DeleteExternalSystemAsync(cmd.ExternalSystemId); await repo.SaveChangesAsync(); return true; } private static async Task HandleListExternalSystemMethods(IServiceProvider sp, ListExternalSystemMethodsCommand cmd) { var repo = sp.GetRequiredService(); return await repo.GetMethodsByExternalSystemIdAsync(cmd.ExternalSystemId); } private static async Task HandleGetExternalSystemMethod(IServiceProvider sp, GetExternalSystemMethodCommand cmd) { var repo = sp.GetRequiredService(); return await repo.GetExternalSystemMethodByIdAsync(cmd.MethodId); } private static async Task HandleCreateExternalSystemMethod(IServiceProvider sp, CreateExternalSystemMethodCommand cmd) { var repo = sp.GetRequiredService(); var method = new ExternalSystemMethod(cmd.Name, cmd.HttpMethod, cmd.Path) { ExternalSystemDefinitionId = cmd.ExternalSystemId, ParameterDefinitions = cmd.ParameterDefinitions, ReturnDefinition = cmd.ReturnDefinition }; await repo.AddExternalSystemMethodAsync(method); await repo.SaveChangesAsync(); return method; } private static async Task HandleUpdateExternalSystemMethod(IServiceProvider sp, UpdateExternalSystemMethodCommand cmd) { var repo = sp.GetRequiredService(); var method = await repo.GetExternalSystemMethodByIdAsync(cmd.MethodId) ?? throw new InvalidOperationException($"ExternalSystemMethod with ID {cmd.MethodId} not found."); if (cmd.Name != null) method.Name = cmd.Name; if (cmd.HttpMethod != null) method.HttpMethod = cmd.HttpMethod; if (cmd.Path != null) method.Path = cmd.Path; if (cmd.ParameterDefinitions != null) method.ParameterDefinitions = cmd.ParameterDefinitions; if (cmd.ReturnDefinition != null) method.ReturnDefinition = cmd.ReturnDefinition; await repo.UpdateExternalSystemMethodAsync(method); await repo.SaveChangesAsync(); return method; } private static async Task HandleDeleteExternalSystemMethod(IServiceProvider sp, DeleteExternalSystemMethodCommand cmd) { var repo = sp.GetRequiredService(); await repo.DeleteExternalSystemMethodAsync(cmd.MethodId); await repo.SaveChangesAsync(); return true; } // ======================================================================== // Notification handlers // ======================================================================== private static async Task HandleListNotificationLists(IServiceProvider sp) { var repo = sp.GetRequiredService(); return await repo.GetAllNotificationListsAsync(); } private static async Task HandleGetNotificationList(IServiceProvider sp, GetNotificationListCommand cmd) { var repo = sp.GetRequiredService(); return await repo.GetNotificationListByIdAsync(cmd.NotificationListId); } private static async Task HandleCreateNotificationList(IServiceProvider sp, CreateNotificationListCommand cmd) { var repo = sp.GetRequiredService(); var list = new NotificationList(cmd.Name); foreach (var email in cmd.RecipientEmails) { list.Recipients.Add(new NotificationRecipient(email, email)); } await repo.AddNotificationListAsync(list); await repo.SaveChangesAsync(); return list; } private static async Task HandleUpdateNotificationList(IServiceProvider sp, UpdateNotificationListCommand cmd) { var repo = sp.GetRequiredService(); var list = await repo.GetNotificationListByIdAsync(cmd.NotificationListId) ?? throw new InvalidOperationException($"NotificationList with ID {cmd.NotificationListId} not found."); list.Name = cmd.Name; // Remove existing recipients and re-add var existingRecipients = await repo.GetRecipientsByListIdAsync(cmd.NotificationListId); foreach (var r in existingRecipients) { await repo.DeleteRecipientAsync(r.Id); } foreach (var email in cmd.RecipientEmails) { await repo.AddRecipientAsync(new NotificationRecipient(email, email) { NotificationListId = cmd.NotificationListId }); } await repo.UpdateNotificationListAsync(list); await repo.SaveChangesAsync(); return list; } private static async Task HandleDeleteNotificationList(IServiceProvider sp, DeleteNotificationListCommand cmd) { var repo = sp.GetRequiredService(); await repo.DeleteNotificationListAsync(cmd.NotificationListId); await repo.SaveChangesAsync(); return true; } private static async Task HandleListSmtpConfigs(IServiceProvider sp) { var repo = sp.GetRequiredService(); return await repo.GetAllSmtpConfigurationsAsync(); } private static async Task HandleUpdateSmtpConfig(IServiceProvider sp, UpdateSmtpConfigCommand cmd) { var repo = sp.GetRequiredService(); var config = await repo.GetSmtpConfigurationByIdAsync(cmd.SmtpConfigId) ?? throw new InvalidOperationException($"SmtpConfiguration with ID {cmd.SmtpConfigId} not found."); config.Host = cmd.Server; config.Port = cmd.Port; config.AuthType = cmd.AuthMode; config.FromAddress = cmd.FromAddress; await repo.UpdateSmtpConfigurationAsync(config); await repo.SaveChangesAsync(); return config; } // ======================================================================== // Security handlers // ======================================================================== private static async Task HandleListRoleMappings(IServiceProvider sp) { var repo = sp.GetRequiredService(); return await repo.GetAllMappingsAsync(); } private static async Task HandleCreateRoleMapping(IServiceProvider sp, CreateRoleMappingCommand cmd) { var repo = sp.GetRequiredService(); var mapping = new LdapGroupMapping(cmd.LdapGroupName, cmd.Role); await repo.AddMappingAsync(mapping); await repo.SaveChangesAsync(); return mapping; } private static async Task HandleUpdateRoleMapping(IServiceProvider sp, UpdateRoleMappingCommand cmd) { var repo = sp.GetRequiredService(); var mapping = await repo.GetMappingByIdAsync(cmd.MappingId) ?? throw new InvalidOperationException($"RoleMapping with ID {cmd.MappingId} not found."); mapping.LdapGroupName = cmd.LdapGroupName; mapping.Role = cmd.Role; await repo.UpdateMappingAsync(mapping); await repo.SaveChangesAsync(); return mapping; } private static async Task HandleDeleteRoleMapping(IServiceProvider sp, DeleteRoleMappingCommand cmd) { var repo = sp.GetRequiredService(); await repo.DeleteMappingAsync(cmd.MappingId); await repo.SaveChangesAsync(); return true; } private static async Task HandleListApiKeys(IServiceProvider sp) { var repo = sp.GetRequiredService(); return await repo.GetAllApiKeysAsync(); } private static async Task HandleCreateApiKey(IServiceProvider sp, CreateApiKeyCommand cmd) { var repo = sp.GetRequiredService(); var keyValue = Convert.ToBase64String(RandomNumberGenerator.GetBytes(32)); var apiKey = new ApiKey(cmd.Name, keyValue) { IsEnabled = true }; await repo.AddApiKeyAsync(apiKey); await repo.SaveChangesAsync(); return apiKey; } private static async Task HandleDeleteApiKey(IServiceProvider sp, DeleteApiKeyCommand cmd) { var repo = sp.GetRequiredService(); await repo.DeleteApiKeyAsync(cmd.ApiKeyId); await repo.SaveChangesAsync(); return true; } // ======================================================================== // Deployment handlers // ======================================================================== private static async Task HandleDeployArtifacts(IServiceProvider sp, MgmtDeployArtifactsCommand cmd, string user) { var svc = sp.GetRequiredService(); var command = await svc.BuildDeployArtifactsCommandAsync(); var result = await svc.DeployToAllSitesAsync(command, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleQueryDeployments(IServiceProvider sp, QueryDeploymentsCommand cmd) { var repo = sp.GetRequiredService(); if (cmd.InstanceId.HasValue) return await repo.GetDeploymentsByInstanceIdAsync(cmd.InstanceId.Value); return await repo.GetAllDeploymentRecordsAsync(); } // ======================================================================== // Audit Log handler // ======================================================================== private static async Task HandleQueryAuditLog(IServiceProvider sp, QueryAuditLogCommand cmd) { var repo = sp.GetRequiredService(); return await repo.GetAuditLogEntriesAsync( cmd.User, cmd.EntityType, cmd.Action, cmd.From, cmd.To, page: cmd.Page, pageSize: cmd.PageSize); } // ======================================================================== // Health handlers // ======================================================================== private static object? HandleGetHealthSummary(IServiceProvider sp) { var aggregator = sp.GetRequiredService(); return aggregator.GetAllSiteStates(); } private static object? HandleGetSiteHealth(IServiceProvider sp, GetSiteHealthCommand cmd) { var aggregator = sp.GetRequiredService(); return aggregator.GetSiteState(cmd.SiteIdentifier); } // ======================================================================== // Template member handlers // ======================================================================== private static async Task HandleAddAttribute(IServiceProvider sp, AddTemplateAttributeCommand cmd, string user) { var svc = sp.GetRequiredService(); var attr = new TemplateAttribute(cmd.Name) { DataType = Enum.Parse(cmd.DataType, ignoreCase: true), Value = cmd.Value, Description = cmd.Description, DataSourceReference = cmd.DataSourceReference, IsLocked = cmd.IsLocked }; var result = await svc.AddAttributeAsync(cmd.TemplateId, attr, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleUpdateAttribute(IServiceProvider sp, UpdateTemplateAttributeCommand cmd, string user) { var svc = sp.GetRequiredService(); var attr = new TemplateAttribute(cmd.Name) { DataType = Enum.Parse(cmd.DataType, ignoreCase: true), Value = cmd.Value, Description = cmd.Description, DataSourceReference = cmd.DataSourceReference, IsLocked = cmd.IsLocked }; var result = await svc.UpdateAttributeAsync(cmd.AttributeId, attr, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleDeleteAttribute(IServiceProvider sp, DeleteTemplateAttributeCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.DeleteAttributeAsync(cmd.AttributeId, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleAddAlarm(IServiceProvider sp, AddTemplateAlarmCommand cmd, string user) { var svc = sp.GetRequiredService(); var alarm = new TemplateAlarm(cmd.Name) { TriggerType = Enum.Parse(cmd.TriggerType, ignoreCase: true), PriorityLevel = cmd.PriorityLevel, Description = cmd.Description, TriggerConfiguration = cmd.TriggerConfiguration, IsLocked = cmd.IsLocked }; var result = await svc.AddAlarmAsync(cmd.TemplateId, alarm, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleUpdateAlarm(IServiceProvider sp, UpdateTemplateAlarmCommand cmd, string user) { var svc = sp.GetRequiredService(); var alarm = new TemplateAlarm(cmd.Name) { TriggerType = Enum.Parse(cmd.TriggerType, ignoreCase: true), PriorityLevel = cmd.PriorityLevel, Description = cmd.Description, TriggerConfiguration = cmd.TriggerConfiguration, IsLocked = cmd.IsLocked }; var result = await svc.UpdateAlarmAsync(cmd.AlarmId, alarm, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleDeleteAlarm(IServiceProvider sp, DeleteTemplateAlarmCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.DeleteAlarmAsync(cmd.AlarmId, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleAddScript(IServiceProvider sp, AddTemplateScriptCommand cmd, string user) { var svc = sp.GetRequiredService(); var script = new TemplateScript(cmd.Name, cmd.Code) { TriggerType = cmd.TriggerType, TriggerConfiguration = cmd.TriggerConfiguration, IsLocked = cmd.IsLocked, ParameterDefinitions = cmd.ParameterDefinitions, ReturnDefinition = cmd.ReturnDefinition }; var result = await svc.AddScriptAsync(cmd.TemplateId, script, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleUpdateScript(IServiceProvider sp, UpdateTemplateScriptCommand cmd, string user) { var svc = sp.GetRequiredService(); var script = new TemplateScript(cmd.Name, cmd.Code) { TriggerType = cmd.TriggerType, TriggerConfiguration = cmd.TriggerConfiguration, IsLocked = cmd.IsLocked, ParameterDefinitions = cmd.ParameterDefinitions, ReturnDefinition = cmd.ReturnDefinition }; var result = await svc.UpdateScriptAsync(cmd.ScriptId, script, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleDeleteScript(IServiceProvider sp, DeleteTemplateScriptCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.DeleteScriptAsync(cmd.ScriptId, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleAddComposition(IServiceProvider sp, AddTemplateCompositionCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.AddCompositionAsync(cmd.TemplateId, cmd.ComposedTemplateId, cmd.InstanceName, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleDeleteComposition(IServiceProvider sp, DeleteTemplateCompositionCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.DeleteCompositionAsync(cmd.CompositionId, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } // ======================================================================== // Shared Script handlers // ======================================================================== private static async Task HandleListSharedScripts(IServiceProvider sp) { var svc = sp.GetRequiredService(); return await svc.GetAllSharedScriptsAsync(); } private static async Task HandleGetSharedScript(IServiceProvider sp, GetSharedScriptCommand cmd) { var svc = sp.GetRequiredService(); return await svc.GetSharedScriptByIdAsync(cmd.SharedScriptId); } private static async Task HandleCreateSharedScript(IServiceProvider sp, CreateSharedScriptCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.CreateSharedScriptAsync(cmd.Name, cmd.Code, cmd.ParameterDefinitions, cmd.ReturnDefinition, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleUpdateSharedScript(IServiceProvider sp, UpdateSharedScriptCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.UpdateSharedScriptAsync(cmd.SharedScriptId, cmd.Code, cmd.ParameterDefinitions, cmd.ReturnDefinition, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } private static async Task HandleDeleteSharedScript(IServiceProvider sp, DeleteSharedScriptCommand cmd, string user) { var svc = sp.GetRequiredService(); var result = await svc.DeleteSharedScriptAsync(cmd.SharedScriptId, user); return result.IsSuccess ? result.Value : throw new InvalidOperationException(result.Error); } // ======================================================================== // Database Connection Definition handlers // ======================================================================== private static async Task HandleListDatabaseConnections(IServiceProvider sp) { var repo = sp.GetRequiredService(); return await repo.GetAllDatabaseConnectionsAsync(); } private static async Task HandleGetDatabaseConnection(IServiceProvider sp, GetDatabaseConnectionCommand cmd) { var repo = sp.GetRequiredService(); return await repo.GetDatabaseConnectionByIdAsync(cmd.DatabaseConnectionId); } private static async Task HandleCreateDatabaseConnection(IServiceProvider sp, CreateDatabaseConnectionDefCommand cmd) { var repo = sp.GetRequiredService(); var def = new DatabaseConnectionDefinition(cmd.Name, cmd.ConnectionString); await repo.AddDatabaseConnectionAsync(def); await repo.SaveChangesAsync(); return def; } private static async Task HandleUpdateDatabaseConnection(IServiceProvider sp, UpdateDatabaseConnectionDefCommand cmd) { var repo = sp.GetRequiredService(); var def = await repo.GetDatabaseConnectionByIdAsync(cmd.DatabaseConnectionId) ?? throw new InvalidOperationException($"DatabaseConnection with ID {cmd.DatabaseConnectionId} not found."); def.Name = cmd.Name; def.ConnectionString = cmd.ConnectionString; await repo.UpdateDatabaseConnectionAsync(def); await repo.SaveChangesAsync(); return def; } private static async Task HandleDeleteDatabaseConnection(IServiceProvider sp, DeleteDatabaseConnectionDefCommand cmd) { var repo = sp.GetRequiredService(); await repo.DeleteDatabaseConnectionAsync(cmd.DatabaseConnectionId); await repo.SaveChangesAsync(); return true; } // ======================================================================== // Inbound API Method handlers // ======================================================================== private static async Task HandleListApiMethods(IServiceProvider sp) { var repo = sp.GetRequiredService(); return await repo.GetAllApiMethodsAsync(); } private static async Task HandleGetApiMethod(IServiceProvider sp, GetApiMethodCommand cmd) { var repo = sp.GetRequiredService(); return await repo.GetApiMethodByIdAsync(cmd.ApiMethodId); } private static async Task HandleCreateApiMethod(IServiceProvider sp, CreateApiMethodCommand cmd) { var repo = sp.GetRequiredService(); var method = new ApiMethod(cmd.Name, cmd.Script) { TimeoutSeconds = cmd.TimeoutSeconds, ParameterDefinitions = cmd.ParameterDefinitions, ReturnDefinition = cmd.ReturnDefinition }; await repo.AddApiMethodAsync(method); await repo.SaveChangesAsync(); // Hot-register the compiled script so it's immediately available sp.GetService()?.CompileAndRegister(method); return method; } private static async Task HandleUpdateApiMethod(IServiceProvider sp, UpdateApiMethodCommand cmd) { var repo = sp.GetRequiredService(); var method = await repo.GetApiMethodByIdAsync(cmd.ApiMethodId) ?? throw new InvalidOperationException($"ApiMethod with ID {cmd.ApiMethodId} not found."); method.Script = cmd.Script; method.TimeoutSeconds = cmd.TimeoutSeconds; method.ParameterDefinitions = cmd.ParameterDefinitions; method.ReturnDefinition = cmd.ReturnDefinition; await repo.UpdateApiMethodAsync(method); await repo.SaveChangesAsync(); // Re-compile and register the updated script sp.GetService()?.CompileAndRegister(method); return method; } private static async Task HandleDeleteApiMethod(IServiceProvider sp, DeleteApiMethodCommand cmd) { var repo = sp.GetRequiredService(); var method = await repo.GetApiMethodByIdAsync(cmd.ApiMethodId); await repo.DeleteApiMethodAsync(cmd.ApiMethodId); await repo.SaveChangesAsync(); // Remove the compiled script handler if (method != null) sp.GetService()?.RemoveHandler(method.Name); return true; } // ======================================================================== // Additional Security handlers (API key update, scope rules) // ======================================================================== private static async Task HandleUpdateApiKey(IServiceProvider sp, UpdateApiKeyCommand cmd) { var repo = sp.GetRequiredService(); var key = await repo.GetApiKeyByIdAsync(cmd.ApiKeyId) ?? throw new InvalidOperationException($"ApiKey with ID {cmd.ApiKeyId} not found."); key.IsEnabled = cmd.IsEnabled; await repo.UpdateApiKeyAsync(key); await repo.SaveChangesAsync(); return key; } private static async Task HandleListScopeRules(IServiceProvider sp, ListScopeRulesCommand cmd) { var repo = sp.GetRequiredService(); return await repo.GetScopeRulesForMappingAsync(cmd.MappingId); } private static async Task HandleAddScopeRule(IServiceProvider sp, AddScopeRuleCommand cmd) { var repo = sp.GetRequiredService(); var rule = new SiteScopeRule { LdapGroupMappingId = cmd.MappingId, SiteId = cmd.SiteId }; await repo.AddScopeRuleAsync(rule); await repo.SaveChangesAsync(); return rule; } private static async Task HandleDeleteScopeRule(IServiceProvider sp, DeleteScopeRuleCommand cmd) { var repo = sp.GetRequiredService(); await repo.DeleteScopeRuleAsync(cmd.ScopeRuleId); await repo.SaveChangesAsync(); return true; } // ======================================================================== // Area update handler // ======================================================================== private static async Task HandleUpdateArea(IServiceProvider sp, UpdateAreaCommand cmd) { var repo = sp.GetRequiredService(); var area = await repo.GetAreaByIdAsync(cmd.AreaId) ?? throw new InvalidOperationException($"Area with ID {cmd.AreaId} not found."); area.Name = cmd.Name; await repo.UpdateAreaAsync(area); await repo.SaveChangesAsync(); return area; } // ======================================================================== // Remote Query handlers // ======================================================================== private static async Task HandleQueryEventLogs(IServiceProvider sp, QueryEventLogsCommand cmd) { var commService = sp.GetRequiredService(); var request = new EventLogQueryRequest( Guid.NewGuid().ToString("N"), cmd.SiteIdentifier, cmd.From, cmd.To, cmd.EventType, cmd.Severity, null, // InstanceId cmd.Keyword, null, // ContinuationToken cmd.PageSize, DateTimeOffset.UtcNow); return await commService.QueryEventLogsAsync(cmd.SiteIdentifier, request); } private static async Task HandleQueryParkedMessages(IServiceProvider sp, QueryParkedMessagesCommand cmd) { var commService = sp.GetRequiredService(); var request = new ParkedMessageQueryRequest( Guid.NewGuid().ToString("N"), cmd.SiteIdentifier, cmd.Page, cmd.PageSize, DateTimeOffset.UtcNow); return await commService.QueryParkedMessagesAsync(cmd.SiteIdentifier, request); } private static async Task HandleDebugSnapshot(IServiceProvider sp, DebugSnapshotCommand cmd) { var instanceRepo = sp.GetRequiredService(); var instance = await instanceRepo.GetInstanceByIdAsync(cmd.InstanceId) ?? throw new InvalidOperationException($"Instance {cmd.InstanceId} not found."); var siteRepo = sp.GetRequiredService(); var site = await siteRepo.GetSiteByIdAsync(instance.SiteId) ?? throw new InvalidOperationException($"Site {instance.SiteId} not found."); var commService = sp.GetRequiredService(); var request = new DebugSnapshotRequest(instance.UniqueName, Guid.NewGuid().ToString("N")); return await commService.RequestDebugSnapshotAsync(site.SiteIdentifier, request); } }