Skip to content

Commit

Permalink
Merge pull request #11 from IliyanIlievPH/10
Browse files Browse the repository at this point in the history
Closes #10
  • Loading branch information
starkmsu committed Jun 17, 2020
2 parents 6a7045f + 60bda89 commit e543d90
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 2 deletions.
21 changes: 20 additions & 1 deletion settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ KycService:
settings-key: UserManagementRabbitMQ
types:
- RabbitMq
BackOfficeLink:
settings-key: AdminManagementService-BackOfficeLink
KycApprovedEmail:
EmailTemplateId:
settings-key: KycService-KycApprovedEmail-EmailTemplateId
SubjectTemplateId:
settings-key: KycService-KycApprovedEmail-SubjectTemplateId
VoucherManagerUrl:
settings-key: KycService-KycApprovedEmail-VoucherManagerUrl
KycRejectedEmail:
EmailTemplateId:
settings-key: KycService-KycRejectedEmail-EmailTemplateId
SubjectTemplateId:
settings-key: KycService-KycRejectedEmail-SubjectTemplateId
SlackNotifications:
AzureQueue:
ConnectionString:
Expand All @@ -28,4 +42,9 @@ SlackNotifications:
MonitoringServiceClient:
MonitoringServiceUrl:
settings-key: MonitoringServiceUrl

PartnerManagementService:
ServiceUrl:
settings-key: PartnerManagementServiceUrl
AdminManagementService:
ServiceUrl:
settings-key: AdminManagementServiceUrl
13 changes: 13 additions & 0 deletions src/MAVN.Service.Kyc.Domain/Services/INotificationsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Threading.Tasks;

namespace MAVN.Service.Kyc.Domain.Services
{
public interface INotificationsService
{
Task NotifyKycApprovedAsync(string adminUserId, string adminUserEmail, string adminUserName,
string partnerName);

Task NotifyKycRejectedAsync(string adminUserId, string adminUserEmail, string adminUserName, string partnerName,
string rejectionComment);
}
}
46 changes: 45 additions & 1 deletion src/MAVN.Service.Kyc.DomainServices/KycService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,39 @@
using System.Threading.Tasks;
using Common.Log;
using Lykke.Common.Log;
using MAVN.Service.AdminManagement.Client;
using MAVN.Service.AdminManagement.Client.Models.Enums;
using MAVN.Service.AdminManagement.Client.Models.Requests;
using MAVN.Service.Kyc.Domain.Enums;
using MAVN.Service.Kyc.Domain.Models;
using MAVN.Service.Kyc.Domain.Repositories;
using MAVN.Service.Kyc.Domain.Services;
using MAVN.Service.PartnerManagement.Client;

