Skip to content

Commit

Permalink
My suggested changes to ensure better separation between copied and l…
Browse files Browse the repository at this point in the history
…ocal code

Allow copied code from core to use #if LOCALTEST

This makes it simpler to maintain (almost) the code in multiple
repositories greatly simplifying maintanance when copying changes.
  • Loading branch information
ivarne committed Nov 17, 2023
1 parent a854230 commit 03d7ba0
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 49 deletions.
3 changes: 3 additions & 0 deletions src/LocalTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<TargetFramework>net6.0</TargetFramework>
<UserSecretsId>56f36ce2-b44b-415e-a8a5-f399a76e35b9</UserSecretsId>
<ImplicitUsings>enable</ImplicitUsings>

<!-- Allow copied code to use #if LOCALTEST to maintain changes for localtest from core services -->
<DefineConstants>$(DefineConstants);LOCALTEST</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using Altinn.Notifications.Core.Models;
#nullable enable
#if !LOCALTEST
using Altinn.Notifications.Configuration;
#endif
using Altinn.Notifications.Core.Models;
using Altinn.Notifications.Core.Models.Orders;
using Altinn.Notifications.Core.Services.Interfaces;
using Altinn.Notifications.Extensions;
Expand All @@ -8,15 +12,28 @@

using FluentValidation;

#if !LOCALTEST
using Microsoft.AspNetCore.Authorization;
#endif
using Microsoft.AspNetCore.Mvc;

#if !LOCALTEST
using Swashbuckle.AspNetCore.Annotations;
using Swashbuckle.AspNetCore.Filters;
#endif

namespace Altinn.Notifications.Controllers;

