Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #69 #70

Merged
merged 1 commit into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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