Skip to content

Commit

Permalink
Merge pull request #46 from IliyanIlievPH/42
Browse files Browse the repository at this point in the history
Closes #42
  • Loading branch information
starkmsu authored May 29, 2020
2 parents bfe2679 + 28327a6 commit 987dcf4
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using System;
using JetBrains.Annotations;

namespace MAVN.Service.SmartVouchers.Client.Models.Requests
{
Expand All @@ -17,5 +18,10 @@ public class VoucherRedeptionModel
/// Voucher validation code
/// </summary>
public string VoucherValidationCode { get; set; }

/// <summary>
/// Id of the seller
/// </summary>
public Guid? SellerCustomerId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ public class VoucherResponseModel
/// <summary>Voucher short code</summary>
public string ShortCode { get; set; }

/// <summary>Voucher validation code hash</summary>
public string ValidationCodeHash { get; set; }

/// <summary>Voucher campaign id</summary>
public Guid CampaignId { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/MAVN.Service.SmartVouchers.Domain/Models/Voucher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ public class Voucher
{
public long Id { get; set; }
public string ShortCode { get; set; }
public string ValidationCodeHash { get; set; }
public Guid CampaignId { get; set; }
public VoucherStatus Status { get; set; }
public Guid? OwnerId { get; set; }
public Guid? SellerId { get; set; }
public DateTime? PurchaseDate { get; set; }
public DateTime? RedemptionDate { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface IVouchersService
Task<ProcessingVoucherError> ProcessPaymentRequestAsync(Guid paymentRequestId);
Task<VoucherReservationResult> ReserveVoucherAsync(Guid voucherCampaignId, Guid ownerId);
Task<ProcessingVoucherError> CancelVoucherReservationAsync(string shortCode);
Task<RedeemVoucherError> RedeemVoucherAsync(string voucherShortCode, string validationCode);
Task<RedeemVoucherError> RedeemVoucherAsync(string voucherShortCode, string validationCode, Guid? sellerCustomerId);
Task<TransferVoucherError> TransferVoucherAsync(
string voucherShortCode,
Guid oldOwnerId,
Expand Down
24 changes: 9 additions & 15 deletions src/MAVN.Service.SmartVouchers.DomainServices/VouchersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,11 @@ public async Task<VoucherReservationResult> ReserveVoucherAsync(Guid voucherCamp
return new VoucherReservationResult { ErrorCode = ProcessingVoucherError.NoAvailableVouchers };
}

var (validationCode, hash) = GenerateValidation();
var validationCode = GenerateValidation();
voucher = new Voucher
{
CampaignId = voucherCampaignId,
Status = voucherPriceIsZero ? VoucherStatus.Sold : VoucherStatus.Reserved,
ValidationCodeHash = hash,
OwnerId = ownerId,
PurchaseDate = DateTime.UtcNow,
};
Expand Down Expand Up @@ -232,7 +231,7 @@ public async Task<ProcessingVoucherError> CancelVoucherReservationAsync(string s
return ProcessingVoucherError.None;
}

public async Task<RedeemVoucherError> RedeemVoucherAsync(string voucherShortCode, string validationCode)
public async Task<RedeemVoucherError> RedeemVoucherAsync(string voucherShortCode, string validationCode, Guid? sellerCustomerId)
{
var voucher = await _vouchersRepository.GetWithValidationByShortCodeAsync(voucherShortCode);
if (voucher == null)
Expand All @@ -250,6 +249,7 @@ public async Task<RedeemVoucherError> RedeemVoucherAsync(string voucherShortCode

voucher.Status = VoucherStatus.Used;
voucher.RedemptionDate = DateTime.UtcNow;
voucher.SellerId = sellerCustomerId;

await _vouchersRepository.UpdateAsync(voucher);
await PublishVoucherUsedEvent(campaign, voucher);
Expand All @@ -272,10 +272,9 @@ public async Task<TransferVoucherError> TransferVoucherAsync(
return TransferVoucherError.NotAnOwner;

voucher.OwnerId = newOwnerId;
var (code, hash) = GenerateValidation();
voucher.ValidationCodeHash = hash;
var validationCode = GenerateValidation();

await _vouchersRepository.UpdateAsync(voucher, code);
await _vouchersRepository.UpdateAsync(voucher, validationCode);

return TransferVoucherError.None;
}
Expand Down Expand Up @@ -380,29 +379,24 @@ await _voucherSoldPublisher.PublishAsync(new SmartVoucherSoldEvent

private async Task PublishVoucherUsedEvent(VoucherCampaign voucherCampaign, Voucher voucher)
{
await _voucherUsedPublisher.PublishAsync(new SmartVoucherUsedEvent()
await _voucherUsedPublisher.PublishAsync(new SmartVoucherUsedEvent
{
CustomerId = voucher.OwnerId.Value,
Timestamp = DateTime.UtcNow,
PartnerId = voucherCampaign.PartnerId,
CampaignId = voucher.CampaignId,
Amount = voucherCampaign.VoucherPrice,
Currency = voucherCampaign.Currency,
// TODO: Need to set value LinkedCustomerId
//LinkedCustomerId =
LinkedCustomerId = voucher.SellerId,
});
}

private (string, string) GenerateValidation()
private static string GenerateValidation()
{
var bytes = Encoding.ASCII.GetBytes(Guid.NewGuid().ToString());
var validationCode = Base32Helper.Encode(bytes);

var cryptoTransformSha1 = SHA1.Create();
var sha1 = cryptoTransformSha1.ComputeHash(Encoding.ASCII.GetBytes(validationCode));
var codeHash = Convert.ToBase64String(sha1);

return (validationCode, codeHash);
return validationCode;
}

private string GenerateShortCodeFromId(long voucherId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ public class VoucherEntity
[Column("short_code")]
public string ShortCode { get; set; }

[Column("validation_code_hash")]
[Required]
public string ValidationCodeHash { get; set; }
[Column("seller_id")]
public Guid? SellerId { get; set; }

[Column("campaign_id")]
[Required]
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 987dcf4

Please sign in to comment.