Skip to content

Commit

Permalink
Merge pull request #74 from IliyanIlievPH/73
Browse files Browse the repository at this point in the history
Closes #73
  • Loading branch information
starkmsu committed Jun 19, 2020
2 parents 0e002bc + 43175b2 commit c018ef3
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 0 deletions.
6 changes: 6 additions & 0 deletions settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ SmartVouchersService:
settings-key: SmartVouchersService-Redis-ConnectionString
VoucherLockTimeOut:
settings-key: SmartVouchersService-VoucherLockTimeOut
Notifications:
VoucherRedemptionSucceededPushNotificationTemplateId:
settings-key: SmartVouchersService-Notifications-VoucherRedemptionSucceededPushNotificationTemplateId
SmartVouchersJob:
Db:
LogsConnString:
Expand Down Expand Up @@ -64,6 +67,9 @@ SmartVouchersJob:
settings-key: SmartVouchersJob-FinishPaymentTimeoutPeriod
CompletedCampaignsJobIdlePeriod:
settings-key: SmartVouchersJob-CompletedCampaignsJobIdlePeriod
Notifications:
VoucherRedemptionSucceededPushNotificationTemplateId:
settings-key: SmartVouchersService-Notifications-VoucherRedemptionSucceededPushNotificationTemplateId
SlackNotifications:
AzureQueue:
ConnectionString:
Expand Down
5 changes: 5 additions & 0 deletions src/MAVN.Job.SmartVouchers/Modules/RabbitMqModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Lykke.SettingsReader;
using MAVN.Job.SmartVouchers.Settings;
using MAVN.Job.SmartVouchers.Settings.JobSettings;
using MAVN.Service.NotificationSystem.SubscriberContract;
using MAVN.Service.PaymentManagement.Contract;
using MAVN.Service.SmartVouchers.Contract;
using MAVN.Service.SmartVouchers.DomainServices.RabbitSubscribers;
Expand All @@ -17,6 +18,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 PubPushNotificationExchangeName = "notificationsystem.command.pushnotification";
private const string SubExchangeName = "lykke.payment.completed"; // TODO pass proper exchange name

private readonly RabbitMqSettings _settings;
Expand Down Expand Up @@ -47,6 +49,9 @@ private void RegisterRabbitMqPublishers(ContainerBuilder builder)
builder.RegisterJsonRabbitPublisher<SmartVoucherTransferredEvent>(
_settings.Publishers.ConnectionString,
PubVoucherTransferredExchangeName);
builder.RegisterJsonRabbitPublisher<PushNotificationEvent>(
_settings.Publishers.ConnectionString,
PubPushNotificationExchangeName);
}

private void RegisterRabbitMqSubscribers(ContainerBuilder builder)
Expand Down
5 changes: 5 additions & 0 deletions src/MAVN.Job.SmartVouchers/Modules/ServiceModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ protected override void Load(ContainerBuilder builder)
.As<IRedisLocksService>()
.SingleInstance();

builder.RegisterType<NotificationsService>()
.As<INotificationsService>()
.WithParameter(TypedParameter.From(_settings.SmartVouchersJob.Notifications.VoucherRedemptionSucceededPushNotificationTemplateId))
.SingleInstance();