/// <summary>
/// Controller for all operations related to email notification orders
/// </summary>
[Route("notifications/api/v1/orders/email")]
[ApiController]
#if !LOCALTEST
[Authorize(Policy = AuthorizationConstants.POLICY_CREATE_SCOPE_OR_PLATFORM_ACCESS)]
[SwaggerResponse(401, "Caller is unauthorized")]
[SwaggerResponse(403, "Caller is not authorized to access the requested resource")]
# endif
public class EmailNotificationOrdersController : ControllerBase
{
private readonly IValidator<EmailNotificationOrderRequestExt> _validator;
Expand All @@ -42,6 +59,11 @@ public EmailNotificationOrdersController(IValidator<EmailNotificationOrderReques
[HttpPost]
[Consumes("application/json")]
[Produces("application/json")]
#if !LOCALTEST
[SwaggerResponse(202, "The notification order was accepted", typeof(OrderIdExt))]
[SwaggerResponse(400, "The notification order is invalid", typeof(ValidationProblemDetails))]
[SwaggerResponseHeader(202, "Location", "string", "Link to access the newly created notification order.")]
#endif
public async Task<ActionResult<OrderIdExt>> Post(EmailNotificationOrderRequestExt emailNotificationOrderRequest)
{
var validationResult = _validator.Validate(emailNotificationOrderRequest);
Expand All @@ -51,10 +73,19 @@ public async Task<ActionResult<OrderIdExt>> Post(EmailNotificationOrderRequestEx
return ValidationProblem(ModelState);
}

#if LOCALTEST
string creator = "localtest";
#else
string? creator = HttpContext.GetOrg();

if (creator == null)
{
return Forbid();
}
#endif

var orderRequest = emailNotificationOrderRequest.MapToOrderRequest(creator);
(NotificationOrder registeredOrder, ServiceError error) = await _orderService.RegisterEmailNotificationOrder(orderRequest);
(NotificationOrder? registeredOrder, ServiceError? error) = await _orderService.RegisterEmailNotificationOrder(orderRequest);

if (error != null)
{
Expand Down
19 changes: 0 additions & 19 deletions src/Notifications/API/Extensions/HttpContextExtensions.cs

This file was deleted.

12 changes: 12 additions & 0 deletions src/Notifications/Core/Configuration/NotificationOrderConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Altinn.Notifications.Core.Configuration;

/// <summary>
/// Configuration class for notification orders
/// </summary>
public class NotificationOrderConfig
{
/// <summary>
/// Default from address for email notifications
/// </summary>
public string DefaultEmailFromAddress { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#nullable enable
using System.Text.Json;
using System.Text.Json.Serialization;

using Altinn.Notifications.Core.Enums;
using Altinn.Notifications.Core.Models.Orders;

namespace Altinn.Notifications.Core.Models.Notification;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#nullable enable
using Altinn.Notifications.Core.Configuration;
using Altinn.Notifications.Core.Models;
using Altinn.Notifications.Core.Models.NotificationTemplate;
using Altinn.Notifications.Core.Models.Orders;
using Altinn.Notifications.Core.Repository.Interfaces;
using Altinn.Notifications.Core.Services.Interfaces;

using Microsoft.Extensions.Options;

namespace Altinn.Notifications.Core.Services;

/// <summary>
Expand All @@ -20,12 +23,12 @@ public class EmailNotificationOrderService : IEmailNotificationOrderService
/// <summary>
/// Initializes a new instance of the <see cref="EmailNotificationOrderService"/> class.
/// </summary>
public EmailNotificationOrderService(IOrderRepository repository, IGuidService guid, IDateTimeService dateTime)
public EmailNotificationOrderService(IOrderRepository repository, IGuidService guid, IDateTimeService dateTime, IOptions<NotificationOrderConfig> config)
{
_repository = repository;
_guid = guid;
_dateTime = dateTime;
_defaultFromAddress = "[email protected]";
_defaultFromAddress = config.Value.DefaultEmailFromAddress;
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

namespace LocalTest.Notifications.Persistence.Repository
{
public class OrderRepository : IOrderRepository
public class LocalOrderRepository : IOrderRepository
{
private readonly LocalPlatformSettings _localPlatformSettings;
private readonly JsonSerializerOptions _serializerOptions;

public OrderRepository(
public LocalOrderRepository(
IOptions<LocalPlatformSettings> localPlatformSettings)
{
_localPlatformSettings = localPlatformSettings.Value;
Expand All @@ -35,13 +35,13 @@ public Task<NotificationOrder> Create(NotificationOrder order)

string serializedOrder = JsonSerializer.Serialize(order, _serializerOptions);
FileInfo file = new FileInfo(path);
file.Directory.Create();
file.Directory?.Create();
File.WriteAllText(file.FullName, serializedOrder);

return Task.FromResult(order);
}

public Task<NotificationOrder> GetOrderById(Guid id, string creator)
public Task<NotificationOrder?> GetOrderById(Guid id, string creator)
{
throw new NotImplementedException();
}
Expand All @@ -51,7 +51,7 @@ public Task<List<NotificationOrder>> GetOrdersBySendersReference(string sendersR
throw new NotImplementedException();
}

public Task<NotificationOrderWithStatus> GetOrderWithStatusById(Guid id, string creator)
public Task<NotificationOrderWithStatus?> GetOrderWithStatusById(Guid id, string creator)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Altinn.Notifications.Core.Configuration;
using Altinn.Notifications.Core.Repository.Interfaces;
using Altinn.Notifications.Core.Services;
using Altinn.Notifications.Core.Services.Interfaces;
using Altinn.Notifications.Extensions;
using Altinn.Notifications.Models;
using Altinn.Notifications.Validators;
using FluentValidation;
using LocalTest.Notifications.Persistence.Repository;

namespace LocalTest.Notifications.LocalTestNotifications;

public static class NotificationsServiceExtentions
{
public static void AddNotificationServices(this IServiceCollection services, string baseUrl)
{
// Notifications services
ValidatorOptions.Global.LanguageManager.Enabled = false;
ResourceLinkExtensions.Initialize(baseUrl);

services.Configure<NotificationOrderConfig>((c) => c.DefaultEmailFromAddress = "[email protected]");

services
.AddSingleton<IValidator<EmailNotificationOrderRequestExt>, EmailNotificationOrderRequestValidator>()
.AddSingleton<IOrderRepository, LocalOrderRepository>()
.AddSingleton<IGuidService, GuidService>()
.AddSingleton<IDateTimeService, DateTimeService>()
.AddSingleton<IEmailNotificationOrderService, EmailNotificationOrderService>();
}
}
25 changes: 5 additions & 20 deletions src/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
using Altinn.Common.PEP.Clients;
using Altinn.Common.PEP.Implementation;
using Altinn.Common.PEP.Interfaces;
using Altinn.Notifications.Core.Repository.Interfaces;
using Altinn.Notifications.Core.Services;
using Altinn.Notifications.Core.Services.Interfaces;
using Altinn.Notifications.Extensions;
using Altinn.Notifications.Models;
using Altinn.Notifications.Validators;
using Altinn.Platform.Authorization.ModelBinding;
using Altinn.Platform.Authorization.Repositories;
using Altinn.Platform.Authorization.Repositories.Interface;
Expand All @@ -31,12 +25,10 @@
using AltinnCore.Authentication.Constants;
using AltinnCore.Authentication.JwtCookie;

using FluentValidation;

using LocalTest.Clients.CdnAltinnOrgs;
using LocalTest.Configuration;
using LocalTest.Helpers;
using LocalTest.Notifications.Persistence.Repository;
using LocalTest.Notifications.LocalTestNotifications;
using LocalTest.Services.Authentication.Implementation;
using LocalTest.Services.Authentication.Interface;
using LocalTest.Services.Authorization.Implementation;
Expand Down Expand Up @@ -129,18 +121,11 @@ public void ConfigureServices(IServiceCollection services)
services.AddTransient<IAuthorizationHandler, StorageAccessHandler>();
services.AddTransient<IAuthorizationHandler, ClaimAccessHandler>();

// Notifications services
ValidatorOptions.Global.LanguageManager.Enabled = false;
// Notifications services

GeneralSettings generalSettings = Configuration.GetSection("GeneralSettings").Get<GeneralSettings>();
ResourceLinkExtensions.Initialize(generalSettings.BaseUrl);

services
.AddSingleton<IValidator<EmailNotificationOrderRequestExt>, EmailNotificationOrderRequestValidator>()
.AddSingleton<IOrderRepository, OrderRepository>()
.AddSingleton<IGuidService, GuidService>()
.AddSingleton<IDateTimeService, DateTimeService>()
.AddSingleton<IEmailNotificationOrderService, EmailNotificationOrderService>();

services.AddNotificationServices(generalSettings.BaseUrl);

// Storage services
services.AddSingleton<IClaimsPrincipalProvider, ClaimsPrincipalProvider>();
services.AddTransient<IAuthorization, AuthorizationService>();
Expand Down

0 comments on commit 03d7ba0

Please sign in to comment.