Skip to content

Commit b8ef315

Browse files
committed
From email needs to be configured in environment variables
1 parent fc18979 commit b8ef315

7 files changed

Lines changed: 27 additions & 8 deletions

File tree

source/backend/api/Models/Configuration/ChesConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ public class ChesConfig
1111
public string ServiceClientId { get; set; }
1212

1313
public string ServiceClientSecret { get; set; }
14+
15+
public string FromEmail { get; set; }
1416
}
1517
}

source/backend/api/Services/ChesService.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,37 @@
33
using Microsoft.Extensions.Logging;
44
using Pims.Api.Models.Ches;
55
using Pims.Api.Models.CodeTypes;
6+
using Pims.Api.Models.Config;
67
using Pims.Api.Models.Requests.Http;
78
using Pims.Api.Repositories.Ches;
89

910
namespace Pims.Api.Services
10-
/// <summary>
11-
/// Default implementation of IEmailService using CHES.
12-
/// </summary>
11+
/// <summary>
12+
/// Default implementation of IEmailService using CHES.
13+
/// </summary>
1314
{
1415
public class ChesService : IEmailService
1516
{
1617
private readonly IEmailRepository _chesRepository;
1718
private readonly ILogger<ChesService> _logger;
19+
private readonly ChesConfig _chesConfig;
1820

19-
public ChesService(IEmailRepository chesRepository, ILogger<ChesService> logger)
21+
public ChesService(IEmailRepository chesRepository, ILogger<ChesService> logger, ChesConfig chesConfig)
2022
{
2123
_chesRepository = chesRepository;
2224
_logger = logger;
25+
_chesConfig = chesConfig;
2326
}
2427

2528
public async Task<ExternalResponse<EmailResponse>> SendEmailAsync(EmailRequest request)
2629
{
2730
_logger.LogInformation("Email send requested. Recipient count: {recipientCount}.", request.To?.Count ?? 0);
2831

32+
if (string.IsNullOrEmpty(request.From))
33+
{
34+
request.From = _chesConfig.FromEmail;
35+
}
36+
2937
ExternalResponse<EmailResponse>? response = await _chesRepository.SendEmailAsync(request);
3038

3139
if (response == null || response.Payload == null)

source/backend/api/Startup.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,13 @@ private static void AddPimsApiServices(IServiceCollection services)
586586
services.AddScoped<IManagementActivityService, ManagementActivityService>();
587587
services.AddScoped<IManagementFileStatusSolver, ManagementFileStatusSolver>();
588588
services.AddScoped<IFilePropertyLocationUpdateSolver, FilePropertyLocationUpdateSolver>();
589+
services.AddSingleton(sp =>
590+
{
591+
var config = sp.GetRequiredService<IConfiguration>();
592+
var chesConfig = new ChesConfig();
593+
config.GetSection("Ches").Bind(chesConfig);
594+
return chesConfig;
595+
});
589596
services.AddScoped<IEmailService, ChesService>();
590597
}
591598

source/backend/api/appsettings.Development.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"ServiceClientId": "",
6868
"ServiceClientSecret": "",
6969
"Issuer": "https://dev.loginproxy.gov.bc.ca/auth/realms/comsvcauth",
70-
"AuthEndpoint": "https://dev.loginproxy.gov.bc.ca/auth/realms/comsvcauth/protocol/openid-connect/token"
70+
"AuthEndpoint": "https://dev.loginproxy.gov.bc.ca/auth/realms/comsvcauth/protocol/openid-connect/token",
71+
"FromEmail": ""
7172
}
7273
}

source/backend/api/appsettings.Local.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"ServiceClientId": "",
9191
"ServiceClientSecret": "",
9292
"Issuer": "https://dev.loginproxy.gov.bc.ca/auth/realms/comsvcauth",
93-
"AuthEndpoint": "https://dev.loginproxy.gov.bc.ca/auth/realms/comsvcauth/protocol/openid-connect/token"
93+
"AuthEndpoint": "https://dev.loginproxy.gov.bc.ca/auth/realms/comsvcauth/protocol/openid-connect/token",
94+
"FromEmail": ""
9495
}
9596
}

source/backend/api/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@
161161
"AuthEndpoint": "[AUTH_ENDPOINT]",
162162
"ChesHost": "[CDOGS_HOST]",
163163
"ServiceClientId": "[CLIENT_ID]",
164-
"ServiceClientSecret": "[CLIENT_SECRET]"
164+
"ServiceClientSecret": "[CLIENT_SECRET]",
165+
"FromEmail": "[FROM_EMAIL]"
165166
},
166167
"Polly": {
167168
"MaxRetries": 3,

source/backend/apimodels/Models/Ches/EmailRequest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public class EmailRequest
3232
/// All email addresses can be plain 'sender@server.com' or formatted '"Sender Name" sender@server.com'.
3333
/// </summary>
3434
[JsonPropertyName("from")]
35-
[System.ComponentModel.DataAnnotations.Required]
3635
public string From { get; set; }
3736

3837
/// <summary>

0 commit comments

Comments
 (0)