builder.RegisterPaymentManagementClient(_settings.PaymentManagementServiceClient, null);
builder.RegisterPartnerManagementClient(_settings.PartnerManagementServiceClient, null);
builder.RegisterCustomerProfileClient(_settings.CustomerProfileServiceClient, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace MAVN.Job.SmartVouchers.Settings.JobSettings
{
public class NotificationsSettings
{
public string VoucherRedemptionSucceededPushNotificationTemplateId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class SmartVouchersJobSettings

public RedisSettings Redis { set; get; }

public NotificationsSettings Notifications { get; set; }

public TimeSpan VoucherLockTimeOut { get; set; }

public TimeSpan ExpiredVouchersJobIdlePeriod { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace MAVN.Service.SmartVouchers.Domain.Services
{
public interface INotificationsService
{
Task PublishVoucherSuccessfullyRedeemed(string customerId, string partnerName, string voucherShortCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="Lykke.RabbitMqBroker" Version="7.13.1" />
<PackageReference Include="MAVN.Service.NotificationSystem.SubscriberContract" Version="1.1.0" />
<PackageReference Include="MAVN.Service.PartnerManagement.Client" Version="2.8.2" />
<PackageReference Include="MAVN.Service.PaymentManagement.Client" Version="1.18.0" />
<PackageReference Include="MAVN.Service.PaymentManagement.Contract" Version="1.18.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Lykke.Common;
using Lykke.RabbitMqBroker.Publisher;
using MAVN.Service.NotificationSystem.SubscriberContract;
using MAVN.Service.SmartVouchers.Domain.Services;

namespace MAVN.Service.SmartVouchers.DomainServices
{
public class NotificationsService : INotificationsService
{
private readonly IRabbitPublisher<PushNotificationEvent> _pushNotificationPublisher;
private readonly string _voucherRedemptionSucceededTemplateId;

public NotificationsService(
IRabbitPublisher<PushNotificationEvent> pushNotificationPublisher,
string voucherRedemptionSucceededTemplateId)
{
_pushNotificationPublisher = pushNotificationPublisher;
_voucherRedemptionSucceededTemplateId = voucherRedemptionSucceededTemplateId;
}

public Task PublishVoucherSuccessfullyRedeemed(string customerId, string partnerName, string voucherShortCode)
{
return _pushNotificationPublisher.PublishAsync(new PushNotificationEvent
{
CustomerId = customerId,
Source = $"{AppEnvironment.Name} - {AppEnvironment.Version}",
MessageTemplateId = _voucherRedemptionSucceededTemplateId,
TemplateParameters =
new Dictionary<string, string>
{
{"PartnerName", partnerName}, {"VoucherShortCode", voucherShortCode}
},
CustomPayload = new Dictionary<string, string>
{
{"route", "voucher-usage-success"},
{"partner-name", partnerName},
{"voucher-short-code", voucherShortCode}
}
});
}
}
}
10 changes: 10 additions & 0 deletions src/MAVN.Service.SmartVouchers.DomainServices/VouchersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class VouchersService : IVouchersService
private readonly ICampaignsRepository _campaignsRepository;
private readonly IPaymentRequestsRepository _paymentRequestsRepository;
private readonly IRedisLocksService _redisLocksService;
private readonly INotificationsService _notificationsService;
private readonly IRabbitPublisher<SmartVoucherSoldEvent> _voucherSoldPublisher;
private readonly IRabbitPublisher<SmartVoucherUsedEvent> _voucherUsedPublisher;
private readonly IRabbitPublisher<SmartVoucherTransferredEvent> _voucherTransferredPublisher;
Expand All @@ -49,6 +50,7 @@ public class VouchersService : IVouchersService
IPaymentRequestsRepository paymentRequestsRepository,
ILogFactory logFactory,
IRedisLocksService redisLocksService,
INotificationsService notificationsService,
IRabbitPublisher<SmartVoucherSoldEvent> voucherSoldPublisher,
IRabbitPublisher<SmartVoucherUsedEvent> voucherUsedPublisher,
IRabbitPublisher<SmartVoucherTransferredEvent> voucherTransferredPublisher,
Expand All @@ -61,6 +63,7 @@ public class VouchersService : IVouchersService
_campaignsRepository = campaignsRepository;
_paymentRequestsRepository = paymentRequestsRepository;
_redisLocksService = redisLocksService;
_notificationsService = notificationsService;
_voucherSoldPublisher = voucherSoldPublisher;
_log = logFactory.CreateLog(this);
_lockTimeOut = lockTimeOut;
Expand Down Expand Up @@ -418,6 +421,13 @@ public async Task<RedeemVoucherError> RedeemVoucherAsync(string voucherShortCode
await _vouchersRepository.UpdateAsync(voucher);
await PublishVoucherUsedEvent(campaign, voucher);

var partner = await _partnerManagementClient.Partners.GetByIdAsync(campaign.PartnerId);
if (partner == null)
_log.Warning("Missing partner when redeeming smart voucher", context: campaign.PartnerId);

await _notificationsService.PublishVoucherSuccessfullyRedeemed(voucher.OwnerId.Value.ToString(),
partner?.Name, voucher.ShortCode);

return RedeemVoucherError.None;
}

Expand Down
5 changes: 5 additions & 0 deletions src/MAVN.Service.SmartVouchers/Modules/RabbitMqModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Lykke.RabbitMqBroker.Publisher;
using Lykke.RabbitMqBroker.Subscriber;
using Lykke.SettingsReader;
using MAVN.Service.NotificationSystem.SubscriberContract;
using MAVN.Service.PaymentManagement.Contract;
using MAVN.Service.SmartVouchers.Contract;

Expand All @@ -16,6 +17,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 PubPushNotificationExchangeName = "notificationsystem.command.pushnotification";
private const string SubExchangeName = "lykke.payment.completed"; // TODO pass proper exchange name

private readonly RabbitMqSettings _settings;
Expand Down Expand Up @@ -46,6 +48,9 @@ private void RegisterRabbitMqPublishers(ContainerBuilder builder)
builder.RegisterJsonRabbitPublisher<SmartVoucherTransferredEvent>(
_settings.Publishers.ConnectionString,
PubVoucherTransferredExchangeName);
builder.RegisterJsonRabbitPublisher<PushNotificationEvent>(
_settings.Publishers.ConnectionString,
PubPushNotificationExchangeName);
}

private void RegisterRabbitMqSubscribers(ContainerBuilder builder)
Expand Down
5 changes: 5 additions & 0 deletions src/MAVN.Service.SmartVouchers/Modules/ServiceModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ protected override void Load(ContainerBuilder builder)
.As<IRedisLocksService>()
.SingleInstance();

builder.RegisterType<NotificationsService>()
.As<INotificationsService>()
.WithParameter(TypedParameter.From(_settings.SmartVouchersService.Notifications.VoucherRedemptionSucceededPushNotificationTemplateId))
.SingleInstance();

builder.RegisterPaymentManagementClient(_settings.PaymentManagementServiceClient, null);
builder.RegisterPartnerManagementClient(_settings.PartnerManagementServiceClient, null);
builder.RegisterCustomerProfileClient(_settings.CustomerProfileServiceClient, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace MAVN.Service.SmartVouchers.Settings
{
public class NotificationsSettings
{
public string VoucherRedemptionSucceededPushNotificationTemplateId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ public class SmartVouchersSettings
public RedisSettings Redis { set; get; }

public TimeSpan VoucherLockTimeOut { get; set; }

public NotificationsSettings Notifications { get; set; }
}
}

0 comments on commit c018ef3

Please sign in to comment.