public async Task <IParentChildIdentifierResult> HandleAsync(ICreateAccountingGroupUserAuthorization command, ICorrelationContext context) { if (command == null) { throw new ArgumentNullException(nameof(command)); } if (!Enum.TryParse <UserAuthorizationLevel>(command.Level, out var userAuthLevel)) { throw new BaristaException("invalid_user_auth_level", $"Could not parse user authorization level '{command.Level}'"); } await Task.WhenAll( _userVerifier.AssertExists(command.UserId), _agVerifier.AssertExists(command.AccountingGroupId) ); var authorizedUser = new Domain.UserAuthorization(command.AccountingGroupId, command.UserId, userAuthLevel); await _repository.AddAsync(authorizedUser); try { await _repository.SaveChanges(); } catch (EntityAlreadyExistsException) { throw new BaristaException("accounting_group_user_authorization_already_exists", $"User with ID '{command.AccountingGroupId}' is already an authorized user to accounting group with ID '{command.UserId}'."); } await _busPublisher.Publish(new UserAuthorizationCreated(command.AccountingGroupId, command.UserId, command.Level)); return(new ParentChildIdentifierResult(command.AccountingGroupId, authorizedUser.UserId)); }
public void UpdatesAuthorizationInRepository_RaisesIntegrationEvent() { var ua = new Domain.UserAuthorization(PointOfSaleId, UserId, UserAuthorizationLevel.Owner); var repository = new Mock <IUserAuthorizationRepository>(MockBehavior.Strict); repository.Setup(r => r.GetAsync(PointOfSaleId, UserId)).Returns(Task.FromResult(ua)).Verifiable(); repository.Setup(r => r.UpdateAsync(ua)).Returns(Task.CompletedTask).Verifiable(); repository.Setup(r => r.SaveChanges()).Returns(Task.CompletedTask).Verifiable(); var busPublisher = new Mock <IBusPublisher>(MockBehavior.Strict); busPublisher.Setup(p => p.Publish <IPointOfSaleUserAuthorizationUpdated>(It.Is <IPointOfSaleUserAuthorizationUpdated>(e => ValidateEquality(e)))).Returns(Task.CompletedTask).Verifiable(); var handler = new UpdateUserAuthorizationHandler(repository.Object, busPublisher.Object); handler.HandleAsync(Cmd, new Mock <ICorrelationContext>().Object).GetAwaiter().GetResult(); repository.Verify(); busPublisher.Verify(); ValidateEquality(ua); }
public async Task <IParentChildIdentifierResult> HandleAsync(ICreatePointOfSaleUserAuthorization command, ICorrelationContext context) { if (!Enum.TryParse <UserAuthorizationLevel>(command.Level, out var authLevel)) { throw new BaristaException("invalid_user_authorization_level", $"Unknown user authorization level '{command.Level}'"); } var authorizedUser = new Domain.UserAuthorization(command.PointOfSaleId, command.UserId, authLevel); await _repository.AddAsync(authorizedUser); try { await _repository.SaveChanges(); } catch (EntityAlreadyExistsException) { throw new BaristaException("point_of_sale_authorized_user_already_exists", $"The user with ID '{command.UserId}' is already an authorized user to point of sale with ID '{command.PointOfSaleId}'."); } await _busPublisher.Publish(new UserAuthorizationCreated(authorizedUser.PointOfSaleId, authorizedUser.UserId, authorizedUser.Level.ToString())); return(new ParentChildIdentifierResult(authorizedUser.PointOfSaleId, authorizedUser.UserId)); }