public void GetValidInvoiceTotalAmount() { Invoice invoice = new Invoice() { Currency = Enumerations.Currency.ARS, Month = 4, Year = 2019, User = MockData.GetTestUser() }; Assert.DoesNotThrow(() => { //Add 2 valid april 2019 charges invoice.AddCharge(GetTestCharge(new DateTime(2019, 4, 01))); invoice.AddCharge(GetTestCharge(new DateTime(2019, 4, 23))); }); var totalInvoiceAmount = invoice.GetAmountCurrency(); Assert.Multiple(() => { Assert.IsNotNull(totalInvoiceAmount, "Invoice must have a total amount and it is null"); Assert.AreEqual(200, totalInvoiceAmount.Amount, "Invoice total must be $200"); Assert.AreEqual(Currency.ARS, totalInvoiceAmount.Currency, "Invoice total currency must be ARS"); Assert.AreEqual(Currency.ARS, invoice.Currency, "Invoice currency must be ARS"); }); }
public void ShouldNotAllowToAddInvalidCharges() { Invoice invoice = new Invoice() { Currency = Enumerations.Currency.ARS, Month = 4, Year = 2019, User = MockData.GetTestUser() }; Assert.Throws(typeof(InvalidChargeException), () => { //Add valid april 2019 charges invoice.AddCharge(GetTestCharge(new DateTime(2019, 4, 01))); //Add invalid 2018 charge invoice.AddCharge(GetTestCharge(new DateTime(2018, 4, 23))); }, "Cannot add charge with invalid Date"); Assert.Throws(typeof(InvalidChargeException), () => { //Add valid april 2019 charges invoice.AddCharge(GetTestCharge(new DateTime(2019, 4, 01))); //Add invalid 2018 charge invoice.AddCharge(GetTestCharge(new DateTime(2019, 4, 23), new AmountCurrency(500, "US"))); }, "Cannot add charge with invalid Currency"); }
public async Task <Invoice> AddCharge(Charge charge, User user) { Currency?chargeCurrency = charge?.GetCurrency(); int month = 0; int year = 0; if (!chargeCurrency.HasValue) { throw new InvalidEntityException(charge); } //Try to get month and year from charge event if (charge.Event != null && charge.Event.Date != default(DateTime)) { month = charge.Event.Date.Month; year = charge.Event.Date.Year; } Invoice foundInvoice = await _invoiceRepository.FindAsync(x => x.User.Id == user.Id && x.Month == month && x.Year == year && x.Currency == chargeCurrency); //If no invoice exists for this user/month/year combination //create a new one if (foundInvoice == null) { Invoice newInvoice = new Invoice( month, year, chargeCurrency.Value, user ); newInvoice.AddCharge(charge); if (newInvoice.IsValid()) { return(_invoiceRepository.Insert(newInvoice)); } else { throw new InvalidEntityException(newInvoice); } } else { //If invoice was found, add charge foundInvoice.AddCharge(charge); if (foundInvoice.IsValid()) { return(_invoiceRepository.Update(foundInvoice)); } else { throw new InvalidEntityException(foundInvoice); } } }