public void VoucherAggregate_Redeem_VoucherAlreadyRedeemed_ErrorThrown() { VoucherAggregate aggregate = VoucherAggregate.Create(TestData.VoucherId); aggregate.Generate(TestData.OperatorIdentifier, TestData.EstateId, TestData.TransactionId, TestData.GeneratedDateTime, TestData.Value); aggregate.Issue(TestData.RecipientEmail, TestData.RecipientMobile, TestData.IssuedDateTime); aggregate.Redeem(TestData.RedeemedDateTime); Should.Throw <InvalidOperationException>(() => { aggregate.Redeem(TestData.RedeemedDateTime); }); }
/// <summary> /// Redeems the voucher. /// </summary> /// <param name="estateId">The estate identifier.</param> /// <param name="voucherCode">The voucher code.</param> /// <param name="redeemedDateTime">The redeemed date time.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> /// <exception cref="NotFoundException">No voucher found with voucher code [{voucherCode}]</exception> public async Task <RedeemVoucherResponse> RedeemVoucher(Guid estateId, String voucherCode, DateTime redeemedDateTime, CancellationToken cancellationToken) { await this.ValidateVoucherRedemption(estateId, cancellationToken); // Find the voucher based on the voucher code EstateReportingGenericContext context = await this.DbContextFactory.GetContext(estateId, cancellationToken); var voucher = await context.Vouchers.SingleOrDefaultAsync(v => v.VoucherCode == voucherCode, cancellationToken); if (voucher == null) { throw new NotFoundException($"No voucher found with voucher code [{voucherCode}]"); } // Now get the aggregate VoucherAggregate voucherAggregate = await this.VoucherAggregateRepository.GetLatestVersion(voucher.VoucherId, cancellationToken); // Redeem the voucher voucherAggregate.Redeem(redeemedDateTime); // Save the changes await this.VoucherAggregateRepository.SaveChanges(voucherAggregate, cancellationToken); Voucher voucherModel = voucherAggregate.GetVoucher(); return(new RedeemVoucherResponse { RemainingBalance = voucherModel.Balance, ExpiryDate = voucherModel.ExpiryDate, VoucherCode = voucherModel.VoucherCode }); }
public void VoucherAggregate_Redeem_VoucherNotGenerated_ErrorThrown() { VoucherAggregate aggregate = VoucherAggregate.Create(TestData.VoucherId); Should.Throw <InvalidOperationException>(() => { aggregate.Redeem(TestData.RedeemedDateTime); }); }
public void VoucherAggregate_Redeem_VoucherIsRedeemed() { VoucherAggregate aggregate = VoucherAggregate.Create(TestData.VoucherId); aggregate.Generate(TestData.OperatorIdentifier, TestData.EstateId, TestData.TransactionId, TestData.GeneratedDateTime, TestData.Value); aggregate.Issue(TestData.RecipientEmail, TestData.RecipientMobile, TestData.IssuedDateTime); aggregate.Redeem(TestData.RedeemedDateTime); var voucher = aggregate.GetVoucher(); voucher.IsRedeemed.ShouldBeTrue(); }