public void TransferInventory_FailFor_LackOfInventory() { //arrange var command = new TransferInventory { ArticleId = Product1InteriorDoor.Id, Amount = AmountTooMuchToHandle, SourceShopId = ProductAssemblyShop.Id, TargetShopId = ProductStockpileShop.Id, InitiatorId = GlobalAdmin.Id }; //assert () => act var ex = Assert.ThrowsAsync <DomainException>(async() => await _transferHandler.HandleAsync(command)); Assert.That(ex.Message, Is.EqualTo(ArticleNotInStock(Product1InteriorDoor, ProductAssemblyShop))); Assert.That(GetRecordedEvents <InventoryTransferred>(), Is.Empty); Assert.That(GetRecordedEvents <ShopInventoryChanged>(), Is.Empty); }
public void TransferInventory_FailFor_ArticleNotRegisteredWithReceivingWorkshop() { //arrange var command = new TransferInventory { ArticleId = Product1InteriorDoor.Id, Amount = AmountToHandle, SourceShopId = ProductAssemblyShop.Id, TargetShopId = MdfComponentShop.Id, InitiatorId = GlobalAdmin.Id }; //assert () => act var ex = Assert.ThrowsAsync <DomainException>(async() => await _transferHandler.HandleAsync(command)); Assert.That(ex.Message, Is.EqualTo(ArticleNotAllowedInShop(Product1InteriorDoor, MdfComponentShop))); Assert.That(GetRecordedEvents <InventoryTransferred>(), Is.Empty); Assert.That(GetRecordedEvents <ShopInventoryChanged>(), Is.Empty); }
public async Task TransferInventory_Success() { //adhere var initialSourceInventory = MaterialStockpileShop.Inventory.DeepCopy(); var initialTargetInventory = TimberComponentShop.Inventory.DeepCopy(); //arrange var command = new TransferInventory { ArticleId = Material1Timber.Id, Amount = AmountToHandle, SourceShopId = MaterialStockpileShop.Id, TargetShopId = TimberComponentShop.Id, InitiatorId = GlobalAdmin.Id }; //act await _transferHandler.HandleAsync(command); //assert var sourceDiff = initialSourceInventory.Of(Material1Timber).Amount - MaterialStockpileShop.Inventory.Of(Material1Timber).Amount; Assert.That(sourceDiff, Is.EqualTo(command.Amount)); var targetDiff = TimberComponentShop.Inventory.Of(Material1Timber).Amount - initialTargetInventory.Of(Material1Timber).Amount; Assert.That(targetDiff, Is.EqualTo(command.Amount)); Assert.That(sourceDiff, Is.EqualTo(targetDiff)); Assert.That(GetRecordedEvents <InventoryTransferred>().Count, Is.EqualTo(1)); Assert.That(GetRecordedEvents <ShopInventoryChanged>().Count, Is.EqualTo(2)); //annul MaterialStockpileShop.Inventory = initialSourceInventory; TimberComponentShop.Inventory = initialTargetInventory; }
public async Task <IActionResult> PostTransferInventory([FromBody] TransferInventory transferInventory) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } System.Security.Claims.ClaimsPrincipal currentUser = this.User; var userId = _userManager.GetUserId(User); var date = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "Pacific Standard Time"); var fromProductInventoryHistory = new ProductInventoryHistory { CreatedByUserId = userId, ModifiedDate = date, ProductId = transferInventory.ProductId, Balance = transferInventory.TransferQuantity * -1, LocationId = transferInventory.FromLocationId, BinCode = "", Notes = "Transfer - " + transferInventory.TransferNotes, TransactionType = "Transfer", }; var toProductInventoryHistory = new ProductInventoryHistory { CreatedByUserId = userId, ModifiedDate = date, ProductId = transferInventory.ProductId, Balance = transferInventory.TransferQuantity, LocationId = transferInventory.ToLocationId, BinCode = "", Notes = "Transfer - " + transferInventory.TransferNotes, TransactionType = "Transfer", }; // Update Product Inventory var fromProductInventory = await _context.ProductInventory.FirstOrDefaultAsync(m => m.ProductId == transferInventory.ProductId && m.LocationId == transferInventory.FromLocationId); if (fromProductInventory != null) { fromProductInventory.Balance = fromProductInventory.Balance - transferInventory.TransferQuantity; fromProductInventory.ModifiedDate = fromProductInventoryHistory.ModifiedDate; } else { var newProductInventory = new ProductInventory { Balance = -transferInventory.TransferQuantity, BinCode = "", LocationId = transferInventory.FromLocationId, ModifiedDate = fromProductInventoryHistory.ModifiedDate, ProductId = transferInventory.ProductId }; _context.ProductInventory.Add(newProductInventory); } var toProductInventory = await _context.ProductInventory.FirstOrDefaultAsync(m => m.ProductId == transferInventory.ProductId && m.LocationId == transferInventory.ToLocationId); if (toProductInventory != null) { toProductInventory.Balance = toProductInventory.Balance + transferInventory.TransferQuantity; toProductInventory.ModifiedDate = toProductInventoryHistory.ModifiedDate; } else { var newProductInventory = new ProductInventory { Balance = transferInventory.TransferQuantity, BinCode = "", LocationId = transferInventory.ToLocationId, ModifiedDate = toProductInventoryHistory.ModifiedDate, ProductId = transferInventory.ProductId }; _context.ProductInventory.Add(newProductInventory); } var product = await _context.Product.FirstOrDefaultAsync(p => p.ProductId == transferInventory.ProductId); var fromLocation = await _context.Location.FirstOrDefaultAsync(p => p.LocationId == transferInventory.FromLocationId); var toLocation = await _context.Location.FirstOrDefaultAsync(p => p.LocationId == transferInventory.ToLocationId); var subject = $"Inventory Transfer From: {fromLocation.LocationName} To: {toLocation.LocationName}"; var message = $"Product: {product.ProductCode} - {product.ProductName}.\n"; message += $"Inventory Transfer.\n"; message += $"From: {fromLocation.LocationName} To: {toLocation.LocationName}.\n"; message += $"Amount: {transferInventory.TransferQuantity}.\n"; message += $"Date: {toProductInventoryHistory.ModifiedDate}.\n"; message += $"User: {userId}.\n"; await _emailSender.SendEmailAsync(null, subject, null, message, null, null); _context.ProductInventoryHistory.Add(fromProductInventoryHistory); _context.ProductInventoryHistory.Add(toProductInventoryHistory); await _context.SaveChangesAsync(); return(Ok(transferInventory.ProductId)); }