public async Task <IIdentifierResult> HandleAsync(ICreateManualStockOperation command, ICorrelationContext context) { await Task.WhenAll( _stockItemVerifier.AssertExists(command.StockItemId), _userVerifier.AssertExists(command.CreatedByUserId) ); var manualStockOp = new Domain.ManualStockOperation(command.Id, command.StockItemId, command.Quantity, command.CreatedByUserId, command.Comment); await _repository.AddAsync(manualStockOp); try { await _repository.SaveChanges(); } catch (EntityAlreadyExistsException) { throw new BaristaException("manual_stock_operation_already_exists", $"A manual stock operation with the ID '{command.Id}' already exists."); } await _busPublisher.Publish(new ManualStockOperationCreated(manualStockOp.Id, manualStockOp.StockItemId, manualStockOp.Quantity, manualStockOp.CreatedByUserId, manualStockOp.Comment)); return(new IdentifierResult(manualStockOp.Id)); }
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)); }
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 async Task <IIdentifierResult> HandleAsync(ICreateSaleStateChange command, ICorrelationContext context) { var sale = await _salesRepository.GetAsync(command.ParentSaleId); if (sale is null) { throw new BaristaException("sale_not_found", $"Sale with ID '{command.ParentSaleId}' was not found"); } if (command.CausedByUserId != null) { await _userVerifier.AssertExists(command.CausedByUserId.Value); } if (command.CausedByPointOfSaleId != null) { await _posVerifier.AssertExists(command.CausedByPointOfSaleId.Value); } var saleStateChange = new Domain.SaleStateChange(command.Id, command.Reason, Enum.Parse <SaleState>(command.State), command.CausedByPointOfSaleId, command.CausedByUserId); sale.AddStateChange(saleStateChange); await _salesRepository.UpdateAsync(sale); await _salesRepository.SaveChanges(); await _busPublisher.Publish(new SaleStateChangeCreated(saleStateChange.Id, sale.Id, saleStateChange.Created, saleStateChange.Reason, saleStateChange.State.ToString(), saleStateChange.CausedByPointOfSaleId, saleStateChange.CausedByUserId)); return(new IdentifierResult(sale.Id)); }
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()); }
public async Task <IOperationResult> HandleAsync(IUpdateAssignmentToUser command, ICorrelationContext context) { var assignment = await _repository.GetAsync(command.Id); if (assignment is null) { throw new BaristaException("assignment_not_found", $"Assignment with ID '{command.Id}' was not found"); } if (!(assignment is Domain.AssignmentToUser assignmentToUser)) { throw new BaristaException("invalid_assignment_update_command", "This assignment does not assign means to an user and as such cannot be updated with this command."); } await Task.WhenAll( _meansVerifier.AssertExists(command.MeansId), _userVerifier.AssertExists(command.UserId) ); assignment.SetMeansId(command.MeansId); assignment.SetValidity(command.ValidSince, command.ValidUntil); assignmentToUser.SetIsShared(command.IsShared); assignmentToUser.SetUserId(command.UserId); await _repository.UpdateAsync(assignmentToUser); await _repository.SaveChanges(); try { await _exclVerifier.VerifyAssignmentExclusivity(command.MeansId); } catch (BaristaException) { await _repository.DeleteAsync(assignment); await _repository.SaveChanges(); throw; } await _busPublisher.Publish(new AssignmentToUserUpdated(assignmentToUser.Id, assignmentToUser.MeansId, assignmentToUser.ValidSince, assignmentToUser.ValidUntil, assignmentToUser.UserId, assignmentToUser.IsShared)); return(OperationResult.Ok()); }
public async Task <IIdentifierResult> HandleAsync(ICreatePayment command, ICorrelationContext context) { await _userVerifier.AssertExists(command.UserId); var payment = new Domain.Payment(command.Id, command.Amount, command.UserId, command.Source, command.ExternalId); await _paymentsRepository.AddAsync(payment); try { await _paymentsRepository.SaveChanges(); } catch (EntityAlreadyExistsException) { throw new BaristaException("payment_already_exists", $"A payment with the ID '{command.Id}' already exists."); } await _busPublisher.Publish(new PaymentCreated(payment)); return(new IdentifierResult(payment.Id)); }
public async Task <IOperationResult> HandleAsync(IUpdateSaleStateChange command, ICorrelationContext context) { var sale = await _salesRepository.GetAsync(command.ParentSaleId); if (sale is null) { throw new BaristaException("sale_not_found", $"Sale with ID '{command.ParentSaleId}' was not found"); } var saleStateChange = sale.StateChanges.FirstOrDefault(chg => chg.Id == command.Id); if (saleStateChange is null) { throw new BaristaException("sale_state_change_not_found", $"Sale state change with ID '{command.Id}' not found for sale with ID '{command.ParentSaleId}"); } if (command.CausedByUserId != null) { await _userVerifier.AssertExists(command.CausedByUserId.Value); } if (command.CausedByPointOfSaleId != null) { await _posVerifier.AssertExists(command.CausedByPointOfSaleId.Value); } saleStateChange.SetReason(command.Reason); saleStateChange.SetCausedByPointOfSaleId(command.CausedByPointOfSaleId); saleStateChange.SetCausedByUserId(command.CausedByUserId); await _salesRepository.UpdateAsync(sale); await _salesRepository.SaveChanges(); await _busPublisher.Publish(new SaleStateChangeUpdated(saleStateChange.Id, saleStateChange.Created, saleStateChange.Reason, saleStateChange.State.ToString(), saleStateChange.CausedByPointOfSaleId, saleStateChange.CausedByUserId)); return(OperationResult.Ok()); }
public async Task <IOperationResult> HandleAsync(IUpdateManualStockOperation command, ICorrelationContext context) { if (command == null) { throw new ArgumentNullException(nameof(command)); } var stockOp = await _repository.GetAsync(command.Id); if (stockOp is null) { throw new BaristaException("stock_operation_not_found", $"Could not find stock operation with ID '{command.Id}'"); } if (!(stockOp is Domain.ManualStockOperation manualStockOp)) { throw new BaristaException("invalid_stock_operation_update_command", $"The stock operation with ID '{command.Id}' is not a manual stock operation and therefore cannot be updated with this command."); } await Task.WhenAll( _stockItemVerifier.AssertExists(command.StockItemId), _userVerifier.AssertExists(command.CreatedByUserId) ); manualStockOp.SetQuantity(command.Quantity); manualStockOp.SetStockItem(command.StockItemId); manualStockOp.SetComment(command.Comment); manualStockOp.SetCreatedByUserId(command.CreatedByUserId); await _repository.UpdateAsync(manualStockOp); await _repository.SaveChanges(); await _busPublisher.Publish(new ManualStockOperationUpdated(manualStockOp.Id, manualStockOp.StockItemId, manualStockOp.Quantity, manualStockOp.CreatedByUserId, manualStockOp.Comment)); return(OperationResult.Ok()); }
public async Task <IIdentifierResult> HandleAsync(ICreateAssignmentToUser command, ICorrelationContext context) { await Task.WhenAll( _meansVerifier.AssertExists(command.MeansId), _userVerifier.AssertExists(command.UserId) ); var assignment = new Domain.AssignmentToUser(command.Id, command.MeansId, command.ValidSince, command.ValidUntil, command.UserId, command.IsShared); await _repository.AddAsync(assignment); try { await _repository.SaveChanges(); } catch (EntityAlreadyExistsException) { throw new BaristaException("assignment_to_user_already_exists", $"An assignment with the ID '{command.Id}' already exists."); } try { await _exclVerifier.VerifyAssignmentExclusivity(command.MeansId); } catch (BaristaException) { await _repository.DeleteAsync(assignment); await _repository.SaveChanges(); throw; } await _busPublisher.Publish(new AssignmentToUserCreated(assignment.Id, assignment.MeansId, assignment.ValidSince, assignment.ValidUntil, assignment.UserId, assignment.IsShared)); return(new IdentifierResult(command.Id)); }
public async Task <IOperationResult> HandleAsync(IUpdatePayment command, ICorrelationContext context) { var payment = await _paymentsRepository.GetAsync(command.Id); if (payment is null) { throw new BaristaException("payment_not_found", $"Payment with ID '{command.Id}' was not found"); } await _userVerifier.AssertExists(command.UserId); payment.SetUserId(command.UserId); payment.SetAmount(command.Amount); payment.SetExternalId(command.ExternalId); payment.SetSource(command.Source); await _paymentsRepository.UpdateAsync(payment); await _paymentsRepository.SaveChanges(); await _busPublisher.Publish(new PaymentUpdated(payment)); return(OperationResult.Ok()); }