Skip to content

Commit

Permalink
fix(mailService): added mail service
Browse files Browse the repository at this point in the history
  • Loading branch information
VPrasannaK94 committed Aug 30, 2023
1 parent f22d010 commit 69af79e
Show file tree
Hide file tree
Showing 13 changed files with 404 additions and 130 deletions.
30 changes: 30 additions & 0 deletions src/Portal.Backend.sln
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Keycloak.Seeding", "keycloa
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Keycloak.Seeding.Tests", "..\tests\keycloak\Keycloak.Seeding.Tests\Keycloak.Seeding.Tests.csproj", "{A5BEDD89-7280-466E-8D14-EC5E177AAD07}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mailing.Service", "mailing\Mailing.Service\Mailing.Service.csproj", "{6113B579-C995-47F8-9AC1-4CC6EFDDD883}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mailing.Service.Tests", "..\tests\mailing\Mailing.Service.Tests\Mailing.Service.Tests.csproj", "{1EAF34DA-6D16-4F5E-86F4-344185F53942}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1340,6 +1344,30 @@ Global
{A5BEDD89-7280-466E-8D14-EC5E177AAD07}.Release|x64.Build.0 = Release|Any CPU
{A5BEDD89-7280-466E-8D14-EC5E177AAD07}.Release|x86.ActiveCfg = Release|Any CPU
{A5BEDD89-7280-466E-8D14-EC5E177AAD07}.Release|x86.Build.0 = Release|Any CPU
{6113B579-C995-47F8-9AC1-4CC6EFDDD883}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6113B579-C995-47F8-9AC1-4CC6EFDDD883}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6113B579-C995-47F8-9AC1-4CC6EFDDD883}.Debug|x64.ActiveCfg = Debug|Any CPU
{6113B579-C995-47F8-9AC1-4CC6EFDDD883}.Debug|x64.Build.0 = Debug|Any CPU
{6113B579-C995-47F8-9AC1-4CC6EFDDD883}.Debug|x86.ActiveCfg = Debug|Any CPU
{6113B579-C995-47F8-9AC1-4CC6EFDDD883}.Debug|x86.Build.0 = Debug|Any CPU
{6113B579-C995-47F8-9AC1-4CC6EFDDD883}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6113B579-C995-47F8-9AC1-4CC6EFDDD883}.Release|Any CPU.Build.0 = Release|Any CPU
{6113B579-C995-47F8-9AC1-4CC6EFDDD883}.Release|x64.ActiveCfg = Release|Any CPU
{6113B579-C995-47F8-9AC1-4CC6EFDDD883}.Release|x64.Build.0 = Release|Any CPU
{6113B579-C995-47F8-9AC1-4CC6EFDDD883}.Release|x86.ActiveCfg = Release|Any CPU
{6113B579-C995-47F8-9AC1-4CC6EFDDD883}.Release|x86.Build.0 = Release|Any CPU
{1EAF34DA-6D16-4F5E-86F4-344185F53942}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1EAF34DA-6D16-4F5E-86F4-344185F53942}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1EAF34DA-6D16-4F5E-86F4-344185F53942}.Debug|x64.ActiveCfg = Debug|Any CPU
{1EAF34DA-6D16-4F5E-86F4-344185F53942}.Debug|x64.Build.0 = Debug|Any CPU
{1EAF34DA-6D16-4F5E-86F4-344185F53942}.Debug|x86.ActiveCfg = Debug|Any CPU
{1EAF34DA-6D16-4F5E-86F4-344185F53942}.Debug|x86.Build.0 = Debug|Any CPU
{1EAF34DA-6D16-4F5E-86F4-344185F53942}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1EAF34DA-6D16-4F5E-86F4-344185F53942}.Release|Any CPU.Build.0 = Release|Any CPU
{1EAF34DA-6D16-4F5E-86F4-344185F53942}.Release|x64.ActiveCfg = Release|Any CPU
{1EAF34DA-6D16-4F5E-86F4-344185F53942}.Release|x64.Build.0 = Release|Any CPU
{1EAF34DA-6D16-4F5E-86F4-344185F53942}.Release|x86.ActiveCfg = Release|Any CPU
{1EAF34DA-6D16-4F5E-86F4-344185F53942}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -1348,6 +1376,7 @@ Global
SolutionGuid = {2EB6265F-323A-4BF3-969E-003D64A14B64}
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{1EAF34DA-6D16-4F5E-86F4-344185F53942} = {323C198D-A8C6-4EB0-8B79-72624275E35F}
{A5BEDD89-7280-466E-8D14-EC5E177AAD07} = {323C198D-A8C6-4EB0-8B79-72624275E35F}
{EA9BA26E-83F6-47C4-BA3B-880AF1AD6A82} = {323C198D-A8C6-4EB0-8B79-72624275E35F}
{4B40193E-2C67-4DC4-8EF4-3286DAA01D8B} = {323C198D-A8C6-4EB0-8B79-72624275E35F}
Expand Down Expand Up @@ -1441,5 +1470,6 @@ Global
{47E089E3-E875-4045-9E58-C1223BE899E9} = {23500169-FC01-4D2B-A997-E7FAE2169FC0}
{9D574E57-75A6-4965-AF23-ACE0BB9CD0B3} = {323C198D-A8C6-4EB0-8B79-72624275E35F}
{E1D41A07-F468-4D13-8185-35F127230B17} = {46383371-8252-4598-9350-A97692851408}
{6113B579-C995-47F8-9AC1-4CC6EFDDD883} = {68D43DB1-DFC5-4F15-A2B4-6BA18B875F9E}
EndGlobalSection
EndGlobal
28 changes: 28 additions & 0 deletions src/mailing/Mailing.Service/IRoleBaseMailService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/********************************************************************************
* Copyright (c) 2021, 2023 BMW Group AG
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Configuration;

namespace Org.Eclipse.TractusX.Portal.Backend.Mailing.Service;

public interface IRoleBaseMailService
{
public Task RoleBaseSendMail(IEnumerable<UserRoleConfig> receiverRoles, IDictionary<string, string> parameters, IEnumerable<string> template, Guid companyId);
}
33 changes: 33 additions & 0 deletions src/mailing/Mailing.Service/Mailing.Service.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!--
- Copyright (c) 2021, 2023 BMW Group AG
- Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
-
- See the NOTICE file(s) distributed with this work for additional
- information regarding copyright ownership.
-
- This program and the accompanying materials are made available under the
- terms of the Apache License, Version 2.0 which is available at
- https://www.apache.org/licenses/LICENSE-2.0.
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- License for the specific language governing permissions and limitations
- under the License.
-
- SPDX-License-Identifier: Apache-2.0
-->
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\portalbackend\PortalBackend.DBAccess\PortalBackend.DBAccess.csproj" />
<ProjectReference Include="..\..\portalbackend\PortalBackend.PortalEntities\PortalBackend.PortalEntities.csproj" />
<ProjectReference Include="..\..\framework\Framework.Models\Framework.Models.csproj" />
<ProjectReference Include="..\Mailing.SendMail\Mailing.SendMail.csproj" />
</ItemGroup>
<PropertyGroup>
<AssemblyName>Org.Eclipse.TractusX.Portal.Backend.Mailing.Service</AssemblyName>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
68 changes: 68 additions & 0 deletions src/mailing/Mailing.Service/RoleBaseMailService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/********************************************************************************
* Copyright (c) 2021, 2023 BMW Group AG
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Configuration;
using Org.Eclipse.TractusX.Portal.Backend.Mailing.SendMail;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Repositories;

namespace Org.Eclipse.TractusX.Portal.Backend.Mailing.Service;

public class RoleBaseMailService : IRoleBaseMailService
{

private readonly IPortalRepositories _portalRepositories;
private readonly IMailingService _mailingService;

public RoleBaseMailService(IPortalRepositories portalRepositories, IMailingService mailingService)
{
_portalRepositories = portalRepositories;
_mailingService = mailingService;
}
public async Task RoleBaseSendMail(IEnumerable<UserRoleConfig> receiverRoles, IDictionary<string, string> parameters, IEnumerable<string> template, Guid companyId)
{
var receiverUserRoles = receiverRoles;
var userRolesRepository = _portalRepositories.GetInstance<IUserRolesRepository>();
var roleData = await userRolesRepository
.GetUserRoleIdsUntrackedAsync(receiverUserRoles)
.ToListAsync()
.ConfigureAwait(false);
if (roleData.Count < receiverUserRoles.Sum(clientRoles => clientRoles.UserRoleNames.Count()))
{
throw new ConfigurationException(
$"invalid configuration, at least one of the configured roles does not exist in the database: {string.Join(", ", receiverUserRoles.Select(clientRoles => $"client: {clientRoles.ClientId}, roles: [{string.Join(", ", clientRoles.UserRoleNames)}]"))}");
}

var companyUserWithRoleIdForCompany = _portalRepositories.GetInstance<IUserRepository>()
.GetCompanyUserEmailForCompanyAndRoleId(roleData, companyId);
await foreach (var (receiver, firstName, lastName) in companyUserWithRoleIdForCompany)
{
var userName = string.Join(" ", new[] { firstName, lastName }.Where(item => !string.IsNullOrWhiteSpace(item)));

if (!string.IsNullOrWhiteSpace(userName) && parameters.Keys.Contains("offerProviderName"))
{
parameters["offerProviderName"] = userName;
}

await _mailingService.SendMails(receiver, parameters, template).ConfigureAwait(false);
}
}
}
1 change: 1 addition & 0 deletions src/marketplace/Offers.Library/Offers.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<ProjectReference Include="..\..\notifications\Notifications.Library\Notifications.Library.csproj" />
<ProjectReference Include="..\..\processes\OfferSubscription.Library\OfferSubscription.Library.csproj" />
<ProjectReference Include="..\..\provisioning\Provisioning.Library\Provisioning.Library.csproj" />
<ProjectReference Include="..\..\mailing\Mailing.Service\Mailing.Service.csproj" />
</ItemGroup>

</Project>
42 changes: 9 additions & 33 deletions src/marketplace/Offers.Library/Service/OfferService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Configuration;
using Org.Eclipse.TractusX.Portal.Backend.Mailing.SendMail;
using Org.Eclipse.TractusX.Portal.Backend.Mailing.Service;
using Org.Eclipse.TractusX.Portal.Backend.Notifications.Library;
using Org.Eclipse.TractusX.Portal.Backend.Offers.Library.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess;
Expand All @@ -40,7 +40,7 @@ public class OfferService : IOfferService
{
private readonly IPortalRepositories _portalRepositories;
private readonly INotificationService _notificationService;
private readonly IMailingService _mailingService;
private readonly IRoleBaseMailService _roleBaseMailService;
private readonly IOfferSetupService _offerSetupService;

/// <summary>
Expand All @@ -52,12 +52,12 @@ public class OfferService : IOfferService
/// <param name="offerSetupService">The offer Setup Service</param>
public OfferService(IPortalRepositories portalRepositories,
INotificationService notificationService,
IMailingService mailingService,
IRoleBaseMailService roleBaseMailService,
IOfferSetupService offerSetupService)
{
_portalRepositories = portalRepositories;
_notificationService = notificationService;
_mailingService = mailingService;
_roleBaseMailService = roleBaseMailService;
_offerSetupService = offerSetupService;
}

Expand Down Expand Up @@ -566,38 +566,14 @@ public async Task DeclineOfferAsync(Guid offerId, Guid userId, OfferDeclineReque

await _portalRepositories.SaveAsync().ConfigureAwait(false);

await SendMail(notificationRecipients, declineData.OfferName, basePortalAddress, data.Message, declineData.CompanyId.Value);
}

private async Task SendMail(IEnumerable<UserRoleConfig> receiverRoles, string offerName, string basePortalAddress, string message, Guid companyId)
{
var receiverUserRoles = receiverRoles;
var userRolesRepository = _portalRepositories.GetInstance<IUserRolesRepository>();
var roleData = await userRolesRepository
.GetUserRoleIdsUntrackedAsync(receiverUserRoles)
.ToListAsync()
.ConfigureAwait(false);
if (roleData.Count < receiverUserRoles.Sum(clientRoles => clientRoles.UserRoleNames.Count()))
{
throw new ConfigurationException(
$"invalid configuration, at least one of the configured roles does not exist in the database: {string.Join(", ", receiverUserRoles.Select(clientRoles => $"client: {clientRoles.ClientId}, roles: [{string.Join(", ", clientRoles.UserRoleNames)}]"))}");
}

var companyUserWithRoleIdForCompany = _portalRepositories.GetInstance<IUserRepository>()
.GetCompanyUserEmailForCompanyAndRoleId(roleData, companyId);
await foreach (var (receiver, firstName, lastName) in companyUserWithRoleIdForCompany)
{
var userName = string.Join(" ", new[] { firstName, lastName }.Where(item => !string.IsNullOrWhiteSpace(item)));

var mailParams = new Dictionary<string, string>
var mailParams = new Dictionary<string, string>
{
{ "offerName", offerName },
{ "offerName", declineData.OfferName },
{ "url", basePortalAddress },
{ "declineMessage", message },
{ "offerProviderName", !string.IsNullOrWhiteSpace(userName) ? userName : "Service Manager"},
{ "declineMessage", data.Message },
{ "offerProviderName", "Service Manager"},
};
await _mailingService.SendMails(receiver, mailParams, new List<string> { "offer-request-decline" }).ConfigureAwait(false);
}
await _roleBaseMailService.RoleBaseSendMail(notificationRecipients, mailParams, new List<string> { "offer-request-decline" }, declineData.CompanyId.Value).ConfigureAwait(false);
}

private async Task CheckLanguageCodesExist(IEnumerable<string> languageCodes)
Expand Down
45 changes: 11 additions & 34 deletions src/marketplace/Offers.Library/Service/OfferSubscriptionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Configuration;
using Org.Eclipse.TractusX.Portal.Backend.Mailing.SendMail;
using Org.Eclipse.TractusX.Portal.Backend.Mailing.Service;
using Org.Eclipse.TractusX.Portal.Backend.Offers.Library.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
Expand All @@ -34,7 +34,7 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Offers.Library.Service;
public class OfferSubscriptionService : IOfferSubscriptionService
{
private readonly IPortalRepositories _portalRepositories;
private readonly IMailingService _mailingService;
private readonly IRoleBaseMailService _roleBaseMailService;

/// <summary>
/// Constructor.
Expand All @@ -43,10 +43,10 @@ public class OfferSubscriptionService : IOfferSubscriptionService
/// <param name="mailingService">Mail service.</param>
public OfferSubscriptionService(
IPortalRepositories portalRepositories,
IMailingService mailingService)
IRoleBaseMailService roleBaseMailService)
{
_portalRepositories = portalRepositories;
_mailingService = mailingService;
_roleBaseMailService = roleBaseMailService;
}

/// <inheritdoc />
Expand All @@ -71,7 +71,13 @@ public async Task<Guid> AddOfferSubscriptionAsync(Guid offerId, IEnumerable<Offe
CreateConsentsForSubscription(offerSubscription.Id, offerAgreementConsentData, companyInformation.CompanyId, identity.UserId);
await _portalRepositories.SaveAsync().ConfigureAwait(false);

await SendMail(notificationRecipients, offerProviderDetails.OfferName!, basePortalAddress, offerProviderDetails.ProviderCompanyId.Value);
var mailParams = new Dictionary<string, string>
{
{ "offerName", offerProviderDetails.OfferName! },
{ "offerProviderName", "User"},
{ "url", basePortalAddress }
};
await _roleBaseMailService.RoleBaseSendMail(notificationRecipients, mailParams, new List<string> { "subscription-request" }, offerProviderDetails.ProviderCompanyId.Value).ConfigureAwait(false);
return offerSubscription.Id;
}

Expand Down Expand Up @@ -172,33 +178,4 @@ public static IEnumerable<OfferSubscriptionStatusId> GetOfferSubscriptionFilterS
OfferSubscriptionStatusId.PENDING => _offerSubcriptionStatusIdFilterPending,
_ => _offerSubcriptionStatusIdFilterDefault
};
private async Task SendMail(IEnumerable<UserRoleConfig> receiverRoles, string offerName, string basePortalAddress, Guid companyId)
{
var receiverUserRoles = receiverRoles;
var userRolesRepository = _portalRepositories.GetInstance<IUserRolesRepository>();
var roleData = await userRolesRepository
.GetUserRoleIdsUntrackedAsync(receiverUserRoles)
.ToListAsync()
.ConfigureAwait(false);
if (roleData.Count < receiverUserRoles.Sum(clientRoles => clientRoles.UserRoleNames.Count()))
{
throw new ConfigurationException(
$"invalid configuration, at least one of the configured roles does not exist in the database: {string.Join(", ", receiverUserRoles.Select(clientRoles => $"client: {clientRoles.ClientId}, roles: [{string.Join(", ", clientRoles.UserRoleNames)}]"))}");
}

var companyUserWithRoleIdForCompany = _portalRepositories.GetInstance<IUserRepository>()
.GetCompanyUserEmailForCompanyAndRoleId(roleData, companyId);
await foreach (var (receiver, firstName, lastName) in companyUserWithRoleIdForCompany)
{
var userName = string.Join(" ", new[] { firstName, lastName }.Where(item => !string.IsNullOrWhiteSpace(item)));

var mailParams = new Dictionary<string, string>
{
{ "offerName", offerName },
{ "offerProviderName", !string.IsNullOrWhiteSpace(userName) ? userName : "User"},
{ "url", basePortalAddress }
};
await _mailingService.SendMails(receiver, mailParams, new List<string> { "subscription-request" }).ConfigureAwait(false);
}
}
}
Loading

0 comments on commit 69af79e

Please sign in to comment.