Skip to content

Commit

Permalink
Use unique OrderId for Vouchers (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasanker authored Sep 23, 2023
1 parent 5fb42da commit 759fb74
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion coffeecard/CoffeeCard.Library/Services/IPurchaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ namespace CoffeeCard.Library.Services
{
public interface IPurchaseService : IDisposable
{
Purchase RedeemVoucher(string voucherCode, IEnumerable<Claim> claims);
Task<Purchase> RedeemVoucher(string voucherCode, IEnumerable<Claim> claims);
}
}
19 changes: 17 additions & 2 deletions coffeecard/CoffeeCard.Library/Services/PurchaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +23,7 @@ public PurchaseService(CoffeeCardContext context)
_context = context;
}

public Purchase RedeemVoucher(string voucherCode, IEnumerable<Claim> claims)
public async Task<Purchase> RedeemVoucher(string voucherCode, IEnumerable<Claim> claims)
{
var userId = claims.FirstOrDefault(x => x.Type == Constants.UserId);
if (userId == null) throw new ApiException("The token is invalid!", StatusCodes.Status401Unauthorized);
Expand All @@ -39,7 +40,7 @@ public Purchase RedeemVoucher(string voucherCode, IEnumerable<Claim> claims)
{
DateCreated = DateTime.UtcNow,
NumberOfTickets = voucher.Product.NumberOfTickets,
OrderId = voucherCode,
OrderId = (await GenerateUniqueOrderId()).ToString(),
Price = 0,
ProductId = voucher.Product.Id,
ProductName = voucher.Product.Name,
Expand All @@ -63,6 +64,20 @@ public Purchase RedeemVoucher(string voucherCode, IEnumerable<Claim> claims)
return purchase;
}

private async Task<Guid> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public async Task<SimplePurchaseResponse> 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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -58,9 +59,9 @@ public ActionResult Get()
[ProducesResponseType(typeof(ApiError), StatusCodes.Status409Conflict)]
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(ApiError), StatusCodes.Status404NotFound)]
public ActionResult<PurchaseDto> RedeemVoucher([FromQuery] string voucherCode)
public async Task<ActionResult<PurchaseDto>> RedeemVoucher([FromQuery] string voucherCode)
{
var purchase = _purchaseService.RedeemVoucher(voucherCode, User.Claims);
var purchase = await _purchaseService.RedeemVoucher(voucherCode, User.Claims);
return Ok(_mapperService.Map(purchase));
}

Expand Down

0 comments on commit 759fb74

Please sign in to comment.