public async Task <int> PlaatsBestelling(NieuweBestellingCommand nieuweBestellingCommand) { var bestellingItems = new List <BestellingItem>(); nieuweBestellingCommand.BesteldeArtikelen.ForEach(a => { bestellingItems.Add(new BestellingItem { Artikelnummer = a.Artikelnummer, Aantal = a.Aantal }); }); var bestelling = new Bestelling { KlantId = nieuweBestellingCommand.KlantId, AdresRegel1 = nieuweBestellingCommand.AdresRegel1, AdresRegel2 = nieuweBestellingCommand.AdresRegel2, Postcode = nieuweBestellingCommand.Postcode, Plaats = nieuweBestellingCommand.Plaats, BesteldeArtikelen = bestellingItems }; await _datamapper.Insert(bestelling); //get full bestelling so all data like artikel prices are filled in var fullBestelling = await _datamapper.GetBestelling(bestelling.Id); if (fullBestelling.GetTotaalPrijs + fullBestelling.Klant.KredietMetSales > NameConstants.KredietLimit) { fullBestelling.BestellingStatus = BestellingStatus.TerControleVoorSales; } else { fullBestelling.BestellingStatus = BestellingStatus.GereedVoorBehandeling; fullBestelling.Klant.KredietOver -= fullBestelling.GetTotaalPrijs; } fullBestelling.Klant.KredietMetSales += fullBestelling.GetTotaalPrijs; await _klantDatamapper.Update(fullBestelling.Klant); await _datamapper.Update(fullBestelling); _eventPublisher.Publish(new BestellingToegevoegdEvent { Bestelling = bestelling }); _eventPublisher.Publish(new KlantKredietAangepastEvent() { KlantId = fullBestelling.Klant.Id, NieuweKrediet = fullBestelling.Klant.KredietOver }); _logger.LogInformation("Created new Bestelling with id {id} and {amount} artikelen", bestelling.Id, bestelling.BesteldeArtikelen.Count); return(bestelling.Id); }
public async Task NieuweBestellingThatGoesAboveKredietLimitGoesToSale() { var bestelling = new NieuweBestellingCommand { KlantId = "1", AdresRegel1 = "Laagstraat 11", Plaats = "Laaghoven", Postcode = "1234FG", BesteldeArtikelen = new List <BestellingItem> { new BestellingItem(1, 3), new BestellingItem(2, 3) } }; var commandPublisher = new CommandPublisher(_context); await commandPublisher.Publish <bool>(bestelling, NameConstants.NieuweBestellingCommandQueue); Thread.Sleep(500); using (var context = new BeheerContext(options)) { var datamapper = new BestellingDatamapper(context); var result = await datamapper.GetBestelling(1); Assert.AreEqual("Hans", result.Klant.Voornaam); Assert.AreEqual("Van Huizen", result.Klant.Achternaam); Assert.AreEqual("1", result.KlantId); Assert.AreEqual("Laagstraat 11", result.AdresRegel1); Assert.AreEqual("Laaghoven", result.Plaats); Assert.AreEqual("1234FG", result.Postcode); Assert.AreEqual(2, result.BesteldeArtikelen.Count); Assert.AreEqual(BestellingStatus.TerControleVoorSales, result.BestellingStatus); Assert.IsTrue(result.BesteldeArtikelen.Any(b => b.Artikel.Naam == "Fiets" && b.Aantal == 3)); var klantMapper = new KlantDatamapper(context); var klant = await klantMapper.GetKlant("1"); //Krediet should not be added yet Assert.AreEqual(508.2m, klant.KredietMetSales); } }
public async Task InsertNieuweBestellingUnder500EuroFromEventSetsStatusToGereed() { var bestelling = new NieuweBestellingCommand { KlantId = "1", AdresRegel1 = "Laagstraat 11", Plaats = "Laaghoven", Postcode = "1234FG", BesteldeArtikelen = new List <BestellingItem> { new BestellingItem(1, 1), new BestellingItem(2, 1) } }; var commandPublisher = new CommandPublisher(_context); await commandPublisher.Publish <bool>(bestelling, NameConstants.NieuweBestellingCommandQueue); Thread.Sleep(500); using (var context = new BeheerContext(options)) { var datamapper = new BestellingDatamapper(context); var result = await datamapper.GetBestelling(1); Assert.AreEqual("Hans", result.Klant.Voornaam); Assert.AreEqual("Van Huizen", result.Klant.Achternaam); Assert.AreEqual("1", result.KlantId); Assert.AreEqual("Laagstraat 11", result.AdresRegel1); Assert.AreEqual("Laaghoven", result.Plaats); Assert.AreEqual("1234FG", result.Postcode); Assert.AreEqual(2, result.BesteldeArtikelen.Count); Assert.AreEqual(BestellingStatus.GereedVoorBehandeling, result.BestellingStatus); Assert.IsTrue(result.BesteldeArtikelen.Any(b => b.Artikel.Naam == "Fiets" && b.Aantal == 1)); var klantMapper = new KlantDatamapper(context); var klant = await klantMapper.GetKlant("1"); Assert.AreEqual(169.4m, klant.KredietMetSales); } }
public void GivenDatErEenBestellingVanIsGeplaatst(decimal totaalBedrag) { connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); options = new DbContextOptionsBuilder <BeheerContext>() .UseSqlite(connection) .Options; using (var context = new BeheerContext(options)) { context.Database.EnsureCreated(); } artikel = new Artikel { Artikelnummer = 1, Beschrijving = "Test beschrijving", LeverbaarTot = new DateTime(2019, 01, 22), LeverbaarVanaf = new DateTime(2019, 01, 21), Naam = "Fiets", Prijs = (totaalBedrag / 1.21M), Voorraad = 10 }; using (var context = new BeheerContext(options)) { ArtikelDatamapper artikelDatamapper = new ArtikelDatamapper(context); artikelDatamapper.Insert(artikel); } bestelling = new NieuweBestellingCommand { KlantId = "1", AdresRegel1 = "Laagstraat 11", Plaats = "Laaghoven", Postcode = "1234FG", BesteldeArtikelen = new List <BestellingItem> { new BestellingItem(1, 1) } }; }
public async Task <int> NieuweBestelling(Bestelling bestelling) { var besteldeArtikelen = new List <BestellingItemDB>(); foreach (var bestellingItem in bestelling.BesteldeArtikelen) { besteldeArtikelen.Add(new BestellingItemDB(bestellingItem.Artikelnummer, bestellingItem.Aantal)); } var command = new NieuweBestellingCommand { KlantId = bestelling.KlantId, AdresRegel1 = bestelling.AdresRegel1, AdresRegel2 = bestelling.AdresRegel2, BesteldeArtikelen = besteldeArtikelen, Plaats = bestelling.Plaats, Postcode = bestelling.Postcode }; var result = await _commandPublisher.Publish <int>(command, NameConstants.NieuweBestellingCommandQueue); _logger.LogInformation("New bestelling with id {id} added", result); return(result); }
public async Task MultipleBestellingenInsertIntoDatabaseWithStatusGereedvoorBehandelingUntilKredietLimietIsBereikt() { var klant = new Klant() { Id = "1", KredietOver = 500 }; var bestelling1 = new NieuweBestellingCommand { KlantId = "1", AdresRegel1 = "Laagstraat 11", Plaats = "Laaghoven", Postcode = "1234FG", BesteldeArtikelen = new List <BestellingItem> { new BestellingItem(1, 1), } }; var bestelling2 = new NieuweBestellingCommand { KlantId = "1", AdresRegel1 = "Laagstraat 11", Plaats = "Laaghoven", Postcode = "1234FG", BesteldeArtikelen = new List <BestellingItem> { new BestellingItem(1, 1), } }; var bestelling3 = new NieuweBestellingCommand { KlantId = "1", AdresRegel1 = "Laagstraat 11", Plaats = "Laaghoven", Postcode = "1234FG", BesteldeArtikelen = new List <BestellingItem> { new BestellingItem(1, 1), } }; var finalBestelling1 = new Bestelling { Id = 1, KlantId = "1", Klant = klant, AdresRegel1 = "Laagstraat 11", Plaats = "Laaghoven", Postcode = "1234FG", BesteldeArtikelen = new List <BestellingItem> { new BestellingItem(1, 1) { Artikel = new Artikel { Prijs = 19.99m } }, } }; var finalBestelling2 = new Bestelling { Id = 2, KlantId = "1", Klant = klant, AdresRegel1 = "Laagstraat 11", Plaats = "Laaghoven", Postcode = "1234FG", BesteldeArtikelen = new List <BestellingItem> { new BestellingItem(1, 1) { Artikel = new Artikel { Prijs = 199.99m } }, } }; var finalBestelling3 = new Bestelling { Id = 3, KlantId = "1", Klant = klant, AdresRegel1 = "Laagstraat 11", Plaats = "Laaghoven", Postcode = "1234FG", BesteldeArtikelen = new List <BestellingItem> { new BestellingItem(1, 1) { Artikel = new Artikel { Prijs = 249.99m } }, } }; var eventpublisherMock = new Mock <IEventPublisher>(MockBehavior.Strict); eventpublisherMock.Setup(m => m.Publish( It.Is <BestellingToegevoegdEvent>(d => d.RoutingKey == NameConstants.BestellingToegevoegdEvent))); int id = 0; var datamapperMock = new Mock <IBestellingDatamapper>(MockBehavior.Strict); datamapperMock .Setup(m => m.Insert(It.Is <Bestelling>(b => b.KlantId == "1"))) .Callback((Bestelling b) => b.Id = ++id) .Returns(Task.CompletedTask) .Verifiable("Bestelling needs to have ControlevoorSales status"); eventpublisherMock .Setup(ep => ep.Publish(It.Is <KlantKredietAangepastEvent>(e => e.RoutingKey == NameConstants.KlantKredietAangepastEvent))) .Verifiable(); datamapperMock.Setup(m => m.GetBestelling(1)).ReturnsAsync(finalBestelling1); datamapperMock.Setup(m => m.GetBestelling(2)).ReturnsAsync(finalBestelling2); datamapperMock.Setup(m => m.GetBestelling(3)).ReturnsAsync(finalBestelling3); datamapperMock .Setup(m => m.Update( It.Is <Bestelling>(b => (b.BestellingStatus == BestellingStatus.GereedVoorBehandeling && (b.Id == 1 || b.Id == 2)) || (b.BestellingStatus == BestellingStatus.TerControleVoorSales && b.Id == 3)))) .Returns(Task.CompletedTask) .Verifiable("Bestelling should have GereedVoorBehandeling status"); var klantDatamapperMock = new Mock <IKlantDatamapper>(MockBehavior.Strict); klantDatamapperMock.Setup(k => k.Update(It.Is <Klant>(kl => kl.Id == "1"))).Returns(Task.CompletedTask).Verifiable(); var listener = new BestellingListener(datamapperMock.Object, klantDatamapperMock.Object, null, eventpublisherMock.Object); var result1 = await listener.PlaatsBestelling(bestelling1); var result2 = await listener.PlaatsBestelling(bestelling2); var result3 = await listener.PlaatsBestelling(bestelling3); Assert.AreEqual(1, result1); Assert.AreEqual(2, result2); Assert.AreEqual(3, result3); Assert.AreEqual(568.67m, klant.KredietMetSales); Assert.AreEqual(233.82m, klant.KredietOver); //2 bestellingen will go to magazijn, 1 not so check for 2 times klantDatamapperMock.Verify(k => k.Update(It.Is <Klant>(kl => kl.Id == "1")), Times.Exactly(3)); }
public async Task NewBestellingUnderKredietLimitEuroInsertsIntoDatabaseWithStatusGereed() { var bestelling = new NieuweBestellingCommand { KlantId = "1", AdresRegel1 = "Laagstraat 11", Plaats = "Laaghoven", Postcode = "1234FG", BesteldeArtikelen = new List <BestellingItem> { new BestellingItem(1, 3), new BestellingItem(2, 5) } }; var eventpublisherMock = new Mock <IEventPublisher>(MockBehavior.Strict); eventpublisherMock.Setup(m => m.Publish( It.Is <BestellingToegevoegdEvent>(d => d.RoutingKey == NameConstants.BestellingToegevoegdEvent))); eventpublisherMock .Setup(ep => ep.Publish(It.Is <KlantKredietAangepastEvent>(e => e.RoutingKey == NameConstants.KlantKredietAangepastEvent))) .Verifiable(); var datamapperMock = new Mock <IBestellingDatamapper>(MockBehavior.Strict); datamapperMock .Setup(m => m.Insert(It.Is <Bestelling>(b => b.KlantId == "1" && b.BesteldeArtikelen.Count == 2))) .Callback((Bestelling b) => b.Id = 1) .Returns(Task.CompletedTask).Verifiable(); var klant = new Klant() { Id = "1", KredietOver = 500m }; var finalBestelling = new Bestelling { Id = 1, KlantId = "1", Klant = klant, AdresRegel1 = "Laagstraat 11", Plaats = "Laaghoven", Postcode = "1234FG", BesteldeArtikelen = new List <BestellingItem> { new BestellingItem(1, 3) { Artikel = new Artikel { Prijs = 19.99m } }, new BestellingItem(2, 5) { Artikel = new Artikel { Prijs = 14.99m } } } }; datamapperMock.Setup(m => m.GetBestelling(1)).ReturnsAsync(finalBestelling).Verifiable(); datamapperMock .Setup(m => m.Update( It.Is <Bestelling>(b => b.BestellingStatus == BestellingStatus.GereedVoorBehandeling))) .Returns(Task.CompletedTask) .Verifiable("Bestelling should have GereedVoorBehandeling status"); var klantDatamapperMock = new Mock <IKlantDatamapper>(MockBehavior.Strict); klantDatamapperMock.Setup(kd => kd.Update(It.Is <Klant>(k => k.Id == "1"))).Returns(Task.CompletedTask).Verifiable(); var listener = new BestellingListener(datamapperMock.Object, klantDatamapperMock.Object, null, eventpublisherMock.Object); var result = await listener.PlaatsBestelling(bestelling); Assert.AreEqual(1, result); Assert.AreEqual(163.27m, klant.KredietMetSales); Assert.AreEqual(336.73m, klant.KredietOver); }