diff --git a/src/Application/Apps/EventHandlers/CreateInitialChannel.cs b/src/Application/Apps/EventHandlers/CreateInitialChannel.cs new file mode 100644 index 000000000..0c2ccc3a0 --- /dev/null +++ b/src/Application/Apps/EventHandlers/CreateInitialChannel.cs @@ -0,0 +1,45 @@ +using Hippo.Application.Channels.Commands; +using Hippo.Application.Common.Config; +using Hippo.Application.Common.Models; +using Hippo.Core.Entities; +using Hippo.Core.Enums; +using Hippo.Core.Events; +using MediatR; +using Microsoft.Extensions.Logging; + +namespace Hippo.Application.Apps.EventHandlers; + +public class CreateInitialChannel : INotificationHandler>> +{ + private readonly ILogger _logger; + + private readonly IMediator _mediator; + + private readonly HippoConfig _config; + + public CreateInitialChannel(ILogger logger, IMediator mediator, HippoConfig config) + { + _logger = logger; + _mediator = mediator; + _config = config; + } + + public async Task Handle(DomainEventNotification> notification, CancellationToken cancellationToken) + { + var domainEvent = notification.DomainEvent; + var app = domainEvent.Entity; + + var command = new CreateChannelCommand + { + AppId = app.Id, + Name = "Production", + RevisionSelectionStrategy = ChannelRevisionSelectionStrategy.UseRangeRule, + RangeRule = "*", + Domain = $"{app.Name}.{_config.PlatformDomain}".Replace('_', '-').ToLower(), + }; + + await _mediator.Send(command, cancellationToken); + + _logger.LogInformation("Hippo Domain Event: {DomainEvent}", domainEvent.GetType().Name); + } +} diff --git a/src/Application/Channels/Commands/CreateChannelCommand.cs b/src/Application/Channels/Commands/CreateChannelCommand.cs index 3235e2d2a..176716130 100644 --- a/src/Application/Channels/Commands/CreateChannelCommand.cs +++ b/src/Application/Channels/Commands/CreateChannelCommand.cs @@ -49,13 +49,16 @@ public async Task Handle(CreateChannelCommand request, CancellationToken c .Where(a => a.Id == request.AppId) .SingleOrDefaultAsync(cancellationToken); _ = app ?? throw new NotFoundException(nameof(App), request.AppId); + var defaultDomain = $"{request.Name}.{app.Name}.{_config.PlatformDomain}" + .Replace('_', '-') + .ToLower(); var entity = new Channel { AppId = request.AppId, App = app, Name = request.Name, - Domain = (request.Domain is not null) ? request.Domain : $"{request.Name}.{app.Name}.{_config.PlatformDomain}", + Domain = (request.Domain is not null) ? request.Domain : defaultDomain, RevisionSelectionStrategy = request.RevisionSelectionStrategy, RangeRule = request.RangeRule, ActiveRevisionId = request.ActiveRevisionId,