Skip to content

Commit

Permalink
Closes #69
Browse files Browse the repository at this point in the history
  • Loading branch information
IliyanIlievPH committed Jun 17, 2020
1 parent cf081da commit 0d4bfca
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ public class ReserveVoucherResponse

/// <summary>Payment url</summary>
public string PaymentUrl { get; set; }

/// <summary>Short code of already reserved voucher</summary>
public string AlreadyReservedVoucherShortCode { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ public class VoucherReservationResult
public ProcessingVoucherError ErrorCode { get; set; }

public string PaymentUrl { get; set; }

public string AlreadyReservedVoucherShortCode { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Task<VouchersPage> GetByOwnerIdAsync(
Task<List<Voucher>> GetByCampaignIdAndStatusAsync(Guid campaignId, VoucherStatus status);
Task<List<Voucher>> GetReservedVouchersBeforeDateAsync(DateTime reservationTimeoutDate);
Task SetVouchersFromCampaignsAsExpired(Guid[] campaignsIds);
Task<bool> AnyReservedVouchersAsync(Guid customerId);
Task<Voucher> GetReservedVoucherForCustomerAsync(Guid customerId);
Task<int> GetReservedVouchersCountForCampaign(Guid campaignId);
}
}
10 changes: 7 additions & 3 deletions src/MAVN.Service.SmartVouchers.DomainServices/VouchersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,13 @@ public async Task<VoucherReservationResult> ReserveVoucherAsync(Guid voucherCamp
continue;
}

var hasAnyReservedVouchers = await _vouchersRepository.AnyReservedVouchersAsync(ownerId);
if (hasAnyReservedVouchers)
return new VoucherReservationResult { ErrorCode = ProcessingVoucherError.CustomerHaveAnotherReservedVoucher };
var alreadyReservedVoucher = await _vouchersRepository.GetReservedVoucherForCustomerAsync(ownerId);
if (alreadyReservedVoucher != null)
return new VoucherReservationResult
{
ErrorCode = ProcessingVoucherError.CustomerHaveAnotherReservedVoucher,
AlreadyReservedVoucherShortCode = alreadyReservedVoucher.ShortCode
};

var vouchers = await _vouchersRepository.GetByCampaignIdAndStatusAsync(voucherCampaignId, VoucherStatus.InStock);
Voucher voucher = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ public async Task SetVouchersFromCampaignsAsExpired(Guid[] campaignsIds)
}
}

public async Task<bool> AnyReservedVouchersAsync(Guid customerId)
public async Task<Voucher> GetReservedVoucherForCustomerAsync(Guid customerId)
{
using (var context = _contextFactory.CreateDataContext())
{
var result = await context.Vouchers
.AnyAsync(x => x.OwnerId == customerId && x.Status == VoucherStatus.Reserved);
var entity = await context.Vouchers
.FirstOrDefaultAsync(x => x.OwnerId == customerId && x.Status == VoucherStatus.Reserved);

return result;
return _mapper.Map<Voucher>(entity);
}
}

Expand Down

0 comments on commit 0d4bfca

Please sign in to comment.