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));
        }
Пример #2
0
        public async Task <IIdentifierResult> HandleAsync(ICreateSale command, ICorrelationContext context)
        {
            await Task.WhenAll(
                _agVerifier.AssertExists(command.AccountingGroupId),
                _userVerifier.AssertExists(command.UserId),
                _posVerifier.AssertExists(command.PointOfSaleId),
                _amVerifier.AssertExists(command.AuthenticationMeansId),
                _productVerifier.AssertExists(command.ProductId),
                _offerVerifier.AssertExists(command.OfferId)
                );

            var sale = new Domain.Sale(command.Id, command.Cost, command.Quantity, command.AccountingGroupId, command.UserId, command.AuthenticationMeansId, command.PointOfSaleId, command.ProductId, command.OfferId);
            var initialSaleStateChange = new Domain.SaleStateChange(Guid.NewGuid(), "Created", default(SaleState), command.PointOfSaleId, null);

            sale.AddStateChange(initialSaleStateChange);

            await _salesRepository.AddAsync(sale);

            try
            {
                await _salesRepository.SaveChanges();
            }
            catch (EntityAlreadyExistsException)
            {
                throw new BaristaException("sale_already_exists", $"A sale with the ID '{command.Id}' already exists.");
            }

            await _busPublisher.Publish(new SaleCreated(sale));

            await _busPublisher.Publish(new SaleStateChangeCreated(initialSaleStateChange.Id, sale.Id,
                                                                   initialSaleStateChange.Created, initialSaleStateChange.Reason, initialSaleStateChange.State.ToString(),
                                                                   initialSaleStateChange.CausedByPointOfSaleId, initialSaleStateChange.CausedByUserId));

            return(new IdentifierResult(sale.Id));
        }
Пример #3
0
        public async Task <IOperationResult> HandleAsync(IUpdatePointOfSale command, ICorrelationContext context)
        {
            var pos = await _repository.GetAsync(command.Id);

            if (pos is null)
            {
                throw new BaristaException("point_of_sale_not_found", $"Could not find point of sale with ID '{command.Id}'");
            }

            await Task.WhenAll(
                _agVerifier.AssertExists(command.ParentAccountingGroupId),
                command.SaleStrategyId != null?_ssVerifier.AssertExists(command.SaleStrategyId.Value) : Task.CompletedTask
                );

            pos.SetDisplayName(command.DisplayName);
            pos.SetSaleStrategyId(command.SaleStrategyId);
            pos.SetParentAccountingGroupId(command.ParentAccountingGroupId);
            pos.SetFeatures(command.Features ?? new string[0]);
            await _repository.UpdateAsync(pos);

            await _repository.SaveChanges();

            await _busPublisher.Publish(new PointOfSaleUpdated(pos.Id, pos.DisplayName, pos.ParentAccountingGroupId, pos.SaleStrategyId));

            return(OperationResult.Ok());
        }
Пример #4
0
        public async Task <IOperationResult> HandleAsync(IUpdateSale command, ICorrelationContext context)
        {
            var sale = await _salesRepository.GetAsync(command.Id);

            if (sale is null)
            {
                throw new BaristaException("sale_not_found", $"Sale with ID '{command.Id}' was not found");
            }

            await Task.WhenAll(
                _agVerifier.AssertExists(command.AccountingGroupId),
                _userVerifier.AssertExists(command.UserId),
                _posVerifier.AssertExists(command.PointOfSaleId),
                _amVerifier.AssertExists(command.AuthenticationMeansId),
                _productVerifier.AssertExists(command.ProductId),
                _offerVerifier.AssertExists(command.OfferId)
                );

            sale.SetAuthenticationMeansId(command.AuthenticationMeansId);
            sale.SetAccountingGroupId(command.AccountingGroupId);
            sale.SetCost(command.Cost);
            sale.SetPointOfSaleId(command.PointOfSaleId);
            sale.SetQuantity(command.Quantity);
            sale.SetUserId(command.UserId);
            sale.SetProductId(command.ProductId);
            sale.SetOfferId(command.OfferId);

            await _salesRepository.UpdateAsync(sale);

            await _salesRepository.SaveChanges();

            await _busPublisher.Publish(new SaleUpdated(sale));

            return(OperationResult.Ok());
        }
Пример #5
0
        public async Task <IIdentifierResult> HandleAsync(ICreatePointOfSale command, ICorrelationContext context)
        {
            await Task.WhenAll(
                _agVerifier.AssertExists(command.ParentAccountingGroupId),
                command.SaleStrategyId != null?_ssVerifier.AssertExists(command.SaleStrategyId.Value) : Task.CompletedTask
                );

            var pointOfSale = new Domain.PointOfSale(command.Id, command.DisplayName, command.ParentAccountingGroupId, command.SaleStrategyId, command.Features ?? new string[0]);
            await _posRepository.AddAsync(pointOfSale);

            try
            {
                await _posRepository.SaveChanges();
            }
            catch (EntityAlreadyExistsException)
            {
                throw new BaristaException("point_of_sale_already_exists", $"A point of sale with the ID '{command.Id}' already exists.");
            }


            await _busPublisher.Publish(new PointOfSaleCreated(pointOfSale.Id, pointOfSale.DisplayName, pointOfSale.ParentAccountingGroupId, pointOfSale.SaleStrategyId));

            return(new IdentifierResult(pointOfSale.Id));
        }