diff --git a/settings.yaml b/settings.yaml index cad783b..c4c35c8 100644 --- a/settings.yaml +++ b/settings.yaml @@ -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: @@ -64,6 +67,9 @@ SmartVouchersJob: settings-key: SmartVouchersJob-FinishPaymentTimeoutPeriod CompletedCampaignsJobIdlePeriod: settings-key: SmartVouchersJob-CompletedCampaignsJobIdlePeriod + Notifications: + VoucherRedemptionSucceededPushNotificationTemplateId: + settings-key: SmartVouchersService-Notifications-VoucherRedemptionSucceededPushNotificationTemplateId SlackNotifications: AzureQueue: ConnectionString: diff --git a/src/MAVN.Job.SmartVouchers/Modules/RabbitMqModule.cs b/src/MAVN.Job.SmartVouchers/Modules/RabbitMqModule.cs index 1a472bf..72763d9 100644 --- a/src/MAVN.Job.SmartVouchers/Modules/RabbitMqModule.cs +++ b/src/MAVN.Job.SmartVouchers/Modules/RabbitMqModule.cs @@ -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; @@ -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; @@ -47,6 +49,9 @@ private void RegisterRabbitMqPublishers(ContainerBuilder builder) builder.RegisterJsonRabbitPublisher( _settings.Publishers.ConnectionString, PubVoucherTransferredExchangeName); + builder.RegisterJsonRabbitPublisher( + _settings.Publishers.ConnectionString, + PubPushNotificationExchangeName); } private void RegisterRabbitMqSubscribers(ContainerBuilder builder) diff --git a/src/MAVN.Job.SmartVouchers/Modules/ServiceModule.cs b/src/MAVN.Job.SmartVouchers/Modules/ServiceModule.cs index c859f43..1930de6 100644 --- a/src/MAVN.Job.SmartVouchers/Modules/ServiceModule.cs +++ b/src/MAVN.Job.SmartVouchers/Modules/ServiceModule.cs @@ -54,6 +54,11 @@ protected override void Load(ContainerBuilder builder) .As() .SingleInstance(); + builder.RegisterType() + .As() + .WithParameter(TypedParameter.From(_settings.SmartVouchersJob.Notifications.VoucherRedemptionSucceededPushNotificationTemplateId)) + .SingleInstance(); + builder.RegisterPaymentManagementClient(_settings.PaymentManagementServiceClient, null); builder.RegisterPartnerManagementClient(_settings.PartnerManagementServiceClient, null); builder.RegisterCustomerProfileClient(_settings.CustomerProfileServiceClient, null); diff --git a/src/MAVN.Job.SmartVouchers/Settings/JobSettings/NotificationsSettings.cs b/src/MAVN.Job.SmartVouchers/Settings/JobSettings/NotificationsSettings.cs new file mode 100644 index 0000000..c258bf8 --- /dev/null +++ b/src/MAVN.Job.SmartVouchers/Settings/JobSettings/NotificationsSettings.cs @@ -0,0 +1,7 @@ +namespace MAVN.Job.SmartVouchers.Settings.JobSettings +{ + public class NotificationsSettings + { + public string VoucherRedemptionSucceededPushNotificationTemplateId { get; set; } + } +} diff --git a/src/MAVN.Job.SmartVouchers/Settings/JobSettings/SmartVouchersJobSettings.cs b/src/MAVN.Job.SmartVouchers/Settings/JobSettings/SmartVouchersJobSettings.cs index 0935425..df31564 100644 --- a/src/MAVN.Job.SmartVouchers/Settings/JobSettings/SmartVouchersJobSettings.cs +++ b/src/MAVN.Job.SmartVouchers/Settings/JobSettings/SmartVouchersJobSettings.cs @@ -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; } diff --git a/src/MAVN.Service.SmartVouchers.Domain/Services/INotificationsService.cs b/src/MAVN.Service.SmartVouchers.Domain/Services/INotificationsService.cs new file mode 100644 index 0000000..420ac0e --- /dev/null +++ b/src/MAVN.Service.SmartVouchers.Domain/Services/INotificationsService.cs @@ -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); + } +} diff --git a/src/MAVN.Service.SmartVouchers.DomainServices/MAVN.Service.SmartVouchers.DomainServices.csproj b/src/MAVN.Service.SmartVouchers.DomainServices/MAVN.Service.SmartVouchers.DomainServices.csproj index adfbdc3..01c6f60 100644 --- a/src/MAVN.Service.SmartVouchers.DomainServices/MAVN.Service.SmartVouchers.DomainServices.csproj +++ b/src/MAVN.Service.SmartVouchers.DomainServices/MAVN.Service.SmartVouchers.DomainServices.csproj @@ -6,6 +6,7 @@ + diff --git a/src/MAVN.Service.SmartVouchers.DomainServices/NotificationsService.cs b/src/MAVN.Service.SmartVouchers.DomainServices/NotificationsService.cs new file mode 100644 index 0000000..c2c4402 --- /dev/null +++ b/src/MAVN.Service.SmartVouchers.DomainServices/NotificationsService.cs @@ -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 _pushNotificationPublisher; + private readonly string _voucherRedemptionSucceededTemplateId; + + public NotificationsService( + IRabbitPublisher 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 + { + {"PartnerName", partnerName}, {"VoucherShortCode", voucherShortCode} + }, + CustomPayload = new Dictionary + { + {"route", "voucher-usage-success"}, + {"partner-name", partnerName}, + {"voucher-short-code", voucherShortCode} + } + }); + } + } +} diff --git a/src/MAVN.Service.SmartVouchers.DomainServices/VouchersService.cs b/src/MAVN.Service.SmartVouchers.DomainServices/VouchersService.cs index 91aa8a7..0321027 100644 --- a/src/MAVN.Service.SmartVouchers.DomainServices/VouchersService.cs +++ b/src/MAVN.Service.SmartVouchers.DomainServices/VouchersService.cs @@ -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 _voucherSoldPublisher; private readonly IRabbitPublisher _voucherUsedPublisher; private readonly IRabbitPublisher _voucherTransferredPublisher; @@ -49,6 +50,7 @@ public VouchersService( IPaymentRequestsRepository paymentRequestsRepository, ILogFactory logFactory, IRedisLocksService redisLocksService, + INotificationsService notificationsService, IRabbitPublisher voucherSoldPublisher, IRabbitPublisher voucherUsedPublisher, IRabbitPublisher voucherTransferredPublisher, @@ -61,6 +63,7 @@ public VouchersService( _campaignsRepository = campaignsRepository; _paymentRequestsRepository = paymentRequestsRepository; _redisLocksService = redisLocksService; + _notificationsService = notificationsService; _voucherSoldPublisher = voucherSoldPublisher; _log = logFactory.CreateLog(this); _lockTimeOut = lockTimeOut; @@ -418,6 +421,13 @@ public async Task 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; } diff --git a/src/MAVN.Service.SmartVouchers/Modules/RabbitMqModule.cs b/src/MAVN.Service.SmartVouchers/Modules/RabbitMqModule.cs index 4b16920..f9aa2c5 100644 --- a/src/MAVN.Service.SmartVouchers/Modules/RabbitMqModule.cs +++ b/src/MAVN.Service.SmartVouchers/Modules/RabbitMqModule.cs @@ -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; @@ -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; @@ -46,6 +48,9 @@ private void RegisterRabbitMqPublishers(ContainerBuilder builder) builder.RegisterJsonRabbitPublisher( _settings.Publishers.ConnectionString, PubVoucherTransferredExchangeName); + builder.RegisterJsonRabbitPublisher( + _settings.Publishers.ConnectionString, + PubPushNotificationExchangeName); } private void RegisterRabbitMqSubscribers(ContainerBuilder builder) diff --git a/src/MAVN.Service.SmartVouchers/Modules/ServiceModule.cs b/src/MAVN.Service.SmartVouchers/Modules/ServiceModule.cs index e44b403..e246ba3 100644 --- a/src/MAVN.Service.SmartVouchers/Modules/ServiceModule.cs +++ b/src/MAVN.Service.SmartVouchers/Modules/ServiceModule.cs @@ -68,6 +68,11 @@ protected override void Load(ContainerBuilder builder) .As() .SingleInstance(); + builder.RegisterType() + .As() + .WithParameter(TypedParameter.From(_settings.SmartVouchersService.Notifications.VoucherRedemptionSucceededPushNotificationTemplateId)) + .SingleInstance(); + builder.RegisterPaymentManagementClient(_settings.PaymentManagementServiceClient, null); builder.RegisterPartnerManagementClient(_settings.PartnerManagementServiceClient, null); builder.RegisterCustomerProfileClient(_settings.CustomerProfileServiceClient, null); diff --git a/src/MAVN.Service.SmartVouchers/Settings/NotificationsSettings.cs b/src/MAVN.Service.SmartVouchers/Settings/NotificationsSettings.cs new file mode 100644 index 0000000..20835c0 --- /dev/null +++ b/src/MAVN.Service.SmartVouchers/Settings/NotificationsSettings.cs @@ -0,0 +1,7 @@ +namespace MAVN.Service.SmartVouchers.Settings +{ + public class NotificationsSettings + { + public string VoucherRedemptionSucceededPushNotificationTemplateId { get; set; } + } +} diff --git a/src/MAVN.Service.SmartVouchers/Settings/SmartVouchersSettings.cs b/src/MAVN.Service.SmartVouchers/Settings/SmartVouchersSettings.cs index 55779d0..e7bb8d4 100644 --- a/src/MAVN.Service.SmartVouchers/Settings/SmartVouchersSettings.cs +++ b/src/MAVN.Service.SmartVouchers/Settings/SmartVouchersSettings.cs @@ -14,5 +14,7 @@ public class SmartVouchersSettings public RedisSettings Redis { set; get; } public TimeSpan VoucherLockTimeOut { get; set; } + + public NotificationsSettings Notifications { get; set; } } }