Skip to content

Commit

Permalink
Merge pull request #57 from IliyanIlievPH/56
Browse files Browse the repository at this point in the history
Closes #56
  • Loading branch information
starkmsu authored Jun 3, 2020
2 parents 4b38fff + ad6505e commit b70b8b2
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ public enum TransferVoucherErrorCodes
VoucherNotFound,
/// <summary>Not an owner</summary>
NotAnOwner,
/// <summary>Voucher is already used</summary>
VoucherIsUsed,
/// <summary>Voucher cannot be transferred because it is not in a correct state</summary>
VoucherIsNotInTheCorrectStateToTransfer,
/// <summary>
/// Campaign is missing for smart voucher
/// </summary>
VoucherCampaignNotFound,
}
}
3 changes: 0 additions & 3 deletions contract/MAVN.Service.SmartVouchers.Contract/Readme.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;

namespace MAVN.Service.SmartVouchers.Contract
{
public class SmartVoucherTransferredEvent
{
/// <summary>
/// Timestamp
/// </summary>
public DateTime Timestamp { get; set; }

/// <summary>
/// Customer id of the sender
/// </summary>
public Guid OldCustomerId { get; set; }

/// <summary>
/// Customer id of the receiver
/// </summary>
public Guid NewCustomerId { get; set; }

/// <summary>
/// Partner id
/// </summary>
public Guid PartnerId { get; set; }

/// <summary>
/// Id of the smart voucher campaign
/// </summary>
public Guid CampaignId { get; set; }

/// <summary>
/// Short code of the voucher
/// </summary>
public string VoucherShortCode { get; set; }

/// <summary>
/// Amount
/// </summary>
public decimal Amount { get; set; }

/// <summary>
/// Currency
/// </summary>
public string Currency { get; set; }
}
}
4 changes: 4 additions & 0 deletions src/MAVN.Job.SmartVouchers/Modules/RabbitMqModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class RabbitMqModule : Module
{
private const string PubVoucherSoldExchangeName = "lykke.smart-vouchers.vouchersold";
private const string PubVoucherUsedExchangeName = "lykke.smart-vouchers.voucherused";
private const string PubVoucherTransferredExchangeName = "lykke.smart-vouchers.vouchertransferred";
private const string SubExchangeName = "lykke.payment.completed"; // TODO pass proper exchange name

private readonly RabbitMqSettings _settings;
Expand Down Expand Up @@ -43,6 +44,9 @@ private void RegisterRabbitMqPublishers(ContainerBuilder builder)
builder.RegisterJsonRabbitPublisher<SmartVoucherUsedEvent>(
_settings.Publishers.ConnectionString,
PubVoucherUsedExchangeName);
builder.RegisterJsonRabbitPublisher<SmartVoucherTransferredEvent>(
_settings.Publishers.ConnectionString,
PubVoucherTransferredExchangeName);
}

private void RegisterRabbitMqSubscribers(ContainerBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum TransferVoucherError
None,
VoucherNotFound,
NotAnOwner,
VoucherIsUsed,
VoucherIsNotInTheCorrectStateToTransfer,
VoucherCampaignNotFound,
}
}
28 changes: 26 additions & 2 deletions src/MAVN.Service.SmartVouchers.DomainServices/VouchersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class VouchersService : IVouchersService
private readonly IRedisLocksService _redisLocksService;
private readonly IRabbitPublisher<SmartVoucherSoldEvent> _voucherSoldPublisher;
private readonly IRabbitPublisher<SmartVoucherUsedEvent> _voucherUsedPublisher;
private readonly IRabbitPublisher<SmartVoucherTransferredEvent> _voucherTransferredPublisher;
private readonly ILog _log;
private readonly TimeSpan _lockTimeOut;

