using DigitalOps.Authority.Services; using DigitalOps.Authority.Validators; using DigitalOps.CQRS.Services.Client; using DigitalOps.CQRS.Services.Client.Options; using DigitalOps.Dal; using DigitalOps.Dal.DbEntity; using DigitalOps.Dal.Validators; using FluentValidation; using OpenHarbor.CQRS.Abstractions; namespace DigitalOps.CQRS.Commands.Client; public class AddClientCommand { public long OrganizationId { get; set; } public required string Name { get; set; } } public class AddClientCommandHandler(ClientService clientService) : ICommandHandler { public async Task HandleAsync(AddClientCommand command, CancellationToken cancellationToken = default) { await clientService.AddClientAsync(new AddClientOptions { Name = command.Name, OrganizationId = command.OrganizationId }, cancellationToken); } } public class AddClientCommandValidator : AbstractValidator { public AddClientCommandValidator(MainDbContext dbContext, UserIdentityService userIdentityService) { RuleFor(command => command.Name) .NotEmpty() .MinimumLength(3); RuleFor(command => command.OrganizationId) .Cascade(CascadeMode.Stop) .NotEmpty() .SetValidator(new DbEntityExistValidator(dbContext)) .SetValidator(new HasAccessToOrganizationValidator(OrganizationRole.Member, dbContext, userIdentityService)); } }