diff --git a/coffeecard/CoffeeCard.Library/Services/IPurchaseService.cs b/coffeecard/CoffeeCard.Library/Services/IPurchaseService.cs index 740f7af7..c8ff06c9 100644 --- a/coffeecard/CoffeeCard.Library/Services/IPurchaseService.cs +++ b/coffeecard/CoffeeCard.Library/Services/IPurchaseService.cs @@ -10,6 +10,6 @@ namespace CoffeeCard.Library.Services { public interface IPurchaseService : IDisposable { - Purchase RedeemVoucher(string voucherCode, IEnumerable claims); + Task RedeemVoucher(string voucherCode, IEnumerable claims); } } \ No newline at end of file diff --git a/coffeecard/CoffeeCard.Library/Services/PurchaseService.cs b/coffeecard/CoffeeCard.Library/Services/PurchaseService.cs index c9819841..04bd0895 100644 --- a/coffeecard/CoffeeCard.Library/Services/PurchaseService.cs +++ b/coffeecard/CoffeeCard.Library/Services/PurchaseService.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Security.Claims; +using System.Threading.Tasks; using CoffeeCard.Common; using CoffeeCard.Common.Errors; using CoffeeCard.Library.Persistence; @@ -22,7 +23,7 @@ public PurchaseService(CoffeeCardContext context) _context = context; } - public Purchase RedeemVoucher(string voucherCode, IEnumerable claims) + public async Task RedeemVoucher(string voucherCode, IEnumerable claims) { var userId = claims.FirstOrDefault(x => x.Type == Constants.UserId); if (userId == null) throw new ApiException("The token is invalid!", StatusCodes.Status401Unauthorized); @@ -39,7 +40,7 @@ public Purchase RedeemVoucher(string voucherCode, IEnumerable claims) { DateCreated = DateTime.UtcNow, NumberOfTickets = voucher.Product.NumberOfTickets, - OrderId = voucherCode, + OrderId = (await GenerateUniqueOrderId()).ToString(), Price = 0, ProductId = voucher.Product.Id, ProductName = voucher.Product.Name, @@ -63,6 +64,20 @@ public Purchase RedeemVoucher(string voucherCode, IEnumerable claims) return purchase; } + private async Task GenerateUniqueOrderId() + { + while (true) + { + var newOrderId = Guid.NewGuid(); + + var orderIdAlreadyExists = + await _context.Purchases.Where(p => p.OrderId.Equals(newOrderId.ToString())).AnyAsync(); + if (orderIdAlreadyExists) continue; + + return newOrderId; + } + } + public Purchase DeliverProductToUser(Purchase purchase, User user, string transactionId) { Log.Information( diff --git a/coffeecard/CoffeeCard.Library/Services/v2/PurchaseService.cs b/coffeecard/CoffeeCard.Library/Services/v2/PurchaseService.cs index c6fdd52c..2a5c29fb 100644 --- a/coffeecard/CoffeeCard.Library/Services/v2/PurchaseService.cs +++ b/coffeecard/CoffeeCard.Library/Services/v2/PurchaseService.cs @@ -282,7 +282,7 @@ public async Task RedeemVoucher(string voucherCode, User { DateCreated = DateTime.UtcNow, NumberOfTickets = voucher.Product.NumberOfTickets, - OrderId = voucherCode, + OrderId = (await GenerateUniqueOrderId()).ToString(), Price = 0, ProductId = voucher.Product.Id, ProductName = voucher.Product.Name, diff --git a/coffeecard/CoffeeCard.WebApi/Controllers/PurchasesController.cs b/coffeecard/CoffeeCard.WebApi/Controllers/PurchasesController.cs index 65014af5..3066f2a2 100644 --- a/coffeecard/CoffeeCard.WebApi/Controllers/PurchasesController.cs +++ b/coffeecard/CoffeeCard.WebApi/Controllers/PurchasesController.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; using CoffeeCard.Common.Errors; using CoffeeCard.Library.Services; using CoffeeCard.Models.DataTransferObjects.Purchase; @@ -58,9 +59,9 @@ public ActionResult Get() [ProducesResponseType(typeof(ApiError), StatusCodes.Status409Conflict)] [ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)] [ProducesResponseType(typeof(ApiError), StatusCodes.Status404NotFound)] - public ActionResult RedeemVoucher([FromQuery] string voucherCode) + public async Task> RedeemVoucher([FromQuery] string voucherCode) { - var purchase = _purchaseService.RedeemVoucher(voucherCode, User.Claims); + var purchase = await _purchaseService.RedeemVoucher(voucherCode, User.Claims); return Ok(_mapperService.Map(purchase)); }