namespace MAVN.Service.Kyc.DomainServices
{
public class KycService : IKycService
{
private readonly IKycInformationRepository _kycInformationRepository;
private readonly IKycStatusChangeRepository _kycStatusChangeRepository;
private readonly INotificationsService _notificationsService;
private IPartnerManagementClient _partnerManagementClient;
private readonly IAdminManagementServiceClient _adminManagementClient;
private readonly ILog _log;

public KycService(IKycInformationRepository kycInformationRepository, IKycStatusChangeRepository kycStatusChangeRepository, ILogFactory logFactory)
public KycService(
IKycInformationRepository kycInformationRepository,
IKycStatusChangeRepository kycStatusChangeRepository,
INotificationsService notificationsService,
IPartnerManagementClient partnerManagementClient,
IAdminManagementServiceClient adminManagementClient,
ILogFactory logFactory)
{
_kycInformationRepository = kycInformationRepository;
_kycStatusChangeRepository = kycStatusChangeRepository;
_notificationsService = notificationsService;
_partnerManagementClient = partnerManagementClient;
_adminManagementClient = adminManagementClient;
_log = logFactory.CreateLog(this);
}

Expand Down Expand Up @@ -71,6 +87,34 @@ public async Task<UpdateKycStatusErrorCode> UpdateKycInfoAsync(KycInformation mo

await _kycInformationRepository.UpdateAsync(model);

if (model.KycStatus != KycStatus.Accepted && model.KycStatus != KycStatus.Rejected)
return UpdateKycStatusErrorCode.None;

var admin = await _adminManagementClient.AdminsApi.GetByIdAsync(
new GetAdminByIdRequestModel { AdminUserId = model.AdminUserId.ToString() });

if (admin.Error != AdminUserResponseErrorCodes.None || admin.Profile == null)
{
_log.Warning("Missing admin when trying to send KYC notification", context: model.AdminUserId);
return UpdateKycStatusErrorCode.None;
}

var partner = await _partnerManagementClient.Partners.GetByIdAsync(model.PartnerId);

if (partner == null)
{
_log.Warning("Missing partner when trying to send KYC notification", context: model.PartnerId);
return UpdateKycStatusErrorCode.None;
}

if (model.KycStatus == KycStatus.Accepted)
await _notificationsService.NotifyKycApprovedAsync(model.AdminUserId.ToString(),
admin.Profile.Email,
admin.Profile.FirstName, partner.Name);
else
await _notificationsService.NotifyKycRejectedAsync(model.AdminUserId.ToString(),
admin.Profile.Email, admin.Profile.FirstName, partner.Name, model.Comment);

return UpdateKycStatusErrorCode.None;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="Lykke.RabbitMqBroker" Version="7.13.1" />
<PackageReference Include="MAVN.Service.AdminManagement.Client" Version="1.4.0" />
<PackageReference Include="MAVN.Service.NotificationSystem.SubscriberContract" Version="1.1.0" />
<PackageReference Include="MAVN.Service.PartnerManagement.Client" Version="2.12.0" />
<PackageReference Include="MAVN.Service.PartnerManagement.Contract" Version="2.3.0" />
</ItemGroup>
<ItemGroup>
Expand Down
79 changes: 79 additions & 0 deletions src/MAVN.Service.Kyc.DomainServices/NotificationsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Lykke.Common;
using Lykke.RabbitMqBroker.Publisher;
using MAVN.Service.Kyc.Domain.Services;
using MAVN.Service.NotificationSystem.SubscriberContract;

namespace MAVN.Service.Kyc.DomainServices
{
public class NotificationsService : INotificationsService
{
private readonly IRabbitPublisher<EmailMessageEvent> _emailsPublisher;
private readonly string _backOfficeUrl;
private readonly string _kycApprovedEmailTemplateId;
private readonly string _kycApprovedEmailSubjectTemplateId;
private readonly string _kycRejectedEmailTemplateId;
private readonly string _kycRejectedEmailSubjectTemplateId;
private readonly string _voucherManagerUrl;

public NotificationsService(
IRabbitPublisher<EmailMessageEvent> emailsPublisher,
string backOfficeUrl,
string kycApprovedEmailTemplateId,
string kycApprovedEmailSubjectTemplateId,
string kycRejectedEmailTemplateId,
string kycRejectedEmailSubjectTemplateId,
string voucherManagerUrl)
{
_emailsPublisher = emailsPublisher;
_backOfficeUrl = backOfficeUrl;
_kycApprovedEmailTemplateId = kycApprovedEmailTemplateId;
_kycApprovedEmailSubjectTemplateId = kycApprovedEmailSubjectTemplateId;
_kycRejectedEmailTemplateId = kycRejectedEmailTemplateId;
_kycRejectedEmailSubjectTemplateId = kycRejectedEmailSubjectTemplateId;
_voucherManagerUrl = voucherManagerUrl;
}

public async Task NotifyKycApprovedAsync(string adminUserId, string adminUserEmail, string adminUserName, string partnerName)
{
var values = new Dictionary<string, string>
{
{"BusinessName", partnerName},
{"VoucherManagerUrl", _backOfficeUrl + _voucherManagerUrl},
{"AdminUserName", !string.IsNullOrEmpty(adminUserName) ? $" {adminUserName}" : adminUserName},
};

await SendEmailAsync(adminUserId, adminUserEmail, values, _kycApprovedEmailTemplateId,
_kycApprovedEmailSubjectTemplateId);
}

public async Task NotifyKycRejectedAsync(string adminUserId, string adminUserEmail, string adminUserName, string partnerName, string rejectionComment)
{
var values = new Dictionary<string, string>
{
{"BusinessName", partnerName},
{"RejectionComment", rejectionComment},
{"AdminUserName", !string.IsNullOrEmpty(adminUserName) ? $" {adminUserName}" : adminUserName},
};

await SendEmailAsync(adminUserId, adminUserEmail, values, _kycRejectedEmailTemplateId,
_kycRejectedEmailSubjectTemplateId);
}

private async Task SendEmailAsync(string customerId, string destination, Dictionary<string, string> values, string emailTemplateId, string subjectTemplateId)
{
if (!string.IsNullOrWhiteSpace(destination))
values["target_email"] = destination;

await _emailsPublisher.PublishAsync(new EmailMessageEvent
{
CustomerId = customerId,
MessageTemplateId = emailTemplateId,
SubjectTemplateId = subjectTemplateId,
TemplateParameters = values,
Source = $"{AppEnvironment.Name} - {AppEnvironment.Version}"
});
}
}
}
5 changes: 5 additions & 0 deletions src/MAVN.Service.Kyc/Modules/RabbitMqModule.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Autofac;
using JetBrains.Annotations;
using Lykke.RabbitMqBroker.Publisher;
using Lykke.RabbitMqBroker.Subscriber;
using Lykke.SettingsReader;
using MAVN.Service.Kyc.DomainServices.RabbitMq.Subscribers;
using MAVN.Service.Kyc.Settings;
using MAVN.Service.NotificationSystem.SubscriberContract;
using MAVN.Service.PartnerManagement.Contract;

namespace MAVN.Service.Kyc.Modules
Expand All @@ -13,6 +15,7 @@ public class RabbitMqModule : Module
{
private const string DefaultQueueName = "kyc";
private const string PartnerCreatedExchangeName = "lykke.customer.partnercreated";
private const string NotificationSystemEmailExchangeName = "lykke.notificationsystem.command.emailmessage";

private readonly RabbitMqSettings _settings;

Expand All @@ -29,6 +32,8 @@ protected override void Load(ContainerBuilder builder)

private void RegisterRabbitMqPublishers(ContainerBuilder builder)
{
builder.RegisterJsonRabbitPublisher<EmailMessageEvent>(_settings.Publishers.ConnectionString,
NotificationSystemEmailExchangeName);
}

private void RegisterRabbitMqSubscribers(ContainerBuilder builder)
Expand Down
15 changes: 15 additions & 0 deletions src/MAVN.Service.Kyc/Modules/ServiceModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
using Lykke.Sdk;
using Lykke.Sdk.Health;
using Lykke.SettingsReader;
using MAVN.Service.AdminManagement.Client;
using MAVN.Service.Kyc.Domain.Services;
using MAVN.Service.Kyc.DomainServices;
using MAVN.Service.Kyc.Services;
using MAVN.Service.Kyc.Settings;
using MAVN.Service.PartnerManagement.Client;

namespace MAVN.Service.Kyc.Modules
{
Expand Down Expand Up @@ -38,6 +40,19 @@ protected override void Load(ContainerBuilder builder)
builder.RegisterType<KycService>()
.As<IKycService>()
.SingleInstance();

builder.RegisterType<NotificationsService>()
.As<INotificationsService>()
.WithParameter("backOfficeUrl", _appSettings.CurrentValue.KycService.BackOfficeLink)
.WithParameter("kycApprovedEmailTemplateId", _appSettings.CurrentValue.KycService.KycApprovedEmail.EmailTemplateId)
.WithParameter("kycApprovedEmailSubjectTemplateId", _appSettings.CurrentValue.KycService.KycApprovedEmail.SubjectTemplateId)
.WithParameter("kycRejectedEmailTemplateId", _appSettings.CurrentValue.KycService.KycRejectedEmail.EmailTemplateId)
.WithParameter("kycRejectedEmailSubjectTemplateId", _appSettings.CurrentValue.KycService.KycRejectedEmail.SubjectTemplateId)
.WithParameter("voucherManagerUrl", _appSettings.CurrentValue.KycService.KycApprovedEmail.VoucherManagerUrl)
.SingleInstance();

builder.RegisterAdminManagementClient(_appSettings.CurrentValue.AdminManagementService);
builder.RegisterPartnerManagementClient(_appSettings.CurrentValue.PartnerManagementService);
}
}
}
6 changes: 6 additions & 0 deletions src/MAVN.Service.Kyc/Settings/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using JetBrains.Annotations;
using Lykke.Sdk.Settings;
using MAVN.Service.AdminManagement.Client;
using MAVN.Service.PartnerManagement.Client;

namespace MAVN.Service.Kyc.Settings
{
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]
public class AppSettings : BaseAppSettings
{
public KycSettings KycService { get; set; }

public AdminManagementServiceClientSettings AdminManagementService { get; set; }

public PartnerManagementServiceClientSettings PartnerManagementService { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace MAVN.Service.Kyc.Settings.EmailSettings
{
public class KycApprovedEmail
{
public string EmailTemplateId { set; get; }
public string SubjectTemplateId { set; get; }
public string VoucherManagerUrl { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MAVN.Service.Kyc.Settings.EmailSettings
{
public class KycRejectedEmail
{
public string EmailTemplateId { set; get; }
public string SubjectTemplateId { set; get; }
}
}
7 changes: 7 additions & 0 deletions src/MAVN.Service.Kyc/Settings/KycSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using JetBrains.Annotations;
using Lykke.SettingsReader.Attributes;
using MAVN.Service.Kyc.Settings.EmailSettings;

namespace MAVN.Service.Kyc.Settings
{
Expand All @@ -9,5 +10,11 @@ public class KycSettings
public DbSettings Db { get; set; }

public RabbitMqSettings Rabbit { get; set; }

public KycApprovedEmail KycApprovedEmail { get; set; }

public KycRejectedEmail KycRejectedEmail { get; set; }

public string BackOfficeLink { get; set; }
}
}

0 comments on commit e543d90

Please sign in to comment.