Expand All @@ -45,6 +46,7 @@ public VouchersService(
IRedisLocksService redisLocksService,
IRabbitPublisher<SmartVoucherSoldEvent> voucherSoldPublisher,
IRabbitPublisher<SmartVoucherUsedEvent> voucherUsedPublisher,
IRabbitPublisher<SmartVoucherTransferredEvent> voucherTransferredPublisher,
TimeSpan lockTimeOut)
{
_paymentManagementClient = paymentManagementClient;
Expand All @@ -57,6 +59,7 @@ public VouchersService(
_log = logFactory.CreateLog(this);
_lockTimeOut = lockTimeOut;
_voucherUsedPublisher = voucherUsedPublisher;
_voucherTransferredPublisher = voucherTransferredPublisher;
}

public async Task<ProcessingVoucherError> ProcessPaymentRequestAsync(Guid paymentRequestId)
Expand Down Expand Up @@ -290,15 +293,21 @@ public async Task<TransferVoucherError> TransferVoucherAsync(
if (voucher == null)
return TransferVoucherError.VoucherNotFound;

if (voucher.Status != VoucherStatus.InStock)
return TransferVoucherError.VoucherIsUsed;
if (voucher.Status != VoucherStatus.Sold)
return TransferVoucherError.VoucherIsNotInTheCorrectStateToTransfer;

if (voucher.OwnerId != oldOwnerId)
return TransferVoucherError.NotAnOwner;

var campaign = await _campaignsRepository.GetByIdAsync(voucher.CampaignId, false);
if (campaign == null)
return TransferVoucherError.VoucherCampaignNotFound;

voucher.OwnerId = newOwnerId;
var validationCode = GenerateValidation();

await _vouchersRepository.UpdateAsync(voucher, validationCode);
await PublishVoucherTransferredEvent(campaign, voucher, oldOwnerId);

return TransferVoucherError.None;
}
Expand Down Expand Up @@ -415,6 +424,21 @@ await _voucherUsedPublisher.PublishAsync(new SmartVoucherUsedEvent
});
}

private async Task PublishVoucherTransferredEvent(VoucherCampaign voucherCampaign, Voucher voucher, Guid oldOwnerId)
{
await _voucherTransferredPublisher.PublishAsync(new SmartVoucherTransferredEvent
{
Amount = voucherCampaign.VoucherPrice,
Currency = voucherCampaign.Currency,
PartnerId = voucherCampaign.PartnerId,
Timestamp = DateTime.UtcNow,
CampaignId = voucher.CampaignId,
VoucherShortCode = voucher.ShortCode,
NewCustomerId = voucher.OwnerId.Value,
OldCustomerId = oldOwnerId,
});
}

private static string GenerateValidation()
{
var bytes = Encoding.ASCII.GetBytes(Guid.NewGuid().ToString());
Expand Down
4 changes: 4 additions & 0 deletions src/MAVN.Service.SmartVouchers/Modules/RabbitMqModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class RabbitMqModule : Module
{
private const string PubVoucherSoldExchangeName = "lykke.smart-vouchers.vouchersold";
private const string PubVoucherUsedExchangeName = "lykke.smart-vouchers.voucherused";
private const string PubVoucherTransferredExchangeName = "lykke.smart-vouchers.vouchertransferred";
private const string SubExchangeName = "lykke.payment.completed"; // TODO pass proper exchange name

private readonly RabbitMqSettings _settings;
Expand Down Expand Up @@ -42,6 +43,9 @@ private void RegisterRabbitMqPublishers(ContainerBuilder builder)
builder.RegisterJsonRabbitPublisher<SmartVoucherUsedEvent>(
_settings.Publishers.ConnectionString,
PubVoucherUsedExchangeName);
builder.RegisterJsonRabbitPublisher<SmartVoucherTransferredEvent>(
_settings.Publishers.ConnectionString,
PubVoucherTransferredExchangeName);
}

private void RegisterRabbitMqSubscribers(ContainerBuilder builder)
Expand Down

0 comments on commit b70b8b2

Please sign in to